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 | final class Psr17FactoryDiscovery extends ClassDiscovery |
||
19 | { |
||
20 | private static function createException($type, Exception $e) |
||
28 | |||
29 | /** |
||
30 | * @return RequestFactoryInterface |
||
31 | * |
||
32 | * @throws Exception\NotFoundException |
||
33 | */ |
||
34 | View Code Duplication | public static function findRequestFactory() |
|
|
|||
35 | { |
||
36 | try { |
||
37 | $messageFactory = static::findOneByType(RequestFactoryInterface::class); |
||
38 | } catch (DiscoveryFailedException $e) { |
||
39 | throw self::createException('request factory', $e); |
||
40 | } |
||
41 | |||
42 | return static::instantiateClass($messageFactory); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @return ResponseFactoryInterface |
||
47 | * |
||
48 | * @throws Exception\NotFoundException |
||
49 | */ |
||
50 | View Code Duplication | public static function findResponseFactory() |
|
60 | |||
61 | /** |
||
62 | * @return ServerRequestFactoryInterface |
||
63 | * |
||
64 | * @throws Exception\NotFoundException |
||
65 | */ |
||
66 | View Code Duplication | public static function findServerRequestFactory() |
|
76 | |||
77 | /** |
||
78 | * @return StreamFactoryInterface |
||
79 | * |
||
80 | * @throws Exception\NotFoundException |
||
81 | */ |
||
82 | View Code Duplication | public static function findStreamFactory() |
|
92 | |||
93 | /** |
||
94 | * @return UploadedFileFactoryInterface |
||
95 | * |
||
96 | * @throws Exception\NotFoundException |
||
97 | */ |
||
98 | View Code Duplication | public static function findUploadedFileFactory() |
|
108 | |||
109 | /** |
||
110 | * @return UriFactoryInterface |
||
111 | * |
||
112 | * @throws Exception\NotFoundException |
||
113 | */ |
||
114 | View Code Duplication | public static function findUriFactory() |
|
124 | |||
125 | /** |
||
126 | * @return UriFactoryInterface |
||
127 | * |
||
128 | * @throws Exception\NotFoundException |
||
129 | * |
||
130 | * @deprecated This will be removed in 2.0. Consider using the findUrlFactory() method. |
||
131 | */ |
||
132 | public static function findUrlFactory() |
||
136 | } |
||
137 |
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.