[chore] organize stubs matching namespaces (#210)

fixes https://github.com/psalm/psalm-plugin-symfony/issues/209
This commit is contained in:
Farhad Safarov 2021-08-29 13:57:28 +03:00 committed by GitHub
parent 9b2232bec7
commit 97f0652612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
47 changed files with 12 additions and 33 deletions

View File

@ -2,7 +2,6 @@
namespace Psalm\SymfonyPsalmPlugin;
use function array_merge;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Psalm\Exception\ConfigException;
use Psalm\Plugin\PluginEntryPointInterface;
@ -29,30 +28,6 @@ use Symfony\Component\HttpKernel\Kernel;
*/
class Plugin implements PluginEntryPointInterface
{
/**
* @return string[]
*/
protected function getCommonStubs(): array
{
// GLOB_BRACE is undefined on Alpine Linux https://bugs.php.net/bug.php?id=72095
return array_merge(
glob(__DIR__.'/Stubs/common/*/*.stubphp'),
glob(__DIR__.'/Stubs/common/*.stubphp')
) ?: [];
}
/**
* @param int $majorVersion symfony major version
*
* @return string[]
*/
protected function getStubsForMajorVersion(int $majorVersion): array
{
$version = (string) $majorVersion;
return glob(__DIR__.'/Stubs/'.$version.'/*.stubphp') ?: [];
}
/**
* {@inheritdoc}
*/
@ -95,14 +70,8 @@ class Plugin implements PluginEntryPointInterface
$api->registerHooksFromClass(ContainerHandler::class);
$stubs = array_merge(
$this->getCommonStubs(),
$this->getStubsForMajorVersion(Kernel::MAJOR_VERSION)
);
foreach ($stubs as $stubFilePath) {
$api->addStubFile($stubFilePath);
}
$this->addStubs($api, __DIR__.'/Stubs/common');
$this->addStubs($api, __DIR__.'/Stubs/'.Kernel::MAJOR_VERSION);
if (isset($config->twigCachePath)) {
$twig_cache_path = getcwd().DIRECTORY_SEPARATOR.ltrim((string) $config->twigCachePath, DIRECTORY_SEPARATOR);
@ -131,4 +100,14 @@ class Plugin implements PluginEntryPointInterface
TemplateFileAnalyzer::setTemplateRootPath($twig_root_path);
}
}
private function addStubs(RegistrationInterface $api, string $path): void
{
$a = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($a as $file) {
if (!$file->isDir()) {
$api->addStubFile($file->getPathname());
}
}
}
}