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 | class AssetFactory |
||
23 | { |
||
24 | /** |
||
25 | * Creates an Asset from a file path |
||
26 | * |
||
27 | * @param string $path |
||
28 | * @throws Exception\FactoryException If the file could not be loaded |
||
29 | * @return Asset |
||
30 | */ |
||
31 | 10 | public static function createFromPath($path) |
|
39 | |||
40 | /** |
||
41 | * Creates an Asset from a \SplFileInfo wrapper |
||
42 | * |
||
43 | * @param \SplFileInfo $file |
||
44 | * @throws Exception\FactoryException If the file could not be loaded |
||
45 | * @return Asset |
||
46 | */ |
||
47 | 10 | View Code Duplication | public static function createFromSplFileInfo(\SplFileInfo $file) |
60 | |||
61 | /** |
||
62 | * Creates an Asset from an UploadedFile instance |
||
63 | * |
||
64 | * @param UploadedFile $file |
||
65 | * @throws Exception\FactoryException If the file could not be loaded |
||
66 | * @return Asset |
||
67 | */ |
||
68 | 1 | View Code Duplication | public static function createFromUploadedFile(UploadedFile $file) |
82 | |||
83 | /** |
||
84 | * Creates an Asset from a URI |
||
85 | * |
||
86 | * @param string $uri |
||
87 | * @throws Exception\FactoryException If the file could not be loaded |
||
88 | * @return Asset |
||
89 | */ |
||
90 | public static function createFromUri($uri) |
||
97 | } |
||
98 |
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.