Total Complexity | 59 |
Total Lines | 330 |
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 | * Is this an empty line. |
||
74 | * |
||
75 | * @var bool |
||
76 | */ |
||
77 | public bool $isEmpty = false; |
||
78 | |||
79 | /** |
||
80 | * Is UTF8 modes enabled. |
||
81 | * |
||
82 | * @var bool |
||
83 | */ |
||
84 | protected bool $allowUtf8 = false; |
||
85 | |||
86 | /** |
||
87 | * Create a new Source instance. |
||
88 | * |
||
89 | * @param string $source The source line. |
||
90 | * @param int $number The line number in the script. |
||
91 | */ |
||
92 | public function __construct(string $source, int $number) |
||
93 | { |
||
94 | $this->source = remove_whitespace($source); |
||
95 | $this->number = $number; |
||
96 | |||
97 | $this->determineEmpty(); |
||
98 | $this->determineComment(); |
||
99 | $this->determineCommand(); |
||
100 | $this->determineValue(); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns the node's command trigger. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function command(): string |
||
109 | { |
||
110 | return $this->command; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Returns the node's value. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | public function value(): string |
||
119 | { |
||
120 | return $this->value; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Returns the node's source. |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public function source(): string |
||
129 | { |
||
130 | return $this->source; |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Returns the node's line number. |
||
135 | * |
||
136 | * @return int |
||
137 | */ |
||
138 | public function number(): int |
||
139 | { |
||
140 | return $this->number; |
||
141 | } |
||
142 | |||
143 | public function isEmpty(): bool |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Returns true if node is a comment. |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function isComment(): bool |
||
154 | { |
||
155 | return $this->isComment; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Returns true is node has been interrupted. |
||
160 | * |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function isInterrupted(): bool |
||
164 | { |
||
165 | return $this->isInterrupted; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Determine the command type of the node. |
||
170 | * |
||
171 | * @return void |
||
172 | */ |
||
173 | protected function determineCommand(): void |
||
182 | } |
||
183 | |||
184 | protected function determineEmpty(): void |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Determine if the current node source is a comment. |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | protected function determineComment(): void |
||
195 | { |
||
196 | if (starts_with($this->source, '//')) { |
||
197 | $this->isInterrupted = true; |
||
198 | } elseif (starts_with($this->source, '#')) { |
||
199 | log_warning('Using the # symbol for comments is deprecated'); |
||
200 | $this->isInterrupted = true; |
||
201 | } elseif (starts_with($this->source, '/*')) { |
||
202 | if (ends_with($this->source, '*/')) { |
||
203 | return; |
||
204 | } |
||
205 | $this->isComment = true; |
||
206 | } elseif (ends_with($this->source, '*/')) { |
||
207 | $this->isComment = false; |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Determine the value of the node. |
||
213 | * |
||
214 | * @return void |
||
215 | */ |
||
216 | protected function determineValue(): void |
||
217 | { |
||
218 | $this->value = trim(mb_substr($this->source, 1)); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Enable the UTF8 mode. |
||
223 | * |
||
224 | * @param bool $allowUtf8 True of false. |
||
225 | */ |
||
226 | public function setAllowUtf8(bool $allowUtf8): void |
||
227 | { |
||
228 | $this->allowUtf8 = $allowUtf8; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Check the syntax |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function checkSyntax(): string |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Check for patterns in a given string. |
||
347 | * |
||
348 | * @param string $regex The pattern to detect. |
||
349 | * @param string $string The string that could contain the pattern. |
||
350 | * |
||
351 | * @return bool |
||
352 | */ |
||
353 | private function matchesPattern(string $regex = '', string $string = ''): bool |
||
358 | } |
||
359 | } |
||
360 |