From 832a1d4a93cfe7e0df41f7f76c6e04a86bc90cbd Mon Sep 17 00:00:00 2001 From: Arnold Daniels Date: Sat, 20 Oct 2018 14:46:05 +0200 Subject: [PATCH 1/5] Added rules for loose comparison Loose comparison should only be done on numeric or DateTime values. Fixes #40 Closes #12 --- rules.neon | 8 + .../OperandsInComparisonEqualRule.php | 57 +++++++ ...OperandsInComparisonGreaterOrEqualRule.php | 57 +++++++ .../OperandsInComparisonGreaterRule.php | 57 +++++++ .../OperandsInComparisonNotEqualRule.php | 57 +++++++ ...OperandsInComparisonSmallerOrEqualRule.php | 57 +++++++ .../OperandsInComparisonSmallerRule.php | 57 +++++++ .../OperandsInComparisonSpaceshipRule.php | 57 +++++++ src/Rules/Operators/OperatorRuleHelper.php | 45 ++++++ .../OperandsInArithmeticAdditionRuleTest.php | 2 +- .../OperandsInArithmeticDivisionRuleTest.php | 2 +- ...andsInArithmeticExponentiationRuleTest.php | 2 +- .../OperandsInArithmeticModuloRuleTest.php | 2 +- ...andsInArithmeticMultiplicationRuleTest.php | 2 +- ...perandsInArithmeticSubtractionRuleTest.php | 2 +- .../OperandsInComparisonEqualRuleTest.php | 43 +++++ ...andsInComparisonGreaterOrEqualRuleTest.php | 38 +++++ .../OperandsInComparisonGreaterRuleTest.php | 38 +++++ .../OperandsInComparisonNotEqualRuleTest.php | 42 +++++ ...andsInComparisonSmallerOrEqualRuleTest.php | 38 +++++ .../OperandsInComparisonSmallerRuleTest.php | 38 +++++ .../OperandsInComparisonSpaceshipRuleTest.php | 38 +++++ ...operators.php => arithmetic-operators.php} | 0 .../Operators/data/comparison-operators.php | 149 ++++++++++++++++++ 24 files changed, 882 insertions(+), 6 deletions(-) create mode 100644 src/Rules/Operators/OperandsInComparisonEqualRule.php create mode 100644 src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php create mode 100644 src/Rules/Operators/OperandsInComparisonGreaterRule.php create mode 100644 src/Rules/Operators/OperandsInComparisonNotEqualRule.php create mode 100644 src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php create mode 100644 src/Rules/Operators/OperandsInComparisonSmallerRule.php create mode 100644 src/Rules/Operators/OperandsInComparisonSpaceshipRule.php create mode 100644 tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php create mode 100644 tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php create mode 100644 tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php create mode 100644 tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php create mode 100644 tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php create mode 100644 tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php create mode 100644 tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php rename tests/Rules/Operators/data/{operators.php => arithmetic-operators.php} (100%) create mode 100644 tests/Rules/Operators/data/comparison-operators.php diff --git a/rules.neon b/rules.neon index f7fb61cd..97e25e3c 100644 --- a/rules.neon +++ b/rules.neon @@ -33,6 +33,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 diff --git a/src/Rules/Operators/OperandsInComparisonEqualRule.php b/src/Rules/Operators/OperandsInComparisonEqualRule.php new file mode 100644 index 00000000..b8caff64 --- /dev/null +++ b/src/Rules/Operators/OperandsInComparisonEqualRule.php @@ -0,0 +1,57 @@ +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); + $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); + + if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { + return []; + } + + $messages = []; + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in ==, %s given on the left side.', + $leftType->describe(VerbosityLevel::typeOnly()) + ); + } + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in ==, %s given on the right side.', + $rightType->describe(VerbosityLevel::typeOnly()) + ); + } + + return $messages; + } + +} diff --git a/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php b/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php new file mode 100644 index 00000000..341961cf --- /dev/null +++ b/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php @@ -0,0 +1,57 @@ +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); + $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); + + if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { + return []; + } + + $messages = []; + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in >=, %s given on the left side.', + $leftType->describe(VerbosityLevel::typeOnly()) + ); + } + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in >=, %s given on the right side.', + $rightType->describe(VerbosityLevel::typeOnly()) + ); + } + + return $messages; + } + +} diff --git a/src/Rules/Operators/OperandsInComparisonGreaterRule.php b/src/Rules/Operators/OperandsInComparisonGreaterRule.php new file mode 100644 index 00000000..bfecf3e0 --- /dev/null +++ b/src/Rules/Operators/OperandsInComparisonGreaterRule.php @@ -0,0 +1,57 @@ +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); + $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); + + if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { + return []; + } + + $messages = []; + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in >, %s given on the left side.', + $leftType->describe(VerbosityLevel::typeOnly()) + ); + } + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in >, %s given on the right side.', + $rightType->describe(VerbosityLevel::typeOnly()) + ); + } + + return $messages; + } + +} diff --git a/src/Rules/Operators/OperandsInComparisonNotEqualRule.php b/src/Rules/Operators/OperandsInComparisonNotEqualRule.php new file mode 100644 index 00000000..c98396ff --- /dev/null +++ b/src/Rules/Operators/OperandsInComparisonNotEqualRule.php @@ -0,0 +1,57 @@ +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); + $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); + + if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { + return []; + } + + $messages = []; + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in !=, %s given on the left side.', + $leftType->describe(VerbosityLevel::typeOnly()) + ); + } + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in !=, %s given on the right side.', + $rightType->describe(VerbosityLevel::typeOnly()) + ); + } + + return $messages; + } + +} diff --git a/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php b/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php new file mode 100644 index 00000000..71ae8509 --- /dev/null +++ b/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php @@ -0,0 +1,57 @@ +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); + $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); + + if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { + return []; + } + + $messages = []; + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in <=, %s given on the left side.', + $leftType->describe(VerbosityLevel::typeOnly()) + ); + } + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in <=, %s given on the right side.', + $rightType->describe(VerbosityLevel::typeOnly()) + ); + } + + return $messages; + } + +} diff --git a/src/Rules/Operators/OperandsInComparisonSmallerRule.php b/src/Rules/Operators/OperandsInComparisonSmallerRule.php new file mode 100644 index 00000000..26e866a5 --- /dev/null +++ b/src/Rules/Operators/OperandsInComparisonSmallerRule.php @@ -0,0 +1,57 @@ +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); + $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); + + if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { + return []; + } + + $messages = []; + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in <, %s given on the left side.', + $leftType->describe(VerbosityLevel::typeOnly()) + ); + } + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in <, %s given on the right side.', + $rightType->describe(VerbosityLevel::typeOnly()) + ); + } + + return $messages; + } + +} diff --git a/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php b/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php new file mode 100644 index 00000000..4aa2fb28 --- /dev/null +++ b/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php @@ -0,0 +1,57 @@ +helper = $helper; + } + + public function getNodeType(): string + { + return \PhpParser\Node\Expr\BinaryOp\Spaceship::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); + $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); + + if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { + return []; + } + + $messages = []; + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in <=>, %s given on the left side.', + $leftType->describe(VerbosityLevel::typeOnly()) + ); + } + if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) { + $messages[] = sprintf( + 'Only numeric types and DateTime objects are allowed in <=>, %s given on the right side.', + $rightType->describe(VerbosityLevel::typeOnly()) + ); + } + + return $messages; + } + +} diff --git a/src/Rules/Operators/OperatorRuleHelper.php b/src/Rules/Operators/OperatorRuleHelper.php index ba3634ac..ddcca5d0 100644 --- a/src/Rules/Operators/OperatorRuleHelper.php +++ b/src/Rules/Operators/OperatorRuleHelper.php @@ -9,6 +9,7 @@ use PHPStan\Type\FloatType; use PHPStan\Type\IntegerType; use PHPStan\Type\MixedType; +use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use PHPStan\Type\UnionType; @@ -48,6 +49,21 @@ public function isValidForIncrementOrDecrement(Scope $scope, Expr $expr): bool return $this->isSubtypeOfNumber($scope, $expr); } + public function isValidForLooseComparisonOperation(Scope $scope, Expr $expr): bool + { + $type = $scope->getType($expr); + if ($type instanceof MixedType) { + return true; + } + + // already reported by PHPStan core + if ($type->toNumber() instanceof ErrorType) { + return true; + } + + return $this->isSubtypeOfNumberOrDateTime($scope, $expr); + } + private function isSubtypeOfNumber(Scope $scope, Expr $expr): bool { $acceptedType = new UnionType([new IntegerType(), new FloatType()]); @@ -73,4 +89,33 @@ function (Type $type) use ($acceptedType): bool { return $isSuperType->yes(); } + private function isSubtypeOfNumberOrDateTime(Scope $scope, Expr $expr): bool + { + $acceptedType = new UnionType([ + new IntegerType(), + new FloatType(), + new ObjectType('DateTimeInterface'), + ]); + + $type = $this->ruleLevelHelper->findTypeToCheck( + $scope, + $expr, + '', + function (Type $type) use ($acceptedType): bool { + return $acceptedType->isSuperTypeOf($type)->yes(); + } + )->getType(); + + if ($type instanceof ErrorType) { + return true; + } + + $isSuperType = $acceptedType->isSuperTypeOf($type); + if ($type instanceof \PHPStan\Type\BenevolentUnionType) { + return !$isSuperType->no(); + } + + return $isSuperType->yes(); + } + } diff --git a/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php index 0c71907e..ef60fe71 100644 --- a/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php @@ -19,7 +19,7 @@ protected function getRule(): Rule public function testRule(): void { - $this->analyse([__DIR__ . '/data/operators.php'], [ + $this->analyse([__DIR__ . '/data/arithmetic-operators.php'], [ [ 'Only numeric types are allowed in +, string given on the right side.', 25, diff --git a/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php index a43a0bac..51b2c69d 100644 --- a/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php @@ -19,7 +19,7 @@ protected function getRule(): Rule public function testRule(): void { - $this->analyse([__DIR__ . '/data/operators.php'], [ + $this->analyse([__DIR__ . '/data/arithmetic-operators.php'], [ [ 'Only numeric types are allowed in /, string given on the right side.', 64, diff --git a/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php index f8c0f2d8..e3cce15e 100644 --- a/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php @@ -19,7 +19,7 @@ protected function getRule(): Rule public function testRule(): void { - $this->analyse([__DIR__ . '/data/operators.php'], [ + $this->analyse([__DIR__ . '/data/arithmetic-operators.php'], [ [ 'Only numeric types are allowed in **, string given on the right side.', 77, diff --git a/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php index a803c97e..2466ddaf 100644 --- a/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php @@ -19,7 +19,7 @@ protected function getRule(): Rule public function testRule(): void { - $this->analyse([__DIR__ . '/data/operators.php'], [ + $this->analyse([__DIR__ . '/data/arithmetic-operators.php'], [ [ 'Only numeric types are allowed in %, string given on the right side.', 90, diff --git a/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php index 643a3b3d..36bf661f 100644 --- a/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php @@ -19,7 +19,7 @@ protected function getRule(): Rule public function testRule(): void { - $this->analyse([__DIR__ . '/data/operators.php'], [ + $this->analyse([__DIR__ . '/data/arithmetic-operators.php'], [ [ 'Only numeric types are allowed in *, string given on the right side.', 51, diff --git a/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php index 522f2b9c..dd85974d 100644 --- a/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php @@ -19,7 +19,7 @@ protected function getRule(): Rule public function testRule(): void { - $this->analyse([__DIR__ . '/data/operators.php'], [ + $this->analyse([__DIR__ . '/data/arithmetic-operators.php'], [ [ 'Only numeric types are allowed in -, string given on the right side.', 38, diff --git a/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php new file mode 100644 index 00000000..b78711d7 --- /dev/null +++ b/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php @@ -0,0 +1,43 @@ +createBroker(), true, false, true) + ) + ); + } + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ + [ + 'Only numeric types and DateTime objects are allowed in ==, string given on the right side.', + 25, + ], + [ + 'Only numeric types and DateTime objects are allowed in ==, null given on the right side.', + 28, + ], + [ + 'Only numeric types and DateTime objects are allowed in ==, null given on the right side.', + 34, + ], + [ + 'Only numeric types and DateTime objects are allowed in ==, (array|false) given on the ' + . 'left side.', + 148, + ], + ]); + } + +} diff --git a/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php new file mode 100644 index 00000000..dea784e7 --- /dev/null +++ b/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php @@ -0,0 +1,38 @@ +createBroker(), true, false, true) + ) + ); + } + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ + [ + 'Only numeric types and DateTime objects are allowed in >=, string given on the right side.', + 108, + ], + [ + 'Only numeric types and DateTime objects are allowed in >=, null given on the right side.', + 111, + ], + [ + 'Only numeric types and DateTime objects are allowed in >=, null given on the right side.', + 117, + ], + ]); + } + +} diff --git a/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php b/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php new file mode 100644 index 00000000..7374057a --- /dev/null +++ b/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php @@ -0,0 +1,38 @@ +createBroker(), true, false, true) + ) + ); + } + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ + [ + 'Only numeric types and DateTime objects are allowed in >, string given on the right side.', + 76, + ], + [ + 'Only numeric types and DateTime objects are allowed in >, null given on the right side.', + 79, + ], + [ + 'Only numeric types and DateTime objects are allowed in >, null given on the right side.', + 85, + ], + ]); + } + +} diff --git a/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php new file mode 100644 index 00000000..2bd1b6e1 --- /dev/null +++ b/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php @@ -0,0 +1,42 @@ +createBroker(), true, false, true) + ) + ); + } + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ + [ + 'Only numeric types and DateTime objects are allowed in !=, string given on the right side.', + 41, + ], + [ + 'Only numeric types and DateTime objects are allowed in !=, null given on the right side.', + 44, + ], + [ + 'Only numeric types and DateTime objects are allowed in !=, null given on the right side.', + 50, + ], + [ + 'Only numeric types and DateTime objects are allowed in !=, string given on the right side.', + 53, + ], + ]); + } + +} diff --git a/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php new file mode 100644 index 00000000..dfac05b9 --- /dev/null +++ b/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php @@ -0,0 +1,38 @@ +createBroker(), true, false, true) + ) + ); + } + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ + [ + 'Only numeric types and DateTime objects are allowed in <=, string given on the right side.', + 92, + ], + [ + 'Only numeric types and DateTime objects are allowed in <=, null given on the right side.', + 95, + ], + [ + 'Only numeric types and DateTime objects are allowed in <=, null given on the right side.', + 101, + ], + ]); + } + +} diff --git a/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php new file mode 100644 index 00000000..9d69d0f6 --- /dev/null +++ b/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php @@ -0,0 +1,38 @@ +createBroker(), true, false, true) + ) + ); + } + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ + [ + 'Only numeric types and DateTime objects are allowed in <, string given on the right side.', + 60, + ], + [ + 'Only numeric types and DateTime objects are allowed in <, null given on the right side.', + 63, + ], + [ + 'Only numeric types and DateTime objects are allowed in <, null given on the right side.', + 69, + ], + ]); + } + +} diff --git a/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php new file mode 100644 index 00000000..f9a18f18 --- /dev/null +++ b/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php @@ -0,0 +1,38 @@ +createBroker(), true, false, true) + ) + ); + } + + public function testRule(): void + { + $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ + [ + 'Only numeric types and DateTime objects are allowed in <=>, string given on the right side.', + 124, + ], + [ + 'Only numeric types and DateTime objects are allowed in <=>, null given on the right side.', + 127, + ], + [ + 'Only numeric types and DateTime objects are allowed in <=>, null given on the right side.', + 133, + ], + ]); + } + +} diff --git a/tests/Rules/Operators/data/operators.php b/tests/Rules/Operators/data/arithmetic-operators.php similarity index 100% rename from tests/Rules/Operators/data/operators.php rename to tests/Rules/Operators/data/arithmetic-operators.php diff --git a/tests/Rules/Operators/data/comparison-operators.php b/tests/Rules/Operators/data/comparison-operators.php new file mode 100644 index 00000000..16515d4b --- /dev/null +++ b/tests/Rules/Operators/data/comparison-operators.php @@ -0,0 +1,149 @@ + $int; +$int <> $string; + +$int < $int; +$int < $float; +$float < $int; +$float < $float; +$intOrFloat < $int; +$int < $string; +$int < $array; +$int < $object; +$int < $null; +$int < $date; +$date < $date; +$date < $dateImm; +$date < $int; +$date < $object; +$date < $null; + +$int > $int; +$int > $float; +$float > $int; +$float > $float; +$intOrFloat > $int; +$int > $string; +$int > $array; +$int > $object; +$int > $null; +$int > $date; +$date > $date; +$date > $dateImm; +$date > $int; +$date > $object; +$date > $null; + +$int <= $int; +$int <= $float; +$float <= $int; +$float <= $float; +$intOrFloat <= $int; +$int <= $string; +$int <= $array; +$int <= $object; +$int <= $null; +$int <= $date; +$date <= $date; +$date <= $dateImm; +$date <= $int; +$date <= $object; +$date <= $null; + +$int >= $int; +$int >= $float; +$float >= $int; +$float >= $float; +$intOrFloat >= $int; +$int >= $string; +$int >= $array; +$int >= $object; +$int >= $null; +$int >= $date; +$date >= $date; +$date >= $dateImm; +$date >= $int; +$date >= $object; +$date >= $null; + +$int <=> $int; +$int <=> $float; +$float <=> $int; +$float <=> $float; +$intOrFloat <=> $int; +$int <=> $string; +$int <=> $array; +$int <=> $object; +$int <=> $null; +$int <=> $date; +$date <=> $date; +$date <=> $dateImm; +$date <=> $int; +$date <=> $object; +$date <=> $null; + +function ($mixed, int $a, string $b) { + $mixed == $mixed; + $mixed == $a; + $a == $mixed; + $mixed == $b; + $b == $mixed; +}; + +function (array $array, int $int, $mixed) { + foreach ($array as $i => $val) { + $i == $int; + } + + explode($mixed, $mixed) == $int; +}; From c2de273177e74401379f286fc80e92016e28d164 Mon Sep 17 00:00:00 2001 From: Arnold Daniels Date: Tue, 6 Nov 2018 02:26:13 +0100 Subject: [PATCH 2/5] Make the types that are allowed for loose comparison configurable. Added loose comparison checks to README. --- README.md | 1 + rules.neon | 6 ++ .../OperandsInComparisonEqualRule.php | 6 +- ...OperandsInComparisonGreaterOrEqualRule.php | 6 +- .../OperandsInComparisonGreaterRule.php | 6 +- .../OperandsInComparisonNotEqualRule.php | 6 +- ...OperandsInComparisonSmallerOrEqualRule.php | 6 +- .../OperandsInComparisonSmallerRule.php | 6 +- .../OperandsInComparisonSpaceshipRule.php | 6 +- src/Rules/Operators/OperatorRuleHelper.php | 63 +++++++------------ ...ArithmeticIncrementOrDecrementRuleTest.php | 5 +- .../OperandsInArithmeticAdditionRuleTest.php | 5 +- .../OperandsInArithmeticDivisionRuleTest.php | 5 +- ...andsInArithmeticExponentiationRuleTest.php | 5 +- .../OperandsInArithmeticModuloRuleTest.php | 5 +- ...andsInArithmeticMultiplicationRuleTest.php | 5 +- ...perandsInArithmeticSubtractionRuleTest.php | 5 +- .../OperandsInComparisonEqualRuleTest.php | 21 +++++-- ...andsInComparisonGreaterOrEqualRuleTest.php | 18 ++++-- .../OperandsInComparisonGreaterRuleTest.php | 18 ++++-- .../OperandsInComparisonNotEqualRuleTest.php | 20 ++++-- ...andsInComparisonSmallerOrEqualRuleTest.php | 18 ++++-- .../OperandsInComparisonSmallerRuleTest.php | 18 ++++-- .../OperandsInComparisonSpaceshipRuleTest.php | 18 ++++-- 24 files changed, 187 insertions(+), 91 deletions(-) diff --git a/README.md b/README.md index 528d150d..fdf56590 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/rules.neon b/rules.neon index 97e25e3c..4e330a3c 100644 --- a/rules.neon +++ b/rules.neon @@ -9,6 +9,7 @@ parameters: checkMissingClosureNativeReturnTypehintRule: true reportMaybesInMethodSignatures: true reportStaticMethodSignatures: true + allowedLooseComparison: int|float|DateTimeInterface rules: - PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule @@ -54,9 +55,14 @@ services: class: PHPStan\Rules\BooleansInConditions\BooleanRuleHelper - class: PHPStan\Rules\Operators\OperatorRuleHelper +<<<<<<< HEAD - class: PHPStan\Rules\VariableVariables\VariablePropertyFetchRule arguments: universalObjectCratesClasses: %universalObjectCratesClasses% tags: - phpstan.rules.rule +======= + arguments: + allowedLooseComparison: %allowedLooseComparison% +>>>>>>> Make the types that are allowed for loose comparison configurable. diff --git a/src/Rules/Operators/OperandsInComparisonEqualRule.php b/src/Rules/Operators/OperandsInComparisonEqualRule.php index b8caff64..479eb9a5 100644 --- a/src/Rules/Operators/OperandsInComparisonEqualRule.php +++ b/src/Rules/Operators/OperandsInComparisonEqualRule.php @@ -40,13 +40,15 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( - 'Only numeric types and DateTime objects are allowed in ==, %s given on the left side.', + '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 numeric types and DateTime objects are allowed in ==, %s given on the right side.', + 'Only %s is allowed in ==, %s given on the right side.', + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php b/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php index 341961cf..def1223f 100644 --- a/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php +++ b/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php @@ -40,13 +40,15 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( - 'Only numeric types and DateTime objects are allowed in >=, %s given on the left side.', + '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 numeric types and DateTime objects are allowed in >=, %s given on the right side.', + 'Only %s is allowed in >=, %s given on the right side.', + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonGreaterRule.php b/src/Rules/Operators/OperandsInComparisonGreaterRule.php index bfecf3e0..7501577e 100644 --- a/src/Rules/Operators/OperandsInComparisonGreaterRule.php +++ b/src/Rules/Operators/OperandsInComparisonGreaterRule.php @@ -40,13 +40,15 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( - 'Only numeric types and DateTime objects are allowed in >, %s given on the left side.', + '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 numeric types and DateTime objects are allowed in >, %s given on the right side.', + 'Only %s is allowed in >, %s given on the right side.', + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonNotEqualRule.php b/src/Rules/Operators/OperandsInComparisonNotEqualRule.php index c98396ff..43755b6f 100644 --- a/src/Rules/Operators/OperandsInComparisonNotEqualRule.php +++ b/src/Rules/Operators/OperandsInComparisonNotEqualRule.php @@ -40,13 +40,15 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( - 'Only numeric types and DateTime objects are allowed in !=, %s given on the left side.', + '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 numeric types and DateTime objects are allowed in !=, %s given on the right side.', + 'Only %s is allowed in !=, %s given on the right side.', + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php b/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php index 71ae8509..26413376 100644 --- a/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php +++ b/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php @@ -40,13 +40,15 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( - 'Only numeric types and DateTime objects are allowed in <=, %s given on the left side.', + '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 numeric types and DateTime objects are allowed in <=, %s given on the right side.', + 'Only %s is allowed in <=, %s given on the right side.', + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonSmallerRule.php b/src/Rules/Operators/OperandsInComparisonSmallerRule.php index 26e866a5..8de2d43c 100644 --- a/src/Rules/Operators/OperandsInComparisonSmallerRule.php +++ b/src/Rules/Operators/OperandsInComparisonSmallerRule.php @@ -40,13 +40,15 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( - 'Only numeric types and DateTime objects are allowed in <, %s given on the left side.', + '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 numeric types and DateTime objects are allowed in <, %s given on the right side.', + 'Only %s is allowed in <, %s given on the right side.', + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php b/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php index 4aa2fb28..0f6deac5 100644 --- a/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php +++ b/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php @@ -40,13 +40,15 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( - 'Only numeric types and DateTime objects are allowed in <=>, %s given on the left side.', + '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 numeric types and DateTime objects are allowed in <=>, %s given on the right side.', + 'Only %s is allowed in <=>, %s given on the right side.', + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperatorRuleHelper.php b/src/Rules/Operators/OperatorRuleHelper.php index ddcca5d0..ee9d03fb 100644 --- a/src/Rules/Operators/OperatorRuleHelper.php +++ b/src/Rules/Operators/OperatorRuleHelper.php @@ -4,12 +4,12 @@ use PhpParser\Node\Expr; use PHPStan\Analyser\Scope; +use PHPStan\PhpDoc\TypeStringResolver; use PHPStan\Rules\RuleLevelHelper; use PHPStan\Type\ErrorType; use PHPStan\Type\FloatType; use PHPStan\Type\IntegerType; use PHPStan\Type\MixedType; -use PHPStan\Type\ObjectType; use PHPStan\Type\Type; use PHPStan\Type\UnionType; @@ -19,11 +19,28 @@ class OperatorRuleHelper /** @var \PHPStan\Rules\RuleLevelHelper */ private $ruleLevelHelper; - public function __construct(RuleLevelHelper $ruleLevelHelper) - { + /** @var string */ + private $allowedLooseComparison; + + /** @var Type */ + private $looseComparisonAllowedType; + + public function __construct( + RuleLevelHelper $ruleLevelHelper, + TypeStringResolver $typeStringResolver, + string $allowedLooseComparison + ) { $this->ruleLevelHelper = $ruleLevelHelper; + + $this->allowedLooseComparison = $allowedLooseComparison; + $this->looseComparisonAllowedType = $typeStringResolver->resolve($allowedLooseComparison); } + public function getAllowedLooseComparison(): string + { + return $this->allowedLooseComparison; + } + public function isValidForArithmeticOperation(Scope $scope, Expr $expr): bool { $type = $scope->getType($expr); @@ -36,7 +53,7 @@ public function isValidForArithmeticOperation(Scope $scope, Expr $expr): bool return true; } - return $this->isSubtypeOfNumber($scope, $expr); + return $this->isSubtypeOf($scope, $expr, new UnionType([new IntegerType(), new FloatType()])); } public function isValidForIncrementOrDecrement(Scope $scope, Expr $expr): bool @@ -46,7 +63,7 @@ public function isValidForIncrementOrDecrement(Scope $scope, Expr $expr): bool return true; } - return $this->isSubtypeOfNumber($scope, $expr); + return $this->isSubtypeOf($scope, $expr, new UnionType([new IntegerType(), new FloatType()])); } public function isValidForLooseComparisonOperation(Scope $scope, Expr $expr): bool @@ -61,42 +78,11 @@ public function isValidForLooseComparisonOperation(Scope $scope, Expr $expr): bo return true; } - return $this->isSubtypeOfNumberOrDateTime($scope, $expr); - } - - private function isSubtypeOfNumber(Scope $scope, Expr $expr): bool - { - $acceptedType = new UnionType([new IntegerType(), new FloatType()]); - - $type = $this->ruleLevelHelper->findTypeToCheck( - $scope, - $expr, - '', - function (Type $type) use ($acceptedType): bool { - return $acceptedType->isSuperTypeOf($type)->yes(); - } - )->getType(); - - if ($type instanceof ErrorType) { - return true; - } - - $isSuperType = $acceptedType->isSuperTypeOf($type); - if ($type instanceof \PHPStan\Type\BenevolentUnionType) { - return !$isSuperType->no(); - } - - return $isSuperType->yes(); + return $this->isSubtypeOf($scope, $expr, $this->looseComparisonAllowedType); } - private function isSubtypeOfNumberOrDateTime(Scope $scope, Expr $expr): bool + private function isSubtypeOf(Scope $scope, Expr $expr, Type $acceptedType): bool { - $acceptedType = new UnionType([ - new IntegerType(), - new FloatType(), - new ObjectType('DateTimeInterface'), - ]); - $type = $this->ruleLevelHelper->findTypeToCheck( $scope, $expr, @@ -117,5 +103,4 @@ function (Type $type) use ($acceptedType): bool { return $isSuperType->yes(); } - } diff --git a/tests/Rules/Operators/OperandInArithmeticIncrementOrDecrementRuleTest.php b/tests/Rules/Operators/OperandInArithmeticIncrementOrDecrementRuleTest.php index 73a8ef0f..d6832dbb 100644 --- a/tests/Rules/Operators/OperandInArithmeticIncrementOrDecrementRuleTest.php +++ b/tests/Rules/Operators/OperandInArithmeticIncrementOrDecrementRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeStringResolver; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +13,9 @@ protected function getRule(): Rule { return $this->createRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php index ef60fe71..724eaf3d 100644 --- a/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeStringResolver; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +13,9 @@ protected function getRule(): Rule { return new OperandsInArithmeticAdditionRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php index 51b2c69d..5201a86b 100644 --- a/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeStringResolver; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +13,9 @@ protected function getRule(): Rule { return new OperandsInArithmeticDivisionRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php index e3cce15e..9489b96e 100644 --- a/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeStringResolver; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +13,9 @@ protected function getRule(): Rule { return new OperandsInArithmeticExponentiationRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php index 2466ddaf..223a5a9e 100644 --- a/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeStringResolver; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +13,9 @@ protected function getRule(): Rule { return new OperandsInArithmeticModuloRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php index 36bf661f..840fb352 100644 --- a/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeStringResolver; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +13,9 @@ protected function getRule(): Rule { return new OperandsInArithmeticMultiplicationRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php index dd85974d..742a220e 100644 --- a/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php @@ -2,6 +2,7 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeStringResolver; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +13,9 @@ protected function getRule(): Rule { return new OperandsInArithmeticSubtractionRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php index b78711d7..4509ad4f 100644 --- a/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php @@ -2,6 +2,10 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeNodeResolver; +use PHPStan\PhpDoc\TypeStringResolver; +use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\Parser\TypeParser; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +16,13 @@ protected function getRule(): Rule { return new OperandsInComparisonEqualRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } @@ -21,20 +31,19 @@ public function testRule(): void { $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ [ - 'Only numeric types and DateTime objects are allowed in ==, string given on the right side.', + 'Only int|float|DateTimeInterface is allowed in ==, string given on the right side.', 25, ], [ - 'Only numeric types and DateTime objects are allowed in ==, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in ==, null given on the right side.', 28, ], [ - 'Only numeric types and DateTime objects are allowed in ==, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in ==, null given on the right side.', 34, ], [ - 'Only numeric types and DateTime objects are allowed in ==, (array|false) given on the ' - . 'left side.', + 'Only int|float|DateTimeInterface is allowed in ==, (array|false) given on the left side.', 148, ], ]); diff --git a/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php index dea784e7..a17e0c3f 100644 --- a/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php @@ -2,6 +2,10 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeNodeResolver; +use PHPStan\PhpDoc\TypeStringResolver; +use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\Parser\TypeParser; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +16,13 @@ protected function getRule(): Rule { return new OperandsInComparisonGreaterOrEqualRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } @@ -21,15 +31,15 @@ public function testRule(): void { $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ [ - 'Only numeric types and DateTime objects are allowed in >=, string given on the right side.', + 'Only int|float|DateTimeInterface is allowed in >=, string given on the right side.', 108, ], [ - 'Only numeric types and DateTime objects are allowed in >=, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in >=, null given on the right side.', 111, ], [ - 'Only numeric types and DateTime objects are allowed in >=, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in >=, null given on the right side.', 117, ], ]); diff --git a/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php b/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php index 7374057a..f26d827d 100644 --- a/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php @@ -2,6 +2,10 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeNodeResolver; +use PHPStan\PhpDoc\TypeStringResolver; +use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\Parser\TypeParser; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +16,13 @@ protected function getRule(): Rule { return new OperandsInComparisonGreaterRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } @@ -21,15 +31,15 @@ public function testRule(): void { $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ [ - 'Only numeric types and DateTime objects are allowed in >, string given on the right side.', + 'Only int|float|DateTimeInterface is allowed in >, string given on the right side.', 76, ], [ - 'Only numeric types and DateTime objects are allowed in >, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in >, null given on the right side.', 79, ], [ - 'Only numeric types and DateTime objects are allowed in >, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in >, null given on the right side.', 85, ], ]); diff --git a/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php index 2bd1b6e1..de804d54 100644 --- a/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php @@ -2,6 +2,10 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeNodeResolver; +use PHPStan\PhpDoc\TypeStringResolver; +use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\Parser\TypeParser; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +16,13 @@ protected function getRule(): Rule { return new OperandsInComparisonNotEqualRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } @@ -21,19 +31,19 @@ public function testRule(): void { $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ [ - 'Only numeric types and DateTime objects are allowed in !=, string given on the right side.', + 'Only int|float|DateTimeInterface is allowed in !=, string given on the right side.', 41, ], [ - 'Only numeric types and DateTime objects are allowed in !=, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in !=, null given on the right side.', 44, ], [ - 'Only numeric types and DateTime objects are allowed in !=, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in !=, null given on the right side.', 50, ], [ - 'Only numeric types and DateTime objects are allowed in !=, string given on the right side.', + 'Only int|float|DateTimeInterface is allowed in !=, string given on the right side.', 53, ], ]); diff --git a/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php index dfac05b9..66f983bc 100644 --- a/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php @@ -2,6 +2,10 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeNodeResolver; +use PHPStan\PhpDoc\TypeStringResolver; +use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\Parser\TypeParser; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +16,13 @@ protected function getRule(): Rule { return new OperandsInComparisonSmallerOrEqualRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } @@ -21,15 +31,15 @@ public function testRule(): void { $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ [ - 'Only numeric types and DateTime objects are allowed in <=, string given on the right side.', + 'Only int|float|DateTimeInterface is allowed in <=, string given on the right side.', 92, ], [ - 'Only numeric types and DateTime objects are allowed in <=, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in <=, null given on the right side.', 95, ], [ - 'Only numeric types and DateTime objects are allowed in <=, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in <=, null given on the right side.', 101, ], ]); diff --git a/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php index 9d69d0f6..022198a2 100644 --- a/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php @@ -2,6 +2,10 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeNodeResolver; +use PHPStan\PhpDoc\TypeStringResolver; +use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\Parser\TypeParser; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +16,13 @@ protected function getRule(): Rule { return new OperandsInComparisonSmallerRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } @@ -21,15 +31,15 @@ public function testRule(): void { $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ [ - 'Only numeric types and DateTime objects are allowed in <, string given on the right side.', + 'Only int|float|DateTimeInterface is allowed in <, string given on the right side.', 60, ], [ - 'Only numeric types and DateTime objects are allowed in <, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in <, null given on the right side.', 63, ], [ - 'Only numeric types and DateTime objects are allowed in <, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in <, null given on the right side.', 69, ], ]); diff --git a/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php index f9a18f18..34edc12f 100644 --- a/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php @@ -2,6 +2,10 @@ namespace PHPStan\Rules\Operators; +use PHPStan\PhpDoc\TypeNodeResolver; +use PHPStan\PhpDoc\TypeStringResolver; +use PHPStan\PhpDocParser\Lexer\Lexer; +use PHPStan\PhpDocParser\Parser\TypeParser; use PHPStan\Rules\Rule; use PHPStan\Rules\RuleLevelHelper; @@ -12,7 +16,13 @@ protected function getRule(): Rule { return new OperandsInComparisonSpaceshipRule( new OperatorRuleHelper( - new RuleLevelHelper($this->createBroker(), true, false, true) + new RuleLevelHelper($this->createBroker(), true, false, true), + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } @@ -21,15 +31,15 @@ public function testRule(): void { $this->analyse([__DIR__ . '/data/comparison-operators.php'], [ [ - 'Only numeric types and DateTime objects are allowed in <=>, string given on the right side.', + 'Only int|float|DateTimeInterface is allowed in <=>, string given on the right side.', 124, ], [ - 'Only numeric types and DateTime objects are allowed in <=>, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in <=>, null given on the right side.', 127, ], [ - 'Only numeric types and DateTime objects are allowed in <=>, null given on the right side.', + 'Only int|float|DateTimeInterface is allowed in <=>, null given on the right side.', 133, ], ]); From 57efa54268d49aaff2ddc4cfc119f5324a4f8f00 Mon Sep 17 00:00:00 2001 From: Arnold Daniels Date: Tue, 6 Nov 2018 16:24:04 +0100 Subject: [PATCH 3/5] Removed non-sence mixed array check in `OperandsInComparison` rules. Blindly copied from `OperandsInArithmeticAdditionRule`, but doesn't belong here. --- .../OperandsInComparisonEqualRule.php | 11 ++-------- ...OperandsInComparisonGreaterOrEqualRule.php | 11 ++-------- .../OperandsInComparisonGreaterRule.php | 11 ++-------- .../OperandsInComparisonNotEqualRule.php | 11 ++-------- ...OperandsInComparisonSmallerOrEqualRule.php | 11 ++-------- .../OperandsInComparisonSmallerRule.php | 11 ++-------- .../OperandsInComparisonSpaceshipRule.php | 9 +-------- src/Rules/Operators/OperatorRuleHelper.php | 20 ++++++++++--------- ...ArithmeticIncrementOrDecrementRuleTest.php | 4 ++-- .../OperandsInArithmeticAdditionRuleTest.php | 4 ++-- .../OperandsInArithmeticDivisionRuleTest.php | 4 ++-- ...andsInArithmeticExponentiationRuleTest.php | 4 ++-- .../OperandsInArithmeticModuloRuleTest.php | 4 ++-- ...andsInArithmeticMultiplicationRuleTest.php | 4 ++-- ...perandsInArithmeticSubtractionRuleTest.php | 4 ++-- .../OperandsInComparisonEqualRuleTest.php | 12 +++++------ ...andsInComparisonGreaterOrEqualRuleTest.php | 12 +++++------ .../OperandsInComparisonGreaterRuleTest.php | 12 +++++------ .../OperandsInComparisonNotEqualRuleTest.php | 12 +++++------ ...andsInComparisonSmallerOrEqualRuleTest.php | 12 +++++------ .../OperandsInComparisonSmallerRuleTest.php | 12 +++++------ .../OperandsInComparisonSpaceshipRuleTest.php | 12 +++++------ 22 files changed, 80 insertions(+), 127 deletions(-) diff --git a/src/Rules/Operators/OperandsInComparisonEqualRule.php b/src/Rules/Operators/OperandsInComparisonEqualRule.php index 479eb9a5..009e46aa 100644 --- a/src/Rules/Operators/OperandsInComparisonEqualRule.php +++ b/src/Rules/Operators/OperandsInComparisonEqualRule.php @@ -2,8 +2,6 @@ namespace PHPStan\Rules\Operators; -use PHPStan\Type\ArrayType; -use PHPStan\Type\MixedType; use PHPStan\Type\VerbosityLevel; class OperandsInComparisonEqualRule implements \PHPStan\Rules\Rule @@ -31,24 +29,19 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop { $leftType = $scope->getType($node->left); $rightType = $scope->getType($node->right); - $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); - - if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { - return []; - } $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( 'Only %s is allowed in ==, %s given on the left side.', - $this->helper->getAllowedLooseComparison(), + $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(), + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php b/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php index def1223f..025962f7 100644 --- a/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php +++ b/src/Rules/Operators/OperandsInComparisonGreaterOrEqualRule.php @@ -2,8 +2,6 @@ namespace PHPStan\Rules\Operators; -use PHPStan\Type\ArrayType; -use PHPStan\Type\MixedType; use PHPStan\Type\VerbosityLevel; class OperandsInComparisonGreaterOrEqualRule implements \PHPStan\Rules\Rule @@ -31,24 +29,19 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop { $leftType = $scope->getType($node->left); $rightType = $scope->getType($node->right); - $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); - - if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { - return []; - } $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( 'Only %s is allowed in >=, %s given on the left side.', - $this->helper->getAllowedLooseComparison(), + $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(), + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonGreaterRule.php b/src/Rules/Operators/OperandsInComparisonGreaterRule.php index 7501577e..bf537071 100644 --- a/src/Rules/Operators/OperandsInComparisonGreaterRule.php +++ b/src/Rules/Operators/OperandsInComparisonGreaterRule.php @@ -2,8 +2,6 @@ namespace PHPStan\Rules\Operators; -use PHPStan\Type\ArrayType; -use PHPStan\Type\MixedType; use PHPStan\Type\VerbosityLevel; class OperandsInComparisonGreaterRule implements \PHPStan\Rules\Rule @@ -31,24 +29,19 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop { $leftType = $scope->getType($node->left); $rightType = $scope->getType($node->right); - $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); - - if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { - return []; - } $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( 'Only %s is allowed in >, %s given on the left side.', - $this->helper->getAllowedLooseComparison(), + $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(), + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonNotEqualRule.php b/src/Rules/Operators/OperandsInComparisonNotEqualRule.php index 43755b6f..d4834115 100644 --- a/src/Rules/Operators/OperandsInComparisonNotEqualRule.php +++ b/src/Rules/Operators/OperandsInComparisonNotEqualRule.php @@ -2,8 +2,6 @@ namespace PHPStan\Rules\Operators; -use PHPStan\Type\ArrayType; -use PHPStan\Type\MixedType; use PHPStan\Type\VerbosityLevel; class OperandsInComparisonNotEqualRule implements \PHPStan\Rules\Rule @@ -31,24 +29,19 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop { $leftType = $scope->getType($node->left); $rightType = $scope->getType($node->right); - $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); - - if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { - return []; - } $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( 'Only %s is allowed in !=, %s given on the left side.', - $this->helper->getAllowedLooseComparison(), + $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(), + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php b/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php index 26413376..1814463f 100644 --- a/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php +++ b/src/Rules/Operators/OperandsInComparisonSmallerOrEqualRule.php @@ -2,8 +2,6 @@ namespace PHPStan\Rules\Operators; -use PHPStan\Type\ArrayType; -use PHPStan\Type\MixedType; use PHPStan\Type\VerbosityLevel; class OperandsInComparisonSmallerOrEqualRule implements \PHPStan\Rules\Rule @@ -31,24 +29,19 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop { $leftType = $scope->getType($node->left); $rightType = $scope->getType($node->right); - $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); - - if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { - return []; - } $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( 'Only %s is allowed in <=, %s given on the left side.', - $this->helper->getAllowedLooseComparison(), + $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(), + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonSmallerRule.php b/src/Rules/Operators/OperandsInComparisonSmallerRule.php index 8de2d43c..c4cf664c 100644 --- a/src/Rules/Operators/OperandsInComparisonSmallerRule.php +++ b/src/Rules/Operators/OperandsInComparisonSmallerRule.php @@ -2,8 +2,6 @@ namespace PHPStan\Rules\Operators; -use PHPStan\Type\ArrayType; -use PHPStan\Type\MixedType; use PHPStan\Type\VerbosityLevel; class OperandsInComparisonSmallerRule implements \PHPStan\Rules\Rule @@ -31,24 +29,19 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop { $leftType = $scope->getType($node->left); $rightType = $scope->getType($node->right); - $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); - - if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { - return []; - } $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { $messages[] = sprintf( 'Only %s is allowed in <, %s given on the left side.', - $this->helper->getAllowedLooseComparison(), + $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(), + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php b/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php index 0f6deac5..dc9583d6 100644 --- a/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php +++ b/src/Rules/Operators/OperandsInComparisonSpaceshipRule.php @@ -2,8 +2,6 @@ namespace PHPStan\Rules\Operators; -use PHPStan\Type\ArrayType; -use PHPStan\Type\MixedType; use PHPStan\Type\VerbosityLevel; class OperandsInComparisonSpaceshipRule implements \PHPStan\Rules\Rule @@ -31,11 +29,6 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop { $leftType = $scope->getType($node->left); $rightType = $scope->getType($node->right); - $mixedArrayType = new ArrayType(new MixedType(), new MixedType()); - - if ($mixedArrayType->isSuperTypeOf($leftType)->yes() && $mixedArrayType->isSuperTypeOf($rightType)->yes()) { - return []; - } $messages = []; if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->left)) { @@ -48,7 +41,7 @@ public function processNode(\PhpParser\Node $node, \PHPStan\Analyser\Scope $scop if (!$this->helper->isValidForLooseComparisonOperation($scope, $node->right)) { $messages[] = sprintf( 'Only %s is allowed in <=>, %s given on the right side.', - $this->helper->getAllowedLooseComparison(), + $this->helper->getAllowedLooseComparison(), $rightType->describe(VerbosityLevel::typeOnly()) ); } diff --git a/src/Rules/Operators/OperatorRuleHelper.php b/src/Rules/Operators/OperatorRuleHelper.php index ee9d03fb..db017490 100644 --- a/src/Rules/Operators/OperatorRuleHelper.php +++ b/src/Rules/Operators/OperatorRuleHelper.php @@ -19,17 +19,18 @@ class OperatorRuleHelper /** @var \PHPStan\Rules\RuleLevelHelper */ private $ruleLevelHelper; - /** @var string */ - private $allowedLooseComparison; + /** @var string */ + private $allowedLooseComparison; /** @var Type */ private $looseComparisonAllowedType; public function __construct( - RuleLevelHelper $ruleLevelHelper, - TypeStringResolver $typeStringResolver, - string $allowedLooseComparison - ) { + RuleLevelHelper $ruleLevelHelper, + TypeStringResolver $typeStringResolver, + string $allowedLooseComparison + ) + { $this->ruleLevelHelper = $ruleLevelHelper; $this->allowedLooseComparison = $allowedLooseComparison; @@ -37,9 +38,9 @@ public function __construct( } public function getAllowedLooseComparison(): string - { - return $this->allowedLooseComparison; - } + { + return $this->allowedLooseComparison; + } public function isValidForArithmeticOperation(Scope $scope, Expr $expr): bool { @@ -103,4 +104,5 @@ function (Type $type) use ($acceptedType): bool { return $isSuperType->yes(); } + } diff --git a/tests/Rules/Operators/OperandInArithmeticIncrementOrDecrementRuleTest.php b/tests/Rules/Operators/OperandInArithmeticIncrementOrDecrementRuleTest.php index d6832dbb..d2593418 100644 --- a/tests/Rules/Operators/OperandInArithmeticIncrementOrDecrementRuleTest.php +++ b/tests/Rules/Operators/OperandInArithmeticIncrementOrDecrementRuleTest.php @@ -14,8 +14,8 @@ protected function getRule(): Rule return $this->createRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - $this->createMock(TypeStringResolver::class), - '' + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php index 724eaf3d..c87a7974 100644 --- a/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticAdditionRuleTest.php @@ -14,8 +14,8 @@ protected function getRule(): Rule return new OperandsInArithmeticAdditionRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - $this->createMock(TypeStringResolver::class), - '' + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php index 5201a86b..a301ed78 100644 --- a/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticDivisionRuleTest.php @@ -14,8 +14,8 @@ protected function getRule(): Rule return new OperandsInArithmeticDivisionRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - $this->createMock(TypeStringResolver::class), - '' + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php index 9489b96e..f6906359 100644 --- a/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticExponentiationRuleTest.php @@ -14,8 +14,8 @@ protected function getRule(): Rule return new OperandsInArithmeticExponentiationRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - $this->createMock(TypeStringResolver::class), - '' + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php index 223a5a9e..c5d3aba9 100644 --- a/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticModuloRuleTest.php @@ -14,8 +14,8 @@ protected function getRule(): Rule return new OperandsInArithmeticModuloRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - $this->createMock(TypeStringResolver::class), - '' + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php index 840fb352..94aeed49 100644 --- a/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticMultiplicationRuleTest.php @@ -14,8 +14,8 @@ protected function getRule(): Rule return new OperandsInArithmeticMultiplicationRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - $this->createMock(TypeStringResolver::class), - '' + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php b/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php index 742a220e..92db73f0 100644 --- a/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php +++ b/tests/Rules/Operators/OperandsInArithmeticSubtractionRuleTest.php @@ -14,8 +14,8 @@ protected function getRule(): Rule return new OperandsInArithmeticSubtractionRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - $this->createMock(TypeStringResolver::class), - '' + $this->createMock(TypeStringResolver::class), + '' ) ); } diff --git a/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php index 4509ad4f..5fab6374 100644 --- a/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php @@ -17,12 +17,12 @@ protected function getRule(): Rule return new OperandsInComparisonEqualRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - new TypeStringResolver( - new Lexer(), - new TypeParser(), - new TypeNodeResolver([]) - ), - 'int|float|DateTimeInterface' + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } diff --git a/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php index a17e0c3f..40b90c63 100644 --- a/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php @@ -17,12 +17,12 @@ protected function getRule(): Rule return new OperandsInComparisonGreaterOrEqualRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - new TypeStringResolver( - new Lexer(), - new TypeParser(), - new TypeNodeResolver([]) - ), - 'int|float|DateTimeInterface' + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } diff --git a/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php b/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php index f26d827d..305ea7ab 100644 --- a/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php @@ -17,12 +17,12 @@ protected function getRule(): Rule return new OperandsInComparisonGreaterRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - new TypeStringResolver( - new Lexer(), - new TypeParser(), - new TypeNodeResolver([]) - ), - 'int|float|DateTimeInterface' + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } diff --git a/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php index de804d54..a539df41 100644 --- a/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php @@ -17,12 +17,12 @@ protected function getRule(): Rule return new OperandsInComparisonNotEqualRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - new TypeStringResolver( - new Lexer(), - new TypeParser(), - new TypeNodeResolver([]) - ), - 'int|float|DateTimeInterface' + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } diff --git a/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php index 66f983bc..87fcbe33 100644 --- a/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php @@ -17,12 +17,12 @@ protected function getRule(): Rule return new OperandsInComparisonSmallerOrEqualRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - new TypeStringResolver( - new Lexer(), - new TypeParser(), - new TypeNodeResolver([]) - ), - 'int|float|DateTimeInterface' + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } diff --git a/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php index 022198a2..2eded3bd 100644 --- a/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php @@ -17,12 +17,12 @@ protected function getRule(): Rule return new OperandsInComparisonSmallerRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - new TypeStringResolver( - new Lexer(), - new TypeParser(), - new TypeNodeResolver([]) - ), - 'int|float|DateTimeInterface' + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } diff --git a/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php index 34edc12f..459a14f4 100644 --- a/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php @@ -17,12 +17,12 @@ protected function getRule(): Rule return new OperandsInComparisonSpaceshipRule( new OperatorRuleHelper( new RuleLevelHelper($this->createBroker(), true, false, true), - new TypeStringResolver( - new Lexer(), - new TypeParser(), - new TypeNodeResolver([]) - ), - 'int|float|DateTimeInterface' + new TypeStringResolver( + new Lexer(), + new TypeParser(), + new TypeNodeResolver([]) + ), + 'int|float|DateTimeInterface' ) ); } From 34715c2bce6dfd78963130b6fa2569018bdd4848 Mon Sep 17 00:00:00 2001 From: Arnold Daniels Date: Fri, 20 Sep 2019 10:59:49 +0200 Subject: [PATCH 4/5] Fixed tests for operands in comparison rules. Fixed merge error in rules.neon. Fixed tests for v0.11 where TypeNodeResolver requires a container. --- rules.neon | 10 +++++----- .../Operators/OperandsInComparisonEqualRuleTest.php | 2 +- .../OperandsInComparisonGreaterOrEqualRuleTest.php | 2 +- .../Operators/OperandsInComparisonGreaterRuleTest.php | 2 +- .../Operators/OperandsInComparisonNotEqualRuleTest.php | 2 +- .../OperandsInComparisonSmallerOrEqualRuleTest.php | 2 +- .../Operators/OperandsInComparisonSmallerRuleTest.php | 2 +- .../OperandsInComparisonSpaceshipRuleTest.php | 2 +- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/rules.neon b/rules.neon index 4e330a3c..2905f058 100644 --- a/rules.neon +++ b/rules.neon @@ -11,6 +11,9 @@ parameters: reportStaticMethodSignatures: true allowedLooseComparison: int|float|DateTimeInterface +parametersSchema: + allowedLooseComparison: string() + rules: - PHPStan\Rules\BooleansInConditions\BooleanInBooleanAndRule - PHPStan\Rules\BooleansInConditions\BooleanInBooleanNotRule @@ -55,14 +58,11 @@ services: class: PHPStan\Rules\BooleansInConditions\BooleanRuleHelper - class: PHPStan\Rules\Operators\OperatorRuleHelper -<<<<<<< HEAD + arguments: + allowedLooseComparison: %allowedLooseComparison% - class: PHPStan\Rules\VariableVariables\VariablePropertyFetchRule arguments: universalObjectCratesClasses: %universalObjectCratesClasses% tags: - phpstan.rules.rule -======= - arguments: - allowedLooseComparison: %allowedLooseComparison% ->>>>>>> Make the types that are allowed for loose comparison configurable. diff --git a/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php index 5fab6374..62919176 100644 --- a/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonEqualRuleTest.php @@ -20,7 +20,7 @@ protected function getRule(): Rule new TypeStringResolver( new Lexer(), new TypeParser(), - new TypeNodeResolver([]) + new TypeNodeResolver([], self::getContainer()) ), 'int|float|DateTimeInterface' ) diff --git a/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php index 40b90c63..3202a6be 100644 --- a/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonGreaterOrEqualRuleTest.php @@ -20,7 +20,7 @@ protected function getRule(): Rule new TypeStringResolver( new Lexer(), new TypeParser(), - new TypeNodeResolver([]) + new TypeNodeResolver([], self::getContainer()) ), 'int|float|DateTimeInterface' ) diff --git a/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php b/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php index 305ea7ab..716ae8ec 100644 --- a/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonGreaterRuleTest.php @@ -20,7 +20,7 @@ protected function getRule(): Rule new TypeStringResolver( new Lexer(), new TypeParser(), - new TypeNodeResolver([]) + new TypeNodeResolver([], self::getContainer()) ), 'int|float|DateTimeInterface' ) diff --git a/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php index a539df41..3ee028c9 100644 --- a/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonNotEqualRuleTest.php @@ -20,7 +20,7 @@ protected function getRule(): Rule new TypeStringResolver( new Lexer(), new TypeParser(), - new TypeNodeResolver([]) + new TypeNodeResolver([], self::getContainer()) ), 'int|float|DateTimeInterface' ) diff --git a/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php index 87fcbe33..88cb5050 100644 --- a/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonSmallerOrEqualRuleTest.php @@ -20,7 +20,7 @@ protected function getRule(): Rule new TypeStringResolver( new Lexer(), new TypeParser(), - new TypeNodeResolver([]) + new TypeNodeResolver([], self::getContainer()) ), 'int|float|DateTimeInterface' ) diff --git a/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php index 2eded3bd..9f6fc5e0 100644 --- a/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonSmallerRuleTest.php @@ -20,7 +20,7 @@ protected function getRule(): Rule new TypeStringResolver( new Lexer(), new TypeParser(), - new TypeNodeResolver([]) + new TypeNodeResolver([], self::getContainer()) ), 'int|float|DateTimeInterface' ) diff --git a/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php b/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php index 459a14f4..6b9d4892 100644 --- a/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php +++ b/tests/Rules/Operators/OperandsInComparisonSpaceshipRuleTest.php @@ -20,7 +20,7 @@ protected function getRule(): Rule new TypeStringResolver( new Lexer(), new TypeParser(), - new TypeNodeResolver([]) + new TypeNodeResolver([], self::getContainer()) ), 'int|float|DateTimeInterface' ) From 29aaa0fa76fe177a61ef98d515a650cacbfc7414 Mon Sep 17 00:00:00 2001 From: Arnold Daniels Date: Fri, 27 Sep 2019 10:05:42 +0200 Subject: [PATCH 5/5] Update README.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Jáchym Toušek --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fdf56590..4593269f 100644 --- a/README.md +++ b/README.md @@ -9,7 +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`). +* 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)