Conditions | 17 |
Paths | 12 |
Total Lines | 74 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
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 |
||
115 | protected function formatFile($data) |
||
116 | { |
||
117 | // replace windows and mac format with unix format |
||
118 | $data = str_replace("\r\n", "\n", $data); |
||
119 | $data = str_replace("\r", "\n", $data); |
||
120 | |||
121 | // remove comments and tags with tokenizer |
||
122 | |||
123 | // I disabled this, it seems broken somehow. doesn't remove all <?php tags. - david |
||
124 | |||
125 | if (function_exists('token_get_all')) { |
||
126 | $tokens = token_get_all($data); |
||
127 | $tokenized = null; |
||
128 | // has something been written to tokenized? If so, we can optionally append whitespace. |
||
129 | $appended = false; |
||
130 | |||
131 | foreach ($tokens as $token) { |
||
132 | if (is_string($token)) { |
||
133 | $tokenized .= $token; |
||
134 | $appended = true; |
||
135 | } else { |
||
136 | @list($id,$text) = $token; |
||
137 | switch ($id) { |
||
138 | case T_COMMENT: |
||
139 | case T_DOC_COMMENT: |
||
140 | case T_OPEN_TAG: |
||
141 | $appended = false; |
||
142 | break; |
||
143 | case T_CLOSE_TAG: |
||
144 | $appended = false; |
||
145 | break; |
||
146 | |||
147 | case T_WHITESPACE: |
||
148 | // something was appended, optionally add a newline |
||
149 | if ($appended) { |
||
150 | $replace = null; |
||
151 | if (strstr($text, "\n") !== false) { |
||
152 | $replace = "\n"; |
||
153 | } |
||
154 | if ($replace) { |
||
155 | $text = preg_replace('/\s+/m', $replace, $text); |
||
156 | } |
||
157 | $tokenized .= $text; |
||
158 | } |
||
159 | $appended = false; |
||
160 | break; |
||
161 | case T_INLINE_HTML: |
||
162 | // If empty T_INLINE_HTML move on |
||
163 | if (!preg_match('/[^\s]+/m', $text)) { |
||
164 | $appended = false; |
||
165 | break; |
||
166 | } |
||
167 | default: |
||
168 | $tokenized .= $text; |
||
169 | $appended = true; |
||
170 | break; |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | $data = $tokenized; |
||
175 | } |
||
176 | $data = trim($data); |
||
177 | if (substr($data, 0, 5) == '<?php') { |
||
178 | $data = substr($data, 5); |
||
179 | } elseif (substr($data, 0, 2) == '<?') { |
||
180 | $data = substr($data, 2); |
||
181 | } |
||
182 | if (substr($data, -2, 2) == '?>') { |
||
183 | $data = substr($data, 0, -2); |
||
184 | } |
||
185 | $data = preg_replace('/\s*\?>\s*<\?(php)?\s*/', '', $data); |
||
186 | |||
187 | return $data; |
||
188 | } |
||
189 | } |
||
190 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: