Skip to content

Commit

Permalink
Apply some feedback from PR
Browse files Browse the repository at this point in the history
  • Loading branch information
GKFX committed Jan 25, 2021
1 parent 026c6c4 commit ceed528
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Expand Up @@ -193,6 +193,10 @@ public function testInstantiation(): void
'Class TestInstantiation\ClassExtendingAbstractConstructor constructor invoked with 0 parameters, 1 required.',
273,
],
[
'Parameter #2 $y of class TestInstantiation\IntRange constructor expects 1|2|3|4|5|6|7, int<1, 8> given.',
291,
],
]
);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Rules/Classes/data/instantiation.php
Expand Up @@ -274,3 +274,29 @@ public function doBar()
}

}

final class IntRange
{
/**
* @psalm-var 1|2|3|4|5|6|7|8
*/
private $x;

public static function fromInt(int $x): self
{
if ($x < 1 || $x > 8) {
throw new InvalidArgumentException;
}

return new self($x, $x);
}

/**
* @psalm-param 1|2|3|4|5|6|7|8 $x
* @psalm-param 1|2|3|4|5|6|7 $y
*/
private function __construct(int $x, int $y)
{
$y = $this->x = $x;
}
}
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php
Expand Up @@ -61,6 +61,10 @@ public function testReturnTypeRule(): void
'Function ReturnTypes\returnNever() should never return but return statement found.',
181,
],
[
'Function ReturnTypes\returnRangeBad() should return 1|2|3|4|5|6|7 but returns int<1, 8>.',
203,
],
]);
}

Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Functions/data/returnTypes.php
Expand Up @@ -180,3 +180,25 @@ function returnNever()
{
return;
}

/**
* @return 1|2|3|4|5|6|7|8
*/
function returnRange(int $x) : int {
if ($x < 1 || $x > 8) {
throw new InvalidArgumentException;
}

return $x;
}

/**
* @return 1|2|3|4|5|6|7
*/
function returnRangeBad(int $x) : int {
if ($x < 1 || $x > 8) {
throw new InvalidArgumentException;
}

return $x;
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Type/IntegerRangeTypeTest.php
Expand Up @@ -68,7 +68,7 @@ public function testIsSubTypeOf(IntegerRangeType $type, Type $otherType, Trinary
$this->assertSame(
$expectedResult->describe(),
$actualResult->describe(),
sprintf('%s -> isSuperTypeOf(%s)', $type->describe(VerbosityLevel::precise()), $otherType->describe(VerbosityLevel::precise()))
sprintf('%s -> isSubTypeOf(%s)', $type->describe(VerbosityLevel::precise()), $otherType->describe(VerbosityLevel::precise()))
);
}

Expand Down

0 comments on commit ceed528

Please sign in to comment.