-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Can disallow control structures like else, elseif, goto (#257)
Checking params inside ( ... ) doesn't work at the moment, so you can disallow all `declare()`s but can't re-allow e.g. `declare(strict-types = 1)`. If you try to disallow `else if` with the space, an exception will be thrown, because `else if` is parsed as `else` followed by `if`, so disallowing `else if` with the space wouldn't have the desired effect and the result would be unexpected. Close #68
- Loading branch information
Showing
38 changed files
with
2,602 additions
and
1 deletion.
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
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,59 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed\ControlStructures; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Break_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\ShouldNotHappenException; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; | ||
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; | ||
|
||
/** | ||
* Reports on using the break control structure. | ||
* | ||
* @package Spaze\PHPStan\Rules\Disallowed | ||
* @implements Rule<Break_> | ||
*/ | ||
class BreakControlStructure implements Rule | ||
{ | ||
|
||
/** @var DisallowedControlStructureRuleErrors */ | ||
private $disallowedControlStructureRuleErrors; | ||
|
||
/** @var list<DisallowedControlStructure> */ | ||
private $disallowedControlStructures; | ||
|
||
|
||
/** | ||
* @param DisallowedControlStructureRuleErrors $disallowedControlStructureRuleErrors | ||
* @param list<DisallowedControlStructure> $disallowedControlStructures | ||
*/ | ||
public function __construct(DisallowedControlStructureRuleErrors $disallowedControlStructureRuleErrors, array $disallowedControlStructures) | ||
{ | ||
$this->disallowedControlStructureRuleErrors = $disallowedControlStructureRuleErrors; | ||
$this->disallowedControlStructures = $disallowedControlStructures; | ||
} | ||
|
||
|
||
public function getNodeType(): string | ||
{ | ||
return Break_::class; | ||
} | ||
|
||
|
||
/** | ||
* @param Break_ $node | ||
* @param Scope $scope | ||
* @return list<RuleError> | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
return $this->disallowedControlStructureRuleErrors->get($scope, 'break', $this->disallowedControlStructures); | ||
} | ||
|
||
} |
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,59 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed\ControlStructures; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Continue_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\ShouldNotHappenException; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; | ||
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; | ||
|
||
/** | ||
* Reports on using the continue control structure. | ||
* | ||
* @package Spaze\PHPStan\Rules\Disallowed | ||
* @implements Rule<Continue_> | ||
*/ | ||
class ContinueControlStructure implements Rule | ||
{ | ||
|
||
/** @var DisallowedControlStructureRuleErrors */ | ||
private $disallowedControlStructureRuleErrors; | ||
|
||
/** @var list<DisallowedControlStructure> */ | ||
private $disallowedControlStructures; | ||
|
||
|
||
/** | ||
* @param DisallowedControlStructureRuleErrors $disallowedControlStructureRuleErrors | ||
* @param list<DisallowedControlStructure> $disallowedControlStructures | ||
*/ | ||
public function __construct(DisallowedControlStructureRuleErrors $disallowedControlStructureRuleErrors, array $disallowedControlStructures) | ||
{ | ||
$this->disallowedControlStructureRuleErrors = $disallowedControlStructureRuleErrors; | ||
$this->disallowedControlStructures = $disallowedControlStructures; | ||
} | ||
|
||
|
||
public function getNodeType(): string | ||
{ | ||
return Continue_::class; | ||
} | ||
|
||
|
||
/** | ||
* @param Continue_ $node | ||
* @param Scope $scope | ||
* @return list<RuleError> | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
return $this->disallowedControlStructureRuleErrors->get($scope, 'continue', $this->disallowedControlStructures); | ||
} | ||
|
||
} |
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,59 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace Spaze\PHPStan\Rules\Disallowed\ControlStructures; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\Declare_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\ShouldNotHappenException; | ||
use Spaze\PHPStan\Rules\Disallowed\DisallowedControlStructure; | ||
use Spaze\PHPStan\Rules\Disallowed\RuleErrors\DisallowedControlStructureRuleErrors; | ||
|
||
/** | ||
* Reports on using the declare control structure. | ||
* | ||
* @package Spaze\PHPStan\Rules\Disallowed | ||
* @implements Rule<Declare_> | ||
*/ | ||
class DeclareControlStructure implements Rule | ||
{ | ||
|
||
/** @var DisallowedControlStructureRuleErrors */ | ||
private $disallowedControlStructureRuleErrors; | ||
|
||
/** @var list<DisallowedControlStructure> */ | ||
private $disallowedControlStructures; | ||
|
||
|
||
/** | ||
* @param DisallowedControlStructureRuleErrors $disallowedControlStructureRuleErrors | ||
* @param list<DisallowedControlStructure> $disallowedControlStructures | ||
*/ | ||
public function __construct(DisallowedControlStructureRuleErrors $disallowedControlStructureRuleErrors, array $disallowedControlStructures) | ||
{ | ||
$this->disallowedControlStructureRuleErrors = $disallowedControlStructureRuleErrors; | ||
$this->disallowedControlStructures = $disallowedControlStructures; | ||
} | ||
|
||
|
||
public function getNodeType(): string | ||
{ | ||
return Declare_::class; | ||
} | ||
|
||
|
||
/** | ||
* @param Declare_ $node | ||
* @param Scope $scope | ||
* @return list<RuleError> | ||
* @throws ShouldNotHappenException | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
return $this->disallowedControlStructureRuleErrors->get($scope, 'declare', $this->disallowedControlStructures); | ||
} | ||
|
||
} |
Oops, something went wrong.