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
10 changes: 10 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ public function getIterableValueType(): Type
return new MixedType();
}

public function getFirstIterableValueType(): Type
{
return new MixedType();
}

public function getLastIterableValueType(): Type
{
return new MixedType();
}

public function isArray(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
10 changes: 10 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ public function getIterableValueType(): Type
return new MixedType();
}

public function getFirstIterableValueType(): Type
{
return new MixedType();
}

public function getLastIterableValueType(): Type
{
return new MixedType();
}

public function isArray(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
10 changes: 10 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ public function getIterableValueType(): Type
return new MixedType();
}

public function getFirstIterableValueType(): Type
{
return new MixedType();
}

public function getLastIterableValueType(): Type
{
return new MixedType();
}

public function isArray(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
10 changes: 10 additions & 0 deletions src/Type/ArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@ public function getIterableValueType(): Type
return $this->getItemType();
}

public function getFirstIterableValueType(): Type
{
return $this->getItemType();
}

public function getLastIterableValueType(): Type
{
return $this->getItemType();
}

public function isArray(): TrinaryLogic
{
return TrinaryLogic::createYes();
Expand Down
48 changes: 30 additions & 18 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,30 +261,16 @@ public function getValueTypes(): array
return $this->valueTypes;
}

/** @deprecated Use getFirstIterableValueType() instead */
public function getFirstValueType(): Type
{
$valueTypes = [];
foreach ($this->valueTypes as $i => $valueType) {
$valueTypes[] = $valueType;
if (!$this->isOptionalKey($i)) {
break;
}
}

return TypeCombinator::union(...$valueTypes);
return $this->getFirstIterableValueType();
}

/** @deprecated Use getLastIterableValueType() instead */
public function getLastValueType(): Type
{
$valueTypes = [];
for ($i = count($this->keyTypes) - 1; $i >= 0; $i--) {
$valueTypes[] = $this->valueTypes[$i];
if (!$this->isOptionalKey($i)) {
break;
}
}

return TypeCombinator::union(...$valueTypes);
return $this->getLastIterableValueType();
}

public function isOptionalKey(int $i): bool
Expand Down Expand Up @@ -712,6 +698,32 @@ public function isIterableAtLeastOnce(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

public function getFirstIterableValueType(): Type
{
$valueTypes = [];
foreach ($this->valueTypes as $i => $valueType) {
$valueTypes[] = $valueType;
if (!$this->isOptionalKey($i)) {
break;
}
}

return TypeCombinator::union(...$valueTypes);
}

public function getLastIterableValueType(): Type
{
$valueTypes = [];
for ($i = count($this->keyTypes) - 1; $i >= 0; $i--) {
$valueTypes[] = $this->valueTypes[$i];
if (!$this->isOptionalKey($i)) {
break;
}
}

return TypeCombinator::union(...$valueTypes);
}

public function isList(): TrinaryLogic
{
if ($this->isList) {
Expand Down
10 changes: 10 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@ public function getIterableValueType(): Type
return $this->intersectTypes(static fn (Type $type): Type => $type->getIterableValueType());
}

public function getFirstIterableValueType(): Type
{
return $this->intersectTypes(static fn (Type $type): Type => $type->getFirstIterableValueType());
}

public function getLastIterableValueType(): Type
{
return $this->intersectTypes(static fn (Type $type): Type => $type->getLastIterableValueType());
}

public function isArray(): TrinaryLogic
{
return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->isArray());
Expand Down
10 changes: 10 additions & 0 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,16 @@ public function getIterableValueType(): Type
return new self($this->isExplicitMixed);
}

public function getFirstIterableValueType(): Type
{
return new self($this->isExplicitMixed);
}

public function getLastIterableValueType(): Type
{
return new self($this->isExplicitMixed);
}

public function isOffsetAccessible(): TrinaryLogic
{
if ($this->subtractedType !== null) {
Expand Down
29 changes: 5 additions & 24 deletions src/Type/Php/ArrayPointerFunctionsDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\Type;
Expand All @@ -31,10 +30,10 @@ public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): Type
): ?Type
{
if (count($functionCall->getArgs()) === 0) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
return null;
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
Expand All @@ -43,27 +42,9 @@ public function getTypeFromFunctionCall(
return new ConstantBooleanType(false);
}

$constantArrays = $argType->getConstantArrays();
if (count($constantArrays) > 0) {
$keyTypes = [];
foreach ($constantArrays as $constantArray) {
$iterableAtLeastOnce = $argType->isIterableAtLeastOnce();
if (!$iterableAtLeastOnce->yes()) {
$keyTypes[] = new ConstantBooleanType(false);
}
if ($iterableAtLeastOnce->no()) {
continue;
}

$keyTypes[] = $functionReflection->getName() === 'reset'
? $constantArray->getFirstValueType()
: $constantArray->getLastValueType();
}

return TypeCombinator::union(...$keyTypes);
}

$itemType = $argType->getIterableValueType();
$itemType = $functionReflection->getName() === 'reset'
? $argType->getFirstIterableValueType()
: $argType->getLastIterableValueType();
if ($iterableAtLeastOnce->yes()) {
return $itemType;
}
Expand Down
25 changes: 3 additions & 22 deletions src/Type/Php/ArrayPopFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;

class ArrayPopFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
Expand All @@ -20,10 +18,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
return $functionReflection->getName() === 'array_pop';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (!isset($functionCall->getArgs()[0])) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
return null;
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
Expand All @@ -32,24 +30,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return new NullType();
}

$constantArrays = $argType->getConstantArrays();
if (count($constantArrays) > 0) {
$valueTypes = [];
foreach ($constantArrays as $constantArray) {
$iterableAtLeastOnce = $constantArray->isIterableAtLeastOnce();
if (!$iterableAtLeastOnce->yes()) {
$valueTypes[] = new NullType();
}
if ($iterableAtLeastOnce->no()) {
continue;
}
$valueTypes[] = $constantArray->getLastValueType();
}

return TypeCombinator::union(...$valueTypes);
}

$itemType = $argType->getIterableValueType();
$itemType = $argType->getLastIterableValueType();
if ($iterableAtLeastOnce->yes()) {
return $itemType;
}
Expand Down
26 changes: 3 additions & 23 deletions src/Type/Php/ArrayShiftFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\NullType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;

class ArrayShiftFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
Expand All @@ -20,10 +18,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
return $functionReflection->getName() === 'array_shift';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if (!isset($functionCall->getArgs()[0])) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
return null;
}

