Skip to content

Commit 8b456c5

Browse files
authored
List type cast to array is still list
1 parent 945cd32 commit 8b456c5

5 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/Type/Accessory/AccessoryArrayListType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ public function toString(): Type
304304

305305
public function toArray(): Type
306306
{
307-
return new MixedType();
307+
return $this;
308308
}
309309

310310
public function toArrayKey(): Type

src/Type/StringType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ public function toArray(): Type
134134
[new ConstantIntegerType(0)],
135135
[$this],
136136
[1],
137+
[],
138+
true
137139
);
138140
}
139141

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,7 @@ public function dataFileAsserts(): iterable
11191119
yield from $this->gatherAssertTypes(__DIR__ . '/data/composer-treatPhpDocTypesAsCertainBug.php');
11201120
yield from $this->gatherAssertTypes(__DIR__ . '/data/closure-retain-expression-types.php');
11211121
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7913.php');
1122+
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Functions/data/bug-8280.php');
11221123
}
11231124

11241125
/**

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1220,4 +1220,9 @@ public function testCurlSetOpt(): void
12201220
]);
12211221
}
12221222

1223+
public function testBug8280(): void
1224+
{
1225+
$this->analyse([__DIR__ . '/data/bug-8280.php'], []);
1226+
}
1227+
12231228
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug8280;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param list<string> $var
9+
*/
10+
function foo($var): void {}
11+
12+
/** @var string|list<string>|null $var */
13+
if (null !== $var) {
14+
assertType('list<string>', (array) $var);
15+
foo((array) $var); // should work the same as line below
16+
assertType('list<string>', !is_array($var) ? [$var] : $var);
17+
foo(!is_array($var) ? [$var] : $var);
18+
}

0 commit comments

Comments
 (0)