Completed
Push — master ( c6a0ea...3ee08d )
by Christopher
03:07
created

ResourceAggregator   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 238
Duplicated Lines 36.97 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 48
c 1
b 0
f 0
lcom 1
cbo 1
dl 88
loc 238
ccs 142
cts 142
cp 1
rs 8.4864

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
A installConfig() 0 18 4
B installDi() 19 19 5
A installLocale() 15 15 4
B installMigrations() 0 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
        foreach ($this->packages as $packageFile => $package) {
89 1
            $path = $package['path'];
90 1
            array_map($installer = function ($source) {
91 1
                $configPath = $this->testRootDir . '/app/config';
92 1
                $configFile = basename($source);
93 1
                $subDir = basename(dirname($source)) . '/';
94 1
                $subDir == 'config/' && $subDir = '';
95 1
                $destination = $configPath . '/' . $subDir . $configFile;
96 1
                is_file($destination) && unlink($destination);
97 1
                symlinkRelative($source, $destination);
98 1
            }, glob($path . '/phwoolcon-package/config/*.php'));
99 1
            array_map($installer, glob($path . '/phwoolcon-package/config/override-*/*.php'));
100
        }
101 1
        $this->message(' <info>[ OK ]</info>');
102 1
    }
103
104 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...
105
    {
106 1
        $this->message('Installing DI...', static::FLAG_SPACE_PAD_CONTINUE);
107 1
        $diFiles = [];
108 1
        foreach ($this->packages as $packageFile => $package) {
109 1
            $path = $package['path'];
110 1
            foreach ($package as $name => $resources) {
111 1
                if (empty($resources['di'])) {
112 1
                    continue;
113
                }
114 1
                foreach ((array)$resources['di'] as $sort => $file) {
115 1
                    $diFiles[$sort][] = $path . '/phwoolcon-package/' . $file;
116
                }
117
            }
118
        }
119 1
        $target = $this->testRootDir . '/vendor/phwoolcon/di.php';
120 1
        fileSaveInclude($target, arraySortedMerge($diFiles));
121 1
        $this->message(' <info>[ OK ]</info>');
122 1
    }
123
124 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...
125
    {
126 1
        $this->message('Installing locales...', static::FLAG_SPACE_PAD_CONTINUE);
127 1
        $localePath = $this->testRootDir . '/app/locale/';
128 1
        foreach ($this->packages as $packageFile => $package) {
129 1
            $path = $package['path'];
130 1
            if ($items = glob($path . '/phwoolcon-package/locale/*')) {
131 1
                foreach ($items as $source) {
132 1
                    $destination = $localePath . basename($source);
133 1
                    symlinkDirOverride($source, $destination);
134
                }
135
            }
136
        }
137 1
        $this->message(' <info>[ OK ]</info>');
138 1
    }
139
140 1
    protected function installMigrations()
141
    {
142 1
        $this->message('Installing migrations...', static::FLAG_SPACE_PAD_CONTINUE);
143 1
        $migrationPath = $this->testRootDir . '/bin/migrations/';
144 1
        foreach ($this->packages as $packageFile => $package) {
145 1
            $path = $package['path'];
146 1
            if ($items = glob($path . '/phwoolcon-package/migrations/*')) {
147
                // @codeCoverageIgnoreStart
148
                foreach ($items as $source) {
149
                    $destination = $migrationPath . basename($source);
150
                    is_file($destination) && unlink($destination);
151
                    symlinkRelative($source, $destination);
152
                }
153
                // @codeCoverageIgnoreEnd
154
            }
155
        }
156 1
        $this->message(' <info>[ OK ]</info>');
157 1
    }
158
159 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...
160
    {
161 1
        $this->message('Installing routes...', static::FLAG_SPACE_PAD_CONTINUE);
162 1
        $routeFiles = [];
163 1
        foreach ($this->packages as $packageFile => $package) {
164 1
            $path = $package['path'];
165 1
            foreach ($package as $name => $resources) {
166 1
                if (empty($resources['routes'])) {
167 1
                    continue;
168
                }
169
                // @codeCoverageIgnoreStart
170
                foreach ((array)$resources['routes'] as $sort => $file) {
171
                    $routeFiles[$sort][] = $path . '/phwoolcon-package/' . $file;
172
                }
173
                // @codeCoverageIgnoreEnd
174
            }
175
        }
176 1
        $target = $this->testRootDir . '/vendor/phwoolcon/routes.php';
177 1
        fileSaveInclude($target, arraySortedMerge($routeFiles));
178 1
        $this->message(' <info>[ OK ]</info>');
179 1
    }
180
181 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...
182
    {
183 1
        $this->message('Installing views...', static::FLAG_SPACE_PAD_CONTINUE);
184 1
        $viewPath = $this->testRootDir . '/app/views/';
185 1
        foreach ($this->packages as $packageFile => $package) {
186 1
            $path = $package['path'];
187 1
            if ($items = glob($path . '/phwoolcon-package/views/*')) {
188 1
                foreach ($items as $source) {
189 1
                    $destination = $viewPath . basename($source);
190 1
                    symlinkDirOverride($source, $destination);
191
                }
192
            }
193
        }
194 1
        $this->message(' <info>[ OK ]</info>');
195 1
    }
196
197 1
    protected function message($message, $flag = null)
198
    {
199 1
        $eol = !($flag & static::FLAG_CONTINUE);
200 1
        $spacePad = $flag & static::FLAG_SPACE_PAD;
201 1
        if (!$this->cliOutput) {
202 1
            $this->cliOutput = new ConsoleOutput();
203
        }
204 1
        $message = is_array($message) ? implode(PHP_EOL, $message) : $message;
205 1
        $message .= $spacePad ? $this->spacePad($message) : '';
206 1
        $this->cliOutput->write($message, $eol);
207 1
    }
208
209 1
    protected function resetTestRoot()
210
    {
211 1
        $this->message('Resetting test root...', static::FLAG_SPACE_PAD_CONTINUE);
212 1
        removeDir($this->testRootDir);
213
        $assembleDirs = [
214 1
            '/app/config',
215
            '/app/locale',
216
            '/app/views',
217
            '/bin/migrations',
218
            '/storage/cache',
219
            '/storage/logs',
220
            '/storage/session',
221
            '/storage/remote-coverage',
222
            '/vendor/phwoolcon',
223
        ];
224 1
        foreach ($assembleDirs as $dir) {
225 1
            mkdir($this->testRootDir . $dir, 0777, true);
226
        }
227
        $mockCompiledFiles = [
228 1
            '/vendor/phwoolcon/assets.php',
229
            '/vendor/phwoolcon/admin_assets.php',
230
            '/vendor/phwoolcon/commands.php',
231
        ];
232 1
        foreach ($mockCompiledFiles as $mockFile) {
233 1
            $filename = $this->testRootDir . $mockFile;
234 1
            fileSaveArray($filename, []);
235
        }
236 1
        $this->message(' <info>[ OK ]</info>');
237 1
    }
238
239 1
    protected function spacePad($str, $length = 40)
240
    {
241 1
        $spaces = $length - strlen($str);
242 1
        return $spaces > 0 ? str_repeat(' ', $spaces) : ' ';
243
    }
244
}
245