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 namespace Neomerx\JsonApi\Schema; |
||
31 | abstract class SchemaProvider implements SchemaProviderInterface |
||
32 | { |
||
33 | /** Links information */ |
||
34 | const LINKS = DocumentInterface::KEYWORD_LINKS; |
||
35 | |||
36 | /** Linked data key. */ |
||
37 | const DATA = DocumentInterface::KEYWORD_DATA; |
||
38 | |||
39 | /** Relationship meta */ |
||
40 | const META = DocumentInterface::KEYWORD_META; |
||
41 | |||
42 | /** If 'self' URL should be shown. */ |
||
43 | const SHOW_SELF = 'showSelf'; |
||
44 | |||
45 | /** If 'related' URL should be shown. */ |
||
46 | const SHOW_RELATED = 'related'; |
||
47 | |||
48 | /** If data should be shown in relationships. */ |
||
49 | const SHOW_DATA = 'showData'; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $resourceType; |
||
55 | |||
56 | /** |
||
57 | * @var string Must end with '/' |
||
58 | */ |
||
59 | protected $selfSubUrl; |
||
60 | |||
61 | /** |
||
62 | * @var bool |
||
63 | */ |
||
64 | protected $isShowAttributesInIncluded = true; |
||
65 | |||
66 | /** |
||
67 | * @var SchemaFactoryInterface |
||
68 | */ |
||
69 | private $factory; |
||
70 | |||
71 | /** |
||
72 | * @var ContainerInterface |
||
73 | */ |
||
74 | private $container; |
||
75 | |||
76 | /** |
||
77 | * @param SchemaFactoryInterface $factory |
||
78 | * @param ContainerInterface $container |
||
79 | */ |
||
80 | 58 | public function __construct(SchemaFactoryInterface $factory, ContainerInterface $container) |
|
105 | |||
106 | /** |
||
107 | * @inheritdoc |
||
108 | */ |
||
109 | 53 | public function getResourceType() |
|
113 | |||
114 | /** |
||
115 | * @inheritdoc |
||
116 | */ |
||
117 | 1 | public function getSelfSubUrl() |
|
121 | |||
122 | /** |
||
123 | * @inheritdoc |
||
124 | */ |
||
125 | 45 | public function getSelfSubLink($resource) |
|
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | 49 | public function getPrimaryMeta($resource) |
|
137 | |||
138 | /** |
||
139 | * @inheritdoc |
||
140 | */ |
||
141 | 24 | public function getLinkageMeta($resource) |
|
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | 14 | public function getInclusionMeta($resource) |
|
153 | |||
154 | /** |
||
155 | * @inheritdoc |
||
156 | */ |
||
157 | 28 | public function getRelationshipsPrimaryMeta($resource) |
|
161 | |||
162 | /** |
||
163 | * @inheritdoc |
||
164 | */ |
||
165 | 12 | public function getRelationshipsInclusionMeta($resource) |
|
169 | |||
170 | /** |
||
171 | * @inheritdoc |
||
172 | */ |
||
173 | 14 | public function isShowAttributesInIncluded() |
|
177 | |||
178 | /** |
||
179 | * Get resource links. |
||
180 | * |
||
181 | * @param object $resource |
||
182 | * @param array $includeRelationships A list of relationships that will be included as full resources. |
||
183 | * |
||
184 | * @return array |
||
185 | */ |
||
186 | 6 | public function getRelationships($resource, array $includeRelationships = []) |
|
193 | |||
194 | /** |
||
195 | * @inheritdoc |
||
196 | */ |
||
197 | 47 | public function createResourceObject($resource, $isOriginallyArrayed, $attributeKeysFilter = null) |
|
201 | |||
202 | /** |
||
203 | * @inheritdoc |
||
204 | */ |
||
205 | 47 | public function getRelationshipObjectIterator($resource, array $includeRelationships) |
|
211 | |||
212 | /** |
||
213 | * @inheritdoc |
||
214 | */ |
||
215 | 26 | public function getIncludePaths() |
|
219 | |||
220 | /** |
||
221 | * @inheritdoc |
||
222 | */ |
||
223 | 43 | public function getResourceLinks($resource) |
|
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | 12 | public function getIncludedResourceLinks($resource) |
|
239 | |||
240 | /** |
||
241 | * @param string $relationshipName |
||
242 | * @param array $description |
||
243 | * @param bool $isShowSelf |
||
244 | * @param bool $isShowRelated |
||
245 | * |
||
246 | * @return array <string,LinkInterface> |
||
247 | */ |
||
248 | 36 | protected function readLinks($relationshipName, array $description, $isShowSelf, $isShowRelated) |
|
262 | |||
263 | /** |
||
264 | * @param string $name |
||
265 | * @param array $desc |
||
266 | * |
||
267 | * @return RelationshipObjectInterface |
||
268 | */ |
||
269 | 36 | protected function createRelationshipObject($name, array $desc) |
|
280 | |||
281 | /** |
||
282 | * @param array $array |
||
283 | * @param string $key |
||
284 | * @param mixed $default |
||
285 | * |
||
286 | * @return mixed |
||
287 | */ |
||
288 | 36 | private function getValue(array $array, $key, $default = null) |
|
292 | } |
||
293 |
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.