Completed
Push — master ( b2cc89...0bff0f )
by Tobias
42:03 queued 06:38
created

HappyrAnnotationWarmerExtension::load()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.5066
c 0
b 0
f 0
cc 7
nc 10
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace  Happyr\AnnotationWarmer\DependencyInjection;
6
7
use Composer\Autoload\ClassLoader;
8
use Happyr\AnnotationWarmer\AnnotationLintCommand;
9
use Symfony\Component\Config\FileLocator;
10
use Symfony\Component\Debug\DebugClassLoader;
11
use Symfony\Component\DependencyInjection\ContainerBuilder;
12
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
13
use Symfony\Component\Finder\Finder;
14
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
15
16
/**
17
 * @author Tobias Nyholm <[email protected]>
18
 */
19
class HappyrAnnotationWarmerExtension extends Extension
20
{
21
    public function load(array $configs, ContainerBuilder $container): void
22
    {
23
        $configuration = new Configuration();
24
        $config = $this->processConfiguration($configuration, $configs);
25
26
        $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
27
        $loader->load('services.yml');
28
29
        if (empty($config['paths'])) {
30
            $config['paths'] = ['%kernel.project_dir%/src'];
31
        }
32
33
        $paths = $container->getParameterBag()->resolveValue($config['paths']);
34
        $classes = [];
35
36
        foreach ($this->getAutoloadPrefixes() as $prefix => $prefixPaths) {
37
            foreach ($prefixPaths as $prefixPath) {
38
                $real = realpath($prefixPath);
39
                foreach ($paths as $path) {
40
                    if ($real === $path || false !== strstr($path, $real)) {
41
                        $classes = array_merge($classes, $this->getClassesInPath($path, $real, $prefix));
42
                    }
43
                }
44
            }
45
        }
46
47
        $this->addAnnotatedClassesToCompile($classes);
48
        $container->getDefinition(AnnotationLintCommand::class)
49
            ->replaceArgument(1, $classes);
50
    }
51
52
    private function getClassesInPath($path, $basePath, $basePrefix)
53
    {
54
        $classes = [];
55
        $basePathLength = strlen($basePath);
56
        $finder = (new Finder())
57
            ->name('*.php')
58
            ->in($path);
59
60
        foreach ($finder as $file) {
61
            $filePath = substr($file->getRealpath(), $basePathLength+1, -4);
62
            $fqns = $basePrefix.str_replace('/', '\\', $filePath);
63
64
            $classes[] = $fqns;
65
        }
66
        return $classes;
67
    }
68
69
    private function getAutoloadPrefixes()
70
    {
71
        $prefixes = [];
72
73
        foreach (spl_autoload_functions() as $function) {
74
            if (!\is_array($function)) {
75
                continue;
76
            }
77
78
            if ($function[0] instanceof DebugClassLoader) {
79
                $function = $function[0]->getClassLoader();
80
            }
81
82
            if (\is_array($function) && $function[0] instanceof ClassLoader) {
0 ignored issues
show
Bug introduced by
The class Composer\Autoload\ClassLoader does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
83
                $prefixes += array_filter($function[0]->getPrefixesPsr4());
84
            }
85
        }
86
87
        return $prefixes;
88
    }
89
}
90