Completed
Push — master ( 16daf9...8a3ba8 )
by Mike
03:45
created

Version::isEqualTo()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 8
rs 9.4285
1
<?php
2
3
namespace Sugarcrm\UpgradeSpec\Version;
4
5
final class Version implements \Serializable
6
{
7
    /**
8
     * @var string
9
     */
10
    private $version;
11
12
    /**
13
     * Version constructor.
14
     *
15
     * @param $version
16
     */
17
    public function __construct($version)
18
    {
19
        if (!is_string($version) || !preg_match('/\d+(\.\d+){1,3}/', $version)) {
20
            throw new \InvalidArgumentException(sprintf('"%s" is not a valid SugarCRM version', $version));
21
        }
22
23
        $this->version = $version;
24
    }
25
26
    /**
27
     * @return bool
28
     */
29
    public function isMajor()
30
    {
31
        return count(explode('.', $this->version)) == 2;
32
    }
33
34
    /**
35
     * @return bool
36
     */
37
    public function isFull()
38
    {
39
        return count(explode('.', $this->version)) == 4;
40
    }
41
42
    /**
43
     * @return bool
44
     */
45
    public function isMinor()
46
    {
47
        return !$this->isMajor();
48
    }
49
50
    /**
51
     * @param Version $version
52
     *
53
     * @param bool $strict
54
     *
55
     * @return bool
56
     */
57
    public function isEqualTo(Version $version, $strict = false)
58
    {
59
        if ($strict) {
60
            return $this->version == (string) $version;
61
        }
62
63
        return (string) $this->getFull() == (string) $version->getFull();
64
    }
65
66
    /**
67
     * Checks if the current version is a subversion of given parent
68
     *
69
     * @param Version $parent
70
     *
71
     * @return bool
72
     */
73
    public function isChildOf(Version $parent)
74
    {
75
        $parent = (string) $parent;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $parent. This often makes code more readable.
Loading history...
76
77
        // exactly the same version is not a child
78
        if ((string) $this === $parent) {
79
            return false;
80
        }
81
82
        return implode('.', array_slice(explode('.', $this->version), 0, count(explode('.', $parent)))) === $parent;
83
    }
84
85
    /**
86
     * @return Version
87
     */
88
    public function getMajor()
89
    {
90
        if ($this->isMajor()) {
91
            return clone $this;
92
        }
93
94
        return new self(implode('.', array_slice(explode('.', $this->version), 0, 2)));
95
    }
96
97
    /**
98
     * @return Version
99
     */
100
    public function getFull()
101
    {
102
        $parts = explode('.', $this->version);
103
        if (count($parts) == 4) {
104
            return $this->version;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->version; (string) is incompatible with the return type documented by Sugarcrm\UpgradeSpec\Version\Version::getFull of type Sugarcrm\UpgradeSpec\Version\Version.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
105
        }
106
107
        $parts = array_merge($parts, array_fill(0, 4 - count($parts), '0'));
108
109
        return new self(implode('.', $parts));
110
    }
111
112
    /**
113
     * Gets all version aliases
114
     * 7.6.0 -> [7.6, 7.6.0, 7.6.0.0], 7.6.1 -> [7.6.1, 7.6.1.0]
115
     *
116
     * @return OrderedList
117
     */
118
    public function getAliases()
119
    {
120
        if ($this->isMajor()) {
121
            return new OrderedList([$this, $this->version . '.0', $this->version . '.0.0']);
122
        }
123
124
        $parts = explode('.', $this->version);
125
        if ($parts[count($parts) - 1] === '0') {
126
            return $this->getParent()->getAliases();
127
        }
128
129
        $aliases = [$this];
130
        while (count($parts) < 4) {
131
            $parts[] = '0';
132
            $aliases[] = implode('.', $parts);
133
        }
134
135
        return new OrderedList($aliases);
136
    }
137
138
    /**
139
     *
140
     *
141
     * @return Version
142
     */
143
    public function getParent()
144
    {
145
        if ($this->isMajor()) {
146
            throw new \LogicException('Major version doesn\'t have parent');
147
        }
148
149
        $parts = explode('.', $this->version);
150
        array_pop($parts);
151
152
        return new self(implode('.', $parts));
153
    }
154
155
    /**
156
     * String representation of object.
157
     *
158
     * @return string the string representation of the object or null
159
     */
160
    public function serialize()
161
    {
162
        return serialize($this->version);
163
    }
164
165
    /**
166
     * Constructs the object.
167
     *
168
     * @param string $serialized
169
     */
170
    public function unserialize($serialized)
171
    {
172
        $this->version = unserialize($serialized);
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    public function __toString()
179
    {
180
        return $this->version;
181
    }
182
}
183