Skip to content

Commit af1d927

Browse files
committed
use rule identifiers
1 parent bd8de9e commit af1d927

17 files changed

+101
-46
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,28 @@ final class SomeClass
10651065

10661066
<br>
10671067

1068+
## Doctrine-specific Rules
1069+
1070+
### NoGetRepositoryOutsideServiceRule
1071+
1072+
Instead of getting repository from EntityManager, use constructor injection and service pattern to keep code clean
1073+
1074+
```yaml
1075+
rules:
1076+
- Symplify\PHPStanRules\Rules\Doctrine\NoGetRepositoryOutsideServiceRule
1077+
```
1078+
1079+
```php
1080+
class SomeClass
1081+
{
1082+
public function run(EntityManagerInterface $entityManager)
1083+
{
1084+
return $entityManager->getRepository(SomeEntity::class);
1085+
}
1086+
}
1087+
```
1088+
1089+
10681090
<!-- ruledoc-end -->
10691091

10701092
<br>

src/Enum/RuleIdentifier.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,26 @@ final class RuleIdentifier
182182
public const NO_DYNAMIC_NAME = 'symplify.noDynamicName';
183183

184184
public const NO_REFERENCE = 'symplify.noReference';
185+
186+
public const PHPUNIT_NO_MOCK_ONLY = 'phpunit.noMockOnly';
187+
188+
public const SINGLE_ARG_EVENT_DISPATCH = 'symfony.singleArgEventDispatch';
189+
190+
public const NO_ENTITY_MOCKING = 'doctrine.noEntityMocking';
191+
192+
public const NO_STRING_IN_GET_SUBSCRIBED_EVENTS = 'symfony.noStringInGetSubscribedEvents';
193+
194+
public const NO_LISTENER_WITHOUT_CONTRACT = 'symfony.noListenerWithoutContract';
195+
196+
public const DOCTRINE_NO_PARENT_REPOSITORY = 'doctrine.noParentRepository';
197+
198+
public const DOCTRINE_NO_GET_REPOSITORY_OUTSIDE_SERVICE = 'doctrine.noGetRepositoryOutsideService';
199+
200+
public const SYMFONY_NO_REQUIRED_OUTSIDE_CLASS = 'symfony.noRequiredOutsideClass';
201+
202+
public const NO_CONSTRUCTOR_OVERRIDE = 'symplify.noConstructorOverride';
203+
204+
public const SYMFONY_NO_ABSTRACT_CONTROLLER_CONSTRUCTOR = 'symfony.noAbstractControllerConstructor';
205+
206+
public const PHPUNIT_PUBLIC_STATIC_DATA_PROVIDER = 'phpunit.publicStaticDataProvider';
185207
}

src/PHPStan/Rules/Doctrine/NoGetRepositoryOutsideServiceRule.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99
use PhpParser\Node\Identifier;
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\Rules\Rule;
12-
use PHPStan\Rules\RuleError;
1312
use PHPStan\Rules\RuleErrorBuilder;
13+
use Symplify\PHPStanRules\Enum\RuleIdentifier;
1414

1515
/**
16-
* Check if abstract controller has constructor, as it should use
17-
* #[Require] instead to avoid parent constructor override
18-
*
19-
* @see \Symplify\PHPStanRules\Tests\PHPStan\Rule\NoGetRepositoryOutsideServiceRule\NoGetRepositoryOutsideServiceRuleTest
16+
* @see \Symplify\PHPStanRules\Tests\Rules\Doctrine\NoGetRepositoryOutsideServiceRule\NoGetRepositoryOutsideServiceRuleTest
2017
*
2118
* @implements Rule<MethodCall>
2219
*/
@@ -34,7 +31,6 @@ public function getNodeType(): string
3431

3532
/**
3633
* @param MethodCall $node
37-
* @return RuleError[]
3834
*/
3935
public function processNode(Node $node, Scope $scope): array
4036
{
@@ -47,7 +43,10 @@ public function processNode(Node $node, Scope $scope): array
4743
}
4844

4945
if (! $scope->isInClass()) {
50-
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build();
46+
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
47+
->identifier(RuleIdentifier::DOCTRINE_NO_GET_REPOSITORY_OUTSIDE_SERVICE)
48+
->build();
49+
5150
return [$ruleError];
5251
}
5352

@@ -57,7 +56,10 @@ public function processNode(Node $node, Scope $scope): array
5756
return [];
5857
}
5958

