Complex classes like Parser 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Parser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Parser { |
||
12 | |||
13 | /** |
||
14 | * @const string |
||
15 | */ |
||
16 | const USE_STATEMENT_TYPE = 'use'; |
||
17 | |||
18 | /** |
||
19 | * @const string |
||
20 | */ |
||
21 | const ALIAS_STATEMENT_TYPE = 'alias'; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $usesBlock; |
||
27 | |||
28 | /** |
||
29 | * @var UseStatements |
||
30 | */ |
||
31 | private $useStatements; |
||
32 | |||
33 | /** |
||
34 | * @var boolean |
||
35 | */ |
||
36 | private $isUseStatementBuilding = false; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $useStatement = ''; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $committedUseStatement = ''; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | private $aliasStatement = ''; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | private $statementType = ''; |
||
57 | |||
58 | /** |
||
59 | * @var bool |
||
60 | */ |
||
61 | private $isBrace = false; |
||
62 | |||
63 | /** |
||
64 | * Parser constructor. |
||
65 | * @param string $usesBlock |
||
66 | */ |
||
67 | 1 | public function __construct(string $usesBlock) { |
|
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | 1 | public function getUsesBlock(): string { |
|
79 | |||
80 | /** |
||
81 | * @param string $usesBlock |
||
82 | * @return Parser |
||
83 | */ |
||
84 | 1 | public function setUsesBlock(string $usesBlock): Parser { |
|
89 | |||
90 | /** |
||
91 | * @param UseStatement $useStatement |
||
92 | * @return Parser |
||
93 | */ |
||
94 | 1 | private function addUseStatement(UseStatement $useStatement): Parser { |
|
99 | |||
100 | /** |
||
101 | * @return boolean |
||
102 | */ |
||
103 | 1 | private function isUseStatementBuilding(): bool { |
|
106 | |||
107 | /** |
||
108 | * @return Parser |
||
109 | */ |
||
110 | 1 | private function setUseStatementIsBuilding(): Parser { |
|
115 | |||
116 | /** |
||
117 | * @return Parser |
||
118 | */ |
||
119 | 1 | private function setUseStatementIsNotBuilding(): Parser { |
|
124 | |||
125 | /** |
||
126 | * @return string |
||
127 | */ |
||
128 | 1 | private function getUseStatement(): string { |
|
135 | |||
136 | /** |
||
137 | * @param string $statement |
||
138 | * @return Parser |
||
139 | */ |
||
140 | 1 | private function setUseStatement(string $statement): Parser { |
|
145 | |||
146 | /** |
||
147 | * @return Parser |
||
148 | */ |
||
149 | 1 | private function clearUseStatement(): Parser { |
|
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | 1 | private function getCommittedUseStatement(): string { |
|
161 | |||
162 | /** |
||
163 | * @return Parser |
||
164 | */ |
||
165 | 1 | private function commitUseStatement(): Parser { |
|
170 | |||
171 | /** |
||
172 | * @return string |
||
173 | */ |
||
174 | 1 | private function getAliasStatement(): string { |
|
177 | |||
178 | /** |
||
179 | * @param string $statement |
||
180 | * @return Parser |
||
181 | */ |
||
182 | 1 | private function setAliasStatement(string $statement): Parser { |
|
187 | |||
188 | /** |
||
189 | * @return string |
||
190 | */ |
||
191 | 1 | private function getStatementType(): string { |
|
194 | |||
195 | /** |
||
196 | * @param string $statementType |
||
197 | * @return Parser |
||
198 | */ |
||
199 | 1 | private function setStatementType(string $statementType): Parser { |
|
204 | |||
205 | /** |
||
206 | * @return Parser |
||
207 | */ |
||
208 | 1 | private function setIsBrace(): Parser { |
|
214 | |||
215 | /** |
||
216 | * @return Parser |
||
217 | */ |
||
218 | 1 | private function setIsNotBrace(): Parser { |
|
224 | |||
225 | /** |
||
226 | * @return bool |
||
227 | */ |
||
228 | 1 | private function isBrace(): bool { |
|
231 | |||
232 | /** |
||
233 | * @return UseStatements |
||
234 | */ |
||
235 | 1 | public function getUseStatements(): UseStatements { |
|
246 | |||
247 | /** |
||
248 | * @return array |
||
249 | */ |
||
250 | 1 | private function getTokens(): array { |
|
253 | |||
254 | /** |
||
255 | * @uses \Reflection\ClassUseStatements\Parser::setUseStatement() |
||
256 | * @uses \Reflection\ClassUseStatements\Parser::setAliasStatement() |
||
257 | * @param string $value |
||
258 | * @return Parser |
||
259 | */ |
||
260 | 1 | private function setStatement(string $value): Parser { |
|
267 | |||
268 | /** |
||
269 | * @return Parser |
||
270 | */ |
||
271 | 1 | private function clearStatements(): Parser { |
|
275 | |||
276 | /** |
||
277 | * @param string $token |
||
278 | * @return Parser |
||
279 | */ |
||
280 | 1 | private function analyzeDelimiterToken(string $token): Parser { |
|
308 | |||
309 | /** |
||
310 | * @param array $token |
||
311 | * @return Parser |
||
312 | */ |
||
313 | 1 | private function analyzeStatementToken(array $token): Parser { |
|
331 | |||
332 | } |