Skip to content

Commit 3d61987

Browse files
committed
[BE] Stricter ++/-- operator check
1 parent bea5772 commit 3d61987

File tree

4 files changed

+11
-36
lines changed

4 files changed

+11
-36
lines changed

changelog-2.0.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ Bleeding edge (TODO move to other sections)
107107
* Check mixed in binary operator ([#3231](https://github.com/phpstan/phpstan-src/pull/3231)), #7538, #10440, thanks @schlndh!
108108
* Check vprintf/vsprintf arguments against placeholder count ([#3126](https://github.com/phpstan/phpstan-src/pull/3126)), thanks @staabm!
109109
* Check mixed in unary operator ([#3253](https://github.com/phpstan/phpstan-src/pull/3253)), thanks @schlndh!
110-
* Stricter ++/-- operator check ([#3255](https://github.com/phpstan/phpstan-src/pull/3255)), thanks @schlndh!
111110
* Check preg_quote delimiter sanity ([#3252](https://github.com/phpstan/phpstan-src/pull/3252)), #11338, thanks @staabm!
112111
* Improved the type of the `$mode` parameter for the `count()` ([#3190](https://github.com/phpstan/phpstan-src/pull/3190)), thanks @kuma3!
113112
* Check array functions which require stringish values ([#3132](https://github.com/phpstan/phpstan-src/pull/3132)), #11141, #5848, #3694, #11111, thanks @schlndh!
@@ -141,6 +140,7 @@ Improvements 🔧
141140
* OverridingMethodRule - include template types in prototype declaring class description (https://github.com/phpstan/phpstan-src/commit/ca2c66cc4dff59ba44d52b82cb9e0aa3256240f3)
142141
* Detect overriding `@final` method in OverridingMethodRule, #9135
143142
* Improve error wording of the NonexistentOffset, BooleanAndConstantConditionRule, and BooleanOrConstantConditionRule ([#1882](https://github.com/phpstan/phpstan-src/pull/1882)), thanks @VincentLanglet!
143+
* Stricter ++/-- operator check ([#3255](https://github.com/phpstan/phpstan-src/pull/3255)), thanks @schlndh!
144144

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

conf/config.level0.neon

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ rules:
106106
- PHPStan\Rules\Methods\StaticMethodCallableRule
107107
- PHPStan\Rules\Names\UsedNamesRule
108108
- PHPStan\Rules\Operators\InvalidAssignVarRule
109+
- PHPStan\Rules\Operators\InvalidIncDecOperationRule
109110
- PHPStan\Rules\Properties\AccessPropertiesInAssignRule
110111
- PHPStan\Rules\Properties\AccessStaticPropertiesInAssignRule
111112
- PHPStan\Rules\Properties\InvalidCallablePropertyTypeRule
@@ -203,14 +204,6 @@ services:
203204
arguments:
204205
checkFunctionNameCase: %checkFunctionNameCase%
205206

206-
-
207-
class: PHPStan\Rules\Operators\InvalidIncDecOperationRule
208-
tags:
209-
- phpstan.rules.rule
210-
arguments:
211-
bleedingEdge: %featureToggles.bleedingEdge%
212-
checkThisOnly: %checkThisOnly%
213-
214207
-
215208
class: PHPStan\Rules\Properties\AccessPropertiesRule
216209
tags:

src/Rules/Operators/InvalidIncDecOperationRule.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ final class InvalidIncDecOperationRule implements Rule
2929

3030
public function __construct(
3131
private RuleLevelHelper $ruleLevelHelper,
32-
private bool $bleedingEdge,
33-
private bool $checkThisOnly,
3432
)
3533
{
3634
}
@@ -87,30 +85,16 @@ public function processNode(Node $node, Scope $scope): array
8785
];
8886
}
8987

90-
if (!$this->bleedingEdge) {
91-
if ($this->checkThisOnly) {
92-
return [];
93-
}
88+
$allowedTypes = new UnionType([new BooleanType(), new FloatType(), new IntegerType(), new StringType(), new NullType(), new ObjectType('SimpleXMLElement')]);
89+
$varType = $this->ruleLevelHelper->findTypeToCheck(
90+
$scope,
91+
$node->var,
92+
'',
93+
static fn (Type $type): bool => $allowedTypes->isSuperTypeOf($type)->yes(),
94+
)->getType();
9495

95-
$varType = $scope->getType($node->var);
96-
if (!$varType->toString() instanceof ErrorType) {
97-
return [];
98-
}
99-
if (!$varType->toNumber() instanceof ErrorType) {
100-
return [];
101-
}
102-
} else {
103-
$allowedTypes = new UnionType([new BooleanType(), new FloatType(), new IntegerType(), new StringType(), new NullType(), new ObjectType('SimpleXMLElement')]);
104-
$varType = $this->ruleLevelHelper->findTypeToCheck(
105-
$scope,
106-
$node->var,
107-
'',
108-
static fn (Type $type): bool => $allowedTypes->isSuperTypeOf($type)->yes(),
109-
)->getType();
110-
111-
if ($varType instanceof ErrorType || $allowedTypes->isSuperTypeOf($varType)->yes()) {
112-
return [];
113-
}
96+
if ($varType instanceof ErrorType || $allowedTypes->isSuperTypeOf($varType)->yes()) {
97+
return [];
11498
}
11599

116100
return [

tests/PHPStan/Rules/Operators/InvalidIncDecOperationRuleTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ protected function getRule(): Rule
2121
{
2222
return new InvalidIncDecOperationRule(
2323
new RuleLevelHelper($this->createReflectionProvider(), true, false, true, $this->checkExplicitMixed, $this->checkImplicitMixed, false),
24-
true,
25-
false,
2624
);
2725
}
2826

0 commit comments

Comments
 (0)