-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\BooleansInConditions; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Do_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<Do_> | ||
*/ | ||
class BooleanInDoWhileConditionRule implements Rule | ||
{ | ||
|
||
private BooleanRuleHelper $helper; | ||
|
||
public function __construct(BooleanRuleHelper $helper) | ||
{ | ||
$this->helper = $helper; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Do_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->helper->passesAsBoolean($scope, $node->cond)) { | ||
return []; | ||
} | ||
|
||
$conditionExpressionType = $scope->getType($node->cond); | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Only booleans are allowed in a do-while condition, %s given.', | ||
$conditionExpressionType->describe(VerbosityLevel::typeOnly()), | ||
))->identifier('doWhile.condNotBoolean')->build(), | ||
]; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\BooleansInConditions; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\While_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<While_> | ||
*/ | ||
class BooleanInWhileConditionRule implements Rule | ||
{ | ||
|
||
private BooleanRuleHelper $helper; | ||
|
||
public function __construct(BooleanRuleHelper $helper) | ||
{ | ||
$this->helper = $helper; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return While_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ($this->helper->passesAsBoolean($scope, $node->cond)) { | ||
return []; | ||
} | ||
|
||
$conditionExpressionType = $scope->getType($node->cond); | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Only booleans are allowed in a while condition, %s given.', | ||
$conditionExpressionType->describe(VerbosityLevel::typeOnly()), | ||
))->identifier('while.condNotBoolean')->build(), | ||
]; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Rules\BooleansInConditions; | ||
|
||
use PHPStan\Rules\BooleansInConditions\BooleanInDoWhileConditionRule; | ||
use PHPStan\Rules\BooleansInConditions\BooleanRuleHelper; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<BooleanInDoWhileConditionRule> | ||
*/ | ||
class BooleanInDoWhileConditionRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new BooleanInDoWhileConditionRule( | ||
Check failure on line 19 in tests/Rules/BooleansInConditions/BooleanInDoWhileConditionRuleTest.php
|
||
new BooleanRuleHelper( | ||
Check failure on line 20 in tests/Rules/BooleansInConditions/BooleanInDoWhileConditionRuleTest.php
|
||
self::getContainer()->getByType(RuleLevelHelper::class), | ||
), | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/conditions.php'], [ | ||
[ | ||
'Only booleans are allowed in a do-while condition, string given.', | ||
60, | ||
], | ||
]); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Rules\BooleansInConditions; | ||
|
||
use PHPStan\Rules\BooleansInConditions\BooleanInWhileConditionRule; | ||
use PHPStan\Rules\BooleansInConditions\BooleanRuleHelper; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<BooleanInWhileConditionRule> | ||
*/ | ||
class BooleanInWhileConditionRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new BooleanInWhileConditionRule( | ||
Check failure on line 19 in tests/Rules/BooleansInConditions/BooleanInWhileConditionRuleTest.php
|
||
new BooleanRuleHelper( | ||
Check failure on line 20 in tests/Rules/BooleansInConditions/BooleanInWhileConditionRuleTest.php
|
||
self::getContainer()->getByType(RuleLevelHelper::class), | ||
), | ||
); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/conditions.php'], [ | ||
[ | ||
'Only booleans are allowed in a while condition, string given.', | ||
55, | ||
], | ||
]); | ||
} | ||
|
||
} |