Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Type/IntegerRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand Down Expand Up @@ -474,13 +475,15 @@ public function toString(): Type
if ($isZero->no()) {
return new IntersectionType([
new StringType(),
new AccessoryLowercaseStringType(),
new AccessoryNumericStringType(),
new AccessoryNonFalsyStringType(),
]);
}

return new IntersectionType([
new StringType(),
new AccessoryLowercaseStringType(),
new AccessoryNumericStringType(),
]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
Expand Down Expand Up @@ -81,6 +82,7 @@ public function toString(): Type
{
return new IntersectionType([
new StringType(),
new AccessoryLowercaseStringType(),
new AccessoryNumericStringType(),
]);
}
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/array-key-exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ public function doBar(array $a, array $b, array $c, int $key1, string $key2, int
assertType('int', $key1);
}
if (array_key_exists($key2, $a)) {
assertType('numeric-string', $key2);
assertType('lowercase-string&numeric-string', $key2);
}
if (array_key_exists($key3, $a)) {
assertType('int|numeric-string', $key3);
assertType('int|(lowercase-string&numeric-string)', $key3);
}
if (array_key_exists($key4, $a)) {
assertType('(int|numeric-string)', $key4);
assertType('(int|(lowercase-string&numeric-string))', $key4);
}
if (array_key_exists($key5, $a)) {
assertType('int|numeric-string', $key5);
assertType('int|(lowercase-string&numeric-string)', $key5);
}

if (array_key_exists($key1, $b)) {
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-10863.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class Foo
*/
public function doFoo($b): void
{
assertType('non-falsy-string', '@' . $b);
assertType('lowercase-string&non-falsy-string', '@' . $b);
}

/**
* @param int|false $b
*/
public function doFoo2($b): void
{
assertType('non-falsy-string', '@' . $b);
assertType('lowercase-string&non-falsy-string', '@' . $b);
}

}
52 changes: 26 additions & 26 deletions tests/PHPStan/Analyser/nsrt/bug-11129.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public function foo(
$maybeNegativeConstStrings, $maybeNonNumericConstStrings, $maybeFloatConstStrings,
bool $bool, float $float
): void {
assertType('non-falsy-string', '0'.$i);
assertType('non-falsy-string&numeric-string', $i.'0');
assertType('lowercase-string&non-falsy-string', '0'.$i);
assertType('lowercase-string&non-falsy-string&numeric-string', $i.'0');

assertType('non-falsy-string&numeric-string', '0'.$positiveInt);
assertType('non-falsy-string&numeric-string', $positiveInt.'0');
assertType('lowercase-string&non-falsy-string&numeric-string', '0'.$positiveInt);
assertType('lowercase-string&non-falsy-string&numeric-string', $positiveInt.'0');

assertType('non-falsy-string', '0'.$negativeInt);
assertType('non-falsy-string&numeric-string', $negativeInt.'0');
assertType('lowercase-string&non-falsy-string', '0'.$negativeInt);
assertType('lowercase-string&non-falsy-string&numeric-string', $negativeInt.'0');

assertType("'00'|'01'|'02'", '0'.$positiveConstStrings);
assertType( "'00'|'10'|'20'", $positiveConstStrings.'0');
Expand All @@ -39,29 +39,29 @@ public function foo(
assertType("'00'|'01'|'0a'", '0'.$maybeNonNumericConstStrings);
assertType("'00'|'10'|'a0'", $maybeNonNumericConstStrings.'0');

assertType('non-falsy-string&numeric-string', $i.$positiveConstStrings);
assertType( 'non-falsy-string', $positiveConstStrings.$i);
assertType('lowercase-string&non-falsy-string&numeric-string', $i.$positiveConstStrings);
assertType('lowercase-string&non-falsy-string', $positiveConstStrings.$i);

assertType('non-falsy-string', $i.$maybeNegativeConstStrings);
assertType('non-falsy-string', $maybeNegativeConstStrings.$i);
assertType('lowercase-string&non-falsy-string', $i.$maybeNegativeConstStrings);
assertType('lowercase-string&non-falsy-string', $maybeNegativeConstStrings.$i);

assertType('non-falsy-string', $i.$maybeNonNumericConstStrings);
assertType('non-falsy-string', $maybeNonNumericConstStrings.$i);
assertType('lowercase-string&non-falsy-string', $i.$maybeNonNumericConstStrings);
assertType('lowercase-string&non-falsy-string', $maybeNonNumericConstStrings.$i);

assertType('non-falsy-string', $i.$maybeFloatConstStrings); // could be 'non-falsy-string&numeric-string'
assertType('non-falsy-string', $maybeFloatConstStrings.$i);
assertType('lowercase-string&non-falsy-string', $i.$maybeFloatConstStrings); // could be 'lowercase-string&non-falsy-string&numeric-string'
assertType('lowercase-string&non-falsy-string', $maybeFloatConstStrings.$i);

assertType('non-empty-string&numeric-string', $i.$bool);
assertType('non-empty-string', $bool.$i);
assertType('non-falsy-string&numeric-string', $positiveInt.$bool);
assertType('non-falsy-string&numeric-string', $bool.$positiveInt);
assertType('non-falsy-string&numeric-string', $negativeInt.$bool);
assertType('non-falsy-string', $bool.$negativeInt);
assertType('lowercase-string&non-empty-string&numeric-string', $i.$bool);
assertType('lowercase-string&non-empty-string', $bool.$i);
assertType('lowercase-string&non-falsy-string&numeric-string', $positiveInt.$bool);
assertType('lowercase-string&non-falsy-string&numeric-string', $bool.$positiveInt);
assertType('lowercase-string&non-falsy-string&numeric-string', $negativeInt.$bool);
assertType('lowercase-string&non-falsy-string', $bool.$negativeInt);

assertType('non-falsy-string', $i.$i);
assertType('non-falsy-string', $negativeInt.$negativeInt);
assertType('non-falsy-string', $maybeNegativeConstStrings.$negativeInt);
assertType('non-falsy-string', $negativeInt.$maybeNegativeConstStrings);
assertType('lowercase-string&non-falsy-string', $i.$i);
assertType('lowercase-string&non-falsy-string', $negativeInt.$negativeInt);
assertType('lowercase-string&non-falsy-string', $maybeNegativeConstStrings.$negativeInt);
assertType('lowercase-string&non-falsy-string', $negativeInt.$maybeNegativeConstStrings);

// https://3v4l.org/BCS2K
assertType('non-falsy-string', $float.$float);
Expand All @@ -75,9 +75,9 @@ public function foo(
// https://3v4l.org/Ia4r0
$scientificFloatAsString = '3e4';
assertType('non-falsy-string', $numericString.$scientificFloatAsString);
assertType('non-falsy-string', $i.$scientificFloatAsString);
assertType('lowercase-string&non-falsy-string', $i.$scientificFloatAsString);
assertType('non-falsy-string', $scientificFloatAsString.$numericString);
assertType('non-falsy-string', $scientificFloatAsString.$i);
assertType('lowercase-string&non-falsy-string', $scientificFloatAsString.$i);
}

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-11716.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ function narrowKey($mixed, string $s, int $i, array $generalArr, array $intKeyed
assertType('int', $i);

if (isset($intKeyedArr[$s])) {
assertType("numeric-string", $s);
assertType("lowercase-string&numeric-string", $s);
} else {
assertType('string', $s);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-4587.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public function b(): void
$type = array_map(static function (array $result): array {
assertType('array{a: int}', $result);
$result['a'] = (string) $result['a'];
assertType('array{a: numeric-string}', $result);
assertType('array{a: lowercase-string&numeric-string}', $result);

return $result;
}, $results);

assertType('list<array{a: numeric-string}>', $type);
assertType('list<array{a: lowercase-string&numeric-string}>', $type);
}
}
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/bug-7387.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function inputTypes(int $i, float $f, string $s, int $intRange) {

public function specifiers(int $i) {
// https://3v4l.org/fmVIg
assertType('numeric-string', sprintf('%14s', $i));
assertType('lowercase-string&numeric-string', sprintf('%14s', $i));

assertType('numeric-string', sprintf('%d', $i));

Expand Down Expand Up @@ -59,9 +59,9 @@ public function specifiers(int $i) {
*/
public function positionalArgs($mixed, int $i, float $f, string $s, int $posInt, int $negInt, int $nonZeroIntRange, int $intRange) {
// https://3v4l.org/vVL0c
assertType('numeric-string', sprintf('%2$6s', $mixed, $i));
assertType('non-falsy-string&numeric-string', sprintf('%2$6s', $mixed, $posInt));
assertType('non-falsy-string&numeric-string', sprintf('%2$6s', $mixed, $negInt));
assertType('lowercase-string&numeric-string', sprintf('%2$6s', $mixed, $i));
assertType('lowercase-string&non-falsy-string&numeric-string', sprintf('%2$6s', $mixed, $posInt));
assertType('lowercase-string&non-falsy-string&numeric-string', sprintf('%2$6s', $mixed, $negInt));
assertType("' 1'|' 2'|' 3'|' 4'|' 5'", sprintf('%2$6s', $mixed, $nonZeroIntRange));

// https://3v4l.org/1ECIq
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-8568.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class HelloWorld
{
public function sayHello(): void
{
assertType('non-falsy-string', 'a' . $this->get());
assertType('lowercase-string&non-falsy-string', 'a' . $this->get());
}

public function get(): ?int
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-8635.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class HelloWorld
{
public function EchoInt(int $value): void
{
assertType('numeric-string', "$value");
assertType('lowercase-string&numeric-string', "$value");
}
}
28 changes: 14 additions & 14 deletions tests/PHPStan/Analyser/nsrt/cast-to-numeric-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
* @param 1 $constantInt
*/
function foo(int $a, float $b, $numeric, $numeric2, $number, $positive, $negative, $constantInt): void {
assertType('numeric-string', (string)$a);
assertType('lowercase-string&numeric-string', (string)$a);
assertType('numeric-string', (string)$b);
assertType('numeric-string', (string)$numeric);
assertType('numeric-string', (string)$numeric2);
assertType('numeric-string', (string)$number);
assertType('non-falsy-string&numeric-string', (string)$positive);
assertType('non-falsy-string&numeric-string', (string)$negative);
assertType('lowercase-string&non-falsy-string&numeric-string', (string)$positive);
assertType('lowercase-string&non-falsy-string&numeric-string', (string)$negative);
assertType("'1'", (string)$constantInt);
}

Expand All @@ -32,28 +32,28 @@ function foo(int $a, float $b, $numeric, $numeric2, $number, $positive, $negativ
* @param 1 $constantInt
*/
function concatEmptyString(int $a, float $b, $numeric, $numeric2, $number, $positive, $negative, $constantInt): void {
assertType('numeric-string', '' . $a);
assertType('lowercase-string&numeric-string', '' . $a);
assertType('numeric-string', '' . $b);
assertType('numeric-string', '' . $numeric);
assertType('numeric-string', '' . $numeric2);
assertType('numeric-string', '' . $number);
assertType('non-falsy-string&numeric-string', '' . $positive);
assertType('non-falsy-string&numeric-string', '' . $negative);
assertType('lowercase-string&non-falsy-string&numeric-string', '' . $positive);
assertType('lowercase-string&non-falsy-string&numeric-string', '' . $negative);
assertType("'1'", '' . $constantInt);

assertType('numeric-string', $a . '');
assertType('lowercase-string&numeric-string', $a . '');
assertType('numeric-string', $b . '');
assertType('numeric-string', $numeric . '');
assertType('numeric-string', $numeric2 . '');
assertType('numeric-string', $number . '');
assertType('non-falsy-string&numeric-string', $positive . '');
assertType('non-falsy-string&numeric-string', $negative . '');
assertType('lowercase-string&non-falsy-string&numeric-string', $positive . '');
assertType('lowercase-string&non-falsy-string&numeric-string', $negative . '');
assertType("'1'", $constantInt . '');
}

function concatAssignEmptyString(int $i, float $f) {
$i .= '';
assertType('numeric-string', $i);
assertType('lowercase-string&numeric-string', $i);

$s = '';
$s .= $f;
Expand All @@ -66,13 +66,13 @@ function concatAssignEmptyString(int $i, float $f) {
*/
function integerRangeToString($positive, $negative)
{
assertType('numeric-string', (string) $positive);
assertType('numeric-string', (string) $negative);
assertType('lowercase-string&numeric-string', (string) $positive);
assertType('lowercase-string&numeric-string', (string) $negative);

if ($positive !== 0) {
assertType('non-falsy-string&numeric-string', (string) $positive);
assertType('lowercase-string&non-falsy-string&numeric-string', (string) $positive);
}
if ($negative !== 0) {
assertType('non-falsy-string&numeric-string', (string) $negative);
assertType('lowercase-string&non-falsy-string&numeric-string', (string) $negative);
}
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/filter-var.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public function scalars(bool $bool, float $float, int $int, string $string, int
assertType("'17'", filter_var(17.0));
assertType("'17.1'", filter_var(17.1));
assertType("'1.0E-50'", filter_var(1e-50));
assertType('numeric-string', filter_var($int));
assertType('lowercase-string&numeric-string', filter_var($int));
assertType("'0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'", filter_var($intRange));
assertType("'17'", filter_var(17));
assertType('string', filter_var($string));
Expand Down
6 changes: 3 additions & 3 deletions tests/PHPStan/Analyser/nsrt/generics.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ function f($a, $b)
*/
function testF($arrayOfInt, $callableOrNull)
{
assertType('Closure(int): numeric-string', function (int $a): string {
assertType('Closure(int): (lowercase-string&numeric-string)', function (int $a): string {
return (string)$a;
});
assertType('array<numeric-string>', f($arrayOfInt, function (int $a): string {
assertType('array<lowercase-string&numeric-string>', f($arrayOfInt, function (int $a): string {
return (string)$a;
}));
assertType('Closure(mixed): string', function ($a): string {
Expand Down Expand Up @@ -224,7 +224,7 @@ function testArrayMap(array $listOfIntegers)

return (string) $int;
}, $listOfIntegers);
assertType('array<numeric-string>', $strings);
assertType('array<lowercase-string&numeric-string>', $strings);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions tests/PHPStan/Analyser/nsrt/key-exists.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,16 @@ public function doBar(array $a, array $b, array $c, int $key1, string $key2, int
assertType('int', $key1);
}
if (key_exists($key2, $a)) {
assertType('numeric-string', $key2);
assertType('lowercase-string&numeric-string', $key2);
}
if (key_exists($key3, $a)) {
assertType('int|numeric-string', $key3);
assertType('int|(lowercase-string&numeric-string)', $key3);
}
if (key_exists($key4, $a)) {
assertType('(int|numeric-string)', $key4);
assertType('(int|(lowercase-string&numeric-string))', $key4);
}
if (key_exists($key5, $a)) {
assertType('int|numeric-string', $key5);
assertType('int|(lowercase-string&numeric-string)', $key5);
}

if (key_exists($key1, $b)) {
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/range-to-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public function sayHello($i, $ii, $maxlong, $toolong): void
assertType("'10'|'5'|'6'|'7'|'8'|'9'", (string) $i);
assertType("'-1'|'-10'|'-2'|'-3'|'-4'|'-5'|'-6'|'-7'|'-8'|'-9'|'0'|'1'|'10'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'", (string) $ii);
assertType("'0'|'1'|'10'|'100'|'101'|'102'|'103'|'104'|'105'|'106'|'107'|'108'|'109'|'11'|'110'|'111'|'112'|'113'|'114'|'115'|'116'|'117'|'118'|'119'|'12'|'120'|'121'|'122'|'123'|'124'|'125'|'126'|'127'|'128'|'13'|'14'|'15'|'16'|'17'|'18'|'19'|'2'|'20'|'21'|'22'|'23'|'24'|'25'|'26'|'27'|'28'|'29'|'3'|'30'|'31'|'32'|'33'|'34'|'35'|'36'|'37'|'38'|'39'|'4'|'40'|'41'|'42'|'43'|'44'|'45'|'46'|'47'|'48'|'49'|'5'|'50'|'51'|'52'|'53'|'54'|'55'|'56'|'57'|'58'|'59'|'6'|'60'|'61'|'62'|'63'|'64'|'65'|'66'|'67'|'68'|'69'|'7'|'70'|'71'|'72'|'73'|'74'|'75'|'76'|'77'|'78'|'79'|'8'|'80'|'81'|'82'|'83'|'84'|'85'|'86'|'87'|'88'|'89'|'9'|'90'|'91'|'92'|'93'|'94'|'95'|'96'|'97'|'98'|'99'", (string) $maxlong);
assertType("numeric-string", (string) $toolong);
assertType("lowercase-string&numeric-string", (string) $toolong);
}
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/set-type-type-specifying.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function doString(string $s, int $i, float $f, array $a, object $o)
assertType('string', $s);

settype($i, 'string');
assertType('numeric-string', $i);
assertType('lowercase-string&numeric-string', $i);

settype($f, 'string');
assertType('numeric-string', $f);
Expand Down
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/strval.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ function strvalTest(string $string, string $class): void
assertType('\'1\'', strval(true));
assertType('\'\'|\'1\'', strval(rand(0, 1) === 0));
assertType('\'42\'', strval(42));
assertType('numeric-string', strval(rand()));
assertType('lowercase-string&numeric-string', strval(rand()));
assertType('numeric-string', strval(rand() * 0.1));
assertType('numeric-string', strval(strval(rand())));
assertType('lowercase-string&numeric-string', strval(strval(rand())));
assertType('class-string<stdClass>', strval($class));
assertType('string', strval(new \Exception()));
assertType('*ERROR*', strval(new \stdClass()));
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/DeadCode/data/bug-8620.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class HelloWorld
public function nullCoalesceAndConcatenation (?int $a = null): int
{
$key = ($a ?? "x") . "-";
assertType('non-falsy-string', $key);
assertType('lowercase-string&non-falsy-string', $key);
if ($key === "x-") { return 0; }

return 1;
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Rules/Generics/data/bug-6301.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function str($s)
* @param literal-string $literalString
*/
public function foo(int $i, $nonEmpty, $numericString, $literalString):void {
assertType('numeric-string', $this->str((string) $i));
assertType('lowercase-string&numeric-string', $this->str((string) $i));
assertType('non-empty-string', $this->str($nonEmpty));
assertType('numeric-string', $this->str($numericString));
assertType('literal-string', $this->str($literalString));
Expand Down
Loading