Total Complexity | 59 |
Total Lines | 331 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Node 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 Node, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Node |
||
29 | { |
||
30 | /** |
||
31 | * The source string for the node. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected string $source = ''; |
||
36 | |||
37 | /** |
||
38 | * The line number of the node source. |
||
39 | * |
||
40 | * @var int |
||
41 | */ |
||
42 | protected int $number = 0; |
||
43 | |||
44 | /** |
||
45 | * The command symbol. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | protected string $command = ''; |
||
50 | |||
51 | /** |
||
52 | * The source without the command symbol. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected string $value = ''; |
||
57 | |||
58 | /** |
||
59 | * Is this line part of a docblock. |
||
60 | * |
||
61 | * @var bool |
||
62 | */ |
||
63 | protected bool $isInterrupted = false; |
||
64 | |||
65 | /** |
||
66 | * Is this node a comment. |
||
67 | * |
||
68 | * @var bool |
||
69 | */ |
||
70 | protected bool $isComment = false; |
||
71 | |||
72 | |||
73 | /** |
||
74 | * Is this an empty line. |
||
75 | * |
||
76 | * @var bool |
||
77 | */ |
||
78 | public bool $isEmpty = false; |
||
79 | |||
80 | /** |
||
81 | * Is UTF8 modes enabled. |
||
82 | * |
||
83 | * @var bool |
||
84 | */ |
||
85 | protected bool $allowUtf8 = false; |
||
86 | |||
87 | /** |
||
88 | * Create a new Source instance. |
||
89 | * |
||
90 | * @param string $source The source line. |
||
91 | * @param int $number The line number in the script. |
||
92 | */ |
||
93 | public function __construct(string $source, int $number) |
||
94 | { |
||
95 | $this->source = remove_whitespace($source); |
||
96 | $this->number = $number; |
||
97 | |||
98 | $this->determineEmpty(); |
||
99 | $this->determineComment(); |
||
100 | $this->determineCommand(); |
||
101 | $this->determineValue(); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Returns the node's command trigger. |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | public function command(): string |
||
110 | { |
||
111 | return $this->command; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Returns the node's value. |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | public function value(): string |
||
120 | { |
||
121 | return $this->value; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Returns the node's source. |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | public function source(): string |
||
130 | { |
||
131 | return $this->source; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns the node's line number. |
||
136 | * |
||
137 | * @return int |
||
138 | */ |
||
139 | public function number(): int |
||
140 | { |
||
141 | return $this->number; |
||
142 | } |
||
143 | |||
144 | public function isEmpty(): bool |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Returns true if node is a comment. |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function isComment(): bool |
||
155 | { |
||
156 | return $this->isComment; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Returns true is node has been interrupted. |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function isInterrupted(): bool |
||
165 | { |
||
166 | return $this->isInterrupted; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Determine the command type of the node. |
||
171 | * |
||
172 | * @return void |
||
173 | */ |
||
174 | protected function determineCommand(): void |
||
183 | } |
||
184 | |||
185 | protected function determineEmpty(): void |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Determine if the current node source is a comment. |
||
192 | * |
||
193 | * @return void |
||
194 | */ |
||
195 | protected function determineComment(): void |
||
196 | { |
||
197 | if (starts_with($this->source, '//')) { |
||
198 | $this->isInterrupted = true; |
||
199 | } elseif (starts_with($this->source, '#')) { |
||
200 | log_warning('Using the # symbol for comments is deprecated'); |
||
201 | $this->isInterrupted = true; |
||
202 | } elseif (starts_with($this->source, '/*')) { |
||
203 | if (ends_with($this->source, '*/')) { |
||
204 | return; |
||
205 | } |
||
206 | $this->isComment = true; |
||
207 | } elseif (ends_with($this->source, '*/')) { |
||
208 | $this->isComment = false; |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Determine the value of the node. |
||
214 | * |
||
215 | * @return void |
||
216 | */ |
||
217 | protected function determineValue(): void |
||
218 | { |
||
219 | $this->value = trim(mb_substr($this->source, 1)); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Enable the UTF8 mode. |
||
224 | * |
||
225 | * @param bool $allowUtf8 True of false. |
||
226 | */ |
||
227 | public function setAllowUtf8(bool $allowUtf8): void |
||
228 | { |
||
229 | $this->allowUtf8 = $allowUtf8; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Check the syntax |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | public function checkSyntax(): string |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Check for patterns in a given string. |
||
348 | * |
||
349 | * @param string $regex The pattern to detect. |
||
350 | * @param string $string The string that could contain the pattern. |
||
351 | * |
||
352 | * @return bool |
||
353 | */ |
||
354 | private function matchesPattern(string $regex = '', string $string = ''): bool |
||
359 | } |
||
360 | } |
||
361 |