Skip to content

Commit

Permalink
Allow to increment strings
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet authored Feb 6, 2023
1 parent a7ac762 commit 66b378f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 22 deletions.
12 changes: 0 additions & 12 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,6 @@ parameters:
count: 1
path: src/Rules/ForeachLoop/OverwriteVariablesWithForeachRule.php

-
message: "#^Class PHPStan\\\\Rules\\\\Operators\\\\OperandInArithmeticIncrementOrDecrementRule implements generic interface PHPStan\\\\Rules\\\\Rule but does not specify its types\\: TNodeType$#"
count: 1
path: src/Rules/Operators/OperandInArithmeticIncrementOrDecrementRule.php

-
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\\(\\)$#"
count: 1
path: src/Rules/Operators/OperandInArithmeticIncrementOrDecrementRule.php

-
message: "#^Class PHPStan\\\\Rules\\\\Operators\\\\OperandsInArithmeticAdditionRule implements generic interface PHPStan\\\\Rules\\\\Rule but does not specify its types\\: TNodeType$#"
count: 1
Expand Down Expand Up @@ -516,5 +506,3 @@ parameters:
message: "#^Method PHPStan\\\\Rules\\\\VariableVariables\\\\VariableVariablesRuleTest\\:\\:getRule\\(\\) return type with generic interface PHPStan\\\\Rules\\\\Rule does not specify its types\\: TNodeType$#"
count: 1
path: tests/Rules/VariableVariables/VariableVariablesRuleTest.php


Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
use PHPStan\Type\VerbosityLevel;
use function sprintf;

/**
* @phpstan-template TNodeType of PreInc|PreDec|PostInc|PostDec
* @phpstan-implements Rule<TNodeType>
*/
abstract class OperandInArithmeticIncrementOrDecrementRule implements Rule
{

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

if (!$this->helper->isValidForIncrementOrDecrement($scope, $node->var)) {
if (
($node instanceof PreInc || $node instanceof PostInc)
&& !$this->helper->isValidForIncrement($scope, $node->var)
|| ($node instanceof PreDec || $node instanceof PostDec)
&& !$this->helper->isValidForDecrement($scope, $node->var)
) {
$messages[] = sprintf(
'Only numeric types are allowed in %s, %s given.',
$this->describeOperation(),
Expand Down
3 changes: 3 additions & 0 deletions src/Rules/Operators/OperandInArithmeticPostDecrementRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use PhpParser\Node\Expr\PostDec;

/**
* @phpstan-extends OperandInArithmeticIncrementOrDecrementRule<PostDec>
*/
class OperandInArithmeticPostDecrementRule extends OperandInArithmeticIncrementOrDecrementRule
{

Expand Down
3 changes: 3 additions & 0 deletions src/Rules/Operators/OperandInArithmeticPostIncrementRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use PhpParser\Node\Expr\PostInc;

/**
* @phpstan-extends OperandInArithmeticIncrementOrDecrementRule<PostInc>
*/
class OperandInArithmeticPostIncrementRule extends OperandInArithmeticIncrementOrDecrementRule
{

Expand Down
3 changes: 3 additions & 0 deletions src/Rules/Operators/OperandInArithmeticPreDecrementRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use PhpParser\Node\Expr\PreDec;

/**
* @phpstan-extends OperandInArithmeticIncrementOrDecrementRule<PreDec>
*/
class OperandInArithmeticPreDecrementRule extends OperandInArithmeticIncrementOrDecrementRule
{

Expand Down
3 changes: 3 additions & 0 deletions src/Rules/Operators/OperandInArithmeticPreIncrementRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use PhpParser\Node\Expr\PreInc;

/**
* @phpstan-extends OperandInArithmeticIncrementOrDecrementRule<PreInc>
*/
class OperandInArithmeticPreIncrementRule extends OperandInArithmeticIncrementOrDecrementRule
{

Expand Down
17 changes: 16 additions & 1 deletion src/Rules/Operators/OperatorRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,22 @@ public function isValidForArithmeticOperation(Scope $scope, Expr $expr): bool
return $this->isSubtypeOfNumber($scope, $expr);
}

public function isValidForIncrementOrDecrement(Scope $scope, Expr $expr): bool
public function isValidForIncrement(Scope $scope, Expr $expr): bool
{
$type = $scope->getType($expr);
if ($type instanceof MixedType) {
return true;
}

if ($type->isString()->yes()) {
// Because `$a = 'a'; $a++;` is valid
return true;
}

return $this->isSubtypeOfNumber($scope, $expr);
}

public function isValidForDecrement(Scope $scope, Expr $expr): bool
{
$type = $scope->getType($expr);
if ($type instanceof MixedType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ protected function getExpectedErrors(): array
'Only numeric types are allowed in post-increment, false given.',
32,
],
[
'Only numeric types are allowed in post-increment, string given.',
33,
],
[
'Only numeric types are allowed in post-increment, null given.',
34,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ protected function getExpectedErrors(): array
'Only numeric types are allowed in pre-increment, false given.',
54,
],
[
'Only numeric types are allowed in pre-increment, string given.',
55,
],
[
'Only numeric types are allowed in pre-increment, null given.',
56,
Expand Down

0 comments on commit 66b378f

Please sign in to comment.