Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract and test OversizedArrayBuilder
- Loading branch information
1 parent
d78ff13
commit 1573a79
Showing
7 changed files
with
201 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Constant; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Array_; | ||
use PHPStan\Node\Expr\TypeExpr; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\Type\Accessory\AccessoryArrayListType; | ||
use PHPStan\Type\Accessory\NonEmptyArrayType; | ||
use PHPStan\Type\Accessory\OversizedArrayType; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\GeneralizePrecision; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function array_splice; | ||
use function array_values; | ||
use function count; | ||
|
||
class OversizedArrayBuilder | ||
{ | ||
|
||
/** | ||
* @param callable(Expr): Type $getTypeCallback | ||
*/ | ||
public function build(Array_ $expr, callable $getTypeCallback): Type | ||
{ | ||
$isList = true; | ||
$valueTypes = []; | ||
$keyTypes = []; | ||
$nextAutoIndex = 0; | ||
$items = $expr->items; | ||
for ($i = 0; $i < count($items); $i++) { | ||
$item = $items[$i]; | ||
if ($item === null) { | ||
continue; | ||
} | ||
if (!$item->unpack) { | ||
continue; | ||
} | ||
|
||
$valueType = $getTypeCallback($item->value); | ||
if ($valueType instanceof ConstantArrayType) { | ||
array_splice($items, $i, 1); | ||
foreach ($valueType->getKeyTypes() as $j => $innerKeyType) { | ||
$innerValueType = $valueType->getValueTypes()[$j]; | ||
if ($innerKeyType->isString()->no()) { | ||
$keyExpr = null; | ||
} else { | ||
$keyExpr = new TypeExpr($innerKeyType); | ||
} | ||
array_splice($items, $i++, 0, [new Expr\ArrayItem( | ||
new TypeExpr($innerValueType), | ||
$keyExpr, | ||
)]); | ||
} | ||
} else { | ||
array_splice($items, $i, 1, [new Expr\ArrayItem( | ||
new TypeExpr($valueType->getIterableValueType()), | ||
new TypeExpr($valueType->getIterableKeyType()), | ||
)]); | ||
} | ||
} | ||
foreach ($items as $item) { | ||
if ($item === null) { | ||
continue; | ||
} | ||
if ($item->unpack) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
if ($item->key !== null) { | ||
$itemKeyType = $getTypeCallback($item->key); | ||
if (!$itemKeyType instanceof ConstantIntegerType) { | ||
$isList = false; | ||
} elseif ($itemKeyType->getValue() !== $nextAutoIndex) { | ||
$isList = false; | ||
$nextAutoIndex = $itemKeyType->getValue() + 1; | ||
} else { | ||
$itemKeyType = new ConstantIntegerType($nextAutoIndex); | ||
$nextAutoIndex++; | ||
} | ||
} else { | ||
$itemKeyType = new ConstantIntegerType($nextAutoIndex); | ||
$nextAutoIndex++; | ||
} | ||
|
||
$generalizedKeyType = $itemKeyType->generalize(GeneralizePrecision::moreSpecific()); | ||
$keyTypes[$generalizedKeyType->describe(VerbosityLevel::precise())] = $generalizedKeyType; | ||
|
||
$itemValueType = $getTypeCallback($item->value); | ||
$generalizedValueType = $itemValueType->generalize(GeneralizePrecision::moreSpecific()); | ||
$valueTypes[$generalizedValueType->describe(VerbosityLevel::precise())] = $generalizedValueType; | ||
} | ||
|
||
$keyType = TypeCombinator::union(...array_values($keyTypes)); | ||
$valueType = TypeCombinator::union(...array_values($valueTypes)); | ||
|
||
$arrayType = new ArrayType($keyType, $valueType); | ||
if ($isList) { | ||
$arrayType = AccessoryArrayListType::intersectWith($arrayType); | ||
} | ||
|
||
return TypeCombinator::intersect($arrayType, new NonEmptyArrayType(), new OversizedArrayType()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Constant; | ||
|
||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Expr\Array_; | ||
use PhpParser\Node\Stmt\Expression; | ||
use PHPStan\Reflection\InitializerExprContext; | ||
use PHPStan\Reflection\InitializerExprTypeResolver; | ||
use PHPStan\Testing\PHPStanTestCase; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
class OversizedArrayBuilderTest extends PHPStanTestCase | ||
{ | ||
|
||
public function dataBuild(): iterable | ||
{ | ||
yield [ | ||
'[1, 2, 3]', | ||
'non-empty-list<int>&oversized-array', | ||
]; | ||
|
||
yield [ | ||
'[1, 2, 3, ...[1, 2, 3]]', | ||
'non-empty-list<int>&oversized-array', | ||
]; | ||
|
||
yield [ | ||
'[1, 2, 3, ...[1, \'foo\' => 2, 3]]', | ||
'non-empty-array<int|(literal-string&non-falsy-string), int>&oversized-array', | ||
]; | ||
|
||
yield [ | ||
'[1, 2, 2 => 3]', | ||
'non-empty-list<int>&oversized-array', | ||
]; | ||
yield [ | ||
'[1, 2, 3 => 3]', | ||
'non-empty-array<int, int>&oversized-array', | ||
]; | ||
yield [ | ||
'[1, 1 => 2, 3]', | ||
'non-empty-list<int>&oversized-array', | ||
]; | ||
yield [ | ||
'[1, 2 => 2, 3]', | ||
'non-empty-array<int, int>&oversized-array', | ||
]; | ||
yield [ | ||
'[1, \'foo\' => 2, 3]', | ||
'non-empty-array<int|(literal-string&non-falsy-string), int>&oversized-array', | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider dataBuild | ||
*/ | ||
public function testBuild(string $sourceCode, string $expectedTypeDescription): void | ||
{ | ||
$parser = self::getParser(); | ||
$ast = $parser->parseString('<?php ' . $sourceCode . ';'); | ||
$expr = $ast[0]; | ||
$this->assertInstanceOf(Expression::class, $expr); | ||
|
||
$array = $expr->expr; | ||
$this->assertInstanceOf(Array_::class, $array); | ||
|
||
$builder = new OversizedArrayBuilder(); | ||
$initializerExprTypeResolver = self::getContainer()->getByType(InitializerExprTypeResolver::class); | ||
$arrayType = $builder->build($array, static fn (Expr $expr): Type => $initializerExprTypeResolver->getType($expr, InitializerExprContext::createEmpty())); | ||
$this->assertSame($expectedTypeDescription, $arrayType->describe(VerbosityLevel::precise())); | ||
} | ||
|
||
public static function getAdditionalConfigFiles(): array | ||
{ | ||
return [ | ||
__DIR__ . '/../../../../conf/bleedingEdge.neon', | ||
]; | ||
} | ||
|
||
} |