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

Implement InvalidDivisionOperationRule #3417

Closed
wants to merge 1 commit into from
Closed
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
1 change: 1 addition & 0 deletions conf/bleedingEdge.neon
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,6 @@ parameters:
explicitThrow: true
absentTypeChecks: true
requireFileExists: true
divisionByZero: true
stubFiles:
- ../stubs/bleedingEdge/Rule.stub
5 changes: 5 additions & 0 deletions conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ conditionalTags:
phpstan.rules.rule: %featureToggles.requireFileExists%
PHPStan\Rules\Classes\LocalTypeTraitUseAliasesRule:
phpstan.rules.rule: %featureToggles.absentTypeChecks%
PHPStan\Rules\Operators\InvalidDivisionOperationRule:
phpstan.rules.rule: %featureToggles.divisionByZero%

rules:
- PHPStan\Rules\Api\ApiInstantiationRule
Expand Down Expand Up @@ -320,3 +322,6 @@ services:
class: PHPStan\Rules\Keywords\RequireFileExistsRule
arguments:
currentWorkingDirectory: %currentWorkingDirectory%

-
class: PHPStan\Rules\Operators\InvalidDivisionOperationRule
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ parameters:
tooWidePropertyType: false
explicitThrow: false
absentTypeChecks: false
divisionByZero: false
fileExtensions:
- php
checkAdvancedIsset: false
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ parametersSchema:
explicitThrow: bool()
absentTypeChecks: bool()
requireFileExists: bool()
divisionByZero: bool()
])
fileExtensions: listOf(string())
checkAdvancedIsset: bool()
Expand Down
62 changes: 62 additions & 0 deletions src/Rules/Operators/InvalidDivisionOperationRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Operators;

use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\UnionType;
use PHPStan\Type\VerbosityLevel;
use function sprintf;

/**
* @implements Rule<Node\Expr>
*/
final class InvalidDivisionOperationRule implements Rule
{

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

public function processNode(Node $node, Scope $scope): array
{
if ($node instanceof Node\Expr\AssignOp\Div || $node instanceof Node\Expr\AssignOp\Mod) {
$identifier = 'assignOp';
$left = $node->var;
$right = $node->expr;
} elseif ($node instanceof Node\Expr\BinaryOp\Div || $node instanceof Node\Expr\BinaryOp\Mod) {
$identifier = 'binaryOp';
$left = $node->left;
$right = $node->right;
} else {
return [];
}

$leftType = $scope->getType($left);
$rightType = $scope->getType($right);

$zeroType = new UnionType([new ConstantIntegerType(0), new ConstantFloatType(0.0)]);
if (!$rightType->isSuperTypeOf($zeroType)->maybe()) {
// yes() is handled by InvalidBinaryOperationRule
return [];
}

return [
RuleErrorBuilder::message(sprintf(
'Binary operation "%s" between %s and %s might result in an error.',
$node instanceof Node\Expr\AssignOp\Div || $node instanceof Node\Expr\BinaryOp\Div ? '/' : '%',
$leftType->describe(VerbosityLevel::value()),
$rightType->describe(VerbosityLevel::value()),
))
->line($left->getStartLine())
->identifier(sprintf('%s.invalid', $identifier))
->build(),
];
}

}
45 changes: 45 additions & 0 deletions tests/PHPStan/Rules/Operators/InvalidDivisionOperationRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Operators;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/**
* @extends RuleTestCase<InvalidDivisionOperationRule>
*/
class InvalidDivisionOperationRuleTest extends RuleTestCase
{

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

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/invalid-division.php'], [
[
'Binary operation "/" between int and int<0, max> might result in an error.',
12,
],
[
'Binary operation "/" between int and int<0, max> might result in an error.',
13,
],
[
'Binary operation "%" between int and int<0, max> might result in an error.',
21,
],
[
'Binary operation "%" between int and int<0, max> might result in an error.',
22,
],
[
'Binary operation "/" between mixed and int<0, max> might result in an error.',
58,
],
]);
}

}
60 changes: 60 additions & 0 deletions tests/PHPStan/Rules/Operators/data/invalid-division.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace InvalidDivision;

class HelloWorld
{
/**
* @param int<0, max> $x
*/
public function doDiv(int $y, $x): void
{
echo $y / $x;
$y /= $x;
}

/**
* @param int<0, max> $x
*/
public function doMod(int $y, $x): void
{
echo $y % $x;
$y %= $x;
}

public function doMixed(int $y, $mixed): void
{
echo $y % $mixed;
$y %= $mixed;
}

/**
* @param int<min, -1> $negative
* @param int<1, max> $positive
*/
public function doRanges(int $y, $x): void
{
echo $y / $x;
$y /= $x;
}
}

function ($i, int $x): void {
$a = range(1, 3/2);

if ($x !== 0) {
echo $i / $x;
}

if ($x != 0) {
echo $i / $x;
}

if ($x > 0) {
echo $i / $x;
}

if ($x >= 0) {
echo $i / $x;
}
};
Loading