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 |
||
| 22 | View Code Duplication | final class GetXmlViewModel implements TemplateViewModelInterface |
|
|
|
|||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var PaginatorInterface [string, string, string, DateTimeInterface, string, string[]] |
||
| 26 | */ |
||
| 27 | private $postList; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The view model constructor depends on the most raw elements possible. |
||
| 31 | * In this case the view model constructor is private, so that we force the usage of the named constructor. |
||
| 32 | * We want this because it is not possible to have the a constructor with the raw data, since it is a list of data. |
||
| 33 | */ |
||
| 34 | private function __construct(PaginatorInterface $postList) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * We create named constructors for the cases where we need to extract the raw data from complex data structures. |
||
| 41 | */ |
||
| 42 | public static function fromPostDtoList( |
||
| 65 | |||
| 66 | public function getPostList(): PaginatorInterface |
||
| 70 | } |
||
| 71 |
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.