-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring to reuse getServiceClassNames in more places
- Loading branch information
1 parent
8c64537
commit 4e2778b
Showing
2 changed files
with
80 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\DependencyInjection; | ||
|
||
use Nette\DI\Definitions\Statement; | ||
use function array_merge; | ||
use function array_unique; | ||
use function array_values; | ||
use function is_array; | ||
use function is_string; | ||
|
||
class ProjectConfigHelper | ||
{ | ||
|
||
/** | ||
* @param array<mixed> $projectConfig | ||
* @return list<string> | ||
*/ | ||
public static function getServiceClassNames(array $projectConfig): array | ||
{ | ||
$services = array_merge( | ||
$projectConfig['services'] ?? [], | ||
$projectConfig['rules'] ?? [], | ||
); | ||
$classes = []; | ||
foreach ($services as $service) { | ||
$classes = array_merge($classes, self::getClassesFromConfigDefinition($service)); | ||
if (!is_array($service)) { | ||
continue; | ||
} | ||
|
||
foreach (['class', 'factory', 'implement'] as $key) { | ||
if (!isset($service[$key])) { | ||
continue; | ||
} | ||
|
||
$classes = array_merge($classes, self::getClassesFromConfigDefinition($service[$key])); | ||
} | ||
} | ||
|
||
return array_values(array_unique($classes)); | ||
} | ||
|
||
/** | ||
* @param mixed $definition | ||
* @return string[] | ||
*/ | ||
private static function getClassesFromConfigDefinition($definition): array | ||
{ | ||
if (is_string($definition)) { | ||
return [$definition]; | ||
} | ||
|
||
if ($definition instanceof Statement) { | ||
$entity = $definition->entity; | ||
if (is_string($entity)) { | ||
return [$entity]; | ||
} elseif (is_array($entity) && isset($entity[0]) && is_string($entity[0])) { | ||
return [$entity[0]]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |