| Total Complexity | 53 |
| Total Lines | 258 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like HalJsonFormat 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 HalJsonFormat, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class HalJsonFormat extends Format |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public const MIME = 'application/hal+json'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | public const EXTENSION = 'json'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var boolean|null shim for json_encode option JSON_PRETTY_PRINT set |
||
| 32 | * it to null to use smart defaults |
||
| 33 | */ |
||
| 34 | public static ?bool $prettyPrint = null; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * shim for json_encode option JSON_UNESCAPED_SLASHES |
||
| 38 | * set it to null to use smart defaults |
||
| 39 | */ |
||
| 40 | public static bool $unEscapedSlashes = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var boolean|null shim for json_encode JSON_UNESCAPED_UNICODE set it |
||
| 44 | * to null to use smart defaults |
||
| 45 | */ |
||
| 46 | public static ?bool $unEscapedUnicode = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var boolean|null shim for json_decode JSON_BIGINT_AS_STRING set it to |
||
| 50 | * null to |
||
| 51 | * use smart defaults |
||
| 52 | */ |
||
| 53 | public static ?bool $bigIntAsString = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var boolean|null shim for json_decode JSON_NUMERIC_CHECK set it to |
||
| 57 | * null to |
||
| 58 | * use smart defaults |
||
| 59 | */ |
||
| 60 | public static ?bool $numbersAsNumbers = null; |
||
| 61 | |||
| 62 | public function encode($data, $humanReadable = false) |
||
| 63 | { |
||
| 64 | if (self::$prettyPrint !== null) { |
||
| 65 | $humanReadable = self::$prettyPrint; |
||
| 66 | } |
||
| 67 | |||
| 68 | if (self::$unEscapedSlashes === null) { |
||
|
|
|||
| 69 | self::$unEscapedSlashes = $humanReadable; |
||
| 70 | } |
||
| 71 | |||
| 72 | if (self::$unEscapedUnicode === null) { |
||
| 73 | self::$unEscapedUnicode = $this->charset == 'utf-8'; |
||
| 74 | } |
||
| 75 | |||
| 76 | $options = 0; |
||
| 77 | |||
| 78 | if ((PHP_MAJOR_VERSION === 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
||
| 79 | || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
||
| 80 | ) { |
||
| 81 | if ($humanReadable) { |
||
| 82 | $options |= JSON_PRETTY_PRINT; |
||
| 83 | } |
||
| 84 | |||
| 85 | if (self::$unEscapedSlashes) { |
||
| 86 | $options |= JSON_UNESCAPED_SLASHES; |
||
| 87 | } |
||
| 88 | |||
| 89 | if (self::$bigIntAsString === true) { |
||
| 90 | $options |= JSON_BIGINT_AS_STRING; |
||
| 91 | } |
||
| 92 | |||
| 93 | if (self::$unEscapedUnicode) { |
||
| 94 | $options |= JSON_UNESCAPED_UNICODE; |
||
| 95 | } |
||
| 96 | |||
| 97 | if (self::$numbersAsNumbers === true) { |
||
| 98 | $options |= JSON_NUMERIC_CHECK; |
||
| 99 | } |
||
| 100 | |||
| 101 | $result = json_encode(Obj::toArray($data, true), $options); |
||
| 102 | $this->handleJsonError(); |
||
| 103 | |||
| 104 | return $result; |
||
| 105 | } |
||
| 106 | |||
| 107 | $result = json_encode(Obj::toArray($data, true), JSON_THROW_ON_ERROR); |
||
| 108 | $this->handleJsonError(); |
||
| 109 | |||
| 110 | if ($humanReadable) { |
||
| 111 | $result = $this->formatJson($result); |
||
| 112 | } |
||
| 113 | |||
| 114 | if (self::$unEscapedUnicode === true) { |
||
| 115 | $result = preg_replace_callback( |
||
| 116 | '/\\\u(\w\w\w\w)/', |
||
| 117 | static function (array $matches): string|false { |
||
| 118 | if (function_exists('mb_convert_encoding')) { |
||
| 119 | return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16BE'); |
||
| 120 | } |
||
| 121 | |||
| 122 | return iconv('UTF-16BE', 'UTF-8', pack('H*', $matches[1])); |
||
| 123 | }, |
||
| 124 | $result |
||
| 125 | ); |
||
| 126 | } |
||
| 127 | |||
| 128 | if (self::$unEscapedSlashes) { |
||
| 129 | return str_replace('\/', '/', $result); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $result; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function decode($data): ?array |
||
| 136 | { |
||
| 137 | if (empty($data)) { |
||
| 138 | return null; |
||
| 139 | } |
||
| 140 | |||
| 141 | $options = 0; |
||
| 142 | if (self::$bigIntAsString === true) { |
||
| 143 | if ((PHP_MAJOR_VERSION === 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
||
| 144 | || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
||
| 145 | ) { |
||
| 146 | $options |= JSON_BIGINT_AS_STRING; |
||
| 147 | } else { |
||
| 148 | $data = preg_replace( |
||
| 149 | '/:\s*(\-?\d+(\.\d+)?([e|E][\-|\+]\d+)?)/', |
||
| 150 | ': "$1"', |
||
| 151 | $data |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | try { |
||
| 157 | $decoded = json_decode((string) $data, false, 512, $options); |
||
| 158 | $this->handleJsonError(); |
||
| 159 | } catch (\RuntimeException $runtimeException) { |
||
| 160 | throw new RestException('400', $runtimeException->getMessage()); |
||
| 161 | } |
||
| 162 | |||
| 163 | if (strlen((string) $data) && $decoded === null || $decoded === $data) { |
||
| 164 | throw new RestException('400', 'Error parsing JSON'); |
||
| 165 | } |
||
| 166 | |||
| 167 | return Obj::toArray($decoded); |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Throws an exception if an error occurred during the last JSON encoding/decoding |
||
| 172 | */ |
||
| 173 | protected function handleJsonError(): void |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Pretty print JSON string |
||
| 211 | */ |
||
| 212 | private function formatJson(string $json): string |
||
| 276 | } |
||
| 277 | } |
||
| 278 |