Completed
Pull Request — master (#148)
by
unknown
03:16
created

Package::getRequiredVersionValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Bowerphp\Package;
4
5
use Bowerphp\Repository\RepositoryInterface;
6
7
/**
8
 * Package
9
 */
10
class Package implements PackageInterface
11
{
12
    protected $name;
13
    protected $repository;
14
    protected $requiredVersionValue;
15
    protected $requiredVersion;
16
    protected $requiredVersionUrl;
17
    protected $version;
18
    protected $requires = [];
19
    protected $info = [];
20
21
    /**
22
     * All descendants' constructors should call this parent constructor
23
     *
24
     * @param string $name            The package's name
25
     * @param string $requiredVersion E.g. 1.*
26
     * @param string $version         E.g. 1.2.3
27
     * @param array  $requires        The package's dependencies
28
     * @param array  $info            Package info (e.g. info from bower.json)
29
     */
30
    public function __construct($name, $requiredVersion = null, $version = null, $requires = [], $info = [])
31
    {
32
        $this->name = $name;
33
        $this->requiredVersion = $requiredVersion === 'master' ? '*' : $requiredVersion;
34
        
35
        $this->version = $version;
36
        if (!empty($requires)) {
37
            $this->requires = $requires;
38
        }
39
        if (!empty($info)) {
40
            $this->info = $info;
41
        }
42
        
43
        if(false!==($p=strpos($requiredVersion,'#'))){
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
44
            $this->requiredVersionUrl = substr($requiredVersion,0,$p);
45
            $this->requiredVersion = substr($requiredVersion,$p+1);
46
        }
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function getName()
53
    {
54
        return $this->name;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getVersion()
61
    {
62
        return $this->version;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function setVersion($version)
69
    {
70
        return $this->version = $version;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function getRequiredVersionValue(){
77
        return $this->requiredVersionValue;
78
    }
79
    public function getRequiredVersionUrl(){
80
        return $this->requiredVersionUrl;
81
    }
82
    public function getRequiredVersion()
83
    {
84
        return $this->requiredVersion;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function setRequiredVersion($version)
91
    {
92
        return $this->requiredVersion = $version;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function setRepository(RepositoryInterface $repository)
99
    {
100
        if ($this->repository && $repository !== $this->repository) {
101
            throw new \LogicException('A package can only be added to one repository');
102
        }
103
        $this->repository = $repository;
104
    }
105
106
    /**
107
     * Returns package unique name, constructed from name, version and release type.
108
     *
109
     * @return string
110
     */
111
    public function getUniqueName()
112
    {
113
        return $this->getName() . '-' . $this->getVersion();
114
    }
115
116
    /**
117
     * Set the required packages
118
     *
119
     * @param array $requires A set of package links
120
     */
121
    public function setRequires(array $requires = null)
122
    {
123
        $this->requires = $requires;
0 ignored issues
show
Documentation Bug introduced by
It seems like $requires can be null. However, the property $requires is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129
    public function getRequires()
130
    {
131
        // see if there is some inside $this->info (e.g. from bower.json)
132
        if (empty($this->requires) && isset($this->info['dependencies'])) {
133
            $this->requires = $this->info['dependencies'];
134
        }
135
136
        return $this->requires;
137
    }
138
139
    /**
140
     * Set the info
141
     *
142
     * @param array $info
143
     */
144
    public function setInfo(array $info)
145
    {
146
        $this->info = $info;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function getInfo()
153
    {
154
        return $this->info;
155
    }
156
157
    /**
158
     * Converts the package into a readable and unique string
159
     *
160
     * @return string
161
     */
162
    public function __toString()
163
    {
164
        return $this->getUniqueName();
165
    }
166
}
167