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
56 changes: 42 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -652,34 +652,62 @@ interface SomeInterface

<br>

### ForbiddenNodeRule
### ForbiddenTraitRule

"%s" is forbidden to use

:wrench: **configure it!**
Trait is forbidden to use, use explicit service composition instead

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

↓
```php
trait SomeTrait
{
}
```

:x:

<br>

```php
final class SomeService
{
}
```

:+1:

<br>

### ForbiddenSwitchRule

switch() is forbidden to use, use match() or early return instead

```yaml
rules:
- Symplify\PHPStanRules\Rules\ForbiddenSwitchRule
```

```php
return @strlen('...');
switch ($value) {
case 1:
return 'one';
default:
return 'many';
}
```

:x:

<br>

```php
return strlen('...');
return match ($value) {
1 => 'one',
default => 'many',
};
```

:+1:
Expand Down
20 changes: 7 additions & 13 deletions config/configurable-rules.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
conditionalTags:
Symplify\PHPStanRules\Rules\ForbiddenNodeRule:
Symplify\PHPStanRules\Rules\ForbiddenTraitRule:
phpstan.rules.rule: %symplify.configurable%
Symplify\PHPStanRules\Rules\ForbiddenSwitchRule:
phpstan.rules.rule: %symplify.configurable%
Symplify\PHPStanRules\Rules\PreferredClassRule:
phpstan.rules.rule: %symplify.configurable%
Expand All @@ -10,18 +12,10 @@ conditionalTags:

services:
-
class: Symplify\PHPStanRules\Rules\ForbiddenNodeRule
arguments:
forbiddenNodes:
- PhpParser\Node\Stmt\Trait_
- PhpParser\Node\Expr\Empty_
- PhpParser\Node\Stmt\Switch_
- PhpParser\Node\Expr\ErrorSuppress
- PhpParser\Node\Scalar\Encapsed
- PhpParser\Node\Scalar\EncapsedStringPart
# use pre* nodes instead
- PhpParser\Node\Expr\PostInc
- PhpParser\Node\Expr\PostDec
class: Symplify\PHPStanRules\Rules\ForbiddenTraitRule

-
class: Symplify\PHPStanRules\Rules\ForbiddenSwitchRule

-
class: Symplify\PHPStanRules\Rules\PreferredClassRule
Expand Down
12 changes: 2 additions & 10 deletions config/rector-rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,15 @@ rules:
- Symplify\PHPStanRules\Rules\Rector\NoIntegerRefactorReturnRule
- Symplify\PHPStanRules\Rules\Rector\AvoidFeatureSetAttributeInRectorRule
- Symplify\PHPStanRules\Rules\Rector\RectorCheaperGuardsFirstRule
- Symplify\PHPStanRules\Rules\ForbiddenTraitRule
- Symplify\PHPStanRules\Rules\ForbiddenSwitchRule

services:
# $node->getAttribute($1) => Type|null by $1
-
class: Symplify\PHPStanRules\ReturnTypeExtension\NodeGetAttributeTypeExtension
tags: [phpstan.broker.dynamicMethodReturnTypeExtension]

-
class: Symplify\PHPStanRules\Rules\ForbiddenNodeRule
tags: [phpstan.rules.rule]
arguments:
forbiddenNodes:
- PhpParser\Node\Expr\ErrorSuppress
- PhpParser\Node\Stmt\Switch_
- PhpParser\Node\Expr\Empty_
- PhpParser\Node\Stmt\Trait_

-
class: Symplify\PHPStanRules\Rules\SeeAnnotationToTestRule
tags: [phpstan.rules.rule]
Expand Down
7 changes: 6 additions & 1 deletion src/Enum/RuleIdentifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ final class RuleIdentifier
/**
* @var string
*/
public const FORBIDDEN_NODE = 'symplify.forbiddenNode';
public const FORBIDDEN_TRAIT = 'symplify.forbiddenTrait';

/**
* @var string
*/
public const FORBIDDEN_SWITCH = 'symplify.forbiddenSwitch';

/**
* @var string
Expand Down
81 changes: 0 additions & 81 deletions src/Rules/ForbiddenNodeRule.php

This file was deleted.

41 changes: 41 additions & 0 deletions src/Rules/ForbiddenSwitchRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Rules;

use PHPStan\Rules\IdentifierRuleError;
use PhpParser\Node;
use PhpParser\Node\Stmt\Switch_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Enum\RuleIdentifier;

/**
* @implements Rule<Switch_>
* @see \Symplify\PHPStanRules\Tests\Rules\ForbiddenSwitchRule\ForbiddenSwitchRuleTest
*/
final class ForbiddenSwitchRule implements Rule
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'switch() is forbidden to use, use match() or early return instead';

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

/**
* @param Switch_ $node
* @return IdentifierRuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier(RuleIdentifier::FORBIDDEN_SWITCH)
->build()];
}
}
41 changes: 41 additions & 0 deletions src/Rules/ForbiddenTraitRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Rules;

use PHPStan\Rules\IdentifierRuleError;
use PhpParser\Node;
use PhpParser\Node\Stmt\Trait_;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use Symplify\PHPStanRules\Enum\RuleIdentifier;

/**
* @implements Rule<Trait_>
* @see \Symplify\PHPStanRules\Tests\Rules\ForbiddenTraitRule\ForbiddenTraitRuleTest
*/
final class ForbiddenTraitRule implements Rule
{
/**
* @var string
*/
public const ERROR_MESSAGE = 'Trait is forbidden to use, use explicit service composition instead';

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

/**
* @param Trait_ $node
* @return IdentifierRuleError[]
*/
public function processNode(Node $node, Scope $scope): array
{
return [RuleErrorBuilder::message(self::ERROR_MESSAGE)
->identifier(RuleIdentifier::FORBIDDEN_TRAIT)
->build()];
}
}
13 changes: 0 additions & 13 deletions tests/Rules/ForbiddenNodeRule/Fixture/EmptyCall.php

This file was deleted.

14 changes: 0 additions & 14 deletions tests/Rules/ForbiddenNodeRule/config/configured_rule.neon

This file was deleted.

16 changes: 16 additions & 0 deletions tests/Rules/ForbiddenSwitchRule/Fixture/SkipMatch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\ForbiddenSwitchRule\Fixture;

final class SkipMatch
{
public function run(int $value): string
{
return match ($value) {
1 => 'one',
default => 'many',
};
}
}
18 changes: 18 additions & 0 deletions tests/Rules/ForbiddenSwitchRule/Fixture/SomeSwitch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Symplify\PHPStanRules\Tests\Rules\ForbiddenSwitchRule\Fixture;

final class SomeSwitch
{
public function run(int $value): string
{
switch ($value) {
case 1:
return 'one';
default:
return 'many';
}
}
}
Loading
Loading