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
197:30 queued 162:26
created

FixturesFinder   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 5
dl 0
loc 139
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 4 1
A getFixturesFromDirectory() 0 16 3
A getDataLoaders() 0 14 3
A getDataLoadersFromDirectory() 0 9 1
A includePhpFiles() 0 15 3
B loadDataLoaders() 0 26 6
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
        $includedFilesFromPath = $this->includePhpFiles($path, array_flip(get_included_files()));
99 126
100 126
        return $this->loadDataLoaders(
101 123
            get_declared_classes(),
102
            $includedFilesFromPath
103 102
        );
104 102
    }
105 123
106
    /**
107 123
     * Includes all the PHP files present in a folder and returns the list of the included files.
108
     *
109
     * @param string $path
110 123
     * @param array  $includedFiles Real files path as keys
111 123
     *
112 123
     * @return array Loaded real files path as keys
113
     */
114 123
    private function includePhpFiles($path, array $includedFiles)
115 102
    {
116 102
        $includedFilesFromPath = [];
117 102
        $finder = SymfonyFinder::create()->depth(0)->in($path)->files()->name('*.php');
118
        foreach ($finder as $file) {
119 102
            /* @var SplFileInfo $file */
120 102
            $includedFilesFromPath[$fileRealPath = $file->getRealPath()] = true;
121 102
122 120
            if (false === array_key_exists($fileRealPath, $includedFiles)) {
123 104
                require_once $fileRealPath;
124 123
            }
125
        }
126 123
127
        return array_fill_keys(array_keys($includedFilesFromPath), true);
128
    }
129
130
    /**
131
     * Looks for loaders among the classes given.
132
     *
133
     * @param array $classNames
134
     * @param array $includedFilesFromPath Real files path as keys
135
     *
136
     * @return LoaderInterface[]
137
     */
138
    private function loadDataLoaders(array $classNames, array $includedFilesFromPath)
139
    {
140
        $loaders = [];
141
        $loaderInterface = LoaderInterface::class;
142
143
        foreach ($classNames as $className) {
144
            $reflectionClass = new \ReflectionClass($className);
145
            $sourceFile = $reflectionClass->getFileName();
146
147
            if (false === isset($includedFilesFromPath[$sourceFile])) {
148
                // The class does not come from the loaded directories
149
                continue;
150
            }
151
152
            if ($reflectionClass->implementsInterface($loaderInterface) && false === $reflectionClass->isAbstract()) {
153
                $loader = new $className();
154
                $loaders[$className] = $loader;
155
156
                if ($loader instanceof ContainerAwareInterface) {
157
                    $loader->setContainer($this->container);
158
                }
159
            }
160
        }
161
162
        return $loaders;
163
    }
164
}
165