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 |
||
25 | class CentrifugoChecker |
||
26 | { |
||
27 | /** @var int */ |
||
28 | private $channelMaxLength; |
||
29 | |||
30 | /** |
||
31 | * @param int $centrifugoChannelMaxLength |
||
32 | */ |
||
33 | public function __construct(int $centrifugoChannelMaxLength) |
||
37 | |||
38 | /** |
||
39 | * @param string $channelName |
||
40 | * |
||
41 | * @throws InvalidArgumentException |
||
42 | */ |
||
43 | public function assertValidChannelName(string $channelName): void |
||
53 | |||
54 | /** |
||
55 | * @param ResponseInterface $response |
||
56 | * |
||
57 | * @throws CentrifugoException |
||
58 | */ |
||
59 | public function assertValidResponseStatusCode(ResponseInterface $response): void |
||
65 | |||
66 | /** |
||
67 | * @param ResponseInterface $response |
||
68 | * |
||
69 | * @throws CentrifugoException |
||
70 | */ |
||
71 | View Code Duplication | public function assertValidResponseHeaders(ResponseInterface $response): void |
|
|
|||
72 | { |
||
73 | $headers = $response->getHeaders(false); |
||
74 | |||
75 | if (!isset($headers['content-type'])) { |
||
76 | throw new CentrifugoException('Missing "content-type" header in Centrifugo response'); |
||
77 | } |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param ResponseInterface $response |
||
82 | * |
||
83 | * @throws CentrifugoException |
||
84 | */ |
||
85 | View Code Duplication | public function assertValidResponseContentType(ResponseInterface $response): void |
|
93 | } |
||
94 |
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.