Skip to content

Commit

Permalink
Detect even factory calls
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed Dec 23, 2024
1 parent e642b86 commit 384aa1e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
29 changes: 19 additions & 10 deletions src/Provider/SymfonyUsageProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class SymfonyUsageProvider implements MemberUsageProvider
private bool $enabled;

/**
* class => methods[]
* class => [method => true]
*
* @var array<string, array<string, true>>
*/
private array $dicClasses = [];
private array $dicCalls = [];

public function __construct(
?PHPStanSymfonyConfiguration $symfonyConfiguration,
Expand Down Expand Up @@ -166,7 +166,7 @@ private function getUsagesFromReflection(InClassNode $node): array
$usages = [];

foreach ($nativeReflection->getMethods() as $method) {
if (isset($this->dicClasses[$className][$method->getName()])) {
if (isset($this->dicCalls[$className][$method->getName()])) {
$usages[] = $this->createUsage($classReflection->getNativeMethod($method->getName()));
}

Expand Down Expand Up @@ -220,13 +220,9 @@ protected function fillDicClasses(string $containerXmlPath): void
continue;
}

$this->dicClasses[$class]['__construct'] = true;
$this->dicCalls[$class]['__construct'] = true;

if (!isset($serviceDefinition->call)) {
continue;
}

foreach ($serviceDefinition->call as $callDefinition) {
foreach ($serviceDefinition->call ?? [] as $callDefinition) {
/** @var SimpleXMLElement $callAttributes */
$callAttributes = $callDefinition->attributes();
$method = $callAttributes->method !== null ? (string) $callAttributes->method : null;
Expand All @@ -235,7 +231,20 @@ protected function fillDicClasses(string $containerXmlPath): void
continue;
}

$this->dicClasses[$class][$method] = true;
$this->dicCalls[$class][$method] = true;
}

foreach ($serviceDefinition->factory ?? [] as $factoryDefinition) {
/** @var SimpleXMLElement $factoryAttributes */
$factoryAttributes = $factoryDefinition->attributes();
$class = $factoryAttributes->class !== null ? (string) $factoryAttributes->class : null;
$method = $factoryAttributes->method !== null ? (string) $factoryAttributes->method : null;

if ($class === null || $method === null) {
continue;
}

$this->dicCalls[$class][$method] = true;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Rule/data/providers/symfony.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public function __construct() {}

class DicClass1 extends DicClassParent {
public function calledViaDic(): void {}
public function calledViaDicFactory(): void {}
}

class DicClass2 {
Expand Down
1 change: 1 addition & 0 deletions tests/Rule/data/providers/symfony/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<service id="Symfony\DicClass1" class="Symfony\DicClass1" public="true" autowire="true" autoconfigure="true">
<call method="calledViaDic">
</call>
<factory class="Symfony\DicClass1" method="calledViaDicFactory"/>
</service>
<service id="Symfony\DicClass2" class="Symfony\DicClass2" public="true" autowire="true" autoconfigure="true">
</service>
Expand Down

0 comments on commit 384aa1e

Please sign in to comment.