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 |
||
| 15 | class TableFormatter implements FormatterInterface, ConfigureInterface, ValidationInterface |
||
| 16 | { |
||
| 17 | protected $fieldLabels; |
||
| 18 | protected $defaultFields; |
||
| 19 | protected $tableStyle; |
||
| 20 | |||
| 21 | 7 | public function __construct() |
|
| 25 | |||
| 26 | /** |
||
| 27 | * @inheritdoc |
||
| 28 | */ |
||
| 29 | 7 | public function configure($configurationData) |
|
| 30 | { |
||
| 31 | 7 | if (isset($configurationData['table-style'])) { |
|
| 32 | 1 | $this->tableStyle = $configurationData['table-style']; |
|
| 33 | 1 | } |
|
| 34 | 7 | } |
|
| 35 | |||
| 36 | 7 | View Code Duplication | public function validate($structuredData) |
|
|
|||
| 37 | { |
||
| 38 | // If the provided data was of class RowsOfFields |
||
| 39 | // or AssociativeList, it will be converted into |
||
| 40 | // a TableTransformation object by the restructure call. |
||
| 41 | 7 | if (!$structuredData instanceof TableDataInterface) { |
|
| 42 | 2 | throw new IncompatibleDataException( |
|
| 43 | 2 | $this, |
|
| 44 | 2 | $structuredData, |
|
| 45 | [ |
||
| 46 | 2 | new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields'), |
|
| 47 | 2 | new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\AssociativeList'), |
|
| 48 | ] |
||
| 49 | 2 | ); |
|
| 50 | } |
||
| 51 | 5 | return $structuredData; |
|
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @inheritdoc |
||
| 56 | */ |
||
| 57 | 5 | public function write(OutputInterface $output, $tableTransformer, $options = []) |
|
| 75 | } |
||
| 76 |
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.