assert icon indicating copy to clipboard operation
assert copied to clipboard

Addition of two new assert functions for better validation of differe…

Open Dropelikeit opened this issue 1 year ago • 1 comments

…nt integer types

Dropelikeit avatar Jul 10 '24 11:07 Dropelikeit

Unfortunately, I had accidentally deleted the fork in my profile.

An older discussion can be found under the closed PR: https://github.com/webmozarts/assert/pull/297

Dropelikeit avatar Jul 10 '24 11:07 Dropelikeit

@shadowhand

Not directly, notNegativeInt simply checks whether it is 0 or greater than 0 to definitively avoid it being a negative integer. With this assert, as with the other assert functions, Psalm directly understands that the number can never be negative. In the other case, negative-int, Psalm should understand that the given integer can never be 0 (neutral) or positive.

That is my goal with this PR here, a small clarification that Psalm would then also understand:


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 Nov 24 '25 22:11 Dropelikeit

@Dropelikeit so, positive-int means > 0 but non-negative-int means ! < 0?

shadowhand avatar Nov 25 '25 13:11 shadowhand

@Dropelikeit in any case, this would need to be rebased to current master.

shadowhand avatar Nov 25 '25 13:11 shadowhand

@Dropelikeit so, positive-int means > 0 but non-negative-int means ! < 0?

@shadowhand Exactly. 0 (zero) is neither positive nor negative. Non-negative is similar to positive-int, but also includes zero. The only difference is that negative numbers are excluded here.

I have rebased my branch so that there are no more conflicts. I have also fixed a small bug in the Assert.php file. In the propertyExists method, $value was undefined in the return value. I replaced the variable with the variable $classOrObject, as in the methodExist method.

Dropelikeit avatar Nov 25 '25 21:11 Dropelikeit