Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,38 @@ return match ($value) {

<br>

### ForbiddenNodeRule

"%s" is forbidden to use

:wrench: **configure it!**

```yaml
services:
-
class: Symplify\PHPStanRules\Rules\ForbiddenNodeRule
tags: [phpstan.rules.rule]
arguments:
forbiddenNodes:
- PhpParser\Node\Expr\ErrorSuppress
```

```php
return @strlen('...');
```

:x:

<br>

```php
return strlen('...');
```

:+1:

<br>

### ForbiddenStaticClassConstFetchRule

Avoid static access of constants, as they can change value. Use interface and contract method instead
Expand Down
5 changes: 5 additions & 0 deletions src/Enum/RuleIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ final class RuleIdentifier
*/
public const FORBIDDEN_SWITCH = 'symplify.forbiddenSwitch';

/**
* @var string
*/
public const FORBIDDEN_NODE = 'symplify.forbiddenNode';

/**
* @var string
*/
Expand Down
80 changes: 80 additions & 0 deletions src/Rules/ForbiddenNodeRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Rules;

use Entropy\Validation\Assert;
use PhpParser\Node;
use PhpParser\Node\Scalar\Encapsed;
use PhpParser\Node\Scalar\EncapsedStringPart;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Enum\RuleIdentifier;

/**
* @implements Rule<Node>
*/
final class ForbiddenNodeRule implements Rule
{
/**
* @var array<class-string<Node>>
* @readonly
*/
private array $forbiddenNodes;

/**
* @var string
*/
public const ERROR_MESSAGE = '"%s" is forbidden to use';

/**
* @readonly
*/
private Standard $standard;

/**
* @param array<class-string<Node>> $forbiddenNodes
*/
public function __construct(
array $forbiddenNodes
) {
$this->forbiddenNodes = $forbiddenNodes;
Assert::allIsAOf($forbiddenNodes, Node::class);

$this->standard = new Standard();
}

public function getNodeType(): string
{
return Node::class;
}

public function processNode(Node $node, Scope $scope): array
{
foreach ($this->forbiddenNodes as $forbiddenNode) {
if (! $node instanceof $forbiddenNode) {
continue;
}

// this node can't be printed as standalone
if ($node instanceof EncapsedStringPart) {
$contents = $this->standard->prettyPrintExpr(new Encapsed([$node]));
} else {
$contents = $this->standard->prettyPrint([$node]);
}

$errorMessage = sprintf(self::ERROR_MESSAGE, $contents);

$ruleError = RuleErrorBuilder::message($errorMessage)
->identifier(RuleIdentifier::FORBIDDEN_NODE)
->build();

return [$ruleError];
}

return [];
}
}
Loading