FilesystemRecursive::getDefinitions()   C
last analyzed

Complexity

Conditions 14
Paths 38

Size

Total Lines 56
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 14
eloc 32
c 1
b 0
f 0
nc 38
nop 2
dl 0
loc 56
ccs 0
cts 31
cp 0
crap 210
rs 6.2666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Loader;
4
5
use Kaliop\eZMigrationBundle\API\Exception\MigrationBundleException;
6
use Kaliop\eZMigrationBundle\API\Value\MigrationDefinition;
7
8
/**
9
 * Similar to parent, except that, given a folder, we scan all of its subfolders
10
 *
11
 * @todo allow to specify specific subfolders not to scan, eg: where the media files are stored
12
 */
13
class FilesystemRecursive extends Filesystem
14
{
15
    protected function getDefinitions(array $paths = array(), $returnFilename = false)
16
    {
17
        // if no paths defined, we look in all bundles
18
        if (empty($paths)) {
19
            /** @var $bundle \Symfony\Component\HttpKernel\Bundle\BundleInterface */
20
            foreach ($this->kernel->getBundles() as $bundle)
21
            {
22
                $path = $bundle->getPath() . "/" . $this->versionDirectory;
23
                if (is_dir($path)) {
24
                    $paths[] = $path;
25
                }
26
            }
27
        }
28
29
        $definitions = array();
30
        foreach ($paths as $path) {
31
            if (is_file($path)) {
32
                $definitions[basename($path)] = $returnFilename ? $path : new MigrationDefinition(
33
                    basename($path),
34
                    $path,
35
                    file_get_contents($path)
36
                );
37
            } elseif (is_dir($path)) {
38
39
                // seems like sorting of recursive dirs scan is though...
40
41
                $dirs = array();
42
                $DI = new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::KEY_AS_PATHNAME | \FilesystemIterator::CURRENT_AS_FILEINFO);
43
                foreach (new \RecursiveIteratorIterator($DI) as $file) {
44
                    if ($file->isFile()) {
45
                        $dirs[dirname($file->getRealpath())] = true;
46
                    }
47
                }
48
                $dirs = array_keys($dirs);
49
                asort($dirs);
50
51
                foreach ($dirs as $dir) {
52
                    foreach (glob("$dir/*") as $filename) {
53
                        if (is_file($filename)) {
54
                            $definitions[basename($filename)] =
55
                                $returnFilename ? realpath($filename) : new MigrationDefinition(
56
                                    basename($filename),
57
                                    realpath($filename),
58
                                    file_get_contents($filename)
59
                                );
60
                        }
61
                    }
62
                }
63
            }
64
            else {
65
                throw new MigrationBundleException("Path '$path' is neither a file nor directory");
66
            }
67
        }
68
        ksort($definitions);
69
70
        return $definitions;
71
    }
72
}
73