Conditions | 16 |
Paths | 320 |
Total Lines | 71 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | } |
||
278 |