| Total Complexity | 40 |
| Total Lines | 249 |
| 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 |
||
| 19 | class Json |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @param array $jsonApiArr |
||
| 23 | * |
||
| 24 | * @return array |
||
| 25 | */ |
||
| 26 | public static function getAttributes(array $jsonApiArr) : array |
||
| 27 | { |
||
| 28 | return empty($jsonApiArr[ApiInterface::RAML_DATA][ApiInterface::RAML_ATTRS]) ? [] : $jsonApiArr[ApiInterface::RAML_DATA][ApiInterface::RAML_ATTRS]; |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Returns an array of bulk attributes for each element |
||
| 33 | * |
||
| 34 | * @param array $jsonApiArr |
||
| 35 | * @return array |
||
| 36 | */ |
||
| 37 | public static function getBulkAttributes(array $jsonApiArr) : array |
||
| 38 | { |
||
| 39 | return empty($jsonApiArr[ApiInterface::RAML_DATA]) ? [] : $jsonApiArr[ApiInterface::RAML_DATA]; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param array $jsonApiArr |
||
| 44 | * |
||
| 45 | * @return array |
||
| 46 | */ |
||
| 47 | public static function getRelationships(array $jsonApiArr) : array |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param array $jsonApiArr |
||
| 54 | * |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | public static function getData(array $jsonApiArr) : array |
||
| 58 | { |
||
| 59 | return empty($jsonApiArr[ApiInterface::RAML_DATA]) ? [] : $jsonApiArr[ApiInterface::RAML_DATA]; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param $relations \Illuminate\Database\Eloquent\Collection |
||
| 64 | * @param string $entity |
||
| 65 | * @return array JSON API rels compatible array |
||
| 66 | */ |
||
| 67 | public static function getRelations($relations, string $entity) : array |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Output errors in JSON API compatible format |
||
| 96 | * @param array $errors |
||
| 97 | * @param bool $return |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public static function outputErrors(array $errors, bool $return = false) |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * |
||
| 117 | * @param array $errors |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function getErrors(array $errors) : string |
||
| 121 | { |
||
| 122 | $arr[JSONApiInterface::CONTENT_ERRORS] = []; |
||
| 123 | if (empty($errors) === false) { |
||
| 124 | $arr[JSONApiInterface::CONTENT_ERRORS] = $errors; |
||
| 125 | } |
||
| 126 | |||
| 127 | return self::encode($arr, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT); |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Returns composition of relations |
||
| 132 | * |
||
| 133 | * @param Request $request |
||
| 134 | * @param array $data |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public static function prepareSerializedRelations(Request $request, array $data) : string |
||
| 138 | { |
||
| 139 | $arr[JSONApiInterface::CONTENT_LINKS] = [ |
||
| 140 | JSONApiInterface::CONTENT_SELF => $request->getUri(), |
||
| 141 | ]; |
||
| 142 | |||
| 143 | $arr[JSONApiInterface::CONTENT_DATA] = $data; |
||
| 144 | |||
| 145 | return self::encode($arr); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param BaseFormRequest $formRequest |
||
| 150 | * @param $model |
||
| 151 | * @param string $entity |
||
| 152 | * @param bool $isCollection |
||
| 153 | * |
||
| 154 | * @param array $meta |
||
| 155 | * @return Collection|Item |
||
| 156 | */ |
||
| 157 | public static function getResource(BaseFormRequest $formRequest, $model, string $entity, bool $isCollection = false, array $meta = []) |
||
| 158 | { |
||
| 159 | $transformer = new DefaultTransformer($formRequest); |
||
| 160 | if ($isCollection === true) { |
||
| 161 | $collection = new Collection($model, $transformer, strtolower($entity)); |
||
| 162 | if (empty($meta) === false) { |
||
| 163 | $collection->setMeta($meta); |
||
| 164 | } |
||
| 165 | |||
| 166 | return $collection; |
||
| 167 | } |
||
| 168 | |||
| 169 | $item = new Item($model, $transformer, strtolower($entity)); |
||
| 170 | $item->setMeta($meta); |
||
| 171 | |||
| 172 | return $item; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param ResourceInterface $resource |
||
| 177 | * @param array $data |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public static function prepareSerializedData(ResourceInterface $resource, $data = ModelsInterface::DEFAULT_DATA) : string |
||
| 181 | { |
||
| 182 | if (empty($resource->getData())) { // preventing 3d party libs (League etc) from crash on empty data |
||
| 183 | return self::encode([ |
||
| 184 | ModelsInterface::PARAM_DATA => [] |
||
| 185 | ]); |
||
| 186 | } |
||
| 187 | |||
| 188 | $host = $_SERVER['HTTP_HOST']; |
||
| 189 | $manager = new Manager(); |
||
| 190 | |||
| 191 | if (isset($_GET['include'])) { |
||
| 192 | $manager->parseIncludes($_GET['include']); |
||
| 193 | } |
||
| 194 | |||
| 195 | $manager->setSerializer(new JsonApiSerializer($host)); |
||
| 196 | return self::getSelectedData($manager->createData($resource)->toJson(), $data); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param array $array |
||
| 201 | * @param int $opts |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public static function encode(array $array, int $opts = 0) |
||
| 205 | { |
||
| 206 | return json_encode($array, $opts); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param mixed $json |
||
| 211 | * @return mixed |
||
| 212 | */ |
||
| 213 | public static function decode($json) |
||
| 214 | { |
||
| 215 | return json_decode($json, true); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $json |
||
| 220 | * @param array $data |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | private static function getSelectedData(string $json, array $data) : string |
||
| 224 | { |
||
| 225 | if (current($data) === PhpInterface::ASTERISK) {// do nothing - grab all fields |
||
| 226 | return $json; |
||
| 227 | } |
||
| 228 | |||
| 229 | $jsonArr = self::decode($json); |
||
| 230 | $current = current($jsonArr[ApiInterface::RAML_DATA]); |
||
| 231 | |||
| 232 | if (empty($current[JSONApiInterface::CONTENT_ATTRIBUTES]) === false) {// this is an array of values |
||
| 233 | self::unsetArray($jsonArr, $data); |
||
| 234 | } else {// this is just one element |
||
| 235 | self::unsetObject($jsonArr, $data); |
||
| 236 | } |
||
| 237 | |||
| 238 | return self::encode($jsonArr); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * |
||
| 243 | * @param array &$json |
||
| 244 | * @param array $data |
||
| 245 | */ |
||
| 246 | private static function unsetArray(array &$json, array $data) : void |
||
| 253 | } |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * @param array $json |
||
| 261 | * @param array $data |
||
| 262 | */ |
||
| 263 | private static function unsetObject(array &$json, array $data) : void |
||
| 268 | } |
||
| 269 | } |
||
| 270 | } |
||
| 271 | } |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes 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: