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 | View Code Duplication | class ByHashUploadFileHandler |
|
|
|||
30 | { |
||
31 | /** |
||
32 | * The file factory. |
||
33 | * |
||
34 | * @var FileFactory |
||
35 | */ |
||
36 | private $factory; |
||
37 | |||
38 | /** |
||
39 | * The filesystem. |
||
40 | * |
||
41 | * @var Filesystem |
||
42 | */ |
||
43 | private $filesystem; |
||
44 | |||
45 | /** |
||
46 | * The file repository. |
||
47 | * |
||
48 | * @var FileRepository |
||
49 | */ |
||
50 | private $repository; |
||
51 | |||
52 | /** |
||
53 | * Constructor. |
||
54 | * |
||
55 | * @param Filesystem $filesystem The filesystem |
||
56 | * @param FileRepository $aRepository The file repository |
||
57 | * @param FileFactory $aFactory The file factory |
||
58 | */ |
||
59 | public function __construct(Filesystem $filesystem, FileRepository $aRepository, FileFactory $aFactory) |
||
65 | |||
66 | /** |
||
67 | * Handles the given command. |
||
68 | * |
||
69 | * @param UploadFileCommand $aCommand The command |
||
70 | * |
||
71 | * @throws FileAlreadyExistsException when file is already exists |
||
72 | */ |
||
73 | public function __invoke(UploadFileCommand $aCommand) |
||
90 | } |
||
91 |
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.