Total Complexity | 45 |
Total Lines | 285 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 0 | Features | 0 |
Complex classes like Json often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Json, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class Json extends JsonAbstract |
||
28 | { |
||
29 | private $isCollection = false; |
||
30 | private $meta = []; |
||
31 | |||
32 | /** |
||
33 | * @param $relations \Illuminate\Database\Eloquent\Collection |
||
34 | * @param string $entity |
||
35 | * @return array JSON API rels compatible array |
||
36 | */ |
||
37 | public static function getRelations($relations, string $entity): array |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Output errors in JSON API compatible format |
||
66 | * @param array $errors |
||
67 | * @param bool $return |
||
68 | * @return string |
||
69 | */ |
||
70 | public static function outputErrors(array $errors, bool $return = false) |
||
71 | { |
||
72 | $arr[JSONApiInterface::CONTENT_ERRORS] = []; |
||
73 | if (empty($errors) === false) { |
||
74 | $arr[JSONApiInterface::CONTENT_ERRORS] = $errors; |
||
75 | } |
||
76 | // errors and codes must be clear with readable json |
||
77 | $encoded = self::encode($arr, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); |
||
78 | if ($return === false && env('APP_ENV') !== 'dev') { |
||
79 | echo $encoded; |
||
80 | exit(JSONApiInterface::EXIT_STATUS_ERROR); |
||
81 | } |
||
82 | return $encoded; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * |
||
87 | * @param array $errors |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getErrors(array $errors): string |
||
91 | { |
||
92 | $arr[JSONApiInterface::CONTENT_ERRORS] = []; |
||
93 | if (empty($errors) === false) { |
||
94 | $arr[JSONApiInterface::CONTENT_ERRORS] = $errors; |
||
95 | } |
||
96 | |||
97 | return self::encode($arr, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Returns composition of relations |
||
102 | * |
||
103 | * @param Request $request |
||
104 | * @param array $data |
||
105 | * @return string |
||
106 | */ |
||
107 | public static function prepareSerializedRelations(Request $request, array $data): string |
||
108 | { |
||
109 | $arr[JSONApiInterface::CONTENT_LINKS] = [ |
||
110 | JSONApiInterface::CONTENT_SELF => $request->getUri(), |
||
111 | ]; |
||
112 | |||
113 | $arr[JSONApiInterface::CONTENT_DATA] = $data; |
||
114 | |||
115 | return self::encode($arr); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param BaseFormRequest $formRequest |
||
120 | * @param $model |
||
121 | * @param string $entity |
||
122 | * |
||
123 | * @return Collection|Item |
||
124 | */ |
||
125 | public function getResource(BaseFormRequest $formRequest, $model, string $entity) |
||
126 | { |
||
127 | $transformer = new DefaultTransformer($formRequest); |
||
128 | if ($this->isCollection === true) { |
||
129 | $collection = new Collection($model, $transformer, strtolower($entity)); |
||
130 | if (empty($this->meta) === false) { |
||
131 | $collection->setMeta($this->meta); |
||
132 | } |
||
133 | |||
134 | if ($model instanceof LengthAwarePaginator) { // only for paginator |
||
135 | $collection->setPaginator(new IlluminatePaginatorAdapter($model)); |
||
136 | } |
||
137 | |||
138 | return $collection; |
||
139 | } |
||
140 | |||
141 | $item = new Item($model, $transformer, strtolower($entity)); |
||
142 | $item->setMeta($this->meta); |
||
143 | |||
144 | return $item; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Prepares data to output in json-api format |
||
149 | * |
||
150 | * @param ResourceInterface $resource |
||
151 | * @param array $data |
||
152 | * @return string |
||
153 | */ |
||
154 | public static function prepareSerializedData(ResourceInterface $resource, $data = ModelsInterface::DEFAULT_DATA): string |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Gets data only with those elements of object/array that should be provided as output |
||
173 | * |
||
174 | * @param string $json |
||
175 | * @param array $data |
||
176 | * @return string |
||
177 | */ |
||
178 | private static function getSelectedData(string $json, array $data): string |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * Unsets objects from array that shouldn't be provided as output |
||
198 | * |
||
199 | * @param array &$json |
||
200 | * @param array $data |
||
201 | */ |
||
202 | private static function unsetArray(array &$json, array $data): void |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Unsets objects that shouldn't be provided as output |
||
227 | * |
||
228 | * @param array $json |
||
229 | * @param array $data |
||
230 | */ |
||
231 | private static function unsetObject(array &$json, array $data): void |
||
232 | { |
||
233 | $isDataAndAttrs = empty($json[JSONApiInterface::CONTENT_DATA]) === false |
||
234 | && empty($json[JSONApiInterface::CONTENT_DATA][JSONApiInterface::CONTENT_ATTRIBUTES]) === false; |
||
235 | |||
236 | if ($isDataAndAttrs) { |
||
237 | $attrsCase = ConfigHelper::getParam(ConfigInterface::ATTRIBUTES_CASE); |
||
238 | foreach ($json[JSONApiInterface::CONTENT_DATA][JSONApiInterface::CONTENT_ATTRIBUTES] as $k => $v) { |
||
239 | if (\in_array($k, $data, true) === false) { |
||
240 | unset($json[JSONApiInterface::CONTENT_DATA][JSONApiInterface::CONTENT_ATTRIBUTES][$k]); |
||
241 | } else if ($attrsCase !== ConfigInterface::DEFAULT_CASE) { |
||
242 | self::changeParamKey($json, $k, $v, $attrsCase); |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @param array $json |
||
250 | * @param string $k |
||
251 | * @param mixed $v |
||
252 | * @param string $attrsCase |
||
253 | */ |
||
254 | public static function changeParamKey(array &$json, string $k, $v, string $attrsCase): void |
||
255 | { |
||
256 | $changedKey = $k; |
||
257 | if ($attrsCase === ConfigInterface::CAMEL_CASE) { |
||
258 | $changedKey = self::changeParamKeyToLowerCamelCase($k); |
||
259 | } else if ($attrsCase === ConfigInterface::LISP_CASE) { |
||
260 | $changedKey = self::changeParamKeyToLispCase($k); |
||
261 | } |
||
262 | |||
263 | $json[JSONApiInterface::CONTENT_DATA][JSONApiInterface::CONTENT_ATTRIBUTES][$changedKey] = $v; |
||
264 | if ($changedKey !== $k) { |
||
265 | unset($json[JSONApiInterface::CONTENT_DATA][JSONApiInterface::CONTENT_ATTRIBUTES][$k]); |
||
266 | } |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @param string $param |
||
271 | * @return string |
||
272 | */ |
||
273 | public static function changeParamKeyToLowerCamelCase(string $param): string |
||
274 | { |
||
275 | return lcfirst( |
||
276 | str_replace(' ', '', ucwords( |
||
277 | str_replace('_', ' ', $param) |
||
278 | ) |
||
279 | ) |
||
280 | ); |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param string $param |
||
285 | * @return string |
||
286 | */ |
||
287 | public static function changeParamKeyToLispCase(string $param): string |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @param bool $isCollection |
||
294 | * @return Json |
||
295 | */ |
||
296 | public function setIsCollection(bool $isCollection): Json |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @param array $meta |
||
305 | * @return Json |
||
306 | */ |
||
307 | public function setMeta(array $meta): Json |
||
312 | } |
||
313 | } |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: