Skip to content

Instantly share code, notes, and snippets.

@dr0bz
Last active July 3, 2024 12:37
Show Gist options
  • Save dr0bz/0fd18255f5a5c3b6b17ab3e8152be225 to your computer and use it in GitHub Desktop.
Save dr0bz/0fd18255f5a5c3b6b17ab3e8152be225 to your computer and use it in GitHub Desktop.
Find unused symfony services
<?php
$filename = './srcApp_KernelDevDebugContainer.xml';
$content = file_get_contents($filename);
// the \> at the end of a service definition means this service has no dependencies.
preg_match_all('/\<service id="(App.+?)".* autowire="true" .*?\/>/', $content, $matches);
$serviceIds = $matches[1];
foreach ($serviceIds as $serviceId) {
$serviceId = str_replace('\\', '\\\\', $serviceId);
if (!preg_match(sprintf('/\<argument (key=".*?")? type="service" id="%s"\/>/', $serviceId), $content)) {
echo sprintf("Unused service: %s\n", $serviceId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment