Total Complexity | 81 |
Total Lines | 253 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Constraint often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Constraint, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
6 | class Constraint implements ConstraintInterface |
||
7 | { |
||
8 | const OP_EQ = 0; |
||
9 | const OP_LT = 1; |
||
10 | const OP_LE = 2; |
||
11 | const OP_GT = 3; |
||
12 | const OP_GE = 4; |
||
13 | const OP_NE = 5; |
||
14 | const STR_OP_EQ = '=='; |
||
15 | const STR_OP_EQ_ALT = '='; |
||
16 | const STR_OP_LT = '<'; |
||
17 | const STR_OP_LE = '<='; |
||
18 | const STR_OP_GT = '>'; |
||
19 | const STR_OP_GE = '>='; |
||
20 | const STR_OP_NE = '!='; |
||
21 | const STR_OP_NE_ALT = '<>'; |
||
22 | /** |
||
23 | @phpstan-var |
||
24 | */ |
||
25 | private static $transOpStr = array('=' => self::OP_EQ, '==' => self::OP_EQ, '<' => self::OP_LT, '<=' => self::OP_LE, '>' => self::OP_GT, '>=' => self::OP_GE, '<>' => self::OP_NE, '!=' => self::OP_NE); |
||
26 | /** |
||
27 | @phpstan-var |
||
28 | */ |
||
29 | private static $transOpInt = array(self::OP_EQ => '==', self::OP_LT => '<', self::OP_LE => '<=', self::OP_GT => '>', self::OP_GE => '>=', self::OP_NE => '!='); |
||
30 | /** |
||
31 | @phpstan-var |
||
32 | */ |
||
33 | protected $operator; |
||
34 | protected $version; |
||
35 | protected $prettyString; |
||
36 | protected $lowerBound; |
||
37 | protected $upperBound; |
||
38 | /** |
||
39 | @phpstan-param |
||
40 | */ |
||
41 | public function __construct($operator, $version) |
||
42 | { |
||
43 | if (!isset(self::$transOpStr[$operator])) { |
||
44 | throw new \InvalidArgumentException(\sprintf('Invalid operator "%s" given, expected one of: %s', $operator, \implode(', ', self::getSupportedOperators()))); |
||
45 | } |
||
46 | $this->operator = self::$transOpStr[$operator]; |
||
47 | $this->version = $version; |
||
48 | } |
||
49 | public function getVersion() |
||
52 | } |
||
53 | /** |
||
54 | @phpstan-return |
||
55 | */ |
||
56 | public function getOperator() |
||
59 | } |
||
60 | public function matches(ConstraintInterface $provider) |
||
61 | { |
||
62 | if ($provider instanceof self) { |
||
63 | return $this->matchSpecific($provider); |
||
64 | } |
||
65 | return $provider->matches($this); |
||
66 | } |
||
67 | public function setPrettyString($prettyString) |
||
68 | { |
||
69 | $this->prettyString = $prettyString; |
||
70 | } |
||
71 | public function getPrettyString() |
||
72 | { |
||
73 | if ($this->prettyString) { |
||
74 | return $this->prettyString; |
||
75 | } |
||
76 | return $this->__toString(); |
||
77 | } |
||
78 | /** |
||
79 | @phpstan-return |
||
80 | */ |
||
81 | public static function getSupportedOperators() |
||
82 | { |
||
83 | return \array_keys(self::$transOpStr); |
||
84 | } |
||
85 | /** |
||
86 | @phpstan-param |
||
87 | @phpstan-return |
||
88 | */ |
||
89 | public static function getOperatorConstant($operator) |
||
90 | { |
||
91 | return self::$transOpStr[$operator]; |
||
92 | } |
||
93 | /** |
||
94 | @phpstan-param |
||
95 | */ |
||
96 | public function versionCompare($a, $b, $operator, $compareBranches = \false) |
||
97 | { |
||
98 | if (!isset(self::$transOpStr[$operator])) { |
||
99 | throw new \InvalidArgumentException(\sprintf('Invalid operator "%s" given, expected one of: %s', $operator, \implode(', ', self::getSupportedOperators()))); |
||
100 | } |
||
101 | $aIsBranch = \strpos($a, 'dev-') === 0; |
||
102 | $bIsBranch = \strpos($b, 'dev-') === 0; |
||
103 | if ($operator === '!=' && ($aIsBranch || $bIsBranch)) { |
||
104 | return $a !== $b; |
||
105 | } |
||
106 | if ($aIsBranch && $bIsBranch) { |
||
107 | return $operator === '==' && $a === $b; |
||
108 | } |
||
109 | if (!$compareBranches && ($aIsBranch || $bIsBranch)) { |
||
110 | return \false; |
||
111 | } |
||
112 | return \version_compare($a, $b, $operator); |
||
113 | } |
||
114 | public function compile($otherOperator) |
||
115 | { |
||
116 | if (\strpos($this->version, 'dev-') === 0) { |
||
117 | if (self::OP_EQ === $this->operator) { |
||
118 | if (self::OP_EQ === $otherOperator) { |
||
119 | return \sprintf('$b && $v === %s', \var_export($this->version, \true)); |
||
120 | } |
||
121 | if (self::OP_NE === $otherOperator) { |
||
122 | return \sprintf('!$b || $v !== %s', \var_export($this->version, \true)); |
||
123 | } |
||
124 | return 'false'; |
||
125 | } |
||
126 | if (self::OP_NE === $this->operator) { |
||
127 | if (self::OP_EQ === $otherOperator) { |
||
128 | return \sprintf('!$b || $v !== %s', \var_export($this->version, \true)); |
||
129 | } |
||
130 | if (self::OP_NE === $otherOperator) { |
||
131 | return 'true'; |
||
132 | } |
||
133 | return '!$b'; |
||
134 | } |
||
135 | return 'false'; |
||
136 | } |
||
137 | if (self::OP_EQ === $this->operator) { |
||
138 | if (self::OP_EQ === $otherOperator) { |
||
139 | return \sprintf('\\version_compare($v, %s, \'==\')', \var_export($this->version, \true)); |
||
140 | } |
||
141 | if (self::OP_NE === $otherOperator) { |
||
142 | return \sprintf('$b || \\version_compare($v, %s, \'!=\')', \var_export($this->version, \true)); |
||
143 | } |
||
144 | return \sprintf('!$b && \\version_compare(%s, $v, \'%s\')', \var_export($this->version, \true), self::$transOpInt[$otherOperator]); |
||
145 | } |
||
146 | if (self::OP_NE === $this->operator) { |
||
147 | if (self::OP_EQ === $otherOperator) { |
||
148 | return \sprintf('$b || (!$b && \\version_compare($v, %s, \'!=\'))', \var_export($this->version, \true)); |
||
149 | } |
||
150 | if (self::OP_NE === $otherOperator) { |
||
151 | return 'true'; |
||
152 | } |
||
153 | return '!$b'; |
||
154 | } |
||
155 | if (self::OP_LT === $this->operator || self::OP_LE === $this->operator) { |
||
156 | if (self::OP_LT === $otherOperator || self::OP_LE === $otherOperator) { |
||
157 | return '!$b'; |
||
158 | } |
||
159 | } else { |
||
160 | if (self::OP_GT === $otherOperator || self::OP_GE === $otherOperator) { |
||
161 | return '!$b'; |
||
162 | } |
||
163 | } |
||
164 | if (self::OP_NE === $otherOperator) { |
||
165 | return 'true'; |
||
166 | } |
||
167 | $codeComparison = \sprintf('\\version_compare($v, %s, \'%s\')', \var_export($this->version, \true), self::$transOpInt[$this->operator]); |
||
168 | if ($this->operator === self::OP_LE) { |
||
169 | if ($otherOperator === self::OP_GT) { |
||
170 | return \sprintf('!$b && \\version_compare($v, %s, \'!=\') && ', \var_export($this->version, \true)) . $codeComparison; |
||
171 | } |
||
172 | } elseif ($this->operator === self::OP_GE) { |
||
173 | if ($otherOperator === self::OP_LT) { |
||
174 | return \sprintf('!$b && \\version_compare($v, %s, \'!=\') && ', \var_export($this->version, \true)) . $codeComparison; |
||
175 | } |
||
176 | } |
||
177 | return \sprintf('!$b && %s', $codeComparison); |
||
178 | } |
||
179 | public function matchSpecific(Constraint $provider, $compareBranches = \false) |
||
209 | } |
||
210 | public function __toString() |
||
211 | { |
||
212 | return self::$transOpInt[$this->operator] . ' ' . $this->version; |
||
213 | } |
||
214 | public function getLowerBound() |
||
215 | { |
||
216 | $this->extractBounds(); |
||
217 | return $this->lowerBound; |
||
218 | } |
||
219 | public function getUpperBound() |
||
223 | } |
||
224 | private function extractBounds() |
||
225 | { |
||
226 | if (null !== $this->lowerBound) { |
||
227 | return; |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 |