-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show TypeResult reasons in StrictComparisonOfDifferentTypesRule
- Loading branch information
1 parent
a815d57
commit 34bacd7
Showing
11 changed files
with
173 additions
and
50 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\BinaryOp\Identical; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Reflection\InitializerExprTypeResolver; | ||
use PHPStan\Type\BooleanType; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\TypeResult; | ||
use function is_string; | ||
|
||
final class RicherScopeGetTypeHelper | ||
{ | ||
|
||
public function __construct(private InitializerExprTypeResolver $initializerExprTypeResolver) | ||
{ | ||
} | ||
|
||
/** | ||
* @return TypeResult<BooleanType> | ||
*/ | ||
public function getIdenticalResult(Scope $scope, Identical $expr): TypeResult | ||
{ | ||
if ( | ||
$expr->left instanceof Variable | ||
&& is_string($expr->left->name) | ||
&& $expr->right instanceof Variable | ||
&& is_string($expr->right->name) | ||
&& $expr->left->name === $expr->right->name | ||
) { | ||
return new TypeResult(new ConstantBooleanType(true), []); | ||
} | ||
|
||
$leftType = $scope->getType($expr->left); | ||
$rightType = $scope->getType($expr->right); | ||
|
||
if ( | ||
( | ||
$expr->left instanceof Node\Expr\PropertyFetch | ||
|| $expr->left instanceof Node\Expr\StaticPropertyFetch | ||
) | ||
&& $rightType->isNull()->yes() | ||
&& !$scope->hasPropertyNativeType($expr->left) | ||
) { | ||
return new TypeResult(new BooleanType(), []); | ||
} | ||
|
||
if ( | ||
( | ||
$expr->right instanceof Node\Expr\PropertyFetch | ||
|| $expr->right instanceof Node\Expr\StaticPropertyFetch | ||
) | ||
&& $leftType->isNull()->yes() | ||
&& !$scope->hasPropertyNativeType($expr->right) | ||
) { | ||
return new TypeResult(new BooleanType(), []); | ||
} | ||
|
||
return $this->initializerExprTypeResolver->resolveIdenticalType($leftType, $rightType); | ||
} | ||
|
||
/** | ||
* @return TypeResult<BooleanType> | ||
*/ | ||
public function getNotIdenticalResult(Scope $scope, Node\Expr\BinaryOp\NotIdentical $expr): TypeResult | ||
{ | ||
$identicalResult = $this->getIdenticalResult($scope, new Identical($expr->left, $expr->right)); | ||
$identicalType = $identicalResult->type; | ||
if ($identicalType instanceof ConstantBooleanType) { | ||
return new TypeResult(new ConstantBooleanType(!$identicalType->getValue()), $identicalResult->reasons); | ||
} | ||
|
||
return new TypeResult(new BooleanType(), []); | ||
} | ||
|
||
} |
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
Oops, something went wrong.