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:
| 1 | <?php |
||
| 30 | abstract class TestableInput |
||
| 31 | { |
||
| 32 | protected $tester; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var string $title |
||
| 36 | */ |
||
| 37 | protected $title; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string $auxName |
||
| 41 | */ |
||
| 42 | protected $auxName; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string $selector |
||
| 46 | */ |
||
| 47 | protected $selector; |
||
| 48 | |||
| 49 | const AS_BASE = 'div.advanced-search '; |
||
| 50 | |||
| 51 | const TF_BASE = 'tr.filters '; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * TestableInput constructor. |
||
| 55 | * @param AcceptanceTester $tester |
||
| 56 | * @param $selector |
||
| 57 | */ |
||
| 58 | public function __construct(AcceptanceTester $tester, $selector) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param AcceptanceTester $tester |
||
| 66 | * @param $title |
||
| 67 | * @return TestableInput |
||
| 68 | */ |
||
| 69 | View Code Duplication | public static function asAdvancedSearch(AcceptanceTester $tester, $title): TestableInput |
|
| 78 | |||
| 79 | /** |
||
| 80 | * @param AcceptanceTester $tester |
||
| 81 | * @param $name |
||
| 82 | * @return TestableInput |
||
| 83 | */ |
||
| 84 | View Code Duplication | public static function asTableFilter(AcceptanceTester $tester, $columnName): TestableInput |
|
| 92 | |||
| 93 | /** |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | abstract protected function getSearchSelector(): string; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | abstract protected function getFilterSelector(): string; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $value |
||
| 105 | */ |
||
| 106 | abstract public function setValue(string $value): void; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function getSelector(): string |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $name |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | private function computeAuxName(string $name): string |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Checks whether input is visible |
||
| 127 | */ |
||
| 128 | public function isVisible(): void |
||
| 132 | } |
||
| 133 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.