Skip to content

Commit 7d3dee9

Browse files
authored
Complete README with new rules (#160)
* readme * improve docs
1 parent 6902f4a commit 7d3dee9

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,55 @@ abstract class ParentClass
264264

265265
<br>
266266

267+
### ForbiddenNewArgumentRule
268+
269+
Type "%s" is forbidden to be created manually with `new X()`. Use service and constructor injection instead
270+
271+
```yaml
272+
services:
273+
-
274+
class: Symplify\PHPStanRules\Rules\ForbiddenNewArgumentRule
275+
tag: [phpstan.rules.rule]
276+
arguments:
277+
forbiddenTypes:
278+
- RepositoryService
279+
```
280+
281+
282+
283+
```php
284+
class SomeService
285+
{
286+
public function run()
287+
{
288+
$repositoryService = new RepositoryService();
289+
$item = $repositoryService->get(1);
290+
}
291+
}
292+
```
293+
294+
:x:
295+
296+
<br>
297+
298+
```php
299+
class SomeService
300+
{
301+
public function __construct(private RepositoryService $repositoryService)
302+
{
303+
}
304+
305+
public function run()
306+
{
307+
$item = $this->repositoryService->get(1);
308+
}
309+
}
310+
```
311+
312+
:+1:
313+
314+
<br>
315+
267316
### ForbiddenFuncCallRule
268317

269318
Function `"%s()"` cannot be used/left in the code
@@ -963,6 +1012,17 @@ final class SomeClass
9631012

9641013
## 2. Doctrine-specific Rules
9651014

1015+
### RequireQueryBuilderOnRepositoryRule
1016+
1017+
Prevents using `$entityManager->createQueryBuilder('...')`, use `$repository->createQueryBuilder()` as safer.
1018+
1019+
```yaml
1020+
rules:
1021+
- Symplify\PHPStanRules\Rules\Doctrine\RequireQueryBuilderOnRepositoryRule
1022+
```
1023+
1024+
<br>
1025+
9661026
### NoGetRepositoryOutsideServiceRule
9671027
9681028
Instead of getting repository from EntityManager, use constructor injection and service pattern to keep code clean
@@ -1082,6 +1142,29 @@ final class SomeFixture extends AbstractFixture
10821142

10831143
## 3. Symfony-specific Rules
10841144

1145+
### NoGetDoctrineInControllerRule
1146+
1147+
Prevents using `$this->getDoctrine()` in controllers, to promote dependency injection.
1148+
1149+
```yaml
1150+
rules:
1151+
- Symplify\PHPStanRules\Rules\Symfony\NoGetDoctrineInControllerRule
1152+
```
1153+
1154+
<br>
1155+
1156+
### NoGetInControllerRule
1157+
1158+
Prevents using `$this->get(...)` in controllers, to promote dependency injection.
1159+
1160+
```yaml
1161+
rules:
1162+
- Symplify\PHPStanRules\Rules\Symfony\NoGetInControllerRule
1163+
```
1164+
1165+
<br>
1166+
1167+
10851168
### NoAbstractControllerConstructorRule
10861169

10871170
Abstract controller should not have constructor, as it can lead to tight coupling. Use @required annotation instead

config/doctrine-rules.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ rules:
22
- Symplify\PHPStanRules\Rules\Doctrine\NoGetRepositoryOutsideServiceRule
33
- Symplify\PHPStanRules\Rules\Doctrine\NoParentRepositoryRule
44
- Symplify\PHPStanRules\Rules\Doctrine\NoRepositoryCallInDataFixtureRule
5+
6+
# test fixtures
7+
- Symplify\PHPStanRules\Rules\Doctrine\RequireQueryBuilderOnRepositoryRule

src/Rules/Doctrine/NoEntityMockingRule.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr\MethodCall;
9-
use PhpParser\Node\Identifier;
109
use PHPStan\Analyser\Scope;
1110
use PHPStan\Reflection\ReflectionProvider;
1211
use PHPStan\Rules\Rule;

0 commit comments

Comments
 (0)