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 |
||
37 | class JsonApiService implements JsonApiServiceInterface |
||
38 | { |
||
39 | /** |
||
40 | * @var FactoryInterface |
||
41 | */ |
||
42 | protected $factory; |
||
43 | |||
44 | /** |
||
45 | * @var JsonApiRegistryInterface |
||
46 | */ |
||
47 | protected $registry; |
||
48 | |||
49 | /** |
||
50 | * @var ContainerInterface |
||
51 | */ |
||
52 | protected $schemas; |
||
53 | |||
54 | /** |
||
55 | * @var ValidationServiceInterface |
||
56 | */ |
||
57 | protected $validator; |
||
58 | |||
59 | /** |
||
60 | * @var DataParserInterface |
||
61 | */ |
||
62 | protected $parser; |
||
63 | |||
64 | /** |
||
65 | * Constructor |
||
66 | * |
||
67 | * @param FactoryInterface $factory |
||
68 | * @param JsonApiRegistryInterface $registry |
||
69 | * @param ContainerInterface $schemas |
||
70 | * @param DataParserInterface $parser |
||
71 | * @param ValidationServiceInterface $validator |
||
72 | */ |
||
73 | public function __construct( |
||
86 | |||
87 | /** |
||
88 | * @inheritdoc |
||
89 | */ |
||
90 | public function getFactory() |
||
94 | |||
95 | /** |
||
96 | * @inheritdoc |
||
97 | */ |
||
98 | public function parseRequest(Request $request, EnvironmentInterface $environment = null) |
||
117 | |||
118 | /** |
||
119 | * @inheritdoc |
||
120 | */ |
||
121 | public function validateRequest(RequestInterface $request) |
||
159 | |||
160 | /** |
||
161 | * @inheritdoc |
||
162 | */ |
||
163 | public function getResponseFactory(RequestInterface $request) |
||
167 | |||
168 | /** |
||
169 | * Returns JSON API environment configured in request |
||
170 | * |
||
171 | * @param Request $request |
||
172 | * @return EnvironmentInterface |
||
173 | */ |
||
174 | protected function getRequestEnvironment(Request $request) |
||
190 | |||
191 | /** |
||
192 | * Initialize JSON API environment for specified request |
||
193 | * |
||
194 | * @param EnvironmentInterface $environment |
||
195 | * @param Request $request |
||
196 | */ |
||
197 | private function initializeEnvironment(EnvironmentInterface $environment, Request $request) |
||
208 | |||
209 | /** |
||
210 | * Create codec matcher for specified environment |
||
211 | * |
||
212 | * @param EnvironmentInterface $environment |
||
213 | * @return \Neomerx\JsonApi\Contracts\Codec\CodecMatcherInterface |
||
214 | */ |
||
215 | private function createMatcher(EnvironmentInterface $environment) |
||
240 | |||
241 | /** |
||
242 | * Convert media type string to media type object |
||
243 | * |
||
244 | * @param string $type |
||
245 | * @return \Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface |
||
246 | */ |
||
247 | private function parseMediaTypeString($type) |
||
256 | |||
257 | /** |
||
258 | * Parse request headers and detect appropriate decoder and encoder |
||
259 | * |
||
260 | * @param Request $request |
||
261 | * @param CodecMatcherInterface $matcher |
||
262 | */ |
||
263 | private function parseRequestHeaders(Request $request, CodecMatcherInterface $matcher) |
||
271 | |||
272 | /** |
||
273 | * Create PSR7 request from symfony http foundation request |
||
274 | * |
||
275 | * @param Request $request |
||
276 | * @return Psr7Request |
||
277 | */ |
||
278 | private function createPsr7Request(Request $request) |
||
292 | |||
293 | /** |
||
294 | * Parse request query parameters |
||
295 | * |
||
296 | * @param Request $request |
||
297 | * @param EnvironmentInterface $environment |
||
298 | * @return \Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface|null |
||
299 | */ |
||
300 | private function parseQuery(Request $request, EnvironmentInterface $environment) |
||
315 | |||
316 | /** |
||
317 | * Parse request body |
||
318 | * |
||
319 | * @param Request $request |
||
320 | * @param EnvironmentInterface $environment |
||
321 | * @return mixed|null |
||
322 | */ |
||
323 | private function parseBody(Request $request, EnvironmentInterface $environment) |
||
336 | } |
||
337 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.