Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like MetaManipulator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 MetaManipulator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class MetaManipulator |
||
22 | { |
||
|
|||
23 | const META_TITLE = 1; |
||
24 | const META_DESCRIPTION = 2; |
||
25 | const META_KEYWORDS = 3; |
||
26 | const META_H1 = 4; |
||
27 | |||
28 | /** |
||
29 | * Currency |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $CS; |
||
33 | |||
34 | /** |
||
35 | * ID |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $ID; |
||
39 | |||
40 | /** |
||
41 | * Brand |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $brand; |
||
45 | |||
46 | /** |
||
47 | * Category |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $category; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | protected $descLength; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $description; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $matching = []; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $metaArray = []; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $metaDescription; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $metaH1; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $metaKeywords; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $metaTitle; |
||
91 | |||
92 | /** |
||
93 | * @var SCategory|SBrands|SProducts|array|null |
||
94 | */ |
||
95 | protected $model; |
||
96 | |||
97 | /** |
||
98 | * @var array |
||
99 | */ |
||
100 | protected $morph = []; |
||
101 | |||
102 | /** |
||
103 | * @var string |
||
104 | */ |
||
105 | protected $name; |
||
106 | |||
107 | /** |
||
108 | * @var int |
||
109 | */ |
||
110 | protected $number; |
||
111 | |||
112 | /** |
||
113 | * @var string |
||
114 | */ |
||
115 | protected $pageNumber; |
||
116 | |||
117 | /** |
||
118 | * Price |
||
119 | * @var string |
||
120 | */ |
||
121 | protected $price; |
||
122 | |||
123 | /** |
||
124 | * @var MetaStorage |
||
125 | */ |
||
126 | protected $storage; |
||
127 | |||
128 | /** |
||
129 | * @var string |
||
130 | */ |
||
131 | protected $wrapper = '%'; |
||
132 | |||
133 | /** |
||
134 | * @var array |
||
135 | */ |
||
136 | private $grammens = [ |
||
137 | 'ИМ', |
||
138 | 'РД', |
||
139 | 'ДТ', |
||
140 | 'ВН', |
||
141 | 'ТВ', |
||
142 | 'ПР', |
||
143 | ]; |
||
144 | |||
145 | /** |
||
146 | * method processing meta data |
||
147 | * @var phpMorphy |
||
148 | */ |
||
149 | private $phpMorphy; |
||
150 | |||
151 | /** |
||
152 | * MetaManipulator constructor. |
||
153 | * @param SBrands|SCategory|SProducts|array $model |
||
154 | * @param MetaStorage $storage |
||
155 | * @throws Exception |
||
156 | */ |
||
157 | public function __construct($model, MetaStorage $storage) { |
||
158 | |||
159 | $dir = APPPATH . 'modules/CMSFactory/MetaManipulator/dics'; |
||
160 | |||
161 | // set some options |
||
162 | $opts = [ |
||
163 | // storage type, follow types supported |
||
164 | // PHPMORPHY_STORAGE_FILE - use file operations(fread, fseek) for dictionary access, this is very slow... |
||
165 | // PHPMORPHY_STORAGE_SHM - load dictionary in shared memory(using shmop php extension), this is preferred mode |
||
166 | // PHPMORPHY_STORAGE_MEM - load dict to memory each time when phpMorphy initialized, this useful when shmop ext. not activated. |
||
167 | // Speed same as for PHPMORPHY_STORAGE_SHM type |
||
168 | 'storage' => PHPMORPHY_STORAGE_MEM, |
||
169 | // Enable prediction by suffix |
||
170 | |||
171 | 'predict_by_suffix' => true, |
||
172 | 'graminfo_as_text' => true, |
||
173 | ]; |
||
174 | |||
175 | $this->setPhpMorphy(new phpMorphy($dir, 'ru_RU', $opts)); |
||
176 | |||
177 | if ($model === null) { |
||
178 | throw new Exception('Model not set'); |
||
179 | } |
||
180 | |||
181 | if ($storage === null) { |
||
182 | throw new Exception('Storage not set'); |
||
183 | } |
||
184 | |||
185 | $this->setModel($model); |
||
186 | $this->setStorage($storage); |
||
187 | $this->setMetaTitle($this->getStorage()->getTitleTemplate()); |
||
188 | $this->setMetaDescription($this->getStorage()->getDescriptionTemplate()); |
||
189 | $this->setMetaKeywords($this->getStorage()->getKeywordsTemplate()); |
||
190 | $this->setMetaH1($this->getStorage()->getH1Template()); |
||
191 | |||
192 | $this->setMatching('desc', 'description'); |
||
193 | $this->setMetaArray(['metaTitle', 'metaH1', 'metaDescription', 'metaKeywords']); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @return array |
||
198 | */ |
||
199 | public function getGrammens() { |
||
200 | |||
201 | return $this->grammens; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @return MetaStorage |
||
206 | */ |
||
207 | public function getStorage() { |
||
208 | |||
209 | return $this->storage; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param MetaStorage $storage |
||
214 | */ |
||
215 | public function setStorage($storage) { |
||
216 | |||
217 | $this->storage = $storage; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param string $name |
||
222 | * @param string $arguments |
||
223 | * @return string |
||
224 | */ |
||
225 | public function __call($name, $arguments) { |
||
226 | |||
227 | $prev_string = $name; |
||
228 | $method = substr($name, 0, strpos($name, '[')); |
||
229 | $string = method_exists(__CLASS__, $method) ? $this->$method() : ''; |
||
230 | |||
231 | return $string !== '' ? $this->make($string, $prev_string) : ''; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @param string $string |
||
236 | * @param string $prev_string |
||
237 | * @return null|string |
||
238 | */ |
||
239 | public function make($string, $prev_string) { |
||
240 | |||
241 | $return = $string; |
||
242 | |||
243 | //morphing |
||
244 | if (preg_match('/\[\d\]/', $prev_string, $match)) { |
||
245 | $part = str_replace(['[', ']'], '', $match[0]); |
||
246 | $words = explode(' ', $string); |
||
247 | foreach ($words as $word) { |
||
248 | $array[] = $this->morphing($word, $part); |
||
249 | } |
||
250 | $return = implode(' ', $array); |
||
251 | } |
||
252 | |||
253 | //transliteration |
||
254 | if (preg_match('/\[t\]/', $prev_string)) { |
||
255 | $return = $this->transliteration($return); |
||
256 | } |
||
257 | |||
258 | return $return; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @param string $string |
||
263 | * @param int $part 1-6 |
||
264 | * @return string|null |
||
265 | */ |
||
266 | protected function morphing($string, $part) { |
||
267 | |||
268 | $ucFirst = false; |
||
269 | //check if first letter is uppercase |
||
270 | if (mb_strtolower($string) !== $string) { |
||
271 | $ucFirst = true; |
||
272 | } |
||
273 | |||
274 | $word = mb_strtoupper($string); |
||
275 | |||
276 | if (!array_key_exists($string, $this->morph)) { |
||
277 | try { |
||
278 | |||
279 | if (function_exists('iconv')) { |
||
280 | $word = iconv('utf-8', $this->getPhpMorphy()->getEncoding(), $word); |
||
281 | } |
||
282 | $collection = $this->getPhpMorphy()->findWord($word); |
||
283 | |||
284 | if (false !== $collection) { |
||
285 | $param = $this->getTypeMany($word); |
||
286 | |||
287 | foreach ($collection->getByPartOfSpeech($param['TypeSpeech']) as $paradigm) { |
||
288 | |||
289 | $checkGrammar = $this->checkGrammar($paradigm, $param, $word); |
||
290 | |||
291 | $result = $this->getGrammensWord($paradigm, $param, $checkGrammar); |
||
292 | |||
293 | if ($result[0] == $word) { |
||
294 | break; |
||
295 | } |
||
296 | } |
||
297 | |||
298 | $this->setMorph($string, $result); |
||
299 | } |
||
300 | return $this->getMorph($string, $part, $ucFirst); |
||
301 | } catch (phpMorphy_Exception $e) { |
||
302 | die('Error occurred while creating phpMorphy instance: ' . PHP_EOL . $e->getMessage()); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | return $this->getMorph($string, $part, $ucFirst); |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * @param phpMorphy_Paradigm_ParadigmInterface $paradigm |
||
311 | * @param string $param |
||
312 | * @param null|string $grammar |
||
313 | * @return array |
||
314 | */ |
||
315 | private function getGrammensWord($paradigm, $param, $grammar = null) { |
||
316 | |||
317 | $result = []; |
||
318 | if ($grammar !== null) { |
||
319 | |||
320 | View Code Duplication | foreach ($this->getGrammens() as $key => $val) { |
|
321 | |||
322 | /** @var phpMorphy_WordForm_WordFormInterface $form */ |
||
323 | foreach ($paradigm->getWordFormsByGrammems([$param['param'], $val, $grammar]) as $form) { |
||
324 | if (!$result[$key]) { |
||
325 | $result[$key] = $form->getWord(); |
||
326 | } |
||
327 | } |
||
328 | } |
||
329 | } else { |
||
330 | View Code Duplication | foreach ($this->getGrammens() as $key => $val) { |
|
331 | |||
332 | /** @var phpMorphy_WordForm_WordFormInterface $form */ |
||
333 | foreach ($paradigm->getWordFormsByGrammems([$param['param'], $val]) as $form) { |
||
334 | if (!$result[$key]) { |
||
335 | $result[$key] = $form->getWord(); |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | } |
||
340 | return $result; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * @param string $word |
||
345 | * @return array |
||
346 | */ |
||
347 | private function getTypeMany($word) { |
||
348 | |||
349 | $checkString = $this->getPhpMorphy()->castFormByGramInfo($word, null, ['МН', 'ИМ'], true)[0]; |
||
350 | |||
351 | $param = ($word !== $checkString) ? 'ЕД' : 'МН'; |
||
352 | |||
353 | $typeSpeech = $this->getTypeSpeechWord($word, $param); |
||
354 | |||
355 | $result = [ |
||
356 | 'param' => $param, |
||
357 | 'TypeSpeech' => $typeSpeech, |
||
358 | ]; |
||
359 | |||
360 | return $result; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @param string $word |
||
365 | * @param string $param |
||
366 | * @return array |
||
367 | */ |
||
368 | private function getTypeSpeechWord($word, $param) { |
||
369 | |||
370 | $typeSpeech = $this->getPhpMorphy()->getPartOfSpeech($word); |
||
371 | foreach ($typeSpeech as $type) { |
||
372 | $checkGrammar[$type] = $this->getPhpMorphy()->castFormByGramInfo($word, $type, [$param, 'ИМ'], true); |
||
373 | |||
374 | foreach ($checkGrammar[$type] as $value) { |
||
375 | |||
376 | if (empty($resultType)) { |
||
377 | $resultType = $word === $value ? $type : null; |
||
378 | } |
||
379 | if ($resultType) { |
||
380 | break 2; |
||
381 | } |
||
382 | } |
||
383 | } |
||
384 | |||
385 | return $resultType; |
||
386 | |||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param phpMorphy_Paradigm_ParadigmInterface $paradigm |
||
391 | * @param array $param |
||
392 | * @param string $word |
||
393 | * @return string|null |
||
394 | */ |
||
395 | private function checkGrammar($paradigm, $param, $word) { |
||
396 | /** @var phpMorphy_WordForm_WordFormInterface $form */ |
||
397 | foreach ($paradigm as $form) { |
||
398 | foreach ($form->getGrammems() as $grammatical) { |
||
399 | switch ($grammatical) { |
||
400 | case 'МР': |
||
401 | case 'ЖР': |
||
402 | case 'СР': |
||
403 | if (empty($checkGrammar)) { |
||
404 | |||
405 | $checkGrammar = ($word === $this->getPhpMorphy()->castFormByGramInfo($word, $param['TypeSpeech'], [$param['param'], 'ИМ', $grammatical], true)[0]) ? $grammatical : null; |
||
406 | } |
||
407 | if ($checkGrammar) { |
||
408 | continue; |
||
409 | } |
||
410 | |||
411 | } |
||
412 | } |
||
413 | } |
||
414 | |||
415 | return $checkGrammar; |
||
416 | |||
417 | } |
||
418 | |||
419 | /** |
||
420 | * @param phpMorphy $phpMorphy |
||
421 | */ |
||
422 | private function setPhpMorphy($phpMorphy) { |
||
423 | |||
424 | $this->phpMorphy = $phpMorphy; |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * @return phpMorphy |
||
429 | */ |
||
430 | private function getPhpMorphy() { |
||
431 | |||
432 | return $this->phpMorphy; |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * @param string $string |
||
437 | * @param integer $part |
||
438 | * @param bool $ucFirst |
||
439 | * @return array |
||
440 | */ |
||
441 | public function getMorph($string, $part, $ucFirst = false) { |
||
442 | |||
443 | if (array_key_exists(--$part, $this->morph[$string])) { |
||
444 | $string = mb_strtolower($this->morph[$string][$part]); |
||
445 | } |
||
446 | return $ucFirst ? mb_strtoupper(mb_substr($string, 0, 1)) . mb_substr($string, 1) : $string; |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @param string $string |
||
451 | * @param array $morph |
||
452 | */ |
||
453 | public function setMorph($string, $morph) { |
||
454 | |||
455 | $this->morph[$string] = $morph ?: $string; |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * @param string $string |
||
460 | * @return string |
||
461 | */ |
||
462 | protected function transliteration($string = '') { |
||
463 | |||
464 | CI::$APP->load->helper('translit'); |
||
465 | |||
466 | return translit($string); |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * @return string |
||
471 | */ |
||
472 | public function getBrand() { |
||
473 | |||
474 | return $this->brand; |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * @param string $brand |
||
479 | */ |
||
480 | public function setBrand($brand) { |
||
481 | |||
482 | $this->brand = $brand; |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * @return string |
||
487 | */ |
||
488 | public function getCS() { |
||
489 | |||
490 | if (!$this->CS) { |
||
491 | $this->setCS(Currency::create()->getSymbol()); |
||
492 | } |
||
493 | return $this->CS; |
||
494 | } |
||
495 | |||
496 | /** |
||
497 | * @param string $CS |
||
498 | */ |
||
499 | public function setCS($CS) { |
||
500 | |||
501 | $this->CS = $CS; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * @return string |
||
506 | */ |
||
507 | public function getCategory() { |
||
508 | |||
509 | return $this->category; |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * @param string $category |
||
514 | */ |
||
515 | public function setCategory($category) { |
||
516 | |||
517 | $this->category = $category; |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * @return int |
||
522 | */ |
||
523 | public function getDescLength() { |
||
524 | return $this->descLength; |
||
525 | } |
||
526 | |||
527 | /** |
||
528 | * @param int $descLength |
||
529 | */ |
||
530 | public function setDescLength($descLength) { |
||
531 | |||
532 | if ($descLength === '') { |
||
533 | $this->descLength = 100; |
||
534 | } elseif ((int) $descLength >= 0) { |
||
535 | $this->descLength = (int) $descLength; |
||
536 | } |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * @return string |
||
541 | */ |
||
542 | public function getDescription() { |
||
543 | |||
544 | return $this->description; |
||
545 | } |
||
546 | |||
547 | /** |
||
548 | * @param string $description |
||
549 | */ |
||
550 | public function setDescription($description) { |
||
551 | |||
552 | $description = strip_tags($description); |
||
553 | $description = str_replace([PHP_EOL, ' '], ' ', $description); |
||
554 | $description = rtrim($description, '!,.-:; '); |
||
555 | if (mb_strlen($description) > $this->getDescLength()) { |
||
556 | $description = mb_substr($description, 0, $this->getDescLength()); |
||
557 | $description = mb_substr($description, 0, mb_strrpos($description, ' ')); |
||
558 | } |
||
559 | |||
560 | $this->description = $description; |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * @return array|null|SBrands|SCategory|SProducts |
||
565 | */ |
||
566 | public function getModel() { |
||
567 | |||
568 | return $this->model; |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * @param array|null|SBrands|SCategory|SProducts $model |
||
573 | */ |
||
574 | public function setModel($model) { |
||
575 | |||
576 | $this->model = $model; |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * @return string |
||
581 | */ |
||
582 | public function getID() { |
||
583 | |||
584 | return $this->ID; |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * @param string $ID |
||
589 | */ |
||
590 | public function setID($ID) { |
||
594 | |||
595 | /** |
||
596 | * @return string |
||
597 | */ |
||
598 | public function getMetaDescription() { |
||
602 | |||
603 | /** |
||
604 | * @param string $metaDescription |
||
605 | */ |
||
606 | public function setMetaDescription($metaDescription) { |
||
610 | |||
611 | /** |
||
612 | * @return string |
||
613 | */ |
||
614 | public function getMetaH1() { |
||
618 | |||
619 | /** |
||
620 | * @param string $metaH1 |
||
621 | */ |
||
622 | public function setMetaH1($metaH1) { |
||
626 | |||
627 | /** |
||
628 | * @return string |
||
629 | */ |
||
630 | public function getMetaKeywords() { |
||
634 | |||
635 | /** |
||
636 | * @param string $metaKeywords |
||
637 | */ |
||
638 | public function setMetaKeywords($metaKeywords) { |
||
642 | |||
643 | /** |
||
644 | * @return string |
||
645 | */ |
||
646 | public function getMetaTitle() { |
||
650 | |||
651 | /** |
||
652 | * @param string $metaTitle |
||
653 | */ |
||
654 | public function setMetaTitle($metaTitle) { |
||
658 | |||
659 | /** |
||
660 | * @return string |
||
661 | */ |
||
662 | public function getName() { |
||
663 | |||
664 | return $this->name; |
||
665 | } |
||
666 | |||
667 | /** |
||
668 | * @param string $name |
||
669 | */ |
||
670 | public function setName($name) { |
||
674 | |||
675 | /** |
||
676 | * @return string |
||
677 | */ |
||
678 | public function getPageNumber() { |
||
682 | |||
683 | /** |
||
684 | * @param string $pageNumber |
||
685 | */ |
||
686 | public function setPageNumber($pageNumber) { |
||
690 | |||
691 | /** |
||
692 | * @return string |
||
693 | */ |
||
694 | public function getNumber() { |
||
701 | |||
702 | /** |
||
703 | * @param string $number |
||
704 | */ |
||
705 | public function setNumber($number) { |
||
709 | |||
710 | /** |
||
711 | * @return string |
||
712 | */ |
||
713 | public function getPrice() { |
||
717 | |||
718 | /** |
||
719 | * @param string $price |
||
720 | */ |
||
721 | public function setPrice($price) { |
||
725 | |||
726 | /** |
||
727 | * @return array<String> |
||
728 | */ |
||
729 | public function render() { |
||
765 | |||
766 | /** |
||
767 | * @return string |
||
768 | */ |
||
769 | public function getWrapper() { |
||
773 | |||
774 | /** |
||
775 | * @param string $wrapper |
||
776 | */ |
||
777 | public function setWrapper($wrapper) { |
||
781 | |||
782 | /** |
||
783 | * @param string $key |
||
784 | * @return bool|array |
||
785 | */ |
||
786 | public function getMatching($key) { |
||
790 | |||
791 | /** |
||
792 | * @param string $key |
||
793 | * @param string $value |
||
794 | */ |
||
795 | public function setMatching($key, $value) { |
||
799 | |||
800 | /** |
||
801 | * @return array |
||
802 | */ |
||
803 | public function getMetaArray() { |
||
807 | |||
808 | /** |
||
809 | * @param array $metaArray |
||
810 | */ |
||
811 | public function setMetaArray($metaArray) { |
||
819 | |||
820 | } |