60-
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build();
59+
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
60+
->identifier(RuleIdentifier::DOCTRINE_NO_GET_REPOSITORY_OUTSIDE_SERVICE)
61+
->build();
62+
6163
return [$ruleError];
6264
}
6365
}

src/PHPStan/Rules/Doctrine/NoParentRepositoryRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use PhpParser\Node\Stmt\Class_;
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\Rules\Rule;
12-
use PHPStan\Rules\RuleError;
1312
use PHPStan\Rules\RuleErrorBuilder;
13+
use Symplify\PHPStanRules\Enum\RuleIdentifier;
1414

1515
/**
1616
* Check if class extends repository class,
@@ -39,7 +39,6 @@ public function getNodeType(): string
3939

4040
/**
4141
* @param Class_ $node
42-
* @return RuleError[]
4342
*/
4443
public function processNode(Node $node, Scope $scope): array
4544
{
@@ -53,6 +52,7 @@ public function processNode(Node $node, Scope $scope): array
5352
}
5453

5554
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
55+
->identifier(RuleIdentifier::DOCTRINE_NO_PARENT_REPOSITORY)
5656
->build();
5757

5858
return [$ruleError];

src/Testing/DataProviderMethodResolver.php renamed to src/PHPUnit/DataProviderMethodResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
declare(strict_types=1);
44

5-
namespace Symplify\PHPStanRules\PHPStan;
5+
namespace Symplify\PHPStanRules\PHPUnit;
66

77
use PhpParser\Comment\Doc;
88
use PhpParser\Node\Stmt\ClassMethod;

src/Rules/Complexity/NoConstructorOverrideRule.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use PhpParser\NodeFinder;
1111
use PHPStan\Analyser\Scope;
1212
use PHPStan\Rules\Rule;
13-
use PHPStan\Rules\RuleError;
1413
use PHPStan\Rules\RuleErrorBuilder;
14+
use Symplify\PHPStanRules\Enum\RuleIdentifier;
1515

1616
/**
1717
* @implements Rule<ClassMethod>
@@ -35,7 +35,6 @@ public function getNodeType(): string
3535

3636
/**
3737
* @param ClassMethod $node
38-
* @return RuleError[]
3938
*/
4039
public function processNode(Node $node, Scope $scope): array
4140
{
@@ -69,7 +68,10 @@ public function processNode(Node $node, Scope $scope): array
6968
return [];
7069
}
7170

72-
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build();
71+
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
72+
->identifier(RuleIdentifier::NO_CONSTRUCTOR_OVERRIDE)
73+
->build();
74+
7375
return [$ruleError];
7476
}
7577
}

src/Rules/Doctrine/NoEntityMockingRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\Reflection\ReflectionProvider;
1212
use PHPStan\Rules\Rule;
13-
use PHPStan\Rules\RuleError;
1413
use PHPStan\Rules\RuleErrorBuilder;
14+
use Symplify\PHPStanRules\Enum\RuleIdentifier;
1515
use Symplify\PHPStanRules\PHPStan\DoctrineEntityDocumentAnalyser;
1616

1717
/**
@@ -40,7 +40,6 @@ public function getNodeType(): string
4040

4141
/**
4242
* @param MethodCall $node
43-
* @return RuleError[]
4443
*/
4544
public function processNode(Node $node, Scope $scope): array
4645
{
@@ -62,6 +61,7 @@ public function processNode(Node $node, Scope $scope): array
6261
}
6362

6463
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
64+
->identifier(RuleIdentifier::NO_ENTITY_MOCKING)
6565
->build();
6666

6767
return [$ruleError];

src/Rules/PHPUnit/NoMockOnlyTestRule.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22

33
declare(strict_types=1);
44

5-
namespace Symplify\PHPStanRules\PHPStan\Rule;
5+
namespace Symplify\PHPStanRules\Rules\PHPUnit;
66

77
use PhpParser\Node;
88
use PhpParser\Node\Name;
99
use PhpParser\Node\Stmt\Class_;
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\Node\InClassNode;
1212
use PHPStan\Rules\Rule;
13-
use PHPStan\Rules\RuleError;
1413
use PHPStan\Rules\RuleErrorBuilder;
15-
use Symplify\PHPStanRules\PHPStan\PHPUnitTestAnalyser;
14+
use Symplify\PHPStanRules\Enum\RuleIdentifier;
1615

