-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dmitriy Dymarchuk
authored
Sep 30, 2020
1 parent
334898a
commit c5fa47c
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Operators; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Ternary; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Type\ConstantType; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\Ternary> | ||
*/ | ||
class OperandsInTernaryOperatorRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Ternary::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$ifType = $node->if === null ? $scope->getType($node->cond) : $scope->getType($node->if); | ||
$elseType = $scope->getType($node->else); | ||
if ($ifType instanceof ConstantType && $elseType instanceof ConstantType) { | ||
if ($ifType->equals($elseType)) { | ||
return [sprintf( | ||
'If and else parts of ternary operator are equal (%s).', | ||
$ifType->describe(VerbosityLevel::value()) | ||
)]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
tests/Rules/Operators/OperandsInTernaryOperatorRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Operators; | ||
|
||
use PHPStan\Rules\Rule; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<OperandsInTernaryOperatorRule> | ||
*/ | ||
class OperandsInTernaryOperatorRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new OperandsInTernaryOperatorRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/operators.php'], [ | ||
[ | ||
'If and else parts of ternary operator are equal (true).', | ||
113, | ||
], | ||
[ | ||
'If and else parts of ternary operator are equal (array()).', | ||
118, | ||
], | ||
[ | ||
'If and else parts of ternary operator are equal (\'string_val\').', | ||
119, | ||
], | ||
[ | ||
'If and else parts of ternary operator are equal (array(23, 24)).', | ||
121, | ||
], | ||
[ | ||
'If and else parts of ternary operator are equal (array(1 => 1)).', | ||
122, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters