Skip to content

Commit b73d08e

Browse files
committed
misc
1 parent 1e7eb26 commit b73d08e

File tree

7 files changed

+58
-19
lines changed

7 files changed

+58
-19
lines changed

config/code-complexity-rules.neon

+5
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ services:
1313
class: Symplify\PHPStanRules\Collector\ImplementedInterfaceCollector
1414
tags:
1515
- phpstan.collector
16+
17+
-
18+
class: Symplify\PHPStanRules\Collector\InterfaceOfAbstractClassCollector
19+
tags:
20+
- phpstan.collector

config/services/services.neon

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
services:
2-
- PhpParser\NodeFinder
3-
42
- Symplify\PHPStanRules\NodeTraverser\SimpleCallableNodeTraverser
53
- Symplify\PHPStanRules\PhpDocParser\PhpDocNodeTraverser
64
- Symplify\PHPStanRules\Reflection\ReflectionParser
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Symplify\PHPStanRules\Collector;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Stmt\Class_;
9+
use PHPStan\Analyser\Scope;
10+
use PHPStan\Collectors\Collector;
11+
12+
final class InterfaceOfAbstractClassCollector implements Collector
13+
{
14+
public function getNodeType(): string
15+
{
16+
return Class_::class;
17+
}
18+
19+
/**
20+
* @param Class_ $node
21+
* @return string[]|null
22+
*/
23+
public function processNode(Node $node, Scope $scope): ?array
24+
{
25+
if (! $node->isAbstract()) {
26+
return null;
27+
}
28+
29+
$interfaceNames = [];
30+
31+
foreach ($node->implements as $implement) {
32+
$interfaceNames[] = $implement->toString();
33+
}
34+
35+
return $interfaceNames;
36+
}
37+
}

src/NodeFinder/TypeAwareNodeFinder.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
*/
1313
final class TypeAwareNodeFinder
1414
{
15-
public function __construct(
16-
private readonly NodeFinder $nodeFinder
17-
) {
15+
private NodeFinder $nodeFinder;
16+
17+
public function __construct()
18+
{
19+
$this->nodeFinder = new NodeFinder();
1820
}
1921

2022
/**

src/Rules/NoSingleInterfaceImplementerRule.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPStan\Rules\RuleErrorBuilder;
1414
use Symplify\PHPStanRules\Collector\ImplementedInterfaceCollector;
1515
use Symplify\PHPStanRules\Collector\InterfaceCollector;
16+
use Symplify\PHPStanRules\Collector\InterfaceOfAbstractClassCollector;
1617
use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;
1718
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1819
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -45,10 +46,14 @@ public function processNode(Node $node, Scope $scope): array
4546
{
4647
$implementedInterfaces = Arrays::flatten($node->get(ImplementedInterfaceCollector::class));
4748
$interfaces = Arrays::flatten($node->get(InterfaceCollector::class));
49+
$interfacesOfAbstractClass = Arrays::flatten($node->get(InterfaceOfAbstractClassCollector::class));
4850

4951
$onceUsedInterfaces = $this->resolveOnceUsedInterfaces($implementedInterfaces);
50-
5152
$onceImplementedInterfaces = array_intersect($onceUsedInterfaces, $interfaces);
53+
54+
// remove the abstract class implemented, as required transitionally
55+
$onceImplementedInterfaces = array_diff($onceImplementedInterfaces, $interfacesOfAbstractClass);
56+
5257
if ($onceImplementedInterfaces === []) {
5358
return [];
5459
}

tests/Rules/NoSingleInterfaceImplementerRule/NoSingleInterfaceImplementerRuleTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPUnit\Framework\Attributes\DataProvider;
1111
use Symplify\PHPStanRules\Collector\ImplementedInterfaceCollector;
1212
use Symplify\PHPStanRules\Collector\InterfaceCollector;
13+
use Symplify\PHPStanRules\Collector\InterfaceOfAbstractClassCollector;
1314
use Symplify\PHPStanRules\Rules\NoSingleInterfaceImplementerRule;
1415
use Symplify\PHPStanRules\Tests\Rules\NoSingleInterfaceImplementerRule\Fixture\SimpleInterface;
1516

@@ -61,6 +62,7 @@ protected function getCollectors(): array
6162
return [
6263
self::getContainer()->getByType(ImplementedInterfaceCollector::class),
6364
self::getContainer()->getByType(InterfaceCollector::class),
65+
self::getContainer()->getByType(InterfaceOfAbstractClassCollector::class),
6466
];
6567
}
6668

Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
rules:
2-
- Symplify\PHPStanRules\Rules\NoSingleInterfaceImplementerRule
3-
4-
services:
5-
-
6-
class: Symplify\PHPStanRules\Collector\InterfaceCollector
7-
tags:
8-
- phpstan.collector
9-
10-
-
11-
class: Symplify\PHPStanRules\Collector\ImplementedInterfaceCollector
12-
tags:
13-
- phpstan.collector
1+
includes:
2+
- ../../../../config/code-complexity-rules.neon
3+
- ../../../../config/services/services.neon

0 commit comments

Comments
 (0)