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 |
||
| 29 | abstract class AbstractStringCheck implements ICheck { |
||
| 30 | |||
| 31 | /** @var array[] Nested array: [Pattern => [ActualValue => Regex Result]] */ |
||
| 32 | protected $matches; |
||
| 33 | |||
| 34 | /** @var IL10N */ |
||
| 35 | protected $l; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @param IL10N $l |
||
| 39 | */ |
||
| 40 | public function __construct(IL10N $l) { |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param IStorage $storage |
||
| 46 | * @param string $path |
||
| 47 | */ |
||
| 48 | public function setFileInfo(IStorage $storage, $path) { |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | abstract protected function getActualValue(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param string $operator |
||
| 59 | * @param string $value |
||
| 60 | * @return bool |
||
| 61 | */ |
||
| 62 | public function executeCheck($operator, $value) { |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param string $operator |
||
| 69 | * @param string $checkValue |
||
| 70 | * @param string $actualValue |
||
| 71 | * @return bool |
||
| 72 | */ |
||
| 73 | protected function executeStringCheck($operator, $checkValue, $actualValue) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $operator |
||
| 90 | * @param string $value |
||
| 91 | * @throws \UnexpectedValueException |
||
| 92 | */ |
||
| 93 | public function validateCheck($operator, $value) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $pattern |
||
| 106 | * @param string $subject |
||
| 107 | * @return int|bool |
||
| 108 | */ |
||
| 109 | protected function match($pattern, $subject) { |
||
| 121 | } |
||
| 122 |
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.