@@ -12,6 +12,7 @@ abstract class Constraint
1212 * Indicate if this constraint matches another constraint
1313 *
1414 * @param Constraint $constraint
15+ *
1516 * @return bool
1617 */
1718 abstract public function matches (Constraint $ constraint );
@@ -20,33 +21,37 @@ abstract public function matches(Constraint $constraint);
2021 * Parse a string and return a Constraint.
2122 *
2223 * @param string $input
24+ *
2325 * @return Constraint
26+ *
27+ * @throws \Exception
2428 */
2529 public static function parse ($ input )
2630 {
2731 $ input = trim ($ input );
2832
29- if (strlen ( $ input ) == 0 ) {
33+ if ('' === $ input ) {
3034 throw new \UnexpectedValueException ('Empty. ' );
3135 }
3236
33- $ input = explode (', ' , $ input );
34- if (count ($ input ) > 1 ) {
37+ $ inputParts = explode (', ' , $ input );
38+
39+ if (count ($ inputParts ) > 1 ) {
3540 $ and = true ;
3641 } else {
37- $ input = explode ('| ' , $ input [0 ]);
42+ $ inputParts = explode ('| ' , $ inputParts [0 ]);
3843 $ and = false ;
3944 }
4045
41- if (count ($ input ) > 1 ) {
46+ if (count ($ inputParts ) > 1 ) {
4247 $ constraints = array ();
43- foreach ($ input as $ constraint ) {
48+ foreach ($ inputParts as $ constraint ) {
4449 $ constraints [] = self ::parse ($ constraint );
4550 }
4651 return new MultiConstraint ($ constraints , $ and );
4752 }
4853
49- $ input = $ input [0 ];
54+ $ inputParts = $ inputParts [0 ];
5055
5156 $ regex = '/^ ' .
5257 '(?:([\*|x])\.)? ' .
@@ -55,24 +60,24 @@ public static function parse($input)
5560 '(?:([\*|x]))? ' .
5661 '$/ ' ;
5762
58- if (preg_match ($ regex , $ input , $ matches )) {
63+ if (preg_match ($ regex , $ inputParts , $ matches )) {
5964 return new AnythingConstraint ();
6065 }
6166
6267 $ regex = '/^ ' .
63- '(?:( ' . Operator::REGEX . ') )? * ' .
68+ '( ' . Operator::REGEX . ')? * ' .
6469 '(?:(\d+|\*|x)\.)? ' .
6570 '(?:(\d+|\*|x)\.)? ' .
6671 '(?:(\d+|\*|x)\.)? ' .
67- '(?:( \d+|\*|x) )? ' .
72+ '(\d+|\*|x)? ' .
6873 '(?: ' . Stability::REGEX . ')? ' .
6974 '$/i ' ;
7075
71- if (!preg_match ($ regex , $ input , $ matches )) {
72- throw new \UnexpectedValueException ('Invalid type: ' . $ input );
76+ if (!preg_match ($ regex , $ inputParts , $ matches )) {
77+ throw new \UnexpectedValueException ('Invalid type: ' . $ inputParts );
7378 }
7479
75- if (isset ($ matches [1 ]) && strlen ( $ matches [1 ]) > 0 ) {
80+ if (isset ($ matches [1 ]) && '' !== $ matches [1 ]) {
7681 $ operator = $ matches [1 ];
7782 } else {
7883 $ operator = '= ' ;
@@ -81,20 +86,20 @@ public static function parse($input)
8186
8287 $ parts = array ();
8388
84- if (isset ($ matches [2 ]) && strlen ( $ matches [2 ]) > 0 ) {
89+ if (isset ($ matches [2 ]) && '' !== $ matches [2 ]) {
8590 $ parts [] = $ matches [2 ];
8691 }
87- if (isset ($ matches [3 ]) && strlen ( $ matches [3 ]) > 0 ) {
92+ if (isset ($ matches [3 ]) && '' !== $ matches [3 ]) {
8893 $ parts [] = $ matches [3 ];
8994 }
90- if (isset ($ matches [4 ]) && strlen ( $ matches [4 ]) > 0 ) {
95+ if (isset ($ matches [4 ]) && '' !== $ matches [4 ]) {
9196 $ parts [] = $ matches [4 ];
9297 }
93- if (isset ($ matches [5 ]) && strlen ( $ matches [5 ]) > 0 ) {
98+ if (isset ($ matches [5 ]) && '' !== $ matches [5 ]) {
9499 $ parts [] = $ matches [5 ];
95100 }
96101
97- if ((string )$ operator == '~ ' ) {
102+ if ((string )$ operator === '~ ' ) {
98103 $ end = count ($ parts );
99104 } else {
100105 $ end = null ;
@@ -107,15 +112,15 @@ public static function parse($input)
107112 $ max = $ parts ;
108113
109114 if ($ end ) {
110- if ($ end == 1 ) {
115+ if ($ end === 1 ) {
111116 $ max [0 ]++;
112- } elseif ($ end == 2 ) {
117+ } elseif ($ end === 2 ) {
113118 $ max [0 ]++;
114119 $ max [1 ] = 0 ;
115- } elseif ($ end == 3 ) {
120+ } elseif ($ end === 3 ) {
116121 $ max [1 ]++;
117122 $ max [2 ] = 0 ;
118- } elseif ($ end == 4 ) {
123+ } elseif ($ end === 4 ) {
119124 $ max [2 ]++;
120125 $ max [3 ] = 0 ;
121126 } else {
@@ -142,6 +147,7 @@ public static function parse($input)
142147 }
143148
144149 $ version = new Version ($ parts [0 ]);
150+
145151 if (isset ($ parts [1 ])) {
146152 $ version ->setMinor ($ parts [1 ]);
147153 }
@@ -152,40 +158,46 @@ public static function parse($input)
152158 $ version ->setMicro ($ parts [3 ]);
153159 }
154160
155- if (isset ($ matches [6 ]) && strlen ( $ matches [6 ]) > 0 ) {
156- if (strtolower ($ matches [5 ]) == 'rc ' ) {
161+ if (isset ($ matches [6 ]) && '' !== $ matches [6 ]) {
162+ if (strtolower ($ matches [5 ]) === 'rc ' ) {
157163 $ stability = 'RC ' ;
158164 } elseif (in_array (strtolower ($ matches [6 ]), array ('pl ' , 'patch ' , 'p ' ))) {
159165 $ stability = 'patch ' ;
160166 } elseif (in_array (strtolower ($ matches [6 ]), array ('beta ' , 'b ' ))) {
161167 $ stability = 'beta ' ;
162- } elseif (strtolower ($ matches [6 ]) == 'stable ' ) {
168+ } elseif (strtolower ($ matches [6 ]) === 'stable ' ) {
163169 $ stability = 'stable ' ;
164170 } else {
165- throw new \UnexpectedValueException ('Invalid type: ' . $ input );
171+ throw new \UnexpectedValueException ('Invalid type: ' . $ inputParts );
166172 }
173+
167174 $ version ->setStability (new Stability ($ stability , $ matches [7 ]));
168175 }
169176
170177 foreach ($ parts as $ k => $ v ) {
171- if ($ v != $ max [$ k ]) {
178+ if ($ v !== $ max [$ k ]) {
172179 $ maxVersion = new Version ($ max [0 ]);
180+
173181 if (isset ($ max [1 ])) {
174182 $ maxVersion ->setMinor ($ max [1 ]);
175183 }
184+
176185 if (isset ($ max [2 ])) {
177186 $ maxVersion ->setRevision ($ max [2 ]);
178187 }
188+
179189 if (isset ($ max [3 ])) {
180190 $ maxVersion ->setMicro ($ max [3 ]);
181191 }
182192
183- if ((string )$ version == '0.0.0.0 ' ) {
193+ if ((string )$ version === '0.0.0.0 ' ) {
184194 return new SimpleConstraint (new Operator ('< ' ), $ maxVersion );
185195 }
186- if (isset ($ matches [6 ]) && strtolower ($ matches [6 ]) == 'stable ' ) {
196+
197+ if (isset ($ matches [6 ]) && strtolower ($ matches [6 ]) === 'stable ' ) {
187198 $ version ->setStability (new Stability ());
188199 }
200+
189201 return new MultiConstraint (array (
190202 new SimpleConstraint (new Operator ('>= ' ), $ version ),
191203 new SimpleConstraint (new Operator ('< ' ), $ maxVersion )
0 commit comments