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

FilesystemRecursive   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 61
Duplicated Lines 18.03 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 11
loc 61
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C getDefinitions() 11 58 14

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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