Conditions | 16 |
Paths | 320 |
Total Lines | 48 |
Lines | 8 |
Ratio | 16.67 % |
Changes | 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 |
||
98 | protected function _serialize($serialize) |
||
99 | { |
||
100 | $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response'; |
||
101 | |||
102 | if ($serialize === true) { |
||
103 | $serialize = array_diff( |
||
104 | array_keys($this->viewVars), |
||
105 | $this->_specialVars |
||
106 | ); |
||
107 | |||
108 | if (empty($serialize)) { |
||
109 | $serialize = null; |
||
110 | } elseif (count($serialize) === 1) { |
||
111 | $serialize = current($serialize); |
||
112 | } |
||
113 | } |
||
114 | |||
115 | if (is_array($serialize)) { |
||
116 | $data = [$rootNode => []]; |
||
117 | View Code Duplication | foreach ($serialize as $alias => $key) { |
|
118 | if (is_numeric($alias)) { |
||
119 | $alias = $key; |
||
120 | } |
||
121 | if (array_key_exists($key, $this->viewVars)) { |
||
122 | $data[$rootNode][$alias] = $this->viewVars[$key]; |
||
123 | } |
||
124 | } |
||
125 | } else { |
||
126 | $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null; |
||
127 | if (is_array($data) && Hash::numeric(array_keys($data))) { |
||
128 | $data = [$rootNode => [$serialize => $data]]; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | $options = []; |
||
133 | if (isset($this->viewVars['_xmlOptions'])) { |
||
134 | $options = $this->viewVars['_xmlOptions']; |
||
135 | } |
||
136 | if (Configure::read('debug')) { |
||
137 | $options['pretty'] = true; |
||
138 | } |
||
139 | |||
140 | if (isset($options['return']) && strtolower($options['return']) === 'domdocument') { |
||
141 | return Xml::fromArray($data, $options)->saveXML(); |
||
142 | } |
||
143 | |||
144 | return Xml::fromArray($data, $options)->asXML(); |
||
|
|||
145 | } |
||
146 | } |
||
147 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: