| Total Complexity | 66 |
| Total Lines | 356 |
| 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->source = $this->escape($this->source); |
||
| 98 | |||
| 99 | $this->determineEmpty(); |
||
| 100 | $this->determineComment(); |
||
| 101 | $this->determineCommand(); |
||
| 102 | $this->determineValue(); |
||
| 103 | } |
||
| 104 | |||
| 105 | private function escape(string $source) { |
||
|
|
|||
| 106 | $knownTags = synapse()->memory->tags()->keys()->all(); |
||
| 107 | |||
| 108 | $pattern = '/<(\S*?)*>.*?<\/\1>|<^.*?\/>/'; |
||
| 109 | |||
| 110 | preg_match_all($pattern, $source, $matches); |
||
| 111 | |||
| 112 | $index = 0; |
||
| 113 | if (is_array($matches[$index]) && isset($matches[$index][0]) && is_null($knownTags) === false && count($matches) == 2) { |
||
| 114 | $matches = $matches[$index]; |
||
| 115 | |||
| 116 | foreach ($matches as $match) { |
||
| 117 | $str = str_replace(['<', '>'], "", $match); |
||
| 118 | $parts = explode(' ', $str); |
||
| 119 | $tag = $parts[0] ?? ""; |
||
| 120 | |||
| 121 | if (in_array($tag, $knownTags, true) === false) { |
||
| 122 | $source = str_replace($match, "\x00{$tag}\x01", $source); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | return $source; |
||
| 128 | } |
||
| 129 | /** |
||
| 130 | * Returns the node's command trigger. |
||
| 131 | * |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function command(): string |
||
| 135 | { |
||
| 136 | return $this->command; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Returns the node's value. |
||
| 141 | * |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function value(): string |
||
| 145 | { |
||
| 146 | return $this->value; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns the node's source. |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function source(): string |
||
| 155 | { |
||
| 156 | return $this->source; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Returns the node's line number. |
||
| 161 | * |
||
| 162 | * @return int |
||
| 163 | */ |
||
| 164 | public function number(): int |
||
| 165 | { |
||
| 166 | return $this->number; |
||
| 167 | } |
||
| 168 | |||
| 169 | public function isEmpty(): bool |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Returns true if node is a comment. |
||
| 176 | * |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | public function isComment(): bool |
||
| 180 | { |
||
| 181 | return $this->isComment; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Returns true is node has been interrupted. |
||
| 186 | * |
||
| 187 | * @return bool |
||
| 188 | */ |
||
| 189 | public function isInterrupted(): bool |
||
| 190 | { |
||
| 191 | return $this->isInterrupted; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Determine the command type of the node. |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | protected function determineCommand(): void |
||
| 208 | } |
||
| 209 | |||
| 210 | protected function determineEmpty(): void |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Determine if the current node source is a comment. |
||
| 217 | * |
||
| 218 | * @return void |
||
| 219 | */ |
||
| 220 | protected function determineComment(): void |
||
| 221 | { |
||
| 222 | if (starts_with($this->source, '//')) { |
||
| 223 | $this->isInterrupted = true; |
||
| 224 | } elseif (starts_with($this->source, '#')) { |
||
| 225 | log_warning('Using the # symbol for comments is deprecated'); |
||
| 226 | $this->isInterrupted = true; |
||
| 227 | } elseif (starts_with($this->source, '/*')) { |
||
| 228 | if (ends_with($this->source, '*/')) { |
||
| 229 | return; |
||
| 230 | } |
||
| 231 | $this->isComment = true; |
||
| 232 | } elseif (ends_with($this->source, '*/')) { |
||
| 233 | $this->isComment = false; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Determine the value of the node. |
||
| 239 | * |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | protected function determineValue(): void |
||
| 243 | { |
||
| 244 | $this->value = trim(mb_substr($this->source, 1)); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Enable the UTF8 mode. |
||
| 249 | * |
||
| 250 | * @param bool $allowUtf8 True of false. |
||
| 251 | */ |
||
| 252 | public function setAllowUtf8(bool $allowUtf8): void |
||
| 253 | { |
||
| 254 | $this->allowUtf8 = $allowUtf8; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Check the syntax |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function checkSyntax(): string |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Check for patterns in a given string. |
||
| 373 | * |
||
| 374 | * @param string $regex The pattern to detect. |
||
| 375 | * @param string $string The string that could contain the pattern. |
||
| 376 | * |
||
| 377 | * @return bool |
||
| 378 | */ |
||
| 379 | private function matchesPattern(string $regex = '', string $string = ''): bool |
||
| 384 | } |
||
| 385 | } |
||
| 386 |
This check looks for private methods that have been defined, but are not used inside the class.