VersionedPackage   C
last analyzed

Complexity

Total Complexity 66

Size/Duplication

Total Lines 554
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 66
lcom 2
cbo 2
dl 0
loc 554
rs 5.7474
c 0
b 0
f 0

55 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getMetaData() 0 4 2
A addMetaData() 0 8 3
A replaceMetaData() 0 6 1
A getVersions() 0 4 1
A setVersions() 0 6 1
A addVersion() 0 6 1
A addVersions() 0 8 2
B removeVersion() 0 23 5
A getLatestVersion() 0 17 4
A getName() 0 4 1
A getPrettyName() 0 4 1
A getNames() 0 4 1
A setId() 0 4 1
A getId() 0 4 1
A isDev() 0 4 1
A getType() 0 4 1
A getTargetDir() 0 4 1
A getExtra() 0 4 1
A setInstallationSource() 0 4 1
A getInstallationSource() 0 4 1
A getSourceType() 0 4 1
A getSourceUrl() 0 4 1
A getSourceUrls() 0 4 1
A getSourceReference() 0 4 1
A getSourceMirrors() 0 4 1
A getDistType() 0 4 1
A getDistUrl() 0 4 1
A getDistUrls() 0 4 1
A getDistReference() 0 4 1
A getDistSha1Checksum() 0 4 1
A getDistMirrors() 0 4 1
A getVersion() 0 4 1
A getPrettyVersion() 0 4 1
A getReleaseDate() 0 4 1
A getStability() 0 4 1
A getRequires() 0 4 1
A getConflicts() 0 4 1
A getProvides() 0 4 1
A getReplaces() 0 4 1
A getDevRequires() 0 4 1
A getSuggests() 0 4 1
A getAutoload() 0 4 1
A getDevAutoload() 0 4 1
A getIncludePaths() 0 4 1
A setRepository() 0 4 1
A getRepository() 0 4 1
A getBinaries() 0 4 1
A getUniqueName() 0 4 1
A getNotificationUrl() 0 4 1
A getPrettyString() 0 4 1
A getFullPrettyVersion() 0 4 1
A getArchiveExcludes() 0 4 1
A getTransportOptions() 0 4 1
A __toString() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like VersionedPackage 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 VersionedPackage, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * This file is part of tenside/core.
5
 *