$argType = $scope->getType($functionCall->getArgs()[0]->value);
Expand All @@ -32,25 +30,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
return new NullType();
}

$constantArrays = $argType->getConstantArrays();
if (count($constantArrays) > 0) {
$valueTypes = [];
foreach ($constantArrays as $constantArray) {
$iterableAtLeastOnce = $constantArray->isIterableAtLeastOnce();
if (!$iterableAtLeastOnce->yes()) {
$valueTypes[] = new NullType();
}
if ($iterableAtLeastOnce->no()) {
continue;
}

$valueTypes[] = $constantArray->getFirstValueType();
}

return TypeCombinator::union(...$valueTypes);
}

$itemType = $argType->getIterableValueType();
$itemType = $argType->getFirstIterableValueType();
if ($iterableAtLeastOnce->yes()) {
return $itemType;
}
Expand Down
10 changes: 10 additions & 0 deletions src/Type/StaticType.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ public function getIterableValueType(): Type
return $this->getStaticObjectType()->getIterableValueType();
}

public function getFirstIterableValueType(): Type
{
return $this->getStaticObjectType()->getFirstIterableValueType();
}

public function getLastIterableValueType(): Type
{
return $this->getStaticObjectType()->getLastIterableValueType();
}

public function isOffsetAccessible(): TrinaryLogic
{
return $this->getStaticObjectType()->isOffsetAccessible();
Expand Down
10 changes: 10 additions & 0 deletions src/Type/Traits/LateResolvableTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ public function getIterableValueType(): Type
return $this->resolve()->getIterableValueType();
}

public function getFirstIterableValueType(): Type
{
return $this->resolve()->getFirstIterableValueType();
}

public function getLastIterableValueType(): Type
{
return $this->resolve()->getLastIterableValueType();
}

public function isArray(): TrinaryLogic
{
return $this->resolve()->isArray();
Expand Down
12 changes: 12 additions & 0 deletions src/Type/Traits/MaybeArrayTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace PHPStan\Type\Traits;

use PHPStan\TrinaryLogic;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;

trait MaybeArrayTypeTrait
{
Expand All @@ -17,6 +19,16 @@ public function getConstantArrays(): array
return [];
}

public function getFirstIterableValueType(): Type
{
return new MixedType();
}

public function getLastIterableValueType(): Type
{
return new MixedType();
}

public function isArray(): TrinaryLogic
{
return TrinaryLogic::createMaybe();
Expand Down
Loading