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 |
||
| 11 | class ImporterPrecondition |
||
| 12 | { |
||
| 13 | private $filenames = array(); |
||
| 14 | private $formats = array(); |
||
| 15 | private $fieldcount = null; |
||
| 16 | private $anyfields = array(); |
||
| 17 | private $fieldset = null; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @return ImporterPrecondition |
||
| 21 | */ |
||
| 22 | 3 | public function filename($pattern) |
|
| 28 | |||
| 29 | /** |
||
| 30 | * @return ImporterPrecondition |
||
| 31 | */ |
||
| 32 | 3 | public function format($id) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Fieldset must have this number of fields. |
||
| 41 | * |
||
| 42 | * @return ImporterPrecondition |
||
| 43 | */ |
||
| 44 | 1 | public function fieldcount($count) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * Fieldset must have field with this name, anywhere in fieldset. |
||
| 53 | * |
||
| 54 | * @return ImporterPrecondition |
||
| 55 | */ |
||
| 56 | 2 | public function field($fieldname) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Add required fields, must exist in the given order. |
||
| 65 | * |
||
| 66 | * @return ImporterPrecondition |
||
| 67 | */ |
||
| 68 | 2 | public function fieldset(array $fieldset) |
|
| 74 | |||
| 75 | 11 | public function isSatisfiedBy(StorageInterface $storage, LoggerInterface $logger = null) |
|
| 103 | |||
| 104 | 10 | View Code Duplication | private function isSatisfiedFilename($filename, LoggerInterface $logger = null) |
| 122 | |||
| 123 | 9 | View Code Duplication | private function isSatisfiedFormat(Format $format, LoggerInterface $logger = null) |
| 141 | |||
| 142 | 8 | private function isSatisfiedFieldcount($fieldCount, LoggerInterface $logger = null) |
|
| 158 | |||
| 159 | 8 | private function isSatisfiedAnyFields(array $fields, LoggerInterface $logger = null) |
|
| 160 | { |
||
| 161 | 8 | if (empty($this->anyfields)) { |
|
| 162 | 6 | return true; |
|
| 163 | } |
||
| 164 | |||
| 165 | 2 | $fields = array_map('strtolower', $fields); |
|
| 166 | 2 | $fields = array_map('trim', $fields); |
|
| 167 | |||
| 168 | 2 | foreach ($this->anyfields as $anyField) { |
|
| 169 | 2 | if (!in_array($anyField, $fields)) { |
|
| 170 | 1 | if ($logger) { |
|
| 171 | $logger->debug("Storage does not meet Preconditions due to fields restriction. Missing field: '$anyField'"); |
||
| 172 | } |
||
| 173 | |||
| 174 | 1 | return false; |
|
| 175 | } |
||
| 176 | 1 | } |
|
| 177 | |||
| 178 | 1 | return true; |
|
| 179 | } |
||
| 180 | |||
| 181 | 7 | private function isSatisfiedFieldset(array $fieldset, LoggerInterface $logger = null) |
|
| 197 | } |
||
| 198 |
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.