diff --git a/src/Rules/Operators/InvalidUnaryOperationRule.php b/src/Rules/Operators/InvalidUnaryOperationRule.php index 5ddeda862a..feddcd33be 100644 --- a/src/Rules/Operators/InvalidUnaryOperationRule.php +++ b/src/Rules/Operators/InvalidUnaryOperationRule.php @@ -23,15 +23,24 @@ public function processNode(\PhpParser\Node $node, Scope $scope): array if ( !$node instanceof \PhpParser\Node\Expr\UnaryPlus && !$node instanceof \PhpParser\Node\Expr\UnaryMinus + && !$node instanceof \PhpParser\Node\Expr\BitwiseNot ) { return []; } if ($scope->getType($node) instanceof ErrorType) { + + if ($node instanceof \PhpParser\Node\Expr\UnaryPlus) { + $operator = '+'; + } elseif ($node instanceof \PhpParser\Node\Expr\UnaryMinus) { + $operator = '-'; + } else { + $operator = '~'; + } return [ RuleErrorBuilder::message(sprintf( 'Unary operation "%s" on %s results in an error.', - $node instanceof \PhpParser\Node\Expr\UnaryPlus ? '+' : '-', + $operator, $scope->getType($node->expr)->describe(VerbosityLevel::value()) ))->line($node->expr->getLine())->build(), ]; diff --git a/tests/PHPStan/Rules/Operators/InvalidUnaryOperationRuleTest.php b/tests/PHPStan/Rules/Operators/InvalidUnaryOperationRuleTest.php index c978abb5e4..77bb4fe9e3 100644 --- a/tests/PHPStan/Rules/Operators/InvalidUnaryOperationRuleTest.php +++ b/tests/PHPStan/Rules/Operators/InvalidUnaryOperationRuleTest.php @@ -18,19 +18,23 @@ public function testRule(): void $this->analyse([__DIR__ . '/data/invalid-unary.php'], [ [ 'Unary operation "+" on string results in an error.', - 10, + 11, ], [ 'Unary operation "-" on string results in an error.', - 11, + 12, ], [ 'Unary operation "+" on \'bla\' results in an error.', - 16, + 19, ], [ 'Unary operation "-" on \'bla\' results in an error.', - 17, + 20, + ], + [ + 'Unary operation "~" on array() results in an error.', + 24, ], ]); } diff --git a/tests/PHPStan/Rules/Operators/data/invalid-unary.php b/tests/PHPStan/Rules/Operators/data/invalid-unary.php index 404395b2bc..dea25521ce 100644 --- a/tests/PHPStan/Rules/Operators/data/invalid-unary.php +++ b/tests/PHPStan/Rules/Operators/data/invalid-unary.php @@ -6,13 +6,21 @@ function ( ) { +$i; -$i; + ~$i; +$str; -$str; + ~$str; +'123'; -'123'; + ~'123'; +'bla'; -'bla'; + ~'123'; + + $array = []; + ~$array; + ~1.1; };