Resolver::getAll()   C
last analyzed

Complexity

Conditions 10
Paths 9

Size

Total Lines 50
Code Lines 27

Duplication

Lines 7
Ratio 14 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 50
rs 5.7647
cc 10
eloc 27
nc 9
nop 1

How to fix   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
namespace T4web\Migrations\Version;
3
4
use ArrayIterator;
5
use GlobIterator;
6
use FilesystemIterator;
7
use ReflectionClass;
8
use ReflectionProperty;
9
use T4web\Migrations\Config;
10
use T4web\Migrations\Migration\MigrationInterface;
11
12
class Resolver
13
{
14
    /**
15
     * @var Config
16
     */
17
    private $config;
18
19
    /**
20
     * @var Table
21
     */
22
    private $versionTable;
23
24
    public function __construct(Config $config, $versionTable)
25
    {
26
        $this->config = $config;
27
        $this->versionTable = $versionTable;
28
    }
29
30
    public function getAll($all = false)
31
    {
32
        $classes = new ArrayIterator();
33
34
        $iterator = new GlobIterator(
35
            sprintf('%s/Version_*.php', $this->config->getDir()),
36
            FilesystemIterator::KEY_AS_FILENAME
37
        );
38
        foreach ($iterator as $item) {
39
            /** @var $item \SplFileInfo */
40
            if (preg_match('/(Version_(\d+))\.php/', $item->getFilename(), $matches)) {
41
                $applied = $this->versionTable->applied($matches[2]);
42
                if ($all || !$applied) {
43
                    $className = $this->config->getNamespace() . '\\' . $matches[1];
44
45
                    if (!class_exists($className)) { /** @noinspection PhpIncludeInspection */
46
                        require_once $this->config->getDir() . '/' . $item->getFilename();
47
                    }
48
49
                    if (class_exists($className)) {
50
                        $reflectionClass = new ReflectionClass($className);
51
                        $reflectionDescription = new ReflectionProperty($className, 'description');
52
53
                        if ($reflectionClass->implementsInterface(MigrationInterface::class)) {
54
                            $classes->append(
55
                                [
56
                                    'version' => $matches[2],
57
                                    'class' => $className,
58
                                    'description' => $reflectionDescription->getValue(),
59
                                    'applied' => $applied,
60
                                ]
61
                            );
62
                        }
63
                    }
64
                }
65
            }
66
        }
67
68
        $classes->uasort(
69 View Code Duplication
            function ($a, $b) {
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...
70
                if ($a['version'] == $b['version']) {
71
                    return 0;
72
                }
73
74
                return ($a['version'] < $b['version']) ? -1 : 1;
75
            }
76
        );
77
78
        return $classes;
79
    }
80
}
81