1716
/**
18-
* @see \Symplify\PHPStanRules\Tests\PHPStan\Rule\NoMockOnlyTestRule\NoMockOnlyTestRuleTest
17+
* @see \Symplify\PHPStanRules\Tests\Rules\PHPUnit\NoMockOnlyTestRule\NoMockOnlyTestRuleTest
1918
*
2019
* @implements Rule<InClassNode>
2120
*/
@@ -38,11 +37,10 @@ public function getNodeType(): string
3837

3938
/**
4039
* @param InClassNode $node
41-
* @return RuleError[]
4240
*/
4341
public function processNode(Node $node, Scope $scope): array
4442
{
45-
if (! PHPUnitTestAnalyser::isTestClass($scope)) {
43+
if (! \Symplify\PHPStanRules\Testing\PHPUnitTestAnalyser::isTestClass($scope)) {
4644
return [];
4745
}
4846

@@ -74,7 +72,9 @@ public function processNode(Node $node, Scope $scope): array
7472
}
7573

7674
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
75+
->identifier(RuleIdentifier::PHPUNIT_NO_MOCK_ONLY)
7776
->build();
77+
7878
return [$ruleError];
7979
}
8080
}

src/Rules/PHPUnit/PublicStaticDataProviderRule.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
declare(strict_types=1);
44

5-
namespace Symplify\PHPStanRules\PHPStan\Rule;
5+
namespace Symplify\PHPStanRules\Rules\PHPUnit;
66

77
use PhpParser\Node;
88
use PhpParser\Node\Stmt\ClassMethod;
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Node\InClassNode;
1111
use PHPStan\Rules\Rule;
1212
use PHPStan\Rules\RuleErrorBuilder;
13-
use Symplify\PHPStanRules\PHPStan\DataProviderMethodResolver;
14-
use Symplify\PHPStanRules\PHPStan\PHPUnitTestAnalyser;
13+
use Symplify\PHPStanRules\Enum\RuleIdentifier;
14+
use Symplify\PHPStanRules\PHPUnit\DataProviderMethodResolver;
1515

1616
/**
1717
* PHPUnit data provider have to be public and static
@@ -42,15 +42,15 @@ public function getNodeType(): string
4242
*/
4343
public function processNode(Node $node, Scope $scope): array
4444
{
45-
if (! PHPUnitTestAnalyser::isTestClass($scope)) {
45+
if (! \Symplify\PHPStanRules\Testing\PHPUnitTestAnalyser::isTestClass($scope)) {
4646
return [];
4747
}
4848

4949
$ruleErrors = [];
5050

5151
$classLike = $node->getOriginalNode();
5252
foreach ($classLike->getMethods() as $classMethod) {
53-
if (! PHPUnitTestAnalyser::isTestClassMethod($classMethod)) {
53+
if (! \Symplify\PHPStanRules\Testing\PHPUnitTestAnalyser::class::isTestClassMethod($classMethod)) {
5454
continue;
5555
}
5656

@@ -75,7 +75,7 @@ public function processNode(Node $node, Scope $scope): array
7575
if (! $dataProviderClassMethod->isStatic()) {
7676
$errorMessage = sprintf(self::PUBLIC_ERROR_MESSAGE, $dataProviderMethodName);
7777
$ruleErrors[] = RuleErrorBuilder::message($errorMessage)
78-
->identifier('phpunit.publicDataProvider')
78+
->identifier(RuleIdentifier::PHPUNIT_PUBLIC_STATIC_DATA_PROVIDER)
7979
->line($dataProviderClassMethod->getLine())
8080
->build();
8181
}

src/Rules/Symfony/NoAbstractControllerConstructorRule.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
use PhpParser\Node\Stmt\Class_;
1010
use PHPStan\Analyser\Scope;
1111
use PHPStan\Rules\Rule;
12-
use PHPStan\Rules\RuleError;
1312
use PHPStan\Rules\RuleErrorBuilder;
13+
use Symplify\PHPStanRules\Enum\RuleIdentifier;
1414

1515
/**
1616
* Check if abstract controller has constructor, as it should use
@@ -34,7 +34,6 @@ public function getNodeType(): string
3434

3535
/**
3636
* @param Class_ $node
37-
* @return RuleError[]
3837
*/
3938
public function processNode(Node $node, Scope $scope): array
4039
{
@@ -55,7 +54,10 @@ public function processNode(Node $node, Scope $scope): array
5554
return [];
5655
}
5756

58-
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)->build();
57+
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
58+
->identifier(RuleIdentifier::SYMFONY_NO_ABSTRACT_CONTROLLER_CONSTRUCTOR)
59+
->build();
60+
5961
return [$ruleError];
6062
}
6163
}

0 commit comments

Comments
 (0)