Skip to content

Add rules for operand in pre/post-inc/decrement #26

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

Merged
merged 1 commit into from
Jun 18, 2018
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

* Require booleans in `if`, `elseif`, ternary operator, after `!`, and on both sides of `&&` and `||`.
* Require numeric operands or arrays in `+` and numeric operands in `-`/`*`/`/`/`**`/`%`.
* Require numeric operand in `$var++`, `$var--`, `++$var`and `--$var`.
* These functions contain a `$strict` parameter for better type safety, it must be set to `true`:
* `in_array` (3rd parameter)
* `array_search` (3rd parameter)
Expand Down
4 changes: 4 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ rules:
- PHPStan\Rules\Methods\MissingMethodParameterTypehintRule
- PHPStan\Rules\Methods\MissingMethodReturnTypehintRule
- PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule
- PHPStan\Rules\Operators\OperandInArithmeticPostDecrementRule
- PHPStan\Rules\Operators\OperandInArithmeticPostIncrementRule
- PHPStan\Rules\Operators\OperandInArithmeticPreDecrementRule
- PHPStan\Rules\Operators\OperandInArithmeticPreIncrementRule
- PHPStan\Rules\Operators\OperandsInArithmeticAdditionRule
- PHPStan\Rules\Operators\OperandsInArithmeticDivisionRule
- PHPStan\Rules\Operators\OperandsInArithmeticExponentiationRule
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Operators;

use PHPStan\Rules\Rule;
use PHPStan\Type\VerbosityLevel;

abstract class OperandInArithmeticIncrementOrDecrementRule implements Rule
{

/** @var OperatorRuleHelper */
private $helper;

public function __construct(OperatorRuleHelper $helper)
{
$this->helper = $helper;
}

/**
* @param \PhpParser\Node\Expr\PreInc|\PhpParser\Node\Expr\PreDec|\PhpParser\Node\Expr\PostInc|\PhpParser\Node\Expr\PostDec $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
{
$messages = [];
$varType = $scope->getType($node->var);

if (!$this->helper->isValidForIncrementOrDecrement($scope, $node->var)) {
$messages[] = sprintf(
'Only numeric types are allowed in %s, %s given.',
$this->describeOperation(),
$varType->describe(VerbosityLevel::typeOnly())
);
}

return $messages;
}

abstract protected function describeOperation(): string;

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

namespace PHPStan\Rules\Operators;

class OperandInArithmeticPostDecrementRule extends OperandInArithmeticIncrementOrDecrementRule
{

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

protected function describeOperation(): string
{
return 'post-decrement';
}

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

namespace PHPStan\Rules\Operators;

class OperandInArithmeticPostIncrementRule extends OperandInArithmeticIncrementOrDecrementRule
{

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

protected function describeOperation(): string
{
return 'post-increment';
}

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

namespace PHPStan\Rules\Operators;

class OperandInArithmeticPreDecrementRule extends OperandInArithmeticIncrementOrDecrementRule
{

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

protected function describeOperation(): string
{
return 'pre-decrement';
}

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

namespace PHPStan\Rules\Operators;

class OperandInArithmeticPreIncrementRule extends OperandInArithmeticIncrementOrDecrementRule
{

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

protected function describeOperation(): string
{
return 'pre-increment';
}

}
22 changes: 19 additions & 3 deletions src/Rules/Operators/OperatorRuleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,37 @@ public function isValidForArithmeticOperation(Scope $scope, Expr $expr): bool
return true;
}

// already reported by PHPStan core
if ($type->toNumber() instanceof ErrorType) {
return true;
}

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

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

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

private function isSubtypeOfNumber(Scope $scope, Expr $expr): bool
{
$acceptedType = new UnionType([new IntegerType(), new FloatType()]);

$typeToCheck = $this->ruleLevelHelper->findTypeToCheck(
$type = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$expr,
'',
function (Type $type) use ($acceptedType): bool {
return $acceptedType->isSuperTypeOf($type)->yes();
}
);
$type = $typeToCheck->getType();
)->getType();

if ($type instanceof ErrorType) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Operators;

use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;

abstract class OperandInArithmeticIncrementOrDecrementRuleTest extends \PHPStan\Testing\RuleTestCase
{

protected function getRule(): Rule
{
return $this->createRule(
new OperatorRuleHelper(
new RuleLevelHelper($this->createBroker(), true, false, true)
)
);
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/increment-decrement.php'], $this->getExpectedErrors());
}

abstract protected function createRule(OperatorRuleHelper $helper): Rule;

/**
* @return mixed[][]
*/
abstract protected function getExpectedErrors(): array;

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

namespace PHPStan\Rules\Operators;

use PHPStan\Rules\Rule;

class OperandInArithmeticPostDecrementRuleTest extends OperandInArithmeticIncrementOrDecrementRuleTest
{

protected function createRule(OperatorRuleHelper $helper): Rule
{
return new OperandInArithmeticPostDecrementRule($helper);
}

/**
* {@inheritdoc}
*/
protected function getExpectedErrors(): array
{
return [
[
'Only numeric types are allowed in post-decrement, false given.',
21,
],
[
'Only numeric types are allowed in post-decrement, string given.',
22,
],
[
'Only numeric types are allowed in post-decrement, null given.',
23,
],
[
'Only numeric types are allowed in post-decrement, stdClass given.',
24,
],
[
'Only numeric types are allowed in post-decrement, int|stdClass|string given.',
26,
],
];
}

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

namespace PHPStan\Rules\Operators;

use PHPStan\Rules\Rule;

class OperandInArithmeticPostIncrementRuleTest extends OperandInArithmeticIncrementOrDecrementRuleTest
{

protected function createRule(OperatorRuleHelper $helper): Rule
{
return new OperandInArithmeticPostIncrementRule($helper);
}

/**
* {@inheritdoc}
*/
protected function getExpectedErrors(): array
{
return [
[
'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,
],
[
'Only numeric types are allowed in post-increment, stdClass given.',
35,
],
[
'Only numeric types are allowed in post-increment, int|stdClass|string given.',
37,
],
];
}

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

namespace PHPStan\Rules\Operators;

use PHPStan\Rules\Rule;

class OperandInArithmeticPreDecrementRuleTest extends OperandInArithmeticIncrementOrDecrementRuleTest
{

protected function createRule(OperatorRuleHelper $helper): Rule
{
return new OperandInArithmeticPreDecrementRule($helper);
}

/**
* {@inheritdoc}
*/
protected function getExpectedErrors(): array
{
return [
[
'Only numeric types are allowed in pre-decrement, false given.',
43,
],
[
'Only numeric types are allowed in pre-decrement, string given.',
44,
],
[
'Only numeric types are allowed in pre-decrement, null given.',
45,
],
[
'Only numeric types are allowed in pre-decrement, stdClass given.',
46,
],
[
'Only numeric types are allowed in pre-decrement, int|stdClass|string given.',
48,
],
];
}

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

namespace PHPStan\Rules\Operators;

use PHPStan\Rules\Rule;

class OperandInArithmeticPreIncrementRuleTest extends OperandInArithmeticIncrementOrDecrementRuleTest
{

protected function createRule(OperatorRuleHelper $helper): Rule
{
return new OperandInArithmeticPreIncrementRule($helper);
}

/**
* {@inheritdoc}
*/
protected function getExpectedErrors(): array
{
return [
[
'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,
],
[
'Only numeric types are allowed in pre-increment, stdClass given.',
57,
],
[
'Only numeric types are allowed in pre-increment, int|stdClass|string given.',
59,
],
];
}

}
Loading