Skip to content

Commit f9cd2a0

Browse files
committed
[BE] Check mixed in binary and unary operators
1 parent 3d61987 commit f9cd2a0

File tree

6 files changed

+28
-50
lines changed

6 files changed

+28
-50
lines changed

changelog-2.0.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ Bleeding edge (TODO move to other sections)
104104
* Check `@param-immediately-invoked-callable` and `@param-later-invoked-callable` (https://github.com/phpstan/phpstan-src/commit/580a6add422f4e34191df9e7a77ba1655e914bda), #10932
105105
* RegularExpressionPatternRule: validate preg_quote'd patterns ([#3270](https://github.com/phpstan/phpstan-src/pull/3270)), thanks @staabm!
106106
* Report useless return values of function calls like `var_export` without `$return=true` ([#3225](https://github.com/phpstan/phpstan-src/pull/3225)), #11320, thanks @staabm!
107-
* Check mixed in binary operator ([#3231](https://github.com/phpstan/phpstan-src/pull/3231)), #7538, #10440, thanks @schlndh!
108107
* Check vprintf/vsprintf arguments against placeholder count ([#3126](https://github.com/phpstan/phpstan-src/pull/3126)), thanks @staabm!
109-
* Check mixed in unary operator ([#3253](https://github.com/phpstan/phpstan-src/pull/3253)), thanks @schlndh!
110108
* Check preg_quote delimiter sanity ([#3252](https://github.com/phpstan/phpstan-src/pull/3252)), #11338, thanks @staabm!
111109
* Improved the type of the `$mode` parameter for the `count()` ([#3190](https://github.com/phpstan/phpstan-src/pull/3190)), thanks @kuma3!
112110
* Check array functions which require stringish values ([#3132](https://github.com/phpstan/phpstan-src/pull/3132)), #11141, #5848, #3694, #11111, thanks @schlndh!
@@ -141,6 +139,8 @@ Improvements 🔧
141139
* Detect overriding `@final` method in OverridingMethodRule, #9135
142140
* Improve error wording of the NonexistentOffset, BooleanAndConstantConditionRule, and BooleanOrConstantConditionRule ([#1882](https://github.com/phpstan/phpstan-src/pull/1882)), thanks @VincentLanglet!
143141
* Stricter ++/-- operator check ([#3255](https://github.com/phpstan/phpstan-src/pull/3255)), thanks @schlndh!
142+
* Check mixed in binary operator ([#3231](https://github.com/phpstan/phpstan-src/pull/3231)), #7538, #10440, thanks @schlndh!
143+
* Check mixed in unary operator ([#3253](https://github.com/phpstan/phpstan-src/pull/3253)), thanks @schlndh!
144144

145145
Bugfixes 🐛
146146
=====================

conf/config.level2.neon

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ rules:
3939
- PHPStan\Rules\Generics\UsedTraitsRule
4040
- PHPStan\Rules\Methods\CallPrivateMethodThroughStaticRule
4141
- PHPStan\Rules\Methods\IncompatibleDefaultParameterTypeRule
42+
- PHPStan\Rules\Operators\InvalidBinaryOperationRule
4243
- PHPStan\Rules\Operators\InvalidComparisonOperationRule
44+
- PHPStan\Rules\Operators\InvalidUnaryOperationRule
4345
- PHPStan\Rules\PhpDoc\FunctionConditionalReturnTypeRule
4446
- PHPStan\Rules\PhpDoc\MethodConditionalReturnTypeRule
4547
- PHPStan\Rules\PhpDoc\FunctionAssertRule
@@ -140,15 +142,3 @@ services:
140142

141143
-
142144
class: PHPStan\Rules\Pure\PureMethodRule
143-
-
144-
class: PHPStan\Rules\Operators\InvalidBinaryOperationRule
145-
arguments:
146-
bleedingEdge: %featureToggles.bleedingEdge%
147-
tags:
148-
- phpstan.rules.rule
149-
-
150-
class: PHPStan\Rules\Operators\InvalidUnaryOperationRule
151-
arguments:
152-
bleedingEdge: %featureToggles.bleedingEdge%
153-
tags:
154-
- phpstan.rules.rule

src/Rules/Operators/InvalidBinaryOperationRule.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ final class InvalidBinaryOperationRule implements Rule
2727
public function __construct(
2828
private ExprPrinter $exprPrinter,
2929
private RuleLevelHelper $ruleLevelHelper,
30-
private bool $bleedingEdge,
3130
)
3231
{
3332
}
@@ -46,10 +45,6 @@ public function processNode(Node $node, Scope $scope): array
4645
return [];
4746
}
4847

49-
if (!$scope->getType($node) instanceof ErrorType && !$this->bleedingEdge) {
50-
return [];
51-
}
52-
5348
$leftName = '__PHPSTAN__LEFT__';
5449
$rightName = '__PHPSTAN__RIGHT__';
5550
$leftVariable = new Node\Expr\Variable($leftName);

src/Rules/Operators/InvalidUnaryOperationRule.php

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ final class InvalidUnaryOperationRule implements Rule
2323

2424
public function __construct(
2525
private RuleLevelHelper $ruleLevelHelper,
26-
private bool $bleedingEdge,
2726
)
2827
{
2928
}
@@ -43,38 +42,34 @@ public function processNode(Node $node, Scope $scope): array
4342
return [];
4443
}
4544

46-
if ($this->bleedingEdge) {
47-
$varName = '__PHPSTAN__LEFT__';
48-
$variable = new Node\Expr\Variable($varName);
49-
$newNode = clone $node;
50-
$newNode->setAttribute('phpstan_cache_printer', null);
51-
$newNode->expr = $variable;
45+
$varName = '__PHPSTAN__LEFT__';
46+
$variable = new Node\Expr\Variable($varName);
47+
$newNode = clone $node;
48+
$newNode->setAttribute('phpstan_cache_printer', null);
49+
$newNode->expr = $variable;
5250

53-
if ($node instanceof Node\Expr\BitwiseNot) {
54-
$callback = static fn (Type $type): bool => $type->isString()->yes() || $type->isInteger()->yes() || $type->isFloat()->yes();
55-
} else {
56-
$callback = static fn (Type $type): bool => !$type->toNumber() instanceof ErrorType;
57-
}
51+
if ($node instanceof Node\Expr\BitwiseNot) {
52+
$callback = static fn (Type $type): bool => $type->isString()->yes() || $type->isInteger()->yes() || $type->isFloat()->yes();
53+
} else {
54+
$callback = static fn (Type $type): bool => !$type->toNumber() instanceof ErrorType;
55+
}
5856

59-
$exprType = $this->ruleLevelHelper->findTypeToCheck(
60-
$scope,
61-
$node->expr,
62-
'',
63-
$callback,
64-
)->getType();
65-
if ($exprType instanceof ErrorType) {
66-
return [];
67-
}
57+
$exprType = $this->ruleLevelHelper->findTypeToCheck(
58+
$scope,
59+
$node->expr,
60+
'',
61+
$callback,
62+
)->getType();
63+
if ($exprType instanceof ErrorType) {
64+
return [];
65+
}
6866

69-
if (!$scope instanceof MutatingScope) {
70-
throw new ShouldNotHappenException();
71-
}
67+
if (!$scope instanceof MutatingScope) {
68+
throw new ShouldNotHappenException();
69+
}
7270

73-
$scope = $scope->assignVariable($varName, $exprType, $exprType, TrinaryLogic::createYes());
74-
if (!$scope->getType($newNode) instanceof ErrorType) {
75-
return [];
76-
}
77-
} elseif (!$scope->getType($node) instanceof ErrorType) {
71+
$scope = $scope->assignVariable($varName, $exprType, $exprType, TrinaryLogic::createYes());
72+
if (!$scope->getType($newNode) instanceof ErrorType) {
7873
return [];
7974
}
8075

tests/PHPStan/Rules/Operators/InvalidBinaryOperationRuleTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ protected function getRule(): Rule
2424
return new InvalidBinaryOperationRule(
2525
new ExprPrinter(new Printer()),
2626
new RuleLevelHelper($this->createReflectionProvider(), true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false),
27-
true,
2827
);
2928
}
3029

tests/PHPStan/Rules/Operators/InvalidUnaryOperationRuleTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ protected function getRule(): Rule
2121
{
2222
return new InvalidUnaryOperationRule(
2323
new RuleLevelHelper($this->createReflectionProvider(), true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false),
24-
true,
2524
);
2625
}
2726

0 commit comments

Comments
 (0)