Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Provider/SymfonyUsageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,11 @@ private function fillDicClasses(string $containerXmlPath): void
foreach ($xml->services->service as $serviceDefinition) {
/** @var SimpleXMLElement $serviceAttributes */
$serviceAttributes = $serviceDefinition->attributes();

if ($this->isServiceNotInstantiatedByContainer($serviceDefinition, $serviceAttributes)) {
continue;
}

$class = isset($serviceAttributes->class) ? (string) $serviceAttributes->class : null;
$constructor = isset($serviceAttributes->constructor) ? (string) $serviceAttributes->constructor : '__construct';

Expand Down Expand Up @@ -1191,6 +1196,32 @@ private function fillDicClasses(string $containerXmlPath): void
}
}

private function isServiceNotInstantiatedByContainer(
SimpleXMLElement $serviceDefinition,
SimpleXMLElement $serviceAttributes,
): bool
{
if (isset($serviceAttributes->abstract) && (string) $serviceAttributes->abstract === 'true') {
return true;
}

if (isset($serviceAttributes->synthetic) && (string) $serviceAttributes->synthetic === 'true') {
return true;
}

foreach ($serviceDefinition->tag ?? [] as $tagDefinition) {
/** @var SimpleXMLElement $tagAttributes */
$tagAttributes = $tagDefinition->attributes();
$tagName = $tagAttributes->name !== null ? (string) $tagAttributes->name : null;

if ($tagName === 'container.error' || $tagName === 'container.excluded') {
return true;
}
}

return false;
}

/**
* @return array<string, string>
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/Provider/SymfonyUsageProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function testExplicitContainerXmlPaths(): void
self::assertArrayHasKey('Symfony\DicClass1', $dicCalls);
self::assertArrayHasKey('__construct', $dicCalls['Symfony\DicClass1']);
self::assertArrayHasKey('calledViaDic', $dicCalls['Symfony\DicClass1']);

self::assertArrayNotHasKey('Symfony\DicErroredService', $dicCalls);
self::assertArrayNotHasKey('Symfony\DicExcludedService', $dicCalls);
self::assertArrayNotHasKey('Symfony\DicSyntheticService', $dicCalls);
self::assertArrayNotHasKey('Symfony\DicAbstractService', $dicCalls);
}

public function testExplicitContainerXmlPathsTakesPrecedenceOverContainer(): void
Expand Down
16 changes: 16 additions & 0 deletions tests/Rule/data/providers/symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ class DicClass4 {
public function __construct() {}
}

class DicErroredService {
public function __construct(string $name) {} // error: Unused Symfony\DicErroredService::__construct
}

class DicExcludedService {
public function __construct() {} // error: Unused Symfony\DicExcludedService::__construct
}

class DicSyntheticService {
public function __construct() {} // error: Unused Symfony\DicSyntheticService::__construct
}

class DicAbstractService {
public function __construct() {} // error: Unused Symfony\DicAbstractService::__construct
}

class Sftp {
const RETRY_LIMIT = 3; // used in yaml via !php/const
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Rule/data/providers/symfony/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,13 @@
<service id="Symfony\TaggedServiceB" class="Symfony\TaggedServiceB" public="true" autowire="true" autoconfigure="true">
<tag name="app.tagged_handler"/>
</service>
<service id="Symfony\DicErroredService" class="Symfony\DicErroredService" autowire="true" autoconfigure="true">
<tag name="container.error" message="Cannot autowire service &quot;Symfony\DicErroredService&quot;: argument &quot;$name&quot; of method &quot;__construct()&quot; is type-hinted &quot;string&quot;, you should configure its value explicitly."/>
</service>
<service id="Symfony\DicExcludedService" class="Symfony\DicExcludedService" abstract="true">
<tag name="container.excluded" source="because it's a Doctrine entity"/>
</service>
<service id="Symfony\DicSyntheticService" class="Symfony\DicSyntheticService" synthetic="true" public="true"/>
<service id="Symfony\DicAbstractService" class="Symfony\DicAbstractService" abstract="true"/>
</services>
</container>
Loading