AbstractPackage::directlyRequires()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Composed;
3
4
/**
5
 * @author Josh Di Fabio <[email protected]>
6
 */
7
abstract class AbstractPackage
8
{
9
    private $dirPath;
10
    private $config;
11
    private $root;
12
    private $lockFile;
13
    private $directDependencies;
14
    private $dependencies;
15
16
    protected function __construct(RootPackage $root, string $dirPath, JsonObject $config)
17
    {
18
        $this->root = $root;
19
        $this->dirPath = $dirPath;
20
        $this->config = $config;
21
    }
22
23
    /**
24
     * @return null|string
25
     */
26
    public function getName(bool $includeVendorName = true)
27
    {
28
        $name = $this->config->get(['name']);
29
30
        if ($includeVendorName) {
31
            return $name;
32
        }
33
34
        return ltrim(strstr($name, '/'), '/');
35
    }
36
37
    /**
38
     * @return null|string
39
     */
40
    public function getVendorName()
41
    {
42
        if (null === $name = $this->getName()) {
43
            return null;
44
        }
45
46
        return strstr($name, '/', true);
47
    }
48
49
    public function isRoot() : bool
50
    {
51
        return $this->root === $this;
52
    }
53
54
    /**
55
     * @return mixed
56
     */
57
    public function getConfig($keys = [], $default = null)
58
    {
59
        return $this->config->get($keys, $default);
60
    }
61
62
    public function getPath(string $relativePath = '') : string
63
    {
64
        return $this->dirPath . (strlen($relativePath) ? DIRECTORY_SEPARATOR . $relativePath : '');
65
    }
66
67
    /**
68
     * @return null|LockFile
69
     */
70 View Code Duplication
    public function getLockFile()
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...
71
    {
72
        if (null === $this->lockFile) {
73
            $filePath = $this->getPath('composer.lock');
74
            if (file_exists($filePath)) {
75
                $this->lockFile = LockFile::fromFilePath($this->root, $filePath);
76
            }
77
        }
78
79
        return $this->lockFile;
80
    }
81
82
    public function directlyRequires(string $packageName) : bool
83
    {
84
        return null !== $this->getDirectDependencies()->getPackage($packageName);
85
    }
86
87
    public function requires(string $packageName) : bool
88
    {
89
        return null !== $this->getDependencies()->getPackage($packageName);
90
    }
91
92
    public function getDirectDependencies() : PackageCollection
93
    {
94
        if (null === $this->directDependencies) {
95
            $packageNames = array_keys($this->getConfig('require', $default = []));
0 ignored issues
show
Documentation introduced by
'require' is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
96
            $packages = @array_map(
97
                function ($packageName) {
98
                    return $this->root->getPackages()->getPackage($packageName);
99
                },
100
                $packageNames
101
            );
102
            $packages = array_combine($packageNames, $packages);
103
            $packages = array_filter($packages);
104
            $this->directDependencies = new PackageCollection($packages);
105
        }
106
107
        return $this->directDependencies;
108
    }
109
110
    public function getDependencies() : PackageCollection
111
    {
112
        if (null === $this->dependencies) {
113
            $packages = [];
114
            /** @var Package $package */
115
            foreach ($this->getDirectDependencies() as $name => $package) {
116
                $packages[$name] = $package;
117
                $packages = array_merge($packages, $package->getDependencies()->toArray());
118
            }
119
            $this->dependencies = new PackageCollection($packages);
120
        }
121
122
        return $this->dependencies;
123
    }
124
}
125