Skip to content

Commit 3a3263a

Browse files
authored
[symfony] Add NoDuplicateArg(s)AutowireByTypeRule, and NoServiceSameNameSetClassRule (#210)
* [symfony] Add NoDuplicateArgAutowireByTypeRule * [symfony] Add NoServiceSameNameSetClassRule * README
1 parent 64a07f2 commit 3a3263a

File tree

67 files changed

+837
-44
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+837
-44
lines changed

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,81 @@ rules:
14931493

14941494
<br>
14951495

1496+
### NoServiceSameNameSetClassRule
1497+
1498+
No need to duplicate service class and name. Use only "$services->set(%s::class)" instead
1499+
1500+
```yaml
1501+
rules:
1502+
- Symplify\PHPStanRules\Rules\Symfony\ConfigClosure\NoServiceSameNameSetClassRule
1503+
```
1504+
1505+
```php
1506+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1507+
1508+
return static function (ContainerConfigurator $containerConfigurator): void {
1509+
$services = $containerConfigurator->services();
1510+
1511+
$services->set(SomeService::class, SomeService::class);
1512+
};
1513+
```
1514+
1515+
:x:
1516+
1517+
<br>
1518+
1519+
```php
1520+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1521+
1522+
return static function (ContainerConfigurator $containerConfigurator): void {
1523+
$services = $containerConfigurator->services();
1524+
1525+
$services->set(SomeService::class);
1526+
};
1527+
```
1528+
1529+
<br>
1530+
1531+
### NoDuplicateArgsAutowireByTypeRule
1532+
1533+
Instead of passing "%s" to args(), remove the line and let autowiring handle it
1534+
1535+
```yaml
1536+
rules:
1537+
- Symplify\PHPStanRules\Rules\Symfony\ConfigClosure\NoDuplicateArgAutowireByTypeRule
1538+
- Symplify\PHPStanRules\Rules\Symfony\ConfigClosure\NoDuplicateArgsAutowireByTypeRule
1539+
```
1540+
1541+
```php
1542+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1543+
1544+
return static function (ContainerConfigurator $containerConfigurator): void {
1545+
$services = $containerConfigurator->services();
1546+
1547+
$services->set(SomeService::class)
1548+
->args([
1549+
ref(SomeService::class),
1550+
]);
1551+
};
1552+
```
1553+
1554+
:x:
1555+
1556+
<br>
1557+
1558+
```php
1559+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
1560+
1561+
return static function (ContainerConfigurator $containerConfigurator): void {
1562+
$services = $containerConfigurator->services();
1563+
1564+
$services->set(SomeService::class);
1565+
};
1566+
```
1567+
1568+
:+1:
1569+
1570+
<br>
14961571

14971572
### NoAbstractControllerConstructorRule
14981573

config/services/services.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ services:
2525
# doctrine
2626
- Symplify\PHPStanRules\Doctrine\DoctrineEntityDocumentAnalyser
2727

28+
# symfony
29+
- Symplify\PHPStanRules\Symfony\Reflection\ClassConstructorTypesResolver
30+
2831
# rules enabled by configuration
2932
-
3033
class: Symplify\PHPStanRules\Rules\MaximumIgnoredErrorCountRule

config/symfony-config-rules.neon

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,10 @@ rules:
33
- Symplify\PHPStanRules\Rules\Symfony\ConfigClosure\NoBundleResourceConfigRule
44
- Symplify\PHPStanRules\Rules\Symfony\ConfigClosure\AlreadyRegisteredAutodiscoveryServiceRule
55
- Symplify\PHPStanRules\Rules\Symfony\ConfigClosure\TaggedIteratorOverRepeatedServiceCallRule
6+
7+
# args() and arg() call
8+
- Symplify\PHPStanRules\Rules\Symfony\ConfigClosure\NoDuplicateArgsAutowireByTypeRule
9+
- Symplify\PHPStanRules\Rules\Symfony\ConfigClosure\NoDuplicateArgAutowireByTypeRule
10+
11+
# $services->set('X', 'X')
12+
- Symplify\PHPStanRules\Rules\Symfony\ConfigClosure\NoServiceSameNameSetClassRule

src/Enum/DoctrineRuleIdentifier.php renamed to src/Enum/RuleIdentifier/DoctrineRuleIdentifier.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\Enum;
5+
namespace Symplify\PHPStanRules\Enum\RuleIdentifier;
66

77
final class DoctrineRuleIdentifier
88
{

src/Enum/PHPUnitRuleIdentifier.php renamed to src/Enum/RuleIdentifier/PHPUnitRuleIdentifier.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\Enum;
5+
namespace Symplify\PHPStanRules\Enum\RuleIdentifier;
66

77
final class PHPUnitRuleIdentifier
88
{

src/Enum/RectorRuleIdentifier.php renamed to src/Enum/RuleIdentifier/RectorRuleIdentifier.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\Enum;
5+
namespace Symplify\PHPStanRules\Enum\RuleIdentifier;
66

77
final class RectorRuleIdentifier
88
{

src/Enum/SymfonyRuleIdentifier.php renamed to src/Enum/RuleIdentifier/SymfonyRuleIdentifier.php

Lines changed: 7 additions & 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\Enum;
5+
namespace Symplify\PHPStanRules\Enum\RuleIdentifier;
66

77
final class SymfonyRuleIdentifier
88
{
@@ -47,4 +47,10 @@ final class SymfonyRuleIdentifier
4747
public const NO_BUNDLE_RESOURCE_CONFIG = 'symfony.noBundleResourceConfig';
4848

4949
public const ALREADY_REGISTERED_AUTODISCOVERY_SERVICE = 'symfony.alreadyRegisteredAutodiscoveryService';
50+
51+
public const NO_DUPLICATE_ARGS_AUTOWIRE_BY_TYPE = 'symfony.noDuplicateArgsAutowireByType';
52+
53+
public const NO_DUPLICATE_ARG_AUTOWIRE_BY_TYPE = 'symfony.noDuplicateArgAutowireByType';
54+
55+
public const NO_SERVICE_SAME_NAME_SET_CLASS = 'symfony.noServiceSameNameSetClass';
5056
}

src/Enum/SymfonyFunctionName.php

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

77
final class SymfonyFunctionName
88
{
9-
public const REFERENCE = 'Symfony\Component\DependencyInjection\Loader\Configurator\ref';
9+
public const REF = 'Symfony\Component\DependencyInjection\Loader\Configurator\ref';
1010

1111
public const SERVICE = 'Symfony\Component\DependencyInjection\Loader\Configurator\service';
1212
}

src/Helper/NamingHelper.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Helper;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Identifier;
9+
use PhpParser\Node\Name;
10+
11+
final class NamingHelper
12+
{
13+
public static function getName(Node $node): ?string
14+
{
15+
if ($node instanceof Identifier || $node instanceof Name) {
16+
return $node->toString();
17+
}
18+
19+
return null;
20+
}
21+
22+
public static function isName(Node $node, string $name): bool
23+
{
24+
if ($node instanceof Identifier || $node instanceof Name) {
25+
return $node->toString() === $name;
26+
}
27+
28+
return false;
29+
}
30+
31+
/**
32+
* @param string[] $names
33+
*/
34+
public static function isNames(Node $node, array $names): bool
35+
{
36+
foreach ($names as $name) {
37+
if (self::isName($node, $name)) {
38+
return true;
39+
}
40+
}
41+
42+
return false;
43+
}
44+
}

src/Rules/Doctrine/NoDoctrineListenerWithoutContractRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use PHPStan\Rules\Rule;
1212
use PHPStan\Rules\RuleErrorBuilder;
1313
use Symplify\PHPStanRules\Doctrine\DoctrineEventSubscriberAnalyzer;
14-
use Symplify\PHPStanRules\Enum\DoctrineRuleIdentifier;
14+
use Symplify\PHPStanRules\Enum\RuleIdentifier\DoctrineRuleIdentifier;
1515

1616
/**
1717
* Based on https://tomasvotruba.com/blog/2019/07/22/how-to-convert-listeners-to-subscribers-and-reduce-your-configs

0 commit comments

Comments
 (0)