Skip to content

Commit 48bf682

Browse files
committed
use rule identifiers
1 parent 5edccce commit 48bf682

27 files changed

+134
-71
lines changed

README.md

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

10291029
<br>
10301030

1031+
## Doctrine-specific Rules
1032+
1033+
### NoGetRepositoryOutsideServiceRule
1034+
1035+
Instead of getting repository from EntityManager, use constructor injection and service pattern to keep code clean
1036+
1037+
```yaml
1038+
rules:
1039+
- Symplify\PHPStanRules\Rules\Doctrine\NoGetRepositoryOutsideServiceRule
1040+
```
1041+
1042+
```php
1043+
class SomeClass
1044+
{
1045+
public function run(EntityManagerInterface $entityManager)
1046+
{
1047+
return $entityManager->getRepository(SomeEntity::class);
1048+
}
1049+
}
1050+
```
1051+
1052+
10311053
<!-- ruledoc-end -->
10321054

10331055
<br>

composer-dependency-analyser.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
return (new Configuration())->addPathToScan(__DIR__ . '/src', false)
99
->addPathToExclude(__DIR__ . '/tests/Rules/Rector/NoInstanceOfStaticReflectionRule/Fixture')
1010
->addPathToExclude(__DIR__ . '/tests/Rules/Enum/RequireUniqueEnumConstantRule/Fixture')
11-
->addPathToExclude(__DIR__ . '/tests/Rules/NoReturnArrayVariableListRule/Fixture')
1211
->addPathToExclude(__DIR__ . '/tests/Rules/ForbiddenExtendOfNonAbstractClassRule/Fixture')
1312
->addPathToExclude(__DIR__ . '/tests/Rules/PHPUnit/NoTestMocksRule/Fixture')
1413

config/services/services.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ services:
1313
- Symplify\PHPStanRules\PhpDoc\BarePhpDocParser
1414
- Symplify\PHPStanRules\PhpDoc\PhpDocResolver
1515
- Symplify\PHPStanRules\TypeAnalyzer\CallableTypeAnalyzer
16+
17+
# doctrine
18+
- Symplify\PHPStanRules\Doctrine\DoctrineEntityDocumentAnalyser

src/Doctrine/DoctrineEntityDocumentAnalyser.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\Doctrine;
66

77
use PHPStan\PhpDoc\ResolvedPhpDocBlock;
88
use PHPStan\Reflection\ClassReflection;

src/Enum/ClassName.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ final class ClassName
2424
/**
2525
* @var string
2626
*/
27-
public const EVENT_DISPATCHER_INTERFACE = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
27+
public const EVENT_DISPATCHER_INTERFACE = 'Symfony\Component\EventDispatcher\EventDispatcherInterface';
2828

2929
/**
3030
* @var string

src/Enum/RuleIdentifier.php

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

179179
public const NO_REFERENCE = 'symplify.noReference';
180+
181+
public const PHPUNIT_NO_MOCK_ONLY = 'phpunit.noMockOnly';
182+
183+
public const SINGLE_ARG_EVENT_DISPATCH = 'symfony.singleArgEventDispatch';
184+
185+
public const NO_ENTITY_MOCKING = 'doctrine.noEntityMocking';
186+
187+
public const NO_STRING_IN_GET_SUBSCRIBED_EVENTS = 'symfony.noStringInGetSubscribedEvents';
188+
189+
public const NO_LISTENER_WITHOUT_CONTRACT = 'symfony.noListenerWithoutContract';
190+
191+
public const DOCTRINE_NO_PARENT_REPOSITORY = 'doctrine.noParentRepository';
192+
193+
public const DOCTRINE_NO_GET_REPOSITORY_OUTSIDE_SERVICE = 'doctrine.noGetRepositoryOutsideService';
194+
195+
public const SYMFONY_NO_REQUIRED_OUTSIDE_CLASS = 'symfony.noRequiredOutsideClass';
196+
197+
public const NO_CONSTRUCTOR_OVERRIDE = 'symplify.noConstructorOverride';
198+
199+
public const SYMFONY_NO_ABSTRACT_CONTROLLER_CONSTRUCTOR = 'symfony.noAbstractControllerConstructor';
200+
201+
public const PHPUNIT_PUBLIC_STATIC_DATA_PROVIDER = 'phpunit.publicStaticDataProvider';
180202
}

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: 4 additions & 4 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
{
@@ -52,9 +51,10 @@ public function processNode(Node $node, Scope $scope): array
5251
return [];
5352
}
5453

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

58-
return [$ruleError];
58+
return [$identifierRuleError];
5959
}
6060
}

src/PHPStan/Rules/Doctrine/NoRepositoryCallInDataFixtureRule.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public function processNode(Node $node, Scope $scope): array
5555
return [];
5656
}
5757

58-
$ruleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
58+
$identifierRuleError = RuleErrorBuilder::message(self::ERROR_MESSAGE)
5959
->identifier(RuleIdentifier::DOCTRINE_NO_REPOSITORY_CALL_IN_DATA_FIXTURES)
6060
->build();
6161

62-
return [$ruleError];
62+
return [$identifierRuleError];
6363
}
6464

6565
private function isDataFixtureClass(Scope $scope): bool

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;

0 commit comments

Comments
 (0)