Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition of two new assert functions for better validation of different integer types #297

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Dropelikeit
Copy link

To complete the integer checks, nonNegativeInteger and negativeInteger checks have been added.

This PR belongs to the ticket #296

@loevgaard
Copy link

I guess you are missing a positiveInteger check?

@herndlm
Copy link

herndlm commented Apr 30, 2024

@loevgaard that one exists already

@Dropelikeit
Copy link
Author

@loevgaard sorry, I think the naming is poorly chosen. Maybe it is wise to rename the nonNegativeInteger to unsignedInteger, because it should only be checked if the value is 0 or higher.
What do you think?

@loevgaard
Copy link

@loevgaard that one exists already

Didn't know that :)

@loevgaard sorry, I think the naming is poorly chosen. Maybe it is wise to rename the nonNegativeInteger to unsignedInteger, because it should only be checked if the value is 0 or higher. What do you think?

I think nonNegativeInteger is the way to go because that's what's used in other places, .e.g https://psalm.dev/docs/annotating_code/type_syntax/scalar_types/

@Dropelikeit
Copy link
Author

Dropelikeit commented May 1, 2024

@loevgaard The psalm was the motivation to write this pull request. I think it would be great if this missing implementation could be added to make such checks so that Psalm understands the variables correctly:

class MyObject {
/**
* @psalm-param non-negative-int $age
*/
public function __construct(private readonly $age)
{
}

public static function create(int $age): self
{
      Assert::nonNegativeInteger($age);

      return new self($age);
}

/**
* @psalm-return non-negative-int
*/
public function getAge(): int
{
      return $this->age;
}

}

@loevgaard
Copy link

Yeah, exactly. Makes a lot of sense :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants