Total Complexity | 43 |
Total Lines | 313 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 1 | Features | 0 |
Complex classes like Mailcode_Parser_Statement_Info 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 Mailcode_Parser_Statement_Info, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Mailcode_Parser_Statement_Info |
||
23 | { |
||
24 | /** |
||
25 | * @var Mailcode_Parser_Statement_Tokenizer |
||
26 | */ |
||
27 | protected $tokenizer; |
||
28 | |||
29 | /** |
||
30 | * @var Mailcode_Parser_Statement_Tokenizer_Token[] |
||
31 | */ |
||
32 | protected $tokens = array(); |
||
33 | |||
34 | public function __construct(Mailcode_Parser_Statement_Tokenizer $tokenizer) |
||
35 | { |
||
36 | $this->tokenizer = $tokenizer; |
||
37 | $this->tokens = $this->tokenizer->getTokens(); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Whether the whole statement is a variable being assigned a value. |
||
42 | * |
||
43 | * @return bool |
||
44 | */ |
||
45 | public function isVariableAssignment() : bool |
||
46 | { |
||
47 | $variable = $this->getVariableByIndex(0); |
||
48 | $operand = $this->getOperandByIndex(1); |
||
49 | $value = $this->getTokenByIndex(2); |
||
50 | |||
51 | if($variable && $operand && $value && $operand->isAssignment()) |
||
52 | { |
||
53 | return true; |
||
54 | } |
||
55 | |||
56 | return false; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Whether the whole statement is a variable being compared to something. |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | public function isVariableComparison() : bool |
||
65 | { |
||
66 | $variable = $this->getVariableByIndex(0); |
||
67 | $operand = $this->getOperandByIndex(1); |
||
68 | $value = $this->getTokenByIndex(2); |
||
69 | |||
70 | if($variable && $operand && $value && $operand->isComparator()) |
||
71 | { |
||
72 | return true; |
||
73 | } |
||
74 | |||
75 | return false; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Retrieves all variables used in the statement. |
||
80 | * |
||
81 | * @return \Mailcode\Mailcode_Variables_Variable[] |
||
82 | */ |
||
83 | public function getVariables() |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Retrieves a variable by its position in the command's parameters. |
||
100 | * Returns null if there is no parameter at the specified index, or |
||
101 | * if it is of another type. |
||
102 | * |
||
103 | * @param int $index Zero-based index. |
||
104 | * @return Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL |
||
105 | */ |
||
106 | public function getVariableByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Variable |
||
107 | { |
||
108 | $token = $this->getTokenByIndex($index); |
||
109 | |||
110 | if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) |
||
111 | { |
||
112 | return $token; |
||
113 | } |
||
114 | |||
115 | return null; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Retrieves a string literal by its position in the command's parameters. |
||
120 | * Returns null if there is no parameter at the specified index, or |
||
121 | * if it is of another type. |
||
122 | * |
||
123 | * @param int $index Zero-based index. |
||
124 | * @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL |
||
125 | */ |
||
126 | public function getStringLiteralByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Retrieves a keyword by its position in the command's parameters. |
||
140 | * Returns null if there is no parameter at the specified index, or |
||
141 | * if it is of another type. |
||
142 | * |
||
143 | * @param int $index Zero-based index. |
||
144 | * @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword|NULL |
||
145 | */ |
||
146 | public function getKeywordByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Keyword |
||
147 | { |
||
148 | $token = $this->getTokenByIndex($index); |
||
149 | |||
150 | if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Keyword) |
||
151 | { |
||
152 | return $token; |
||
153 | } |
||
154 | |||
155 | return null; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Retrieves an operand by its position in the command's parameters. |
||
160 | * Returns null if there is no parameter at the specified index, or |
||
161 | * if it is of another type. |
||
162 | * |
||
163 | * @param int $index Zero-based index. |
||
164 | * @return Mailcode_Parser_Statement_Tokenizer_Token_Operand|NULL |
||
165 | */ |
||
166 | public function getOperandByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Operand |
||
167 | { |
||
168 | $token = $this->getTokenByIndex($index); |
||
169 | |||
170 | if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Operand) |
||
171 | { |
||
172 | return $token; |
||
173 | } |
||
174 | |||
175 | return null; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Retrieves a parameter token by its position in the command's parameters, |
||
180 | * regardless of its type. Returns null if there is no parameter at the |
||
181 | * specified index. |
||
182 | * |
||
183 | * @param int $index Zero-based index. |
||
184 | * @return Mailcode_Parser_Statement_Tokenizer_Token|NULL |
||
185 | */ |
||
186 | public function getTokenByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token |
||
187 | { |
||
188 | if(isset($this->tokens[$index])) |
||
189 | { |
||
190 | return $this->tokens[$index]; |
||
191 | } |
||
192 | |||
193 | return null; |
||
194 | } |
||
195 | |||
196 | public function hasTokenAtIndex(int $index) : bool |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Retrieves all tokens. |
||
203 | * @return Mailcode_Parser_Statement_Tokenizer_Token[] |
||
204 | */ |
||
205 | public function getTokens() : array |
||
206 | { |
||
207 | return $this->tokens; |
||
208 | } |
||
209 | |||
210 | /** |
||
211 | * Retrieves all string literals that were found in the command. |
||
212 | * @return \Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[] |
||
213 | */ |
||
214 | public function getStringLiterals() |
||
227 | } |
||
228 | |||
229 | public function createPruner() : Mailcode_Parser_Statement_Info_Pruner |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword[] |
||
236 | */ |
||
237 | public function getKeywords() : array |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Adds or removes a keyword dependin on whether it should be enabled. |
||
254 | * |
||
255 | * @param string $keyword The keyword name, with or without : |
||
256 | * @param bool $enabled |
||
257 | * @return Mailcode_Parser_Statement_Info |
||
258 | * @throws Mailcode_Exception |
||
259 | */ |
||
260 | public function setKeywordEnabled(string $keyword, bool $enabled) : Mailcode_Parser_Statement_Info |
||
261 | { |
||
262 | if($enabled) |
||
263 | { |
||
264 | return $this->addKeyword($keyword); |
||
265 | } |
||
266 | |||
267 | return $this->removeKeyword($keyword); |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * Adds a keyword to the command. |
||
272 | * |
||
273 | * @param string $keyword Keyword name, with or without : |
||
274 | * @return $this |
||
275 | * @throws Mailcode_Exception |
||
276 | */ |
||
277 | protected function addKeyword(string $keyword) : Mailcode_Parser_Statement_Info |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Removes a keyword from the command, if it has one. |
||
292 | * Has no effect otherwise. |
||
293 | * |
||
294 | * @param string $keyword Keyword name, with or without : |
||
295 | * @return $this |
||
296 | */ |
||
297 | public function removeKeyword(string $keyword) : Mailcode_Parser_Statement_Info |
||
298 | { |
||
299 | $keyword = rtrim($keyword, ':').':'; |
||
300 | $keywords = $this->getKeywords(); |
||
301 | |||
302 | foreach ($keywords as $kw) |
||
303 | { |
||
304 | if ($kw->getKeyword() !== $keyword) { |
||
305 | continue; |
||
306 | } |
||
307 | |||
308 | $this->tokenizer->removeToken($kw); |
||
309 | $this->tokens = $this->tokenizer->getTokens(); |
||
310 | } |
||
311 | |||
312 | return $this; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Whether the command has the specified keyword. |
||
317 | * |
||
318 | * @param string $keyword Keyword name, with or without : |
||
319 | * @return bool |
||
320 | */ |
||
321 | public function hasKeyword(string $keyword) : bool |
||
335 | } |
||
336 | } |
||
337 |