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

forbidIncrementDecrementOnNonInteger #179

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ parameters:
forbidIdenticalClassComparison:
enabled: true
blacklist: ['DateTimeInterface']
forbidIncrementDecrementOnNonInteger:
enabled: true
forbidMatchDefaultArmForEnums:
enabled: true
forbidMethodCallOnMixed:
Expand Down Expand Up @@ -516,6 +518,15 @@ parameters:
- Ramsey\Uuid\UuidInterface
```

### forbidIncrementDecrementOnNonInteger
- Denies using `$i++`, `$i--`, `++$i`, `--$i` with any non-integer
- PHP itself is leading towards stricter behaviour here and soft-deprecated **some** non-integer usages in 8.3, see [RFC](https://wiki.php.net/rfc/saner-inc-dec-operators)

```php
$value = '2e0';
$value++; // would be float(3), denied
```

### forbidMatchDefaultArmForEnums
- Denies using default arm in `match()` construct when native enum is passed as subject
- This rules makes sense only as a complement of [native phpstan rule](https://github.com/phpstan/phpstan-src/blob/1.7.x/src/Rules/Comparison/MatchExpressionRule.php#L94) that guards that all enum cases are handled in match arms
Expand Down
9 changes: 9 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ parameters:
forbidIdenticalClassComparison:
enabled: true
blacklist: ['DateTimeInterface']
forbidIncrementDecrementOnNonInteger:
enabled: true
forbidMatchDefaultArmForEnums:
enabled: true
forbidMethodCallOnMixed:
Expand Down Expand Up @@ -156,6 +158,9 @@ parametersSchema:
enabled: bool()
blacklist: arrayOf(string())
])
forbidIncrementDecrementOnNonInteger: structure([
enabled: bool()
])
forbidMatchDefaultArmForEnums: structure([
enabled: bool()
])
Expand Down Expand Up @@ -245,6 +250,8 @@ conditionalTags:
phpstan.rules.rule: %shipmonkRules.forbidFetchOnMixed.enabled%
ShipMonk\PHPStan\Rule\ForbidIdenticalClassComparisonRule:
phpstan.rules.rule: %shipmonkRules.forbidIdenticalClassComparison.enabled%
ShipMonk\PHPStan\Rule\ForbidIncrementDecrementOnNonIntegerRule:
phpstan.rules.rule: %shipmonkRules.forbidIncrementDecrementOnNonInteger.enabled%
ShipMonk\PHPStan\Rule\ForbidMatchDefaultArmForEnumsRule:
phpstan.rules.rule: %shipmonkRules.forbidMatchDefaultArmForEnums.enabled%
ShipMonk\PHPStan\Rule\ForbidMethodCallOnMixedRule:
Expand Down Expand Up @@ -348,6 +355,8 @@ services:
class: ShipMonk\PHPStan\Rule\ForbidIdenticalClassComparisonRule
arguments:
blacklist: %shipmonkRules.forbidIdenticalClassComparison.blacklist%
-
class: ShipMonk\PHPStan\Rule\ForbidIncrementDecrementOnNonIntegerRule
-
class: ShipMonk\PHPStan\Rule\ForbidMethodCallOnMixedRule
arguments:
Expand Down
89 changes: 89 additions & 0 deletions src/Rule/ForbidIncrementDecrementOnNonIntegerRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php declare(strict_types = 1);

namespace ShipMonk\PHPStan\Rule;

use LogicException;
use PhpParser\Node;
use PhpParser\Node\Expr\PostDec;
use PhpParser\Node\Expr\PostInc;
use PhpParser\Node\Expr\PreDec;
use PhpParser\Node\Expr\PreInc;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\VerbosityLevel;
use function get_class;
use function sprintf;

/**
* @implements Rule<Node>
*/
class ForbidIncrementDecrementOnNonIntegerRule implements Rule
{

public function getNodeType(): string
{
return Node::class;
}

/**
* @return list<RuleError>
*/
public function processNode(Node $node, Scope $scope): array
{
if (
$node instanceof PostInc
|| $node instanceof PostDec
|| $node instanceof PreInc
|| $node instanceof PreDec
) {
return $this->process($node, $scope);
}

return [];
}

/**
* @param PostInc|PostDec|PreInc|PreDec $node
* @return list<RuleError>
*/
private function process(Node $node, Scope $scope): array
{
$exprType = $scope->getType($node->var);

if (!$exprType->isInteger()->yes()) {
$errorMessage = sprintf(
'Using %s over non-integer (%s)',
$this->getIncDecSymbol($node),
$exprType->describe(VerbosityLevel::typeOnly()),
);
$error = RuleErrorBuilder::message($errorMessage)
->identifier('shipmonk.incrementDecrementOnNonInteger')
->build();
return [$error];
}

return [];
}

/**
* @param PostInc|PostDec|PreInc|PreDec $node
*/
private function getIncDecSymbol(Node $node): string
{
switch (get_class($node)) {
janedbal marked this conversation as resolved.
Show resolved Hide resolved
case PostInc::class:
case PreInc::class:
return '++';

case PostDec::class:
case PreDec::class:
return '--';

default:
throw new LogicException('Unexpected node given: ' . get_class($node));
}
}

}
24 changes: 24 additions & 0 deletions tests/Rule/ForbidIncrementDecrementOnNonIntegerRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace ShipMonk\PHPStan\Rule;

use PHPStan\Rules\Rule;
use ShipMonk\PHPStan\RuleTestCase;

/**
* @extends RuleTestCase<ForbidIncrementDecrementOnNonIntegerRule>
*/
class ForbidIncrementDecrementOnNonIntegerRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new ForbidIncrementDecrementOnNonIntegerRule();
}

public function testClass(): void
{
$this->analyseFile(__DIR__ . '/data/ForbidIncrementDecrementOnNonIntegerRule/code.php');
}

}
20 changes: 20 additions & 0 deletions tests/Rule/data/ForbidIncrementDecrementOnNonIntegerRule/code.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);

namespace ForbidIncrementDecrementOnNonIntegerRule;


class IncDec
{

public function test(int $int, string $string, float $float, int|string $union, array $array, $mixed)
{
$int++;
$string++; // error: Using ++ over non-integer (string)
$float++; // error: Using ++ over non-integer (float)
$union++; // error: Using ++ over non-integer (int|string)
$array++; // error: Using ++ over non-integer (array)
$mixed++; // error: Using ++ over non-integer (mixed)
}
}


Loading