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
107:31 queued 72:30
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()));
0 ignored issues
show
Unused Code introduced by
$includedFilesFromPath is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100 126
101 123
        \Symfony\Component\VarDumper\VarDumper::dump(array_diff($declaredClassesBeforeLoadingPhpFiles, get_declared_classes()));die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method getDataLoadersFromDirectory() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
102
103 102
        return $this->loadDataLoaders(
0 ignored issues
show
Unused Code introduced by
return $this->loadDataLo...includedFilesFromPath); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
104 102
            array_diff($declaredClassesBeforeLoadingPhpFiles, get_declared_classes()),
105 123
            $includedFilesFromPath
106
        );
107 123
    }
108
109
    /**
110 123
     * Includes all the PHP files present in a folder and returns the list of the included files.
111 123
     *
112 123
     * @param string $path
113
     * @param array  $includedFiles Real files path as keys
114 123
     *
115 102
     * @return array Loaded real files path as keys
116 102
     */
117 102
    private function includePhpFiles($path, array $includedFiles)
118
    {
119 102
        $includedFilesFromPath = [];
120 102
        $finder = SymfonyFinder::create()->depth(0)->in($path)->files()->name('*.php');
121 102
        foreach ($finder as $file) {
122 120
            /* @var SplFileInfo $file */
123 104
            $includedFilesFromPath[$fileRealPath = $file->getRealPath()] = true;
124 123
125
            if (false === array_key_exists($fileRealPath, $includedFiles)) {
126 123
                require_once $fileRealPath;
127
            }
128
        }
129
130
        return $includedFilesFromPath;
131
    }
132
133
    /**
134
     * Looks for loaders among the classes given.
135
     *
136
     * @param array $classNames
137
     * @param array $includedFilesFromPath Real files path as keys
138
     *
139
     * @return LoaderInterface[]
140
     */
141
    private function loadDataLoaders(array $classNames, array $includedFilesFromPath)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
142
    {
143
        $loaders = [];
144
        $loaderInterface = LoaderInterface::class;
145
146
        foreach ($classNames as $className) {
147
            $reflectionClass = new \ReflectionClass($className);
148
            $sourceFile = $reflectionClass->getFileName();
149
150
            if (false === array_key_exists($sourceFile, $includedFilesFromPath)) {
151
                // The class does not come from the loaded directories
152
                continue;
153
            }
154
155
            if ($reflectionClass->implementsInterface($loaderInterface) && false === $reflectionClass->isAbstract()) {
156
                $loader = new $className();
157
                $loaders[$className] = $loader;
158
159
                if ($loader instanceof ContainerAwareInterface) {
160
                    $loader->setContainer($this->container);
161
                }
162
            }
163
        }
164
165
        return $loaders;
166
    }
167
}
168