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 | trait Asserts |
||
26 | { |
||
27 | /** |
||
28 | * Assert request matches against declared specification. |
||
29 | * |
||
30 | * The list of constraints: |
||
31 | * |
||
32 | * - Assert request method defined |
||
33 | * - Assert request URI declared by host, basePath, schemes and parameters (path, query) |
||
34 | * - Assert content-type declared by consumes |
||
35 | * - Assert headers declared by parameters (header) |
||
36 | * - Assert body declared by parameters (body) |
||
37 | */ |
||
38 | 4 | final protected static function assertRequest(Schema $schema, string $path, RequestInterface $request, string $msg = ''): void |
|
45 | |||
46 | /** |
||
47 | * Assert response matches against declared specification. |
||
48 | * |
||
49 | * The list of constraints: |
||
50 | * |
||
51 | * - Assert response status code or default is defined |
||
52 | * - Assert content-type declared by produces from operation |
||
53 | * - Assert headers |
||
54 | * - Assert body |
||
55 | */ |
||
56 | 3 | final protected static function assertResponse(Schema $schema, string $path, string $method, ResponseInterface $response, string $msg = ''): void |
|
76 | |||
77 | /** |
||
78 | * Assert URI matches against declared host, basePath, schemes and parameters (path, query). |
||
79 | * |
||
80 | * The list of constraints: |
||
81 | * |
||
82 | * - Assert URI scheme matches allowed schemes |
||
83 | * - Assert URI host matches defined |
||
84 | * - Assert URI path starts with defined base path |
||
85 | * - Assert URI path matches defined template and path parameters |
||
86 | * - Assert URI path matches defined query parameters |
||
87 | */ |
||
88 | 3 | final protected static function assertUri(Schema $schema, string $path, string $method, UriInterface $uri, string $msg = ''): void |
|
101 | |||
102 | /** |
||
103 | * Assert the endpoint supports given operation. |
||
104 | */ |
||
105 | 4 | final protected static function assertMethodAllowed(Schema $schema, string $path, string $method, string $msg = ''): void |
|
113 | |||
114 | /** |
||
115 | * Assert the response status code defined. |
||
116 | */ |
||
117 | 3 | final protected static function assertResponseDefined(Schema $schema, string $template, string $method, string $status, string $msg = ''): void |
|
124 | |||
125 | /** |
||
126 | * Assert the endpoint supports given operation. |
||
127 | */ |
||
128 | 2 | final protected static function assertRequestContentType(Schema $schema, string $path, string $method, string $contentType, string $msg = ''): void |
|
136 | |||
137 | /** |
||
138 | * Assert the endpoint supports given operation. |
||
139 | */ |
||
140 | 2 | final protected static function assertResponseContentType(Schema $schema, string $path, string $method, string $status, string $contentType, string $msg = ''): void |
|
148 | |||
149 | 3 | final protected static function assertRequestHeaders(Schema $schema, string $path, string $method, array $headers, string $msg = ''): void |
|
167 | |||
168 | 3 | final protected static function assertResponseHeaders(Schema $schema, string $path, string $method, string $status, array $headers, string $msg = ''): void |
|
201 | |||
202 | 2 | View Code Duplication | final protected static function assertRequestBody(Schema $schema, string $path, string $method, StreamInterface $body = null, string $msg = ''): void |
216 | |||
217 | 3 | View Code Duplication | final protected static function assertResponseBody(Schema $schema, string $path, string $method, string $status, StreamInterface $body = null, string $msg = ''): void |
231 | |||
232 | 1 | final protected static function assertDefinitionSchema(Schema $schema, string $class, stdClass $actual, string $msg = ''): void |
|
240 | } |
||
241 |
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.