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 |
||
| 18 | class ExtRefTransformer implements DataTransformerInterface |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var ExtReferenceConverterInterface |
||
| 22 | */ |
||
| 23 | private $converter; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Constructor |
||
| 27 | * |
||
| 28 | * @param ExtReferenceConverterInterface $converter Ext reference converter |
||
| 29 | */ |
||
| 30 | public function __construct(ExtReferenceConverterInterface $converter) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Transforms an object (extref) to a string (url) |
||
| 37 | * |
||
| 38 | * @param ExtReference|null $extref extref object |
||
| 39 | * @return string |
||
| 40 | */ |
||
| 41 | View Code Duplication | public function transform($extref) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * Transforms a string (url) to an object (extref) |
||
| 60 | * |
||
| 61 | * @param string $url extref url |
||
| 62 | * @return ExtReference|null |
||
| 63 | * @throws TransformationFailedException if url is not valid |
||
| 64 | */ |
||
| 65 | View Code Duplication | public function reverseTransform($url) |
|
| 81 | } |
||
| 82 |
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.