Resolver   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 10.14 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 2
dl 7
loc 69
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C getAll() 7 50 10

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
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