Skip to content
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

Feature check mixed in binary operator #3231

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 6 additions & 1 deletion conf/config.level2.neon
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ rules:
- PHPStan\Rules\Generics\UsedTraitsRule
- PHPStan\Rules\Methods\CallPrivateMethodThroughStaticRule
- PHPStan\Rules\Methods\IncompatibleDefaultParameterTypeRule
- PHPStan\Rules\Operators\InvalidBinaryOperationRule
- PHPStan\Rules\Operators\InvalidUnaryOperationRule
- PHPStan\Rules\Operators\InvalidComparisonOperationRule
- PHPStan\Rules\PhpDoc\FunctionConditionalReturnTypeRule
Expand Down Expand Up @@ -138,3 +137,9 @@ services:

-
class: PHPStan\Rules\Pure\PureMethodRule
-
class: PHPStan\Rules\Operators\InvalidBinaryOperationRule
arguments:
bleedingEdge: %featureToggles.bleedingEdge%
tags:
- phpstan.rules.rule
12 changes: 11 additions & 1 deletion src/Collectors/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ public function __construct(array $collectors)
public function getCollectors(string $nodeType): array
{
if (!isset($this->cache[$nodeType])) {
$parentNodeTypes = [$nodeType] + class_parents($nodeType) + class_implements($nodeType);
$parents = class_parents($nodeType);
if ($parents === false) {
$parents = [];
}

$implements = class_implements($nodeType);
if ($implements === false) {
$implements = [];
}

$parentNodeTypes = [$nodeType] + $parents + $implements;

$collectors = [];
foreach ($parentNodeTypes as $parentNodeType) {
Expand Down
4 changes: 4 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,10 @@ public function getModType(Expr $left, Expr $right, callable $getTypeCallback):
return $this->getNeverType($leftType, $rightType);
}

if ($leftType->toNumber() instanceof ErrorType || $rightType->toNumber() instanceof ErrorType) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether this should only be enabled in bleeding edge. On the one hand, I don't see any other case in this class where return new ErrorType would be conditioned by bleeding edge. On the other hand, this can definitely result in new errors being reported without bleeding edge. E.g. 5 % [] now reports Binary operation "%" between 5 and array{} results in an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $int % $string use-case is even more compelling example of why it should apply only to bleeding edge.

return new ErrorType();
}

$leftTypes = $leftType->getConstantScalarTypes();
$rightTypes = $rightType->getConstantScalarTypes();
$leftTypesCount = count($leftTypes);
Expand Down
12 changes: 11 additions & 1 deletion src/Rules/DirectRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,17 @@ public function __construct(array $rules)
public function getRules(string $nodeType): array
{
if (!isset($this->cache[$nodeType])) {
$parentNodeTypes = [$nodeType] + class_parents($nodeType) + class_implements($nodeType);
$parents = class_parents($nodeType);
if ($parents === false) {
$parents = [];
}

$implements = class_implements($nodeType);
if ($implements === false) {
$implements = [];
}

$parentNodeTypes = [$nodeType] + $parents + $implements;

$rules = [];
foreach ($parentNodeTypes as $parentNodeType) {
Expand Down
12 changes: 11 additions & 1 deletion src/Rules/LazyRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,17 @@ public function __construct(private Container $container)
public function getRules(string $nodeType): array
{
if (!isset($this->cache[$nodeType])) {
$parentNodeTypes = [$nodeType] + class_parents($nodeType) + class_implements($nodeType);
$parents = class_parents($nodeType);
schlndh marked this conversation as resolved.
Show resolved Hide resolved
if ($parents === false) {
$parents = [];
}

$implements = class_implements($nodeType);
if ($implements === false) {
$implements = [];
}

$parentNodeTypes = [$nodeType] + $parents + $implements;

$rules = [];
$rulesFromContainer = $this->getRulesFromContainer();
Expand Down
147 changes: 74 additions & 73 deletions src/Rules/Operators/InvalidBinaryOperationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class InvalidBinaryOperationRule implements Rule
public function __construct(
private ExprPrinter $exprPrinter,
private RuleLevelHelper $ruleLevelHelper,
private bool $bleedingEdge,
)
{
}
Expand All @@ -44,81 +45,81 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if ($scope->getType($node) instanceof ErrorType) {
$leftName = '__PHPSTAN__LEFT__';
$rightName = '__PHPSTAN__RIGHT__';
$leftVariable = new Node\Expr\Variable($leftName);
$rightVariable = new Node\Expr\Variable($rightName);
if ($node instanceof Node\Expr\AssignOp) {
$identifier = 'assignOp';
$newNode = clone $node;
$newNode->setAttribute('phpstan_cache_printer', null);
$left = $node->var;
$right = $node->expr;
$newNode->var = $leftVariable;
$newNode->expr = $rightVariable;
} else {
$identifier = 'binaryOp';
$newNode = clone $node;
$newNode->setAttribute('phpstan_cache_printer', null);
$left = $node->left;
$right = $node->right;
$newNode->left = $leftVariable;
$newNode->right = $rightVariable;
}

if ($node instanceof Node\Expr\AssignOp\Concat || $node instanceof Node\Expr\BinaryOp\Concat) {
$callback = static fn (Type $type): bool => !$type->toString() instanceof ErrorType;
} else {
$callback = static fn (Type $type): bool => !$type->toNumber() instanceof ErrorType;
}

$leftType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$left,
'',
$callback,
)->getType();
if ($leftType instanceof ErrorType) {
return [];
}

$rightType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$right,
'',
$callback,
)->getType();
if ($rightType instanceof ErrorType) {
return [];
}

if (!$scope instanceof MutatingScope) {
throw new ShouldNotHappenException();
}

$scope = $scope
->assignVariable($leftName, $leftType, $leftType)
->assignVariable($rightName, $rightType, $rightType);

if (!$scope->getType($newNode) instanceof ErrorType) {
return [];
}

return [
RuleErrorBuilder::message(sprintf(
'Binary operation "%s" between %s and %s results in an error.',
substr(substr($this->exprPrinter->printExpr($newNode), strlen($leftName) + 2), 0, -(strlen($rightName) + 2)),
$scope->getType($left)->describe(VerbosityLevel::value()),
$scope->getType($right)->describe(VerbosityLevel::value()),
))
->line($left->getStartLine())
->identifier(sprintf('%s.invalid', $identifier))
->build(),
];
if (!$scope->getType($node) instanceof ErrorType && !$this->bleedingEdge) {
return [];
}

$leftName = '__PHPSTAN__LEFT__';
$rightName = '__PHPSTAN__RIGHT__';
$leftVariable = new Node\Expr\Variable($leftName);
$rightVariable = new Node\Expr\Variable($rightName);
if ($node instanceof Node\Expr\AssignOp) {
$identifier = 'assignOp';
$newNode = clone $node;
$newNode->setAttribute('phpstan_cache_printer', null);
$left = $node->var;
$right = $node->expr;
$newNode->var = $leftVariable;
$newNode->expr = $rightVariable;
} else {
$identifier = 'binaryOp';
$newNode = clone $node;
$newNode->setAttribute('phpstan_cache_printer', null);
$left = $node->left;
$right = $node->right;
$newNode->left = $leftVariable;
$newNode->right = $rightVariable;
}

if ($node instanceof Node\Expr\AssignOp\Concat || $node instanceof Node\Expr\BinaryOp\Concat) {
$callback = static fn (Type $type): bool => !$type->toString() instanceof ErrorType;
} else {
$callback = static fn (Type $type): bool => !$type->toNumber() instanceof ErrorType;
}

$leftType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$left,
'',
$callback,
)->getType();
if ($leftType instanceof ErrorType) {
return [];
}

$rightType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$right,
'',
$callback,
)->getType();
if ($rightType instanceof ErrorType) {
return [];
}

if (!$scope instanceof MutatingScope) {
throw new ShouldNotHappenException();
}

$scope = $scope
->assignVariable($leftName, $leftType, $leftType)
->assignVariable($rightName, $rightType, $rightType);

if (!$scope->getType($newNode) instanceof ErrorType) {
return [];
}

return [];
return [
RuleErrorBuilder::message(sprintf(
'Binary operation "%s" between %s and %s results in an error.',
substr(substr($this->exprPrinter->printExpr($newNode), strlen($leftName) + 2), 0, -(strlen($rightName) + 2)),
$scope->getType($left)->describe(VerbosityLevel::value()),
$scope->getType($right)->describe(VerbosityLevel::value()),
))
->line($left->getStartLine())
->identifier(sprintf('%s.invalid', $identifier))
->build(),
];
}

}
Loading
Loading