GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — 1.x (#257)
by Théo
100:30 queued 65:32
created

FixturesFinder::loadDataLoaders()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 0
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 14
nc 5
nop 2
crap 42
1
<?php
2
3
/*
4
 * This file is part of the Hautelook\AliceBundle package.
5
 *
6
 * (c) Baldur Rensch <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Hautelook\AliceBundle\Doctrine\Finder;
13
14
use Hautelook\AliceBundle\Doctrine\DataFixtures\LoaderInterface;
15
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17
use Symfony\Component\Finder\Finder as SymfonyFinder;
18
use Symfony\Component\Finder\SplFileInfo;
19
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
20
21
/**
22
 * Extends its parent class to take into account doctrine data loaders.
23
 *
24
 * @author Théo FIDRY <[email protected]>
25
 */
26
class FixturesFinder extends \Hautelook\AliceBundle\Finder\FixturesFinder implements ContainerAwareInterface
27
{
28
    /**
29
     * @var ContainerInterface|null
30
     */
31
    private $container;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 168
    public function setContainer(ContainerInterface $container = null)
37
    {
38 168
        $this->container = $container;
39 168
    }
40
41
    /**
42
     * {@inheritdoc}
43
     *
44
     * Extended to look for data loaders. If a data loader is found, will take the fixtures from it instead of taking
45
     * all the fixtures files.
46
     */
47 117
    public function getFixturesFromDirectory($path)
48
    {
49 117
        $fixtures = [];
50
51 117
        $loaders = $this->getDataLoadersFromDirectory($path);
52 114
        foreach ($loaders as $loader) {
53 96
            $fixtures = array_merge($fixtures, $loader->getFixtures());
54 114
        }
55
56
        // If no data loader is found, takes all fixtures files
57 114
        if (0 === count($loaders)) {
58 75
            return parent::getFixturesFromDirectory($path);
59
        }
60
61 96
        return $fixtures;
62
    }
63
64
    /**
65
     * Gets all data loaders instances.
66
     *
67
     * For first get all the path for where to look for data loaders.
68
     *
69
     * @param BundleInterface[] $bundles
70
     * @param string            $environment
71
     *
72
     * @return LoaderInterface[] Fixtures files real paths.
73
     */
74 87
    public function getDataLoaders(array $bundles, $environment)
75
    {
76 87
        $loadersPaths = $this->getLoadersPaths($bundles, $environment);
77
78
        // Add all fixtures to the new Doctrine loader
79 87
        $loaders = [];
80 87
        foreach ($loadersPaths as $path) {
81 87
            if (is_dir($path)) {
82 87
                $loaders = array_merge($loaders, $this->getDataLoadersFromDirectory($path));
83
            }
84 87
        }
85
86
        return $loaders;
87
    }
88
89
    /**
90
     * Get data loaders inside the given directory.
91
     *
92
     * @param string $path Directory path
93
     *
94 126
     * @return LoaderInterface[]
95
     */
96 126
    private function getDataLoadersFromDirectory($path)
97
    {
98
        $declaredClassesBeforeLoadingPhpFiles = get_declared_classes();
99 126
        $includedFilesFromPath = $this->includePhpFiles($path, array_flip(get_included_files()));
100 126
101 123
        return $this->loadDataLoaders(
102
            array_diff($declaredClassesBeforeLoadingPhpFiles, get_declared_classes()),
103 102
            $includedFilesFromPath
104 102
        );
105 123
    }
106
107 123
    /**
108
     * Includes all the PHP files present in a folder and returns the list of the included files.
109
     *
110 123
     * @param string $path
111 123
     * @param array  $includedFiles Real files path as keys
112 123
     *
113
     * @return array Loaded real files path as keys
114 123
     */
115 102
    private function includePhpFiles($path, array $includedFiles)
116 102
    {
117 102
        $includedFilesFromPath = [];
118
        $finder = SymfonyFinder::create()->depth(0)->in($path)->files()->name('*.php');
119 102
        foreach ($finder as $file) {
120 102
            /* @var SplFileInfo $file */
121 102
            $includedFilesFromPath[$fileRealPath = $file->getRealPath()] = true;
122 120
123 104
            if (false === array_key_exists($fileRealPath, $includedFiles)) {
124 123
                require_once $fileRealPath;
125
            }
126 123
        }
127
128
        return $includedFilesFromPath;
129
    }
130
131
    /**
132
     * Looks for loaders among the classes given.
133
     *
134
     * @param array $classNames
135
     * @param array $includedFilesFromPath Real files path as keys
136
     *
137
     * @return LoaderInterface[]
138
     */
139
    private function loadDataLoaders(array $classNames, array $includedFilesFromPath)
140
    {
141
        $loaders = [];
142
        $loaderInterface = LoaderInterface::class;
143
144
        foreach ($classNames as $className) {
145
            $reflectionClass = new \ReflectionClass($className);
146
            $sourceFile = $reflectionClass->getFileName();
147
148
            if (false === array_key_exists($sourceFile, $includedFilesFromPath)) {
149
                // The class does not come from the loaded directories
150
                continue;
151
            }
152
153
            if ($reflectionClass->implementsInterface($loaderInterface) && false === $reflectionClass->isAbstract()) {
154
                $loader = new $className();
155
                $loaders[$className] = $loader;
156
157
                if ($loader instanceof ContainerAwareInterface) {
158
                    $loader->setContainer($this->container);
159
                }
160
            }
161
        }
162
163
        return $loaders;
164
    }
165
}
166