Skip to content

Commit

Permalink
Implement ArrayAccess->offsetExists narrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 29, 2024
1 parent 06d592d commit f40fa5f
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1149,14 +1149,14 @@ public function hasOffsetValueType(Type $offsetType): TrinaryLogic

public function getOffsetValueType(Type $offsetType): Type
{
if (!$this->isExtraOffsetAccessibleClass()->no()) {
return new MixedType();
}

if ($this->isInstanceOf(ArrayAccess::class)->yes()) {
return RecursionGuard::run($this, fn (): Type => $this->getMethod('offsetGet', new OutOfClassScope())->getOnlyVariant()->getReturnType());
}

if (!$this->isExtraOffsetAccessibleClass()->no()) {
return new MixedType();
}

return new ErrorType();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use ArrayAccess;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Generic\GenericObjectType;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\TypeCombinator;
use function count;

final class ArrayAccessOffsetExistsMethodTypeSpecifyingExtension implements MethodTypeSpecifyingExtension, TypeSpecifierAwareExtension
{

private TypeSpecifier $typeSpecifier;

Check failure on line 27 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 27 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

public function __construct(
private ReflectionProvider $reflectionProvider,

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.

Check failure on line 30 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Property PHPStan\Type\Php\ArrayAccessOffsetExistsMethodTypeSpecifyingExtension::$reflectionProvider is never read, only written.
)
{
}

public function setTypeSpecifier(TypeSpecifier $typeSpecifier): void
{
$this->typeSpecifier = $typeSpecifier;
}

public function getClass(): string
{
return ArrayAccess::class;
}

public function isMethodSupported(
MethodReflection $methodReflection,
MethodCall $node,
TypeSpecifierContext $context,
): bool
{
return $methodReflection->getName() === 'offsetExists' && $context->true();
}

public function specifyTypes(MethodReflection $methodReflection, MethodCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
if (count($node->getArgs()) < 1) {
return new SpecifiedTypes();
}
$key = $node->getArgs()[0]->value;

Check failure on line 59 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 59 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.
$keyType = $scope->getType($key);

if (
!$keyType instanceof ConstantStringType

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.

Check failure on line 63 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Doing instanceof PHPStan\Type\Constant\ConstantStringType is error-prone and deprecated. Use Type::getConstantStrings() instead.
&& !$keyType instanceof ConstantIntegerType
) {
return new SpecifiedTypes();
}

foreach($scope->getType($node->var)->getObjectClassReflections() as $classReflection) {
$implementsTags = $classReflection->getImplementsTags();

Check failure on line 70 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 70 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

if (
!isset($implementsTags[\ArrayAccess::class])
|| !$implementsTags[\ArrayAccess::class]->getType() instanceof GenericObjectType

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.4)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, ubuntu-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.4, windows-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.

Check failure on line 74 in src/Type/Php/ArrayAccessOffsetExistsMethodTypeSpecifyingExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Doing instanceof PHPStan\Type\Generic\GenericObjectType is error-prone and deprecated.
) {
continue;
}

$implementsType = $implementsTags[\ArrayAccess::class]->getType();
$arrayAccessGenericTypes = $implementsType->getTypes();
if (!isset($arrayAccessGenericTypes[1])) {
continue;
}

return $this->typeSpecifier->create(
$node->var,
new HasOffsetValueType($keyType, $arrayAccessGenericTypes[1]),
$context,
$scope,
);
}

return new SpecifiedTypes();
}

}
52 changes: 52 additions & 0 deletions src/Type/Php/ArrayAccessOffsetGetMethodReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use ArrayAccess;
use PhpParser\Node\Expr\MethodCall;
use PHPStan\Analyser\Scope;
use PHPStan\Analyser\SpecifiedTypes;
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\DynamicMethodReturnTypeExtension;
use PHPStan\Type\MethodTypeSpecifyingExtension;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use function count;

final class ArrayAccessOffsetGetMethodReturnTypeExtension implements DynamicMethodReturnTypeExtension
{
public function getClass(): string
{
return ArrayAccess::class;
}

public function isMethodSupported(
MethodReflection $methodReflection,
): bool
{
return $methodReflection->getName() === 'offsetGet';
}

public function getTypeFromMethodCall(MethodReflection $methodReflection, MethodCall $methodCall, Scope $scope): ?Type
{
if (count($methodCall->getArgs()) < 1) {
return null;
}
$key = $methodCall->getArgs()[0]->value;
$keyType = $scope->getType($key);
$objectType = $scope->getType($methodCall->var);

if (!$objectType->hasOffsetValueType($keyType)->yes()) {
return null;
}

return $objectType->getOffsetValueType($keyType);
}

}
38 changes: 38 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-3323.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Bug3323;

use function PHPStan\Testing\assertType;
use ArrayAccess;

/**
* @implements ArrayAccess<string, self>
*/
class FormView implements \ArrayAccess
{
public string $vars = '';
}

function doFoo() {
$formView = new FormView();
assertType('Bug3323\FormView', $formView);
if ($formView->offsetExists('_token')) {
assertType("Bug3323\FormView&hasOffsetValue('_token', FormView)", $formView);

$a = $formView->offsetGet('_token');
assertType("Bug3323\FormView", $a);

$a = $formView->offsetGet(123);
assertType("Bug3323\FormView|null", $a);
} else {
assertType('Bug3323\FormView', $formView);
}
assertType('Bug3323\FormView', $formView);

$a = $formView->offsetGet('_token');
assertType("Bug3323\FormView|null", $a);

$a = $formView->offsetGet(123);
assertType("Bug3323\FormView|null", $a);
}

0 comments on commit f40fa5f

Please sign in to comment.