Skip to content

Commit 66b378f

Browse files
Allow to increment strings
1 parent a7ac762 commit 66b378f

9 files changed

+38
-22
lines changed

Diff for: phpstan-baseline.neon

-12
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,6 @@ parameters:
102102
count: 1
103103
path: src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php
104104

105-
-
106-
message: "#^Class PHPStan\\\\Rules\\\\Operators\\\\OperandInArithmeticIncrementOrDecrementRule implements generic interface PHPStan\\\\Rules\\\\Rule but does not specify its types\\: TNodeType$#"
107-
count: 1
108-
path: src/Rules/Operators/OperandInArithmeticIncrementOrDecrementRule.php
109-
110-
-
111-
message: "#^Parameter \\#1 \\$node \\(PhpParser\\\\Node\\\\Expr\\\\PostDec\\|PhpParser\\\\Node\\\\Expr\\\\PostInc\\|PhpParser\\\\Node\\\\Expr\\\\PreDec\\|PhpParser\\\\Node\\\\Expr\\\\PreInc\\) of method PHPStan\\\\Rules\\\\Operators\\\\OperandInArithmeticIncrementOrDecrementRule\\:\\:processNode\\(\\) should be contravariant with parameter \\$node \\(PhpParser\\\\Node\\) of method PHPStan\\\\Rules\\\\Rule\\<PhpParser\\\\Node\\>\\:\\:processNode\\(\\)$#"
112-
count: 1
113-
path: src/Rules/Operators/OperandInArithmeticIncrementOrDecrementRule.php
114-
115105
-
116106
message: "#^Class PHPStan\\\\Rules\\\\Operators\\\\OperandsInArithmeticAdditionRule implements generic interface PHPStan\\\\Rules\\\\Rule but does not specify its types\\: TNodeType$#"
117107
count: 1
@@ -516,5 +506,3 @@ parameters:
516506
message: "#^Method PHPStan\\\\Rules\\\\VariableVariables\\\\VariableVariablesRuleTest\\:\\:getRule\\(\\) return type with generic interface PHPStan\\\\Rules\\\\Rule does not specify its types\\: TNodeType$#"
517507
count: 1
518508
path: tests/Rules/VariableVariables/VariableVariablesRuleTest.php
519-
520-

Diff for: src/Rules/Operators/OperandInArithmeticIncrementOrDecrementRule.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
use PHPStan\Type\VerbosityLevel;
1313
use function sprintf;
1414

15+
/**
16+
* @phpstan-template TNodeType of PreInc|PreDec|PostInc|PostDec
17+
* @phpstan-implements Rule<TNodeType>
18+
*/
1519
abstract class OperandInArithmeticIncrementOrDecrementRule implements Rule
1620
{
1721

@@ -32,7 +36,12 @@ public function processNode(Node $node, Scope $scope): array
3236
$messages = [];
3337
$varType = $scope->getType($node->var);
3438

35-
if (!$this->helper->isValidForIncrementOrDecrement($scope, $node->var)) {
39+
if (
40+
($node instanceof PreInc || $node instanceof PostInc)
41+
&& !$this->helper->isValidForIncrement($scope, $node->var)
42+
|| ($node instanceof PreDec || $node instanceof PostDec)
43+
&& !$this->helper->isValidForDecrement($scope, $node->var)
44+
) {
3645
$messages[] = sprintf(
3746
'Only numeric types are allowed in %s, %s given.',
3847
$this->describeOperation(),

Diff for: src/Rules/Operators/OperandInArithmeticPostDecrementRule.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PhpParser\Node\Expr\PostDec;
66

7+
/**
8+
* @phpstan-extends OperandInArithmeticIncrementOrDecrementRule<PostDec>
9+
*/
710
class OperandInArithmeticPostDecrementRule extends OperandInArithmeticIncrementOrDecrementRule
811
{
912

Diff for: src/Rules/Operators/OperandInArithmeticPostIncrementRule.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PhpParser\Node\Expr\PostInc;
66

7+
/**
8+
* @phpstan-extends OperandInArithmeticIncrementOrDecrementRule<PostInc>
9+
*/
710
class OperandInArithmeticPostIncrementRule extends OperandInArithmeticIncrementOrDecrementRule
811
{
912

Diff for: src/Rules/Operators/OperandInArithmeticPreDecrementRule.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PhpParser\Node\Expr\PreDec;
66

7+
/**
8+
* @phpstan-extends OperandInArithmeticIncrementOrDecrementRule<PreDec>
9+
*/
710
class OperandInArithmeticPreDecrementRule extends OperandInArithmeticIncrementOrDecrementRule
811
{
912

Diff for: src/Rules/Operators/OperandInArithmeticPreIncrementRule.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PhpParser\Node\Expr\PreInc;
66

7+
/**
8+
* @phpstan-extends OperandInArithmeticIncrementOrDecrementRule<PreInc>
9+
*/
710
class OperandInArithmeticPreIncrementRule extends OperandInArithmeticIncrementOrDecrementRule
811
{
912

Diff for: src/Rules/Operators/OperatorRuleHelper.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,22 @@ public function isValidForArithmeticOperation(Scope $scope, Expr $expr): bool
4242
return $this->isSubtypeOfNumber($scope, $expr);
4343
}
4444

45-
public function isValidForIncrementOrDecrement(Scope $scope, Expr $expr): bool
45+
public function isValidForIncrement(Scope $scope, Expr $expr): bool
46+
{
47+
$type = $scope->getType($expr);
48+
if ($type instanceof MixedType) {
49+
return true;
50+
}
51+
52+
if ($type->isString()->yes()) {
53+
// Because `$a = 'a'; $a++;` is valid
54+
return true;
55+
}
56+
57+
return $this->isSubtypeOfNumber($scope, $expr);
58+
}
59+
60+
public function isValidForDecrement(Scope $scope, Expr $expr): bool
4661
{
4762
$type = $scope->getType($expr);
4863
if ($type instanceof MixedType) {

Diff for: tests/Rules/Operators/OperandInArithmeticPostIncrementRuleTest.php

-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ protected function getExpectedErrors(): array
2222
'Only numeric types are allowed in post-increment, false given.',
2323
32,
2424
],
25-
[
26-
'Only numeric types are allowed in post-increment, string given.',
27-
33,
28-
],
2925
[
3026
'Only numeric types are allowed in post-increment, null given.',
3127
34,

Diff for: tests/Rules/Operators/OperandInArithmeticPreIncrementRuleTest.php

-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ protected function getExpectedErrors(): array
2222
'Only numeric types are allowed in pre-increment, false given.',
2323
54,
2424
],
25-
[
26-
'Only numeric types are allowed in pre-increment, string given.',
27-
55,
28-
],
2925
[
3026
'Only numeric types are allowed in pre-increment, null given.',
3127
56,

0 commit comments

Comments
 (0)