Completed
Push — master ( e36450...afb05a )
by Josh
02:57
created

RootPackage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 16.92 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 11
loc 65
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createFromPath() 0 4 1
A getPackages() 0 15 2
A getLockFile() 0 8 2
A getInstalledPackagesFile() 11 11 3
A getVendorDir() 0 4 1
A getVendorPath() 0 4 2

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
namespace Composed;
3
4
/**
5
 * @author Josh Di Fabio <[email protected]>
6
 */
7
class RootPackage extends AbstractPackage
8
{
9
    private $packages;
10
    private $installedPackagesFile;
11
12
    public function __construct(string $dirPath, JsonObject $config)
13
    {
14
        parent::__construct($this, $dirPath, $config);
15
    }
16
17
    public static function createFromPath(string $dirPath) : self
18
    {
19
        return new static($dirPath, JsonObject::fromFilePath($dirPath . DIRECTORY_SEPARATOR . 'composer.json'));
20
    }
21
22
    /**
23
     * Returns all packages in the project, including the root one
24
     */
25
    public function getPackages() : PackageCollection
26
    {
27
        if (null === $this->packages) {
28
            $packages = array_merge(
29
                array(
30
                    $this->getName() => $this,
31
                ),
32
                $this->getInstalledPackagesFile()->getPackages()->toArray()
33
            );
34
35
            $this->packages = new PackageCollection($packages);
36
        }
37
38
        return $this->packages;
39
    }
40
41
    public function getLockFile() : LockFile
42
    {
43
        if (null === $lockFile = parent::getLockFile()) {
44
            throw new \RuntimeException('Lock file not found.');
45
        }
46
47
        return $lockFile;
48
    }
49
50 View Code Duplication
    public function getInstalledPackagesFile() : InstalledPackagesFile
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...
51
    {
52
        if (null === $this->installedPackagesFile) {
53
            $filePath = $this->getPath('vendor/composer/installed.json');
54
            if (file_exists($filePath)) {
55
                $this->installedPackagesFile = InstalledPackagesFile::fromFilePath($this, $filePath);
56
            }
57
        }
58
59
        return $this->installedPackagesFile;
60
    }
61
62
    public function getVendorDir() : string
63
    {
64
        return 'vendor';
65
    }
66
67
    public function getVendorPath(string $relativePath = '') : string
68
    {
69
        return $this->getPath($this->getVendorDir() . (strlen($relativePath) ? DIRECTORY_SEPARATOR . $relativePath : ''));
70
    }
71
}
72