Skip to content

Commit

Permalink
InvalidUnaryOperationRule: added support for BitwiseNot operator
Browse files Browse the repository at this point in the history
  • Loading branch information
dktapps authored and ondrejmirtes committed Jun 20, 2020
1 parent 9547231 commit 5f5de22
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/Rules/Operators/InvalidUnaryOperationRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
Expand Down
12 changes: 8 additions & 4 deletions tests/PHPStan/Rules/Operators/InvalidUnaryOperationRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
]);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Operators/data/invalid-unary.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ function (
) {
+$i;
-$i;
~$i;

+$str;
-$str;
~$str;

+'123';
-'123';
~'123';

+'bla';
-'bla';
~'123';

$array = [];
~$array;
~1.1;
};

0 comments on commit 5f5de22

Please sign in to comment.