Package::setRequiredVersion()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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
14
    protected $repository;
15
16
    protected $requiredVersion;
17
18
    protected $version;
19
20
    protected $requires = [];
21
22
    protected $info = [];
23
24
    /**
25
     * All descendants' constructors should call this parent constructor
26
     *
27
     * @param string $name            The package's name
28
     * @param string $requiredVersion E.g. 1.*
29
     * @param string $version         E.g. 1.2.3
30
     * @param array  $requires        The package's dependencies
31
     * @param array  $info            Package info (e.g. info from bower.json)
32
     */
33
    public function __construct($name, $requiredVersion = null, $version = null, $requires = [], $info = [])
34
    {
35
        $this->name = $name;
36
        $this->requiredVersion = 'master' === $requiredVersion ? '*' : $requiredVersion;
37
        $this->version = $version;
38
        if (!empty($requires)) {
39
            $this->requires = $requires;
40
        }
41
        if (!empty($info)) {
42
            $this->info = $info;
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getName()
50
    {
51
        return $this->name;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getVersion()
58
    {
59
        return $this->version;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function setVersion($version)
66
    {
67
        return $this->version = $version;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getRequiredVersion()
74
    {
75
        return $this->requiredVersion;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function setRequiredVersion($version)
82
    {
83
        return $this->requiredVersion = $version;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function setRepository(RepositoryInterface $repository)
90
    {
91
        if ($this->repository && $repository !== $this->repository) {
92
            throw new \LogicException('A package can only be added to one repository');
93
        }
94
        $this->repository = $repository;
95
    }
96
97
    /**
98
     * Returns package unique name, constructed from name, version and release type.
99
     *
100
     * @return string
101
     */
102
    public function getUniqueName()
103
    {
104
        return $this->getName() . '-' . $this->getVersion();
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function setRequires(array $requires = null)
111
    {
112
        $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...
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function getRequires()
119
    {
120
        // see if there is some inside $this->info (e.g. from bower.json)
121
        if (empty($this->requires) && isset($this->info['dependencies'])) {
122
            $this->requires = $this->info['dependencies'];
123
        }
124
125
        return $this->requires;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function setInfo(array $info)
132
    {
133
        $this->info = $info;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function getInfo()
140
    {
141
        return $this->info;
142
    }
143
144
    /**
145
     * Converts the package into a readable and unique string
146
     *
147
     * @return string
148
     */
149
    public function __toString()
150
    {
151
        return $this->getUniqueName();
152
    }
153
}
154