Total Complexity | 40 |
Total Lines | 400 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like SingleInterlink 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 SingleInterlink, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class SingleInterlink |
||
8 | { |
||
9 | #region Attributes |
||
10 | /** |
||
11 | * @var string |
||
12 | */ |
||
13 | private $_keyword; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $title; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $_link; |
||
24 | |||
25 | /** |
||
26 | * @var boolean |
||
27 | */ |
||
28 | private $_rawLink = false; |
||
29 | |||
30 | /** |
||
31 | * @var integer |
||
32 | */ |
||
33 | private $_count = 0; |
||
34 | |||
35 | /** |
||
36 | * @var Collection |
||
37 | */ |
||
38 | private $_posts; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $_column; |
||
44 | |||
45 | /** |
||
46 | * @var Collection |
||
47 | */ |
||
48 | private $_result; |
||
49 | |||
50 | /** |
||
51 | * @var bool |
||
52 | */ |
||
53 | private $_markdown = false; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $_blank = false; |
||
59 | |||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $_noFollow = false; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | private $_customAttributes; |
||
69 | #endregion |
||
70 | |||
71 | #region Core |
||
72 | |||
73 | /** |
||
74 | * Warm SingleInterLink System. |
||
75 | * |
||
76 | * @return $this |
||
77 | */ |
||
78 | public function init(): SingleInterlink |
||
79 | { |
||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Internalization links. |
||
85 | * @return SingleInterlink |
||
86 | */ |
||
87 | public function process(): SingleInterlink |
||
88 | { |
||
89 | $result = collect(); |
||
90 | |||
91 | foreach ($this->getPosts() as $post) { |
||
92 | $content = $post[$this->getColumn()]; |
||
93 | |||
94 | $post[$this->getColumn()] = $this->replace($content); |
||
95 | |||
96 | $result->add($post); |
||
97 | } |
||
98 | |||
99 | $this->setResult($result); |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Get updated posts. |
||
106 | * @return Collection |
||
107 | */ |
||
108 | public function getUpdatedPosts(): Collection |
||
109 | { |
||
110 | return $this->getResult(); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Process update action on posts. |
||
115 | */ |
||
116 | public function updatePosts(): void |
||
117 | { |
||
118 | foreach ($this->getResult() as $post) |
||
119 | $post->update(); |
||
120 | } |
||
121 | |||
122 | #endregion |
||
123 | |||
124 | #region Helpers |
||
125 | |||
126 | /** |
||
127 | * Replace post content |
||
128 | * @param string $content |
||
129 | * @return string |
||
130 | */ |
||
131 | private function replace(string $content): string |
||
132 | { |
||
133 | return $this->isCountable() |
||
134 | ? preg_replace($this->normalizedKeyword(), $this->normalizedLink(), $content, $this->getCount()) |
||
135 | : str_replace($this->getKeyword(), $this->normalizedLink(), $content); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * Get normalized link. |
||
140 | * @return string |
||
141 | */ |
||
142 | private function normalizedLink(): string |
||
143 | { |
||
144 | $title = $this->getTitle(); |
||
145 | $link = $this->getLink(); |
||
146 | |||
147 | if ($this->isRawLink()) |
||
148 | return $this->getLink(); |
||
149 | |||
150 | if ($this->isMarkdown()) |
||
151 | return "[$title]($link)"; |
||
152 | |||
153 | return $this->formatHtml($title, $link); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Get HTML formatted link. |
||
158 | * @param string $title |
||
159 | * @param string $link |
||
160 | * @return string |
||
161 | */ |
||
162 | private function formatHtml(string $title, string $link): string |
||
163 | { |
||
164 | $targetAttribute = $this->isBlank() ? '_blank' : '_self'; |
||
165 | $relAttribute = $this->isNoFollow() ? 'rel=nofollow' : ''; |
||
166 | $customAttributes = $this->getCustomAttributes(); |
||
167 | |||
168 | |||
169 | return "<a href='$link' target='$targetAttribute' $relAttribute $customAttributes>$title</a>"; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Normalize keyword for regex search. |
||
174 | * @return string |
||
175 | */ |
||
176 | private function normalizedKeyword(): string |
||
177 | { |
||
178 | return '/' . preg_quote($this->getKeyword(), '/') . '/'; |
||
179 | } |
||
180 | |||
181 | #endregion |
||
182 | |||
183 | #region Setters |
||
184 | |||
185 | /** |
||
186 | * @param string $keyword |
||
187 | * @return SingleInterlink |
||
188 | */ |
||
189 | public function setKeyword(string $keyword): SingleInterlink |
||
190 | { |
||
191 | $this->_keyword = $keyword; |
||
192 | return $this; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @param string $link |
||
197 | * @return SingleInterlink |
||
198 | */ |
||
199 | public function setLink(string $link): SingleInterlink |
||
200 | { |
||
201 | $this->_link = $link; |
||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * @return SingleInterlink |
||
207 | */ |
||
208 | public function rawLink(): SingleInterlink |
||
209 | { |
||
210 | $this->_rawLink = true; |
||
211 | return $this; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * @param int $count |
||
216 | * @return SingleInterlink |
||
217 | */ |
||
218 | public function setCount(int $count): SingleInterlink |
||
219 | { |
||
220 | $this->_count = $count; |
||
221 | return $this; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param Collection $contents |
||
226 | * @return SingleInterlink |
||
227 | */ |
||
228 | public function setPosts(Collection $contents): SingleInterlink |
||
229 | { |
||
230 | $this->_posts = $contents; |
||
231 | return $this; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param string $column |
||
236 | * @return SingleInterlink |
||
237 | */ |
||
238 | public function setColumn(string $column): SingleInterlink |
||
239 | { |
||
240 | $this->_column = $column; |
||
241 | return $this; |
||
242 | } |
||
243 | |||
244 | /** |
||
245 | * @param Collection $result |
||
246 | * @return SingleInterlink |
||
247 | */ |
||
248 | public function setResult(Collection $result): SingleInterlink |
||
249 | { |
||
250 | $this->_result = $result; |
||
251 | return $this; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @param string $title |
||
256 | * @return SingleInterlink |
||
257 | */ |
||
258 | public function setTitle(string $title): SingleInterlink |
||
259 | { |
||
260 | $this->title = $title; |
||
261 | return $this; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * @return SingleInterlink |
||
266 | */ |
||
267 | public function markDown(): SingleInterlink |
||
268 | { |
||
269 | $this->_markdown = true; |
||
270 | return $this; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * @return SingleInterlink |
||
275 | */ |
||
276 | public function blank(): SingleInterlink |
||
277 | { |
||
278 | $this->_blank = true; |
||
279 | return $this; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @return SingleInterlink |
||
284 | */ |
||
285 | public function noFollow(): SingleInterlink |
||
286 | { |
||
287 | $this->_noFollow = true; |
||
288 | return $this; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @param string $customAttributes |
||
293 | * @return SingleInterlink |
||
294 | */ |
||
295 | public function setCustomAttributes(string $customAttributes): SingleInterlink |
||
296 | { |
||
297 | $this->_customAttributes = $customAttributes; |
||
298 | return $this; |
||
299 | } |
||
300 | #endregion |
||
301 | |||
302 | #region Getters |
||
303 | |||
304 | /** |
||
305 | * @return string |
||
306 | */ |
||
307 | private function getKeyword(): string |
||
308 | { |
||
309 | return $this->_keyword; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @return string |
||
314 | */ |
||
315 | private function getLink(): string |
||
316 | { |
||
317 | return $this->_link; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * @return bool |
||
322 | */ |
||
323 | private function isRawLink(): bool |
||
324 | { |
||
325 | return $this->_rawLink; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * @return int |
||
330 | */ |
||
331 | private function getCount(): int |
||
332 | { |
||
333 | return $this->_count; |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @return Collection |
||
338 | */ |
||
339 | private function getPosts(): Collection |
||
340 | { |
||
341 | return $this->_posts; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * @return string |
||
346 | */ |
||
347 | private function getColumn(): string |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @return Collection |
||
354 | */ |
||
355 | private function getResult(): Collection |
||
356 | { |
||
357 | return $this->_result; |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Get replace countable status. |
||
362 | * @return bool |
||
363 | */ |
||
364 | private function isCountable(): bool |
||
365 | { |
||
366 | return $this->getCount() > 0; |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * @return string |
||
371 | */ |
||
372 | private function getTitle(): string |
||
373 | { |
||
374 | return $this->title ?? $this->getKeyword(); |
||
375 | } |
||
376 | |||
377 | /** |
||
378 | * @return bool |
||
379 | */ |
||
380 | private function isMarkdown(): bool |
||
381 | { |
||
382 | return $this->_markdown; |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * @return string |
||
387 | */ |
||
388 | private function getCustomAttributes(): string |
||
391 | } |
||
392 | |||
393 | /** |
||
394 | * @return bool |
||
395 | */ |
||
396 | private function isBlank(): bool |
||
397 | { |
||
398 | return $this->_blank; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @return bool |
||
403 | */ |
||
404 | private function isNoFollow(): bool |
||
407 | } |
||
408 | #endregion |
||
409 | } |
||
410 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths