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 |
||
27 | class HeadersChecker implements HeadersCheckerInterface |
||
28 | { |
||
29 | const INVALID_CONTENT_TYPE_ERROR = 'ebb239c5-221d-42d2-a2f7-ca4bbc3d476f'; |
||
30 | const UNSUPPORTED_CONTENT_TYPE_ERROR = 'aa389b54-1b33-4b0f-a5f4-1d680ca5f6d3'; |
||
31 | const UNSUPPORTED_ACCEPT_ERROR = '6df3b139-d9ef-4576-8d0a-c0ce2e803827'; |
||
32 | /** |
||
33 | * @var CodecMatcherInterface |
||
34 | */ |
||
35 | protected $matcher; |
||
36 | |||
37 | /** |
||
38 | * Constructor |
||
39 | * |
||
40 | * @param CodecMatcherInterface $matcher |
||
41 | */ |
||
42 | 5 | public function __construct(CodecMatcherInterface $matcher) |
|
46 | |||
47 | /** |
||
48 | * @inheritdoc |
||
49 | */ |
||
50 | 4 | public function checkHeaders(HeaderParametersInterface $parameters) |
|
56 | |||
57 | /** |
||
58 | * Check content type header |
||
59 | * |
||
60 | * @param HeaderInterface $header |
||
61 | */ |
||
62 | 4 | private function checkContentTypeHeader(HeaderInterface $header) |
|
91 | |||
92 | /** |
||
93 | * Check accept header |
||
94 | * |
||
95 | * @param AcceptHeaderInterface $header |
||
96 | * @return Error |
||
97 | */ |
||
98 | 2 | private function checkAcceptHeader(AcceptHeaderInterface $header) |
|
115 | } |
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.