Skip to content

Rules to only allow loose comparison with numeric operands #41

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

Closed
wants to merge 5 commits 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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,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`.
* Require numeric operands or `DateTimeInterface` objects in loose comparison `==`/`!=`/`<`/`>`/`<=`/`>=`/`<=>`. Configure this by setting `allowedLooseComparison` to any (union) type (default `int|float|DateTimeInterface`).
* 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
14 changes: 14 additions & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ parameters:
checkMissingClosureNativeReturnTypehintRule: true
reportMaybesInMethodSignatures: true
reportStaticMethodSignatures: true
allowedLooseComparison: int|float|DateTimeInterface

parametersSchema:
allowedLooseComparison: string()

rules:
- PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule
Expand All @@ -33,6 +37,14 @@ rules:
- PHPStan\Rules\Operators\OperandsInArithmeticModuloRule
- PHPStan\Rules\Operators\OperandsInArithmeticMultiplicationRule
- PHPStan\Rules\Operators\OperandsInArithmeticSubtractionRule
- PHPStan\Rules\Operators\OperandsInComparisonEqualRule
- PHPStan\Rules\Operators\OperandsInComparisonGreaterOrEqualRule
- PHPStan\Rules\Operators\OperandsInComparisonGreaterRule
- PHPStan\Rules\Operators\OperandsInComparisonNotEqualRule
- PHPStan\Rules\Operators\OperandsInComparisonSmallerOrEqualRule
- PHPStan\Rules\Operators\OperandsInComparisonSmallerRule
- PHPStan\Rules\Operators\OperandsInComparisonSpaceshipRule
- PHPStan\Rules\Properties\MissingPropertyTypehintRule
- PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule
- PHPStan\Rules\StrictCalls\StrictFunctionCallsRule
- PHPStan\Rules\SwitchConditions\MatchingTypeInSwitchCaseConditionRule
Expand All @@ -46,6 +58,8 @@ services:
class: PHPStan\Rules\BooleansInConditions\BooleanRuleHelper
-
class: PHPStan\Rules\Operators\OperatorRuleHelper
arguments:
allowedLooseComparison: %allowedLooseComparison%
-
class: PHPStan\Rules\VariableVariables\VariablePropertyFetchRule
arguments:
Expand Down
52 changes: 52 additions & 0 deletions src/Rules/Operators/OperandsInComparisonEqualRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Operators;

use PHPStan\Type\VerbosityLevel;

class OperandsInComparisonEqualRule implements \PHPStan\Rules\Rule
{

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

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

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

/**
* @param \PhpParser\Node\Expr\BinaryOp\Equal $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
{
$leftType = $scope->getType($node->left);
$rightType = $scope->getType($node->right);

$messages = [];
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) {
$messages[] = sprintf(
'Only %s is allowed in ==, %s given on the left side.',
$this->helper->getAllowedLooseComparison(),
$leftType->describe(VerbosityLevel::typeOnly())
);
}
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) {
$messages[] = sprintf(
'Only %s is allowed in ==, %s given on the right side.',
$this->helper->getAllowedLooseComparison(),
$rightType->describe(VerbosityLevel::typeOnly())
);
}

return $messages;
}

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

namespace PHPStan\Rules\Operators;

use PHPStan\Type\VerbosityLevel;

class OperandsInComparisonGreaterOrEqualRule implements \PHPStan\Rules\Rule
{

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

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

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

/**
* @param \PhpParser\Node\Expr\BinaryOp\Equal $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
{
$leftType = $scope->getType($node->left);
$rightType = $scope->getType($node->right);

$messages = [];
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) {
$messages[] = sprintf(
'Only %s is allowed in >=, %s given on the left side.',
$this->helper->getAllowedLooseComparison(),
$leftType->describe(VerbosityLevel::typeOnly())
);
}
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) {
$messages[] = sprintf(
'Only %s is allowed in >=, %s given on the right side.',
$this->helper->getAllowedLooseComparison(),
$rightType->describe(VerbosityLevel::typeOnly())
);
}

return $messages;
}

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

namespace PHPStan\Rules\Operators;

use PHPStan\Type\VerbosityLevel;

class OperandsInComparisonGreaterRule implements \PHPStan\Rules\Rule
{

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

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

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

/**
* @param \PhpParser\Node\Expr\BinaryOp\Equal $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
{
$leftType = $scope->getType($node->left);
$rightType = $scope->getType($node->right);

$messages = [];
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) {
$messages[] = sprintf(
'Only %s is allowed in >, %s given on the left side.',
$this->helper->getAllowedLooseComparison(),
$leftType->describe(VerbosityLevel::typeOnly())
);
}
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) {
$messages[] = sprintf(
'Only %s is allowed in >, %s given on the right side.',
$this->helper->getAllowedLooseComparison(),
$rightType->describe(VerbosityLevel::typeOnly())
);
}

return $messages;
}

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

namespace PHPStan\Rules\Operators;

use PHPStan\Type\VerbosityLevel;

class OperandsInComparisonNotEqualRule implements \PHPStan\Rules\Rule
{

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

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

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

/**
* @param \PhpParser\Node\Expr\BinaryOp\BooleanAnd $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
{
$leftType = $scope->getType($node->left);
$rightType = $scope->getType($node->right);

$messages = [];
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) {
$messages[] = sprintf(
'Only %s is allowed in !=, %s given on the left side.',
$this->helper->getAllowedLooseComparison(),
$leftType->describe(VerbosityLevel::typeOnly())
);
}
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) {
$messages[] = sprintf(
'Only %s is allowed in !=, %s given on the right side.',
$this->helper->getAllowedLooseComparison(),
$rightType->describe(VerbosityLevel::typeOnly())
);
}

return $messages;
}

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

namespace PHPStan\Rules\Operators;

use PHPStan\Type\VerbosityLevel;

class OperandsInComparisonSmallerOrEqualRule implements \PHPStan\Rules\Rule
{

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

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

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

/**
* @param \PhpParser\Node\Expr\BinaryOp\Equal $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
{
$leftType = $scope->getType($node->left);
$rightType = $scope->getType($node->right);

$messages = [];
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) {
$messages[] = sprintf(
'Only %s is allowed in <=, %s given on the left side.',
$this->helper->getAllowedLooseComparison(),
$leftType->describe(VerbosityLevel::typeOnly())
);
}
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) {
$messages[] = sprintf(
'Only %s is allowed in <=, %s given on the right side.',
$this->helper->getAllowedLooseComparison(),
$rightType->describe(VerbosityLevel::typeOnly())
);
}

return $messages;
}

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

namespace PHPStan\Rules\Operators;

use PHPStan\Type\VerbosityLevel;

class OperandsInComparisonSmallerRule implements \PHPStan\Rules\Rule
{

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

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

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

/**
* @param \PhpParser\Node\Expr\BinaryOp\Equal $node
* @param \PHPStan\Analyser\Scope $scope
* @return string[] errors
*/
public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scope): array
{
$leftType = $scope->getType($node->left);
$rightType = $scope->getType($node->right);

$messages = [];
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) {
$messages[] = sprintf(
'Only %s is allowed in <, %s given on the left side.',
$this->helper->getAllowedLooseComparison(),
$leftType->describe(VerbosityLevel::typeOnly())
);
}
if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) {
$messages[] = sprintf(
'Only %s is allowed in <, %s given on the right side.',
$this->helper->getAllowedLooseComparison(),
$rightType->describe(VerbosityLevel::typeOnly())
);
}

return $messages;
}

}
Loading