Skip to content

Commit

Permalink
Revert "Fix ImpossibleCheckTypeFunctionCallRule for is_subclass_of
Browse files Browse the repository at this point in the history
…and `is_a`"

This reverts commit 0711bec.
  • Loading branch information
ondrejmirtes committed Jan 19, 2025
1 parent e6cd651 commit dce063a
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 168 deletions.
4 changes: 4 additions & 0 deletions src/Rules/Comparison/ImpossibleCheckTypeFunctionCallRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function sprintf;
use function strtolower;

/**
* @implements Rule<Node\Expr\FuncCall>
Expand Down Expand Up @@ -37,6 +38,9 @@ public function processNode(Node $node, Scope $scope): array
}

$functionName = (string) $node->name;
if (strtolower($functionName) === 'is_a') {
return [];
}
$isAlways = $this->impossibleCheckTypeHelper->findSpecifiedType($scope, $node);
if ($isAlways === null) {
return [];
Expand Down
16 changes: 1 addition & 15 deletions src/Type/Php/IsAFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\TypeCombinator;
use function count;
use function strtolower;

Expand Down Expand Up @@ -50,20 +47,9 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
$allowStringType = isset($node->getArgs()[2]) ? $scope->getType($node->getArgs()[2]->value) : new ConstantBooleanType(false);
$allowString = !$allowStringType->equals(new ConstantBooleanType(false));

$superType = $allowString
? TypeCombinator::union(new ObjectWithoutClassType(), new ClassStringType())
: new ObjectWithoutClassType();

$resultType = $this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, true);

// prevent false-positives in IsAFunctionTypeSpecifyingHelper
if ($resultType->equals($superType) && $resultType->isSuperTypeOf($objectOrClassType)->yes()) {
return new SpecifiedTypes([], []);
}

return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
$resultType,
$this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, true),
$context,
false,
$scope,
Expand Down
16 changes: 1 addition & 15 deletions src/Type/Php/IsSubclassOfFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
use PHPStan\Analyser\TypeSpecifierAwareExtension;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ClassStringType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\TypeCombinator;
use function count;
use function strtolower;

Expand Down Expand Up @@ -51,20 +48,9 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
return new SpecifiedTypes([], []);
}

$superType = $allowString
? TypeCombinator::union(new ObjectWithoutClassType(), new ClassStringType())
: new ObjectWithoutClassType();

$resultType = $this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, false);

// prevent false-positives in IsAFunctionTypeSpecifyingHelper
if ($resultType->equals($superType) && $resultType->isSuperTypeOf($objectOrClassType)->yes()) {
return new SpecifiedTypes([], []);
}

return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
$resultType,
$this->isAFunctionTypeSpecifyingHelper->determineType($objectOrClassType, $classType, $allowString, false),
$context,
false,
$scope,
Expand Down
4 changes: 3 additions & 1 deletion tests/PHPStan/Analyser/TypeSpecifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,9 @@ public function dataCondition(): iterable
new Arg(new Variable('stringOrNull')),
new Arg(new Expr\ConstFetch(new Name('false'))),
]),
[],
[
'$object' => 'object',
],
[],
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1102,11 +1102,4 @@ public function testAlwaysTruePregMatch(): void
$this->analyse([__DIR__ . '/data/always-true-preg-match.php'], []);
}

public function testBug3979(): void
{
$this->checkAlwaysTrueCheckTypeFunctionCall = true;
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-3979.php'], []);
}

}
130 changes: 0 additions & 130 deletions tests/PHPStan/Rules/Comparison/data/bug-3979.php

This file was deleted.

0 comments on commit dce063a

Please sign in to comment.