Skip to content

Commit fe64244

Browse files
authored
Fix adding string[] in explode() for ClassMethodArrayDocblockParamFromLocalCallsRector (#7331)
* add fixture * fix adding string[] in explode() for ClassMethodArrayDocblockParamFromLocalCallsRector
1 parent a92f244 commit fe64244

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;
4+
5+
final class FromExplode
6+
{
7+
public function go()
8+
{
9+
$this->run(explode(':', '1:1'));
10+
}
11+
12+
private function run(array $items)
13+
{
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\ClassMethodArrayDocblockParamFromLocalCallsRector\Fixture;
22+
23+
final class FromExplode
24+
{
25+
public function go()
26+
{
27+
$this->run(explode(':', '1:1'));
28+
}
29+
30+
/**
31+
* @param string[] $items
32+
*/
33+
private function run(array $items)
34+
{
35+
}
36+
}
37+
38+
?>

src/NodeTypeResolver/NodeTypeResolver.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@
3434
use PHPStan\Type\Constant\ConstantBooleanType;
3535
use PHPStan\Type\Constant\ConstantStringType;
3636
use PHPStan\Type\ErrorType;
37+
use PHPStan\Type\IntegerType;
3738
use PHPStan\Type\MixedType;
3839
use PHPStan\Type\NeverType;
3940
use PHPStan\Type\NullType;
4041
use PHPStan\Type\ObjectType;
4142
use PHPStan\Type\ObjectWithoutClassType;
43+
use PHPStan\Type\StringType;
4244
use PHPStan\Type\ThisType;
4345
use PHPStan\Type\Type;
4446
use PHPStan\Type\TypeCombinator;
@@ -178,6 +180,11 @@ public function getType(Node $node): Type
178180
}
179181
}
180182

183+
// correction for explode() that always returns array
184+
if ($node instanceof FuncCall && $node->name instanceof Name && $node->name->toString() === 'explode') {
185+
return new ArrayType(new IntegerType(), new StringType());
186+
}
187+
181188
if ($node instanceof Ternary) {
182189
$ternaryType = $this->resolveTernaryType($node);
183190
if (! $ternaryType instanceof MixedType) {

0 commit comments

Comments
 (0)