6
 * (c) Christian Schiffler <[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
 * This project is provided in good faith and hope to be usable by anyone.
12
 *
13
 * @package    tenside/core
14
 * @author     Christian Schiffler <[email protected]>
15
 * @author     Nico Schneider <[email protected]>
16
 * @copyright  2015 Christian Schiffler <[email protected]>
17
 * @license    https://github.com/tenside/core/blob/master/LICENSE MIT
18
 * @link       https://github.com/tenside/core
19
 * @filesource
20
 */
21
22
namespace Tenside\Core\Composer\Package;
23
24
use Composer\Package\PackageInterface;
25
use Composer\Package\Version\VersionParser;
26
use Composer\Repository\RepositoryInterface;
27
28
/**
29
 * Class VersionedPackage
30
 *
31
 * @SuppressWarnings(PHPMD.ExcessivePublicCount)
32
 */
33
class VersionedPackage implements PackageInterface
34
{
35
    /**
36
     * The package to hold versions for.
37
     *
38
     * @var PackageInterface
39
     */
40
    protected $package;
41
    
42
    /**
43
     * The version list.
44
     *
45
     * @var PackageInterface[]
46
     */
47
    protected $versions;
48
49
    /**
50
     * The meta data.
51
     *
52
     * @var array
53
     */
54
    protected $meta = [];
55
56
    /**
57
     * Create a new instance.
58
     *
59
     * @param PackageInterface $package  The package to hold versions for.
60
     *
61
     * @param array            $versions The initial versions.
62
     */
63
    public function __construct(PackageInterface $package, array $versions = [])
64
    {
65
        $this->package  = $package;
66
        $this->versions = $versions;
67
    }
68
69
    /**
70
     * Retrieve the meta data.
71
     *
72
     * @param string $key The key of the meta data to retrieve.
73
     *
74
     * @return mixed|null
75
     */
76
    public function getMetaData($key)
77
    {
78
        return isset($this->meta[$key]) ? $this->meta[$key] : null;
79
    }
80
81
    /**
82
     * Add meta data.
83
     *
84
     * @param string $key       The key to use.
85
     *
86
     * @param mixed  $value     The value to use.
87
     *
88
     * @param bool   $overwrite Flag if the previous value shall be overridden.
89
     *
90
     * @return $this
91
     */
92
    public function addMetaData($key, $value, $overwrite = true)
93
    {
94
        $this->meta[$key] = isset($this->meta[$key])
95
            ? $overwrite ? $value : $this->meta[$key]
96
            : $value;
97
98
        return $this;
99
    }
100
101
    /**
102
     * Replace the meta data of the package.
103
     *
104
     * @param array $meta The new meta data.
105
     *
106
     * @return $this
107
     */
108
    public function replaceMetaData(array $meta)
109
    {
110
        $this->meta = $meta;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Retrieve the versions.
117
     *
118
     * @return PackageInterface[]
119
     */
120
    public function getVersions()
121
    {
122
        return $this->versions;
123
    }
124
125
    /**
126
     * Set the versions and override all previously registered versions.
127
     *
128
     * @param PackageInterface[] $versions The versions to use.
129
     *
130
     * @return $this
131
     */
132
    public function setVersions(array $versions)
133
    {
134
        $this->versions = $versions;
135
136
        return $this;
137
    }
138
139
    /**
140
     * Add a version to the package.
141
     *
142
     * @param PackageInterface $version The version to add.
143
     *
144
     * @return $this
145
     */
146
    public function addVersion(PackageInterface $version)
147
    {
148
        $this->versions[] = $version;
149
150
        return $this;
151
    }
152
153
    /**
154
     * Add multiple versions.
155
     *
156
     * @param PackageInterface[] $versions The versions to add.
157
     *
158
     * @return $this
159
     */
160
    public function addVersions(array $versions)
161
    {
162
        foreach ($versions as $version) {
163
            $this->addVersion($version);
164
        }
165
166
        return $this;
167
    }
168
169
    /**
170
     * Remove a version from the package.
171
     *
172
     * @param PackageInterface|string $version The version to remove.
173
     *
174
     * @return $this
175
     *
176
     * @throws \InvalidArgumentException For any invalid version.
177
     */
178
    public function removeVersion($version)
179
    {
180
        $versionParser = new VersionParser();
181
182
        if ($version instanceof PackageInterface) {
183
            $normalizedVersion = $version->getVersion();
184
        } elseif (is_string($version)) {
185
            $normalizedVersion = $versionParser->normalize($version);
186
        } else {
187
            throw new \InvalidArgumentException(
188
                'You have to pass either an instance of PackageInterface or a version string to remove a version ' .
189
                'from this package object!'
190
            );
191
        }
192
193
        foreach ($this->getVersions() as $key => $attachedVersion) {
194
            if ($attachedVersion->getVersion() == $normalizedVersion) {
195
                unset($this->versions[$key]);
196
            }
197
        }
198
199
        return $this;
200
    }
201
202
    /**
203
     * Retrieve the latest version of the package (if any available).
204
     *
205
     * @return PackageInterface
206
     */
207
    public function getLatestVersion()
208
    {
209
        if (!count($this->versions)) {
210
            return $this->package;
211
        }
212
213
        $latestVersion = $this->package;
214
        reset($this->versions);
215
216
        foreach ($this->versions as $version) {
217
            if ($version->getReleaseDate() > $latestVersion->getReleaseDate()) {
218
                $latestVersion = $version;
219
            }
220
        }
221
222
        return $latestVersion;
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228
    public function getName()
229
    {
230
        return $this->package->getName();
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236
    public function getPrettyName()
237
    {
238
        return $this->package->getPrettyName();
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244
    public function getNames()
245
    {
246
        return $this->package->getNames();
247
    }
248
249
    /**
250
     * {@inheritdoc}
251
     *
252
     * @SuppressWarnings(PHPMD.ShortVariableName)
253
     */
254
    public function setId($id)
255
    {
256
        $this->package->setId($id);
257
    }
258
259
    /**
260
     * {@inheritdoc}
261
     */
262
    public function getId()
263
    {
264
        return $this->package->getId();
265
    }
266
267
    /**
268
     * {@inheritdoc}
269
     */
270
    public function isDev()
271
    {
272
        return $this->package->isDev();
273
    }
274
275
    /**
276
     * {@inheritdoc}
277
     */
278
    public function getType()
279
    {
280
        return $this->package->getType();
281
    }
282
283
    /**
284
     * {@inheritdoc}
285
     */
286
    public function getTargetDir()
287
    {
288
        return $this->package->getTargetDir();
289
    }
290
291
    /**
292
     * {@inheritdoc}
293
     */
294
    public function getExtra()
295
    {
296
        return $this->package->getExtra();
297
    }
298
299
    /**
300
     * {@inheritdoc}
301
     */
302
    public function setInstallationSource($type)
303
    {
304
        $this->package->setInstallationSource($type);
305
    }
306
307
    /**
308
     * {@inheritdoc}
309
     */
310
    public function getInstallationSource()
311
    {
312
        return $this->package->getInstallationSource();
313
    }
314
315
    /**
316
     * {@inheritdoc}
317
     */
318
    public function getSourceType()
319
    {
320
        return $this->package->getSourceType();
321
    }
322
323
    /**
324
     * {@inheritdoc}
325
     */
326
    public function getSourceUrl()
327
    {
328
        return $this->package->getSourceUrl();
329
    }
330
331
    /**
332
     * {@inheritdoc}
333
     */
334
    public function getSourceUrls()
335
    {
336
        return $this->package->getSourceUrls();
337
    }
338
339
    /**
340
     * {@inheritdoc}
341
     */
342
    public function getSourceReference()
343
    {
344
        return $this->package->getSourceReference();
345
    }
346
347
    /**
348
     * {@inheritdoc}
349
     */
350
    public function getSourceMirrors()
351
    {
352
        return $this->package->getSourceMirrors();
353
    }
354
355
    /**
356
     * {@inheritdoc}
357
     */
358
    public function getDistType()
359
    {
360
        return $this->package->getDistType();
361
    }
362
363
    /**
364
     * {@inheritdoc}
365
     */
366
    public function getDistUrl()
367
    {
368
        return $this->package->getDistUrl();
369
    }
370
371
    /**
372
     * {@inheritdoc}
373
     */
374
    public function getDistUrls()
375
    {
376
        return $this->package->getDistUrls();
377
    }
378
379
    /**
380
     * {@inheritdoc}
381
     */
382
    public function getDistReference()
383
    {
384
        return $this->package->getDistReference();
385
    }
386
387
    /**
388
     * {@inheritdoc}
389
     */
390
    public function getDistSha1Checksum()
391
    {
392
        return $this->package->getDistSha1Checksum();
393
    }
394
395
    /**
396
     * {@inheritdoc}
397
     */
398
    public function getDistMirrors()
399
    {
400
        return $this->package->getDistMirrors();
401
    }
402
403
    /**
404
     * {@inheritdoc}
405
     */
406
    public function getVersion()
407
    {
408
        return $this->package->getVersion();
409
    }
410
411
    /**
412
     * {@inheritdoc}
413
     */
414
    public function getPrettyVersion()
415
    {
416
        return $this->package->getPrettyVersion();
417
    }
418
419
    /**
420
     * {@inheritdoc}
421
     */
422
    public function getReleaseDate()
423
    {
424
        return $this->package->getReleaseDate();
425
    }
426
427
    /**
428
     * {@inheritdoc}
429
     */
430
    public function getStability()
431
    {
432
        return $this->package->getStability();
433
    }
434
435
    /**
436
     * {@inheritdoc}
437
     */
438
    public function getRequires()
439
    {
440
        return $this->package->getRequires();
441
    }
442
443
    /**
444
     * {@inheritdoc}
445
     */
446
    public function getConflicts()
447
    {
448
        return $this->package->getConflicts();
449
    }
450
451
    /**
452
     * {@inheritdoc}
453
     */
454
    public function getProvides()
455
    {
456
        return $this->package->getProvides();
457
    }
458
459
    /**
460
     * {@inheritdoc}
461
     */
462
    public function getReplaces()
463
    {
464
        return $this->package->getReplaces();
465
    }
466
467
    /**
468
     * {@inheritdoc}
469
     */
470
    public function getDevRequires()
471
    {
472
        return $this->package->getDevRequires();
473
    }
474
475
    /**
476
     * {@inheritdoc}
477
     */
478
    public function getSuggests()
479
    {
480
        return $this->package->getSuggests();
481
    }
482
483
    /**
484
     * {@inheritdoc}
485
     */
486
    public function getAutoload()
487
    {
488
        return $this->package->getAutoload();
489
    }
490
491
    /**
492
     * {@inheritdoc}
493
     */
494
    public function getDevAutoload()
495
    {
496
        return $this->package->getDevAutoload();
497
    }
498
499
    /**
500
     * {@inheritdoc}
501
     */
502
    public function getIncludePaths()
503
    {
504
        return $this->package->getIncludePaths();
505
    }
506
507
    /**
508
     * {@inheritdoc}
509
     */
510
    public function setRepository(RepositoryInterface $repository)
511
    {
512
        $this->package->setRepository($repository);
513
    }
514
515
    /**
516
     * {@inheritdoc}
517
     */
518
    public function getRepository()
519
    {
520
        return $this->package->getRepository();
521
    }
522
523
    /**
524
     * {@inheritdoc}
525
     */
526
    public function getBinaries()
527
    {
528
        return $this->package->getBinaries();
529
    }
530
531
    /**
532
     * {@inheritdoc}
533
     */
534
    public function getUniqueName()
535
    {
536
        return $this->package->getUniqueName();
537
    }
538
539
    /**
540
     * {@inheritdoc}
541
     */
542
    public function getNotificationUrl()
543
    {
544
        return $this->package->getNotificationUrl();
545
    }
546
547
    /**
548
     * {@inheritdoc}
549
     */
550
    public function getPrettyString()
551
    {
552
        return $this->package->getPrettyString();
553
    }
554
555
    /**
556
     * {@inheritDoc}
557
     */
558
    public function getFullPrettyVersion($truncate = true)
559
    {
560
        return $this->package->getFullPrettyVersion($truncate);
561
    }
562
563
    /**
564
     * {@inheritdoc}
565
     */
566
    public function getArchiveExcludes()
567
    {
568
        return $this->package->getArchiveExcludes();
569
    }
570
571
    /**
572
     * {@inheritdoc}
573
     */
574
    public function getTransportOptions()
575
    {
576
        return $this->package->getTransportOptions();
577
    }
578
579
    /**
580
     * {@inheritdoc}
581
     */
582
    public function __toString()
583
    {
584
        return $this->package->__toString();
585
    }
586
}
587