Completed
Push — master ( 19e140...7b3764 )
by Christopher
03:11
created

ResourceAggregator   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 247
Duplicated Lines 42.51 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 52
c 1
b 0
f 0
lcom 1
cbo 1
dl 105
loc 247
ccs 146
cts 146
cp 1
rs 7.9487

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A aggregate() 0 14 1
A appendLocalResources() 0 8 1
A detectPackages() 0 17 3
B installAliases() 18 18 6
C installConfig() 8 27 8
B installDi() 19 19 5
A installLocale() 15 15 4
B installMigrations() 9 18 5
B installRoutes() 21 21 5
A installViews() 15 15 4
A message() 0 11 4
B resetTestRoot() 0 29 3
A spacePad() 0 5 2

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ResourceAggregator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ResourceAggregator, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Phwoolcon\TestStarter;
4
5
use Symfony\Component\Console\Output\ConsoleOutput;
6
7
class ResourceAggregator
8
{
9
    const FLAG_CONTINUE = 0b0001;
10
    const FLAG_SPACE_PAD = 0b0010;
11
    const FLAG_SPACE_PAD_CONTINUE = 0b0011;
12
13
    protected $cliOutput;
14
    protected $packages = [];
15
    protected $rootDir;
16
    protected $testRootDir;
17
18 1
    public function __construct($rootDir)
19
    {
20 1
        $this->rootDir = $rootDir;
21 1
        $this->testRootDir = $rootDir . '/tests/root';
22 1
    }
23
24 1
    public function aggregate()
25
    {
26 1
        $this->resetTestRoot();
27 1
        $this->detectPackages();
28
29 1
        $this->installAliases();
30 1
        $this->installConfig();
31 1
        $this->installDi();
32 1
        $this->installLocale();
33 1
        $this->installMigrations();
34 1
        $this->installRoutes();
35 1
        $this->installViews();
36 1
        $this->appendLocalResources();
37 1
    }
38
39 1
    protected function appendLocalResources()
40
    {
41 1
        $this->message('Appending local resources...', static::FLAG_SPACE_PAD_CONTINUE);
42 1
        $source = $this->rootDir . '/tests/resource';
43 1
        $destination = $this->testRootDir;
44 1
        symlinkDirOverride($source, $destination);
45 1
        $this->message(' <info>[ OK ]</info>');
46 1
    }
47
48 1
    protected function detectPackages()
49
    {
50 1
        $this->message('Detecting packages...', static::FLAG_SPACE_PAD_CONTINUE);
51 1
        $packageFiles = detectPhwoolconPackageFiles($this->rootDir . '/vendor');
52 1
        $packageFiles = array_merge($packageFiles, glob($this->rootDir . '/phwoolcon-package/*package*.php'));
53 1
        foreach ($packageFiles as $file) {
54
            // @codeCoverageIgnoreStart
55
            if (!is_array($package = include($file))) {
56
                continue;
57
            }
58
            // @codeCoverageIgnoreEnd
59 1
            $path = dirname(dirname($file));
60 1
            $package['path'] = $path;
61 1
            $this->packages[$file] = $package;
62
        }
63 1
        $this->message(' <info>[ OK ]</info>');
64 1
    }
65
66 1 View Code Duplication
    protected function installAliases()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
67
    {
68 1
        $this->message('Installing class aliases...', static::FLAG_SPACE_PAD_CONTINUE);
69 1
        $aliases = [];
70 1
        foreach ($this->packages as $packageFile => $package) {
71 1
            foreach ($package as $name => $resources) {
72 1
                if (empty($resources['class_aliases']) || !is_array($resources['class_aliases'])) {
73 1
                    continue;
74
                }
75 1
                foreach ($resources['class_aliases'] as $sort => $detectedAliases) {
76 1
                    $aliases[$sort . '-' . $name] = $detectedAliases;
77
                }
78
            }
79
        }
80 1
        $target = $this->testRootDir . '/vendor/phwoolcon/class_aliases.php';
81 1
        fileSaveArray($target, arraySortedMerge($aliases));
82 1
        $this->message(' <info>[ OK ]</info>');
83 1
    }
84
85 1
    protected function installConfig()
86
    {
87 1
        $this->message('Installing config...', static::FLAG_SPACE_PAD_CONTINUE);
88 1
        $configPath = $this->testRootDir . '/app/config';
89 1
        foreach ($this->packages as $packageFile => $package) {
90 1
            $path = $package['path'];
91 1 View Code Duplication
            if ($configFiles = glob($path . '/phwoolcon-package/config/*.php')) {
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...
92 1
                foreach ($configFiles as $source) {
93 1
                    $configFile = basename($source);
94 1
                    $destination = $configPath . '/' . $configFile;
95 1
                    is_file($destination) and unlink($destination);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
96 1
                    symlinkRelative($source, $destination);
97
                }
98
            }
99
100 1
            if ($overrides = glob($path . '/phwoolcon-package/config/override-*/*.php')) {
101 1
                foreach ($overrides as $override) {
102 1
                    $configFile = basename($override);
103 1
                    $dir = basename(dirname($override));
104 1
                    $destination = $configPath . '/' . $dir . '/' . $configFile;
105 1
                    is_file($destination) and unlink($destination);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
106 1
                    symlinkRelative($override, $destination);
107
                }
108
            }
109
        }
110 1
        $this->message(' <info>[ OK ]</info>');
111 1
    }
112
113 1 View Code Duplication
    protected function installDi()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
114
    {
115 1
        $this->message('Installing DI...', static::FLAG_SPACE_PAD_CONTINUE);
116 1
        $diFiles = [];
117 1
        foreach ($this->packages as $packageFile => $package) {
118 1
            $path = $package['path'];
119 1
            foreach ($package as $name => $resources) {
120 1
                if (empty($resources['di'])) {
121 1
                    continue;
122
                }
123 1
                foreach ((array)$resources['di'] as $sort => $file) {
124 1
                    $diFiles[$sort][] = $path . '/phwoolcon-package/' . $file;
125
                }
126
            }
127
        }
128 1
        $target = $this->testRootDir . '/vendor/phwoolcon/di.php';
129 1
        fileSaveInclude($target, arraySortedMerge($diFiles));
130 1
        $this->message(' <info>[ OK ]</info>');
131 1
    }
132
133 1 View Code Duplication
    protected function installLocale()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
134
    {
135 1
        $this->message('Installing locales...', static::FLAG_SPACE_PAD_CONTINUE);
136 1
        $localePath = $this->testRootDir . '/app/locale/';
137 1
        foreach ($this->packages as $packageFile => $package) {
138 1
            $path = $package['path'];
139 1
            if ($items = glob($path . '/phwoolcon-package/locale/*')) {
140 1
                foreach ($items as $source) {
141 1
                    $destination = $localePath . basename($source);
142 1
                    symlinkDirOverride($source, $destination);
143
                }
144
            }
145
        }
146 1
        $this->message(' <info>[ OK ]</info>');
147 1
    }
148
149 1
    protected function installMigrations()
150
    {
151 1
        $this->message('Installing migrations...', static::FLAG_SPACE_PAD_CONTINUE);
152 1
        $migrationPath = $this->testRootDir . '/bin/migrations/';
153 1
        foreach ($this->packages as $packageFile => $package) {
154 1
            $path = $package['path'];
155 1 View Code Duplication
            if ($items = glob($path . '/phwoolcon-package/migrations/*')) {
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...
156
                // @codeCoverageIgnoreStart
157
                foreach ($items as $source) {
158
                    $destination = $migrationPath . basename($source);
159
                    is_file($destination) and unlink($destination);
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
160
                    symlinkRelative($source, $destination);
161
                }
162
                // @codeCoverageIgnoreEnd
163
            }
164
        }
165 1
        $this->message(' <info>[ OK ]</info>');
166 1
    }
167
168 1 View Code Duplication
    protected function installRoutes()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
169
    {
170 1
        $this->message('Installing routes...', static::FLAG_SPACE_PAD_CONTINUE);
171 1
        $routeFiles = [];
172 1
        foreach ($this->packages as $packageFile => $package) {
173 1
            $path = $package['path'];
174 1
            foreach ($package as $name => $resources) {
175 1
                if (empty($resources['routes'])) {
176 1
                    continue;
177
                }
178
                // @codeCoverageIgnoreStart
179
                foreach ((array)$resources['routes'] as $sort => $file) {
180
                    $routeFiles[$sort][] = $path . '/phwoolcon-package/' . $file;
181
                }
182
                // @codeCoverageIgnoreEnd
183
            }
184
        }
185 1
        $target = $this->testRootDir . '/vendor/phwoolcon/routes.php';
186 1
        fileSaveInclude($target, arraySortedMerge($routeFiles));
187 1
        $this->message(' <info>[ OK ]</info>');
188 1
    }
189
190 1 View Code Duplication
    protected function installViews()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
191
    {
192 1
        $this->message('Installing views...', static::FLAG_SPACE_PAD_CONTINUE);
193 1
        $viewPath = $this->testRootDir . '/app/views/';
194 1
        foreach ($this->packages as $packageFile => $package) {
195 1
            $path = $package['path'];
196 1
            if ($items = glob($path . '/phwoolcon-package/views/*')) {
197 1
                foreach ($items as $source) {
198 1
                    $destination = $viewPath . basename($source);
199 1
                    symlinkDirOverride($source, $destination);
200
                }
201
            }
202
        }
203 1
        $this->message(' <info>[ OK ]</info>');
204 1
    }
205
206 1
    protected function message($message, $flag = null)
207
    {
208 1
        $eol = !($flag & static::FLAG_CONTINUE);
209 1
        $spacePad = $flag & static::FLAG_SPACE_PAD;
210 1
        if (!$this->cliOutput) {
211 1
            $this->cliOutput = new ConsoleOutput();
212
        }
213 1
        $message = is_array($message) ? implode(PHP_EOL, $message) : $message;
214 1
        $message .= $spacePad ? $this->spacePad($message) : '';
215 1
        $this->cliOutput->write($message, $eol);
216 1
    }
217
218 1
    protected function resetTestRoot()
219
    {
220 1
        $this->message('Resetting test root...', static::FLAG_SPACE_PAD_CONTINUE);
221 1
        removeDir($this->testRootDir);
222
        $assembleDirs = [
223 1
            '/app/config',
224
            '/app/locale',
225
            '/app/views',
226
            '/bin/migrations',
227
            '/storage/cache',
228
            '/storage/logs',
229
            '/storage/session',
230
            '/storage/remote-coverage',
231
            '/vendor/phwoolcon',
232
        ];
233 1
        foreach ($assembleDirs as $dir) {
234 1
            mkdir($this->testRootDir . $dir, 0777, true);
235
        }
236
        $mockCompiledFiles = [
237 1
            '/vendor/phwoolcon/assets.php',
238
            '/vendor/phwoolcon/admin_assets.php',
239
            '/vendor/phwoolcon/commands.php',
240
        ];
241 1
        foreach ($mockCompiledFiles as $mockFile) {
242 1
            $filename = $this->testRootDir . $mockFile;
243 1
            fileSaveArray($filename, []);
244
        }
245 1
        $this->message(' <info>[ OK ]</info>');
246 1
    }
247
248 1
    protected function spacePad($str, $length = 40)
249
    {
250 1
        $spaces = $length - strlen($str);
251 1
        return $spaces > 0 ? str_repeat(' ', $spaces) : ' ';
252
    }
253
}
254