CollectAction   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 74
Duplicated Lines 16.22 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 5
dl 12
loc 74
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 6 1
F getInstalledRequirements() 12 64 13

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
/*
4
 * This file is part of Laravel Paket.
5
 *
6
 * (c) Anton Komarev <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cog\Laravel\Paket\Http\Controllers\Api\Requirements;
15
16
use Illuminate\Contracts\Support\Responsable as ResponsableContract;
17
use MCStreetguy\ComposerParser\Factory as ComposerParser;
18
19
final class CollectAction
20
{
21
    public function __invoke(): ResponsableContract
22
    {
23
        $requirements = $this->getInstalledRequirements();
24
25
        return new CollectResponse($requirements);
26
    }
27
28
    private function getInstalledRequirements(): array
29
    {
30
        $lockFile = ComposerParser::parseLockfile(base_path('composer.lock'));
31
        $jsonFile = ComposerParser::parseComposerJson(base_path('composer.json'));
32
33
        $packages = $lockFile->getPackages()->getData();
34
        $devPackages = $lockFile->getPackagesDev()->getData();
35
        $platform = $lockFile->getPlatform()->getData();
36
        $devPlatform = $lockFile->getPlatformDev()->getData();
37
38
        foreach ($packages as &$package) {
39
            $package['isDevelopment'] = false;
40
        }
41
        foreach ($devPackages as &$package) {
42
            $package['isDevelopment'] = true;
43
        }
44
45
        $requires = $jsonFile->getRequire()->getData();
46
        $devRequires = $jsonFile->getRequireDev()->getData();
47
48
        $roots = [
49
            'essential' => [],
50
            'dev' => [],
51
        ];
52
        foreach ($platform as $name => $version) {
53
            $roots['essential'][] = [
54
                'name' => $name,
55
                'version' => $version,
56
            ];
57
        }
58
        foreach ($devPlatform as $name => $version) {
59
            $roots['dev'][] = [
60
                'name' => $name,
61
                'version' => $version,
62
            ];
63
        }
64 View Code Duplication
        foreach ($packages as $key => $package) {
0 ignored issues
show
Duplication introduced by antonkomarev
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...
65
            if (isset($requires[$package['name']]) || isset($devRequires[$package['name']])) {
66
                $roots['essential'][] = $package;
67
                unset($packages[$key]);
68
            }
69
        }
70 View Code Duplication
        foreach ($devPackages as $key => $package) {
0 ignored issues
show
Duplication introduced by antonkomarev
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...
71
            if (isset($requires[$package['name']]) || isset($devRequires[$package['name']])) {
72
                $roots['dev'][] = $package;
73
                unset($devPackages[$key]);
74
            }
75
        }
76
        $dependencies = [
77
            'essential' => [],
78
            'dev' => [],
79
        ];
80
        foreach ($packages as $key => $package) {
81
            $dependencies['essential'][] = $package;
82
        }
83
        foreach ($devPackages as $key => $package) {
84
            $dependencies['dev'][] = $package;
85
        }
86
87
        return [
88
            'roots' => $roots,
89
            'dependencies' => $dependencies,
90
        ];
91
    }
92
}
93