Conditions | 16 |
Paths | 320 |
Total Lines | 70 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
45 | public function encode($data, $humanReadable = false) |
||
46 | { |
||
47 | if (!is_null(self::$prettyPrint)) { |
||
48 | $humanReadable = self::$prettyPrint; |
||
49 | } |
||
50 | if (is_null(self::$unEscapedSlashes)) { |
||
51 | self::$unEscapedSlashes = $humanReadable; |
||
52 | } |
||
53 | if (is_null(self::$unEscapedUnicode)) { |
||
54 | self::$unEscapedUnicode = $this->charset == 'utf-8'; |
||
55 | } |
||
56 | |||
57 | $options = 0; |
||
58 | |||
59 | if ((PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 4) // PHP >= 5.4 |
||
60 | || PHP_MAJOR_VERSION > 5 // PHP >= 6.0 |
||
61 | ) { |
||
62 | if ($humanReadable) { |
||
63 | $options |= JSON_PRETTY_PRINT; |
||
64 | } |
||
65 | |||
66 | if (self::$unEscapedSlashes) { |
||
67 | $options |= JSON_UNESCAPED_SLASHES; |
||
68 | } |
||
69 | |||
70 | if (self::$bigIntAsString) { |
||
71 | $options |= JSON_BIGINT_AS_STRING; |
||
72 | } |
||
73 | |||
74 | if (self::$unEscapedUnicode) { |
||
75 | $options |= JSON_UNESCAPED_UNICODE; |
||
76 | } |
||
77 | |||
78 | if (self::$numbersAsNumbers) { |
||
79 | $options |= JSON_NUMERIC_CHECK; |
||
80 | } |
||
81 | |||
82 | $result = json_encode(Object::toArray($data, true), $options); |
||
83 | $this->handleJsonError(); |
||
84 | |||
85 | return $result; |
||
86 | } |
||
87 | |||
88 | $result = json_encode(Object::toArray($data, true)); |
||
89 | $this->handleJsonError(); |
||
90 | |||
91 | if ($humanReadable) { |
||
92 | $result = $this->formatJson($result); |
||
93 | } |
||
94 | |||
95 | if (self::$unEscapedUnicode) { |
||
96 | $result = preg_replace_callback( |
||
97 | '/\\\u(\w\w\w\w)/', |
||
98 | function ($matches) { |
||
99 | if (function_exists('mb_convert_encoding')) { |
||
100 | return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UTF-16BE'); |
||
101 | } else { |
||
102 | return iconv('UTF-16BE', 'UTF-8', pack('H*', $matches[1])); |
||
103 | } |
||
104 | }, |
||
105 | $result |
||
106 | ); |
||
107 | } |
||
108 | |||
109 | if (self::$unEscapedSlashes) { |
||
110 | $result = str_replace('\/', '/', $result); |
||
111 | } |
||
112 | |||
113 | return $result; |
||
114 | } |
||
115 | |||
263 |