assert icon indicating copy to clipboard operation
assert copied to clipboard

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

Open Dropelikeit opened this issue 2 years ago • 6 comments

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

This PR belongs to the ticket https://github.com/webmozarts/assert/issues/296

Dropelikeit avatar Nov 13 '23 10:11 Dropelikeit

I guess you are missing a positiveInteger check?

loevgaard avatar Apr 30 '24 12:04 loevgaard

@loevgaard that one exists already

herndlm avatar Apr 30 '24 16:04 herndlm

@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?

Dropelikeit avatar Apr 30 '24 21:04 Dropelikeit

@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/

loevgaard avatar May 01 '24 06:05 loevgaard

@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;
}

}

Dropelikeit avatar May 01 '24 09:05 Dropelikeit

Yeah, exactly. Makes a lot of sense :)

loevgaard avatar May 01 '24 09:05 loevgaard