Total Complexity | 41 |
Total Lines | 258 |
Duplicated Lines | 0 % |
Changes | 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 |
||
23 | class Json |
||
24 | { |
||
25 | /** |
||
26 | * @param array $jsonApiArr |
||
27 | * |
||
28 | * @return array |
||
29 | */ |
||
30 | public static function getAttributes(array $jsonApiArr) : array |
||
31 | { |
||
32 | return empty($jsonApiArr[ApiInterface::RAML_DATA][ApiInterface::RAML_ATTRS]) ? [] : $jsonApiArr[ApiInterface::RAML_DATA][ApiInterface::RAML_ATTRS]; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Returns an array of bulk attributes for each element |
||
37 | * |
||
38 | * @param array $jsonApiArr |
||
39 | * @return array |
||
40 | */ |
||
41 | public static function getBulkAttributes(array $jsonApiArr) : array |
||
42 | { |
||
43 | return empty($jsonApiArr[ApiInterface::RAML_DATA]) ? [] : $jsonApiArr[ApiInterface::RAML_DATA]; |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param array $jsonApiArr |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | public static function getRelationships(array $jsonApiArr) : array |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @param array $jsonApiArr |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | public static function getData(array $jsonApiArr) : array |
||
62 | { |
||
63 | return empty($jsonApiArr[ApiInterface::RAML_DATA]) ? [] : $jsonApiArr[ApiInterface::RAML_DATA]; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param $relations \Illuminate\Database\Eloquent\Collection |
||
68 | * @param string $entity |
||
69 | * @return array JSON API rels compatible array |
||
70 | */ |
||
71 | public static function getRelations($relations, string $entity) : array |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Output errors in JSON API compatible format |
||
100 | * @param array $errors |
||
101 | * @param bool $return |
||
102 | * @return string |
||
103 | */ |
||
104 | public static function outputErrors(array $errors, bool $return = false) |
||
105 | { |
||
106 | $arr[JSONApiInterface::CONTENT_ERRORS] = []; |
||
107 | if (empty($errors) === false) { |
||
108 | $arr[JSONApiInterface::CONTENT_ERRORS] = $errors; |
||
109 | } |
||
110 | // errors and codes must be clear with readable json |
||
111 | $encoded = self::encode($arr, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); |
||
112 | if (false === $return && env('APP_ENV') !== 'dev') { |
||
113 | echo $encoded; |
||
114 | exit(JSONApiInterface::EXIT_STATUS_ERROR); |
||
115 | } |
||
116 | return $encoded; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * |
||
121 | * @param array $errors |
||
122 | * @return string |
||
123 | */ |
||
124 | public function getErrors(array $errors) : string |
||
125 | { |
||
126 | $arr[JSONApiInterface::CONTENT_ERRORS] = []; |
||
127 | if (empty($errors) === false) { |
||
128 | $arr[JSONApiInterface::CONTENT_ERRORS] = $errors; |
||
129 | } |
||
130 | |||
131 | return self::encode($arr, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns composition of relations |
||
136 | * |
||
137 | * @param Request $request |
||
138 | * @param array $data |
||
139 | * @return string |
||
140 | */ |
||
141 | public static function prepareSerializedRelations(Request $request, array $data) : string |
||
142 | { |
||
143 | $arr[JSONApiInterface::CONTENT_LINKS] = [ |
||
144 | JSONApiInterface::CONTENT_SELF => $request->getUri(), |
||
145 | ]; |
||
146 | |||
147 | $arr[JSONApiInterface::CONTENT_DATA] = $data; |
||
148 | |||
149 | return self::encode($arr); |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param BaseFormRequest $formRequest |
||
154 | * @param $model |
||
155 | * @param string $entity |
||
156 | * @param bool $isCollection |
||
157 | * |
||
158 | * @param array $meta |
||
159 | * @return Collection|Item |
||
160 | */ |
||
161 | public static function getResource(BaseFormRequest $formRequest, $model, string $entity, bool $isCollection = false, array $meta = []) |
||
162 | { |
||
163 | $transformer = new DefaultTransformer($formRequest); |
||
164 | if ($isCollection === true) { |
||
165 | $collection = new Collection($model, $transformer, strtolower($entity)); |
||
166 | if (empty($meta) === false) { |
||
167 | $collection->setMeta($meta); |
||
168 | } |
||
169 | |||
170 | return $collection; |
||
171 | } |
||
172 | |||
173 | $item = new Item($model, $transformer, strtolower($entity)); |
||
174 | $item->setMeta($meta); |
||
175 | |||
176 | return $item; |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * @param ResourceInterface $resource |
||
181 | * @param array $data |
||
182 | * @return string |
||
183 | */ |
||
184 | public static function prepareSerializedData(ResourceInterface $resource, $data = ModelsInterface::DEFAULT_DATA) : string |
||
185 | { |
||
186 | if (empty($resource->getData())) { // preventing 3d party libs (League etc) from crash on empty data |
||
187 | return self::encode([ |
||
188 | ModelsInterface::PARAM_DATA => [] |
||
189 | ]); |
||
190 | } |
||
191 | |||
192 | $host = $_SERVER['HTTP_HOST']; |
||
193 | $manager = new Manager(); |
||
194 | |||
195 | if (isset($_GET['include'])) { |
||
196 | $manager->parseIncludes($_GET['include']); |
||
197 | } |
||
198 | |||
199 | $manager->setSerializer(new JsonApiSerializer($host)); |
||
200 | return self::getSelectedData($manager->createData($resource)->toJson(), $data); |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param array $array |
||
205 | * @param int $opts |
||
206 | * @return string |
||
207 | */ |
||
208 | public static function encode(array $array, int $opts = 0) |
||
209 | { |
||
210 | return json_encode($array, $opts); |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * @param mixed $json |
||
215 | * @return mixed |
||
216 | */ |
||
217 | public static function decode(string $json): array |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @param string $json |
||
224 | * @return array |
||
225 | */ |
||
226 | public static function parse(string $json): array |
||
227 | { |
||
228 | return self::decode($json); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param string $json |
||
233 | * @param array $data |
||
234 | * @return string |
||
235 | */ |
||
236 | private static function getSelectedData(string $json, array $data) : string |
||
237 | { |
||
238 | if (current($data) === PhpInterface::ASTERISK) {// do nothing - grab all fields |
||
239 | return $json; |
||
240 | } |
||
241 | |||
242 | $jsonArr = self::decode($json); |
||
243 | $current = current($jsonArr[ApiInterface::RAML_DATA]); |
||
244 | |||
245 | if (empty($current[JSONApiInterface::CONTENT_ATTRIBUTES]) === false) {// this is an array of values |
||
246 | self::unsetArray($jsonArr, $data); |
||
247 | } else {// this is just one element |
||
248 | self::unsetObject($jsonArr, $data); |
||
249 | } |
||
250 | |||
251 | return self::encode($jsonArr); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * |
||
256 | * @param array &$json |
||
257 | * @param array $data |
||
258 | */ |
||
259 | private static function unsetArray(array &$json, array $data) : void |
||
266 | } |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * @param array $json |
||
274 | * @param array $data |
||
275 | */ |
||
276 | private static function unsetObject(array &$json, array $data) : void |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 | } |
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: