-
Notifications
You must be signed in to change notification settings - Fork 510
Add return type extension for array_column() #948
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
173 changes: 173 additions & 0 deletions
173
src/Type/Php/ArrayColumnFunctionReturnTypeExtension.php
This file contains hidden or 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,173 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\ShouldNotHappenException; | ||
use PHPStan\TrinaryLogic; | ||
use PHPStan\Type\Accessory\NonEmptyArrayType; | ||
use PHPStan\Type\ArrayType; | ||
use PHPStan\Type\Constant\ConstantArrayType; | ||
use PHPStan\Type\Constant\ConstantArrayTypeBuilder; | ||
use PHPStan\Type\DynamicFunctionReturnTypeExtension; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\NeverType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\TypeUtils; | ||
use function count; | ||
|
||
class ArrayColumnFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'array_column'; | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
{ | ||
$numArgs = count($functionCall->getArgs()); | ||
if ($numArgs < 2) { | ||
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); | ||
} | ||
|
||
$arrayType = $scope->getType($functionCall->getArgs()[0]->value); | ||
$columnType = $scope->getType($functionCall->getArgs()[1]->value); | ||
$indexType = $numArgs >= 3 ? $scope->getType($functionCall->getArgs()[2]->value) : null; | ||
|
||
$constantArrayTypes = TypeUtils::getConstantArrays($arrayType); | ||
if (count($constantArrayTypes) === 1) { | ||
$type = $this->handleConstantArray($constantArrayTypes[0], $columnType, $indexType, $scope); | ||
if ($type !== null) { | ||
return $type; | ||
} | ||
} | ||
|
||
return $this->handleAnyArray($arrayType, $columnType, $indexType, $scope); | ||
} | ||
|
||
private function handleAnyArray(Type $arrayType, Type $columnType, ?Type $indexType, Scope $scope): Type | ||
{ | ||
$iterableAtLeastOnce = $arrayType->isIterableAtLeastOnce(); | ||
if ($iterableAtLeastOnce->no()) { | ||
return new ConstantArrayType([], []); | ||
} | ||
|
||
$iterableValueType = $arrayType->getIterableValueType(); | ||
$returnValueType = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope, false); | ||
|
||
if ($returnValueType === null) { | ||
$returnValueType = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope, true); | ||
$iterableAtLeastOnce = TrinaryLogic::createMaybe(); | ||
if ($returnValueType === null) { | ||
throw new ShouldNotHappenException(); | ||
} | ||
} | ||
|
||
if ($returnValueType instanceof NeverType) { | ||
return new ConstantArrayType([], []); | ||
} | ||
|
||
if ($indexType !== null) { | ||
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, false); | ||
if ($type !== null) { | ||
$returnKeyType = $type; | ||
} else { | ||
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, true); | ||
if ($type !== null) { | ||
$returnKeyType = TypeCombinator::union($type, new IntegerType()); | ||
} else { | ||
$returnKeyType = new IntegerType(); | ||
} | ||
} | ||
} else { | ||
$returnKeyType = new IntegerType(); | ||
} | ||
|
||
$returnType = new ArrayType($returnKeyType, $returnValueType); | ||
|
||
if ($iterableAtLeastOnce->yes()) { | ||
$returnType = TypeCombinator::intersect($returnType, new NonEmptyArrayType()); | ||
} | ||
|
||
return $returnType; | ||
} | ||
|
||
private function handleConstantArray(ConstantArrayType $arrayType, Type $columnType, ?Type $indexType, Scope $scope): ?Type | ||
{ | ||
$builder = ConstantArrayTypeBuilder::createEmpty(); | ||
|
||
foreach ($arrayType->getValueTypes() as $iterableValueType) { | ||
$valueType = $this->getOffsetOrProperty($iterableValueType, $columnType, $scope, false); | ||
if ($valueType === null) { | ||
return null; | ||
} | ||
|
||
if ($indexType !== null) { | ||
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, false); | ||
if ($type !== null) { | ||
$keyType = $type; | ||
} else { | ||
$type = $this->getOffsetOrProperty($iterableValueType, $indexType, $scope, true); | ||
if ($type !== null) { | ||
$keyType = TypeCombinator::union($type, new IntegerType()); | ||
} else { | ||
$keyType = null; | ||
} | ||
} | ||
} else { | ||
$keyType = null; | ||
} | ||
|
||
$builder->setOffsetValueType($keyType, $valueType); | ||
} | ||
|
||
return $builder->getArray(); | ||
} | ||
|
||
private function getOffsetOrProperty(Type $type, Type $offsetOrProperty, Scope $scope, bool $allowMaybe): ?Type | ||
{ | ||
$returnTypes = []; | ||
|
||
if (!$type->canAccessProperties()->no()) { | ||
$propertyTypes = TypeUtils::getConstantStrings($offsetOrProperty); | ||
if ($propertyTypes === []) { | ||
return new MixedType(); | ||
} | ||
foreach ($propertyTypes as $propertyType) { | ||
$propertyName = $propertyType->getValue(); | ||
$hasProperty = $type->hasProperty($propertyName); | ||
if ($hasProperty->maybe()) { | ||
return $allowMaybe ? new MixedType() : null; | ||
} | ||
if (!$hasProperty->yes()) { | ||
continue; | ||
} | ||
|
||
$returnTypes[] = $type->getProperty($propertyName, $scope)->getReadableType(); | ||
} | ||
} | ||
|
||
if ($type->isOffsetAccessible()->yes()) { | ||
$hasOffset = $type->hasOffsetValueType($offsetOrProperty); | ||
if (!$allowMaybe && $hasOffset->maybe()) { | ||
return null; | ||
} | ||
if (!$hasOffset->no()) { | ||
$returnTypes[] = $type->getOffsetValueType($offsetOrProperty); | ||
} | ||
} | ||
|
||
if ($returnTypes === []) { | ||
return new NeverType(); | ||
} | ||
|
||
return TypeCombinator::union(...$returnTypes); | ||
} | ||
|
||
} |
This file contains hidden or 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 hidden or 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,92 @@ | ||
<?php | ||
|
||
namespace ArrayColumn; | ||
|
||
use DOMElement; | ||
use function PHPStan\Testing\assertType; | ||
|
||
function testArrays(array $array): void | ||
{ | ||
/** @var array<int, array<string, string>> $array */ | ||
assertType('array<int, string>', array_column($array, 'column')); | ||
assertType('array<int|string, string>', array_column($array, 'column', 'key')); | ||
|
||
/** @var non-empty-array<int, array<string, string>> $array */ | ||
// Note: Array may still be empty! | ||
assertType('array<int, string>', array_column($array, 'column')); | ||
|
||
/** @var array{} $array */ | ||
assertType('array{}', array_column($array, 'column')); | ||
assertType('array{}', array_column($array, 'column', 'key')); | ||
} | ||
|
||
function testConstantArrays(array $array): void | ||
{ | ||
/** @var array<int, array{column: string, key: string}> $array */ | ||
assertType('array<int, string>', array_column($array, 'column')); | ||
assertType('array<string, string>', array_column($array, 'column', 'key')); | ||
|
||
/** @var array<int, array{column: string, key: string}> $array */ | ||
assertType('array{}', array_column($array, 'foo')); | ||
assertType('array{}', array_column($array, 'foo', 'key')); | ||
|
||
/** @var array{array{column: string, key: 'bar'}} $array */ | ||
assertType("array{string}", array_column($array, 'column')); | ||
assertType("array{bar: string}", array_column($array, 'column', 'key')); | ||
|
||
/** @var array{array{column: string, key: string}} $array */ | ||
assertType("non-empty-array<string, string>", array_column($array, 'column', 'key')); | ||
|
||
/** @var array<int, array{column?: 'foo', key?: 'bar'}> $array */ | ||
assertType("array<int, 'foo'>", array_column($array, 'column')); | ||
assertType("array<'bar'|int, 'foo'>", array_column($array, 'column', 'key')); | ||
|
||
/** @var array<int, array{column1: string, column2: bool}> $array */ | ||
assertType('array<int, bool|string>', array_column($array, mt_rand(0, 1) === 0 ? 'column1' : 'column2')); | ||
|
||
/** @var non-empty-array<int, array{column: string, key: string}> $array */ | ||
assertType('non-empty-array<int, string>', array_column($array, 'column')); | ||
assertType('non-empty-array<string, string>', array_column($array, 'column', 'key')); | ||
} | ||
|
||
function testImprecise(array $array): void { | ||
// These cases aren't handled precisely and will return non-constant arrays. | ||
|
||
/** @var array{array{column?: 'foo', key: 'bar'}} $array */ | ||
assertType("array<int, 'foo'>", array_column($array, 'column')); | ||
assertType("array<'bar', 'foo'>", array_column($array, 'column', 'key')); | ||
|
||
/** @var array{array{column: 'foo', key?: 'bar'}} $array */ | ||
assertType("non-empty-array<'bar'|int, 'foo'>", array_column($array, 'column', 'key')); | ||
|
||
/** @var array{array{column: 'foo', key: 'bar'}}|array<int, array<string, string>> $array */ | ||
assertType('array<int, string>', array_column($array, 'column')); | ||
assertType('array<int|string, string>', array_column($array, 'column', 'key')); | ||
|
||
/** @var array{0?: array{column: 'foo', key: 'bar'}} $array */ | ||
assertType("array<int, 'foo'>", array_column($array, 'column')); | ||
assertType("array<'bar', 'foo'>", array_column($array, 'column', 'key')); | ||
} | ||
|
||
function testObjects(array $array): void { | ||
/** @var array<int, DOMElement> $array */ | ||
assertType('array<int, string>', array_column($array, 'nodeName')); | ||
assertType('array<string, string>', array_column($array, 'nodeName', 'tagName')); | ||
assertType('array<int, mixed>', array_column($array, 'foo')); | ||
assertType('array<string, mixed>', array_column($array, 'foo', 'tagName')); | ||
assertType('array<string>', array_column($array, 'nodeName', 'foo')); | ||
|
||
/** @var non-empty-array<int, DOMElement> $array */ | ||
assertType('non-empty-array<int, string>', array_column($array, 'nodeName')); | ||
assertType('non-empty-array<string, string>', array_column($array, 'nodeName', 'tagName')); | ||
assertType('array<int, mixed>', array_column($array, 'foo')); | ||
assertType('array<string, mixed>', array_column($array, 'foo', 'tagName')); | ||
assertType('non-empty-array<string>', array_column($array, 'nodeName', 'foo')); | ||
|
||
/** @var array{DOMElement} $array */ | ||
assertType('array{string}', array_column($array, 'nodeName')); | ||
assertType('non-empty-array<string, string>', array_column($array, 'nodeName', 'tagName')); | ||
assertType('array<int, mixed>', array_column($array, 'foo')); | ||
assertType('array<string, mixed>', array_column($array, 'foo', 'tagName')); | ||
assertType('non-empty-array<int|string, string>', array_column($array, 'nodeName', 'foo')); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the current behaviour with an empty array? If the code doesn't crash then this is a BC break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
min([])
andmax([])
cause fatal errors on PHP >= 8. On PHP < 8 it causes a warning and returnsfalse
, which gets coerced to0
when given tocreate()
. See https://3v4l.org/RS767