Total Complexity | 58 |
Total Lines | 324 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like BBCodeConverter 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 BBCodeConverter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class BBCodeConverter extends Converter |
||
20 | { |
||
21 | /** |
||
22 | * Conaining all callable cleaners. |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $cleaners = []; |
||
27 | |||
28 | public function __construct(string $text = null, string $id = null) |
||
29 | { |
||
30 | parent::__construct($text, $id); |
||
31 | |||
32 | foreach (get_class_methods($this) as $method) { |
||
33 | if (preg_match('#^(remove|replace)[A-Z][a-z]+#', $method)) { |
||
34 | call_user_func([$this, $method]); |
||
35 | } |
||
36 | } |
||
37 | } |
||
38 | |||
39 | public function addCleaner($name, $callback) |
||
43 | } |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @brief Converts the provided BBCode text to an equivalent Markdown text. |
||
48 | */ |
||
49 | public function toMarkdown(string $text = null, $id = null) |
||
50 | { |
||
51 | if (is_null($text) && $this->text) { |
||
52 | $text = $this->text; |
||
53 | } |
||
54 | |||
55 | if ( ! $text) { |
||
56 | return $text; |
||
57 | } |
||
58 | |||
59 | if (is_null($id) && $this->id) { |
||
60 | $id = $this->id; |
||
61 | } |
||
62 | |||
63 | foreach ($this->cleaners as $cleaner) { |
||
64 | if (is_callable($cleaner)) { |
||
65 | do { |
||
66 | $oldText = $text; |
||
67 | $text = $cleaner($text, $id); |
||
68 | } while ($oldText != $text); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $text; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @brief Removes BBCode color. |
||
77 | */ |
||
78 | protected function removeColor() |
||
79 | { |
||
80 | $this->cleaners['removeColor'] = function ($text) { |
||
81 | return preg_replace_callback('%\[color=\#?\w+\]([\W\D\w\s]*?)\[/color\]%iu', |
||
82 | function ($matches) { |
||
83 | return $matches[1]; |
||
84 | }, |
||
85 | |||
86 | $text |
||
87 | ); |
||
88 | }; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @brief Removes BBCode size. |
||
93 | */ |
||
94 | protected function removeSize() |
||
95 | { |
||
96 | $this->cleaners['removeSize'] = function ($text) { |
||
97 | return preg_replace_callback('%\[size=\d*\]([\W\D\w\s]*?)\[/size\]%iu', |
||
98 | function ($matches) { |
||
99 | return $matches[1]; |
||
100 | }, |
||
101 | |||
102 | $text |
||
103 | ); |
||
104 | }; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @brief Removes BBCode center. |
||
109 | */ |
||
110 | protected function removeCenter() |
||
111 | { |
||
112 | $this->cleaners['removeCenter'] = function ($text) { |
||
113 | return preg_replace_callback('%\[center\]([\W\D\w\s]*?)\[/center\]%iu', |
||
114 | function ($matches) { |
||
115 | return $matches[1]; |
||
116 | }, |
||
117 | |||
118 | $text |
||
119 | ); |
||
120 | }; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * @brief Replaces BBCode bold. |
||
125 | */ |
||
126 | protected function replaceBold() |
||
127 | { |
||
128 | $this->cleaners['replaceBold'] = function ($text) { |
||
129 | return preg_replace_callback('%\[b\]([\W\D\w\s]*?)\[/b\]%iu', |
||
130 | function ($matches) { |
||
131 | return '**'.trim($matches[1], ' ').'**'; |
||
132 | }, |
||
133 | |||
134 | $text |
||
135 | ); |
||
136 | }; |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @brief Replaces BBCode italic. |
||
141 | */ |
||
142 | protected function replaceItalic() |
||
151 | ); |
||
152 | }; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * @brief Replaces BBCode underline. Hoedown support underline. |
||
157 | */ |
||
158 | protected function replaceUnderline() |
||
167 | ); |
||
168 | }; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @brief Replaces BBCode strikethrough. |
||
173 | */ |
||
174 | protected function replaceStrikethrough() |
||
175 | { |
||
176 | $this->cleaners['replaceStrikethrough'] = function ($text) { |
||
177 | return preg_replace_callback('%\[s\]([\W\D\w\s]*?)\[/s\]%iu', |
||
178 | function ($matches) { |
||
179 | return '~~'.trim($matches[1], ' ').'~~'; |
||
180 | }, |
||
181 | |||
182 | $text |
||
183 | ); |
||
184 | }; |
||
185 | } |
||
186 | |||
187 | /** |
||
188 | * @brief Replaces BBCode lists. |
||
189 | */ |
||
190 | protected function replaceLists() |
||
230 | ); |
||
231 | }; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @brief Replaces BBCode urls. |
||
236 | */ |
||
237 | protected function replaceUrls() |
||
250 | ); |
||
251 | }; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @brief Replaces BBCode images. |
||
256 | */ |
||
257 | protected function replaceImages() |
||
270 | ); |
||
271 | }; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @brief Replaces BBCode quotes. |
||
276 | * @details Thanks to Casimir et Hippolyte for helping me with this regex. |
||
277 | */ |
||
278 | protected function replaceQuotes() |
||
279 | { |
||
280 | $this->cleaners['replaceQuotes'] = function ($text, $id = null) { |
||
281 | // Removes the inner quotes, leaving just one level. |
||
282 | $text = preg_replace('~\G(?<!^)(?>(\[quote\b[^]]*](?>[^[]++|\[(?!/?quote)|(?1))*\[/quote])|(?<!\[)(?>[^[]++|\[(?!/?quote))+\K)|\[quote\b[^]]*]\K~i', '', $text); |
||
283 | |||
284 | // Replaces all the remaining quotes with '> ' characters. |
||
285 | $text = preg_replace_callback('%\[quote\b[^]]*\]((?>[^[]++|\[(?!/?quote))*)\[/quote\]%i', |
||
286 | function ($matches) { |
||
287 | $quote = preg_replace('/^\s*/mu', '', trim($matches[1])); |
||
288 | |||
289 | return '> '.$quote.PHP_EOL.PHP_EOL; |
||
290 | }, |
||
291 | |||
292 | $text |
||
293 | ); |
||
294 | |||
295 | return $text; |
||
296 | }; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @brief Replaces BBCode snippets. |
||
301 | */ |
||
302 | protected function replaceSnippets() |
||
343 | ); |
||
344 | }; |
||
345 | } |
||
347 |