DependencyAnalyzer::analyzeComposerData()   C
last analyzed

Complexity

Conditions 16
Paths 122

Size

Total Lines 69
Code Lines 33

Duplication

Lines 20
Ratio 28.99 %

Importance

Changes 0
Metric Value
dl 20
loc 69
rs 5.2103
c 0
b 0
f 0
cc 16
eloc 33
nc 122
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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