DependencyAnalyzer   B
last analyzed

Complexity

Total Complexity 47

Size/Duplication

Total Lines 220
Duplicated Lines 9.09 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 47
lcom 1
cbo 3
dl 20
loc 220
rs 8.439
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
B analyze() 0 23 5
C analyzeComposerData() 20 69 16
A addPackages() 0 12 4
A parseJson() 0 9 2
A connect() 0 23 3
C processLockedData() 0 25 7
B hasDependencies() 0 12 5
B hasUserlandDependency() 0 20 5

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 DependencyAnalyzer 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 DependencyAnalyzer, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * Copyright 2012 Johannes M. Schmitt <[email protected]>
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace JMS\Composer;
20
21
use JMS\Composer\Exception\MissingLockFileException;
22
use JMS\Composer\Graph\PackageNode;
23
use JMS\Composer\Graph\DependencyGraph;
24
25
/**
26
 * Analyzes dependencies of a project, and returns them as a graph.
27
 *
28
 * @author Johannes M. Schmitt <[email protected]>
29
 */
30
class DependencyAnalyzer
31
{
32
    /**
33
     * @param string $dir
34
     *
35
     * @throws \RuntimeException
36
     * @throws \InvalidArgumentException
37
     * @return \JMS\Composer\Graph\DependencyGraph
38
     */
39
    public function analyze($dir)
40
    {
41
        if ( ! is_dir($dir)) {
42
            throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
43
        }
44
45
        if (stream_is_local($dir)) {
46
            $dir = realpath($dir);
47
        }
48
49
        if ( ! is_file($dir.'/composer.json')) {
50
            $graph = new DependencyGraph();
51
            $graph->getRootPackage()->setAttribute('dir', $dir);
52
53
            return $graph;
54
        }
55
56
        return $this->analyzeComposerData(
57
            file_get_contents($dir.'/composer.json'),
58
            is_file($dir.'/composer.lock') ? file_get_contents($dir.'/composer.lock') : null,
59
            $dir
60
        );
61
    }
62
63
    public function analyzeComposerData($composerJsonData, $composerLockData = null, $dir = null)
64
    {
65
        $rootPackageData = $this->parseJson($composerJsonData);
66
        if ( ! isset($rootPackageData['name'])) {
67
            $rootPackageData['name'] = '__root';
68
        }
69
70
        // If there is no composer.lock file, then either the project has no
71
        // dependencies, or the dependencies were not installed.
72
        if (empty($composerLockData)) {
73
            if ($this->hasDependencies($rootPackageData)) {
74
                throw new MissingLockFileException();
75
            }
76
77
            $graph = new DependencyGraph(new PackageNode($rootPackageData['name'], $rootPackageData));
78
            $graph->getRootPackage()->setAttribute('dir', $dir);
79
80
            // Connect built-in dependencies for example on the PHP version, or
81
            // on PHP extensions. For these, composer does not create a composer.lock.
82 View Code Duplication
            if (isset($rootPackageData['require'])) {
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...
83
                foreach ($rootPackageData['require'] as $name => $versionConstraint) {
84
                    $this->connect($graph, $rootPackageData['name'], $name, $versionConstraint);
85
                }
86
            }
87
88 View Code Duplication
            if (isset($rootPackageData['require-dev'])) {
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...
89
                foreach ($rootPackageData['require-dev'] as $name => $versionConstraint) {
90
                    $this->connect($graph, $rootPackageData['name'], $name, $versionConstraint);
91
                }
92
            }
93
94
            return $graph;
95
        }
96
97
        $graph = new DependencyGraph(new PackageNode($rootPackageData['name'], $rootPackageData));
98
        $graph->getRootPackage()->setAttribute('dir', $dir);
99
100
        $vendorDir = $dir.'/'.(isset($rootPackageData['config']['vendor-dir']) ? $rootPackageData['config']['vendor-dir'] : 'vendor');
101
        $lockData = $this->parseJson($composerLockData);
102
103
        // Add regular packages.
104
        if (isset($lockData['packages'])) {
105
            $this->addPackages($graph, $lockData['packages'], $vendorDir);
106
        }
107
108
        // Add development packages.
109
        if (isset($lockData['packages-dev'])) {
110
            $this->addPackages($graph, $lockData['packages-dev'], $vendorDir);
111
        }
112
113
        // Connect dependent packages.
114
        foreach ($graph->getPackages() as $packageNode) {
115
            $packageData = $packageNode->getData();
116
117 View Code Duplication
            if (isset($packageData['require'])) {
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...
118
                foreach ($packageData['require'] as $name => $version) {
119
                    $this->connect($graph, $packageData['name'], $name, $version);
120
                }
121
            }
122
123 View Code Duplication
            if (isset($packageData['require-dev'])) {
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...
124
                foreach ($packageData['require-dev'] as $name => $version) {
125
                    $this->connect($graph, $packageData['name'], $name, $version);
126
                }
127
            }
128
        }
129
130
        return $graph;
131
    }
132
133
    private function addPackages(DependencyGraph $graph, array $packages, $vendorDir)
134
    {
135
        foreach ($packages as $packageData) {
136
            if ($graph->isRootPackageName($packageData['name']) || $graph->hasPackage($packageData['name'])) {
137
                continue;
138
            }
139
140
            $package = $graph->createPackage($packageData['name'], $packageData);
141
            $package->setAttribute('dir', $vendorDir.'/'.$packageData['name']);
142
            $this->processLockedData($graph, $packageData);
143
        }
144
    }
145
146
    private function parseJson($data)
147
    {
148
        $parsedData = json_decode($data, true);
149
        if ($parsedData === false) {
150
            throw new \RuntimeException('Could not parse JSON data.');
151
        }
152
153
        return $parsedData;
154
    }
155
156
    private function connect(DependencyGraph $graph, $sourceName, $destName, $version)
157
    {
158
        // If the dest package is available, just connect it.
159
        if ($graph->hasPackage($destName)) {
160
            $graph->connect($sourceName, $destName, $version);
161
162
            return;
163
        }
164
165
        // If the dest package is not available, let's check to see if there is
166
        // some aggregate package that replaces our dest package, and connect to
167
        // this package.
168
        if (null !== $aggregatePackage = $graph->getAggregatePackageContaining($destName)) {
169
            $graph->connect($sourceName, $aggregatePackage->getName(), $version);
170
171
            return;
172
        }
173
174
        // If we reach this, we have stumbled upon a package that is only available
175
        // if the source package is installed with dev dependencies. We still add
176
        // the connection, but we will not have any data about the dest package.
177
        $graph->connect($sourceName, $destName, $version);
178
    }
179
180
    private function processLockedData(DependencyGraph $graph, array $lockedPackageData)
181
    {
182
        $packageName = null;
183
        if (isset($lockedPackageData['name'])) {
184
            $packageName = $lockedPackageData['name'];
185
        } else if (isset($lockedPackageData['package'])) {
186
            $packageName = $lockedPackageData['package'];
187
        }
188
189
        if (null === $packageName) {
190
            return;
191
        }
192
193
        $package = $graph->getPackage($packageName);
194
        if (null === $package) {
195
            return;
196
        }
197
198
        $package->setVersion($lockedPackageData['version']);
199
200
        if (isset($lockedPackageData['source']['reference'])
201
                && $lockedPackageData['version'] !== $lockedPackageData['source']['reference']) {
202
            $package->setSourceReference($lockedPackageData['source']['reference']);
203
        }
204
    }
205
206
    /**
207
     * @param array $config
208
     *
209
     * @return bool
210
     */
211
    private function hasDependencies(array $config)
212
    {
213
        if (isset($config['require']) && $this->hasUserlandDependency($config['require'])) {
214
            return true;
215
        }
216
217
        if (isset($config['require-dev']) && $this->hasUserlandDependency($config['require-dev'])) {
218
            return true;
219
        }
220
221
        return false;
222
    }
223
224
    /**
225
     * @param array $requires
226
     *
227
     * @return bool
228
     */
229
    private function hasUserlandDependency(array $requires)
230
    {
231
        if (empty($requires)) {
232
            return false;
233
        }
234
235
        foreach ($requires as $name => $versionConstraint) {
236
            if ('php' === strtolower($name)) {
237
                continue;
238
            }
239
240
            if (0 === stripos($name, 'ext-')) {
241
                continue;
242
            }
243
244
            return true;
245
        }
246
247
        return false;
248
    }
249
}
250