Completed
Push — master ( a339f9...4f56c0 )
by Gaetano
09:21
created

FilesystemRecursive::getDefinitions()   C

Complexity

Conditions 14
Paths 38

Size

Total Lines 58
Code Lines 34

Duplication

Lines 11
Ratio 18.97 %

Importance

Changes 0
Metric Value
dl 11
loc 58
rs 6.4881
c 0
b 0
f 0
cc 14
eloc 34
nc 38
nop 2

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\Value\MigrationDefinition;
6
7
/**
8
 * Similar to parent, except that, given a folder, we scan all of its subfolders
9
 *
10
 * @todo allow to specify specific subfolders not to scan, eg: where the media files are stored
11
 */
12
class FilesystemRecursive extends Filesystem
13
{
14
    protected function getDefinitions(array $paths = array(), $returnFilename = false)
15
    {
16
        // if no paths defined, we look in all bundles
17 View Code Duplication
        if (empty($paths)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
            $paths = array();
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 \Exception("Path '$path' is neither a file nor directory");
66
            }
67
        }
68
        ksort($definitions);
69
70
        return $definitions;
71
    }
72
}
73