Conditions | 15 |
Paths | 6 |
Total Lines | 60 |
Code Lines | 38 |
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 |
||
193 | function array_change_key_case(array $array, $case = CASE_LOWER) |
||
194 | { |
||
195 | if ($case === CASE_LOWER || $case === CASE_UPPER) { |
||
196 | return \array_change_key_case($array, $case); |
||
197 | } |
||
198 | |||
199 | // If case is invalid, no need to perform expensive operation. |
||
200 | if (!is_int($case) || $case < CASE_SNAKE || $case > CASE_LISP) { |
||
201 | return $array; |
||
202 | } |
||
203 | |||
204 | static $changeCase; |
||
205 | $changeCase or $changeCase = function ($key, $case) { |
||
|
|||
206 | if (is_numeric($key)) { |
||
207 | return $key; |
||
208 | } |
||
209 | |||
210 | // change "camelCase" to "camel Case", "foo123bar" to "foo 123 bar", "foo!bar" to "foo! bar" |
||
211 | $key = preg_replace( |
||
212 | [ |
||
213 | '/([^a-zA-Z0-9\s\_\-])([a-zA-Z0-9])/', |
||
214 | '/([a-z])([A-Z])/', |
||
215 | '/([a-zA-Z])([\d])/', |
||
216 | '/([\d])([a-zA-Z])/', |
||
217 | ], |
||
218 | '${1} ${2}', |
||
219 | $key |
||
220 | ); |
||
221 | |||
222 | $words = preg_split('/_+|-+|\s+/', strtolower($key)); |
||
223 | |||
224 | switch ($case) { |
||
225 | case CASE_SNAKE: |
||
226 | return implode('_', $words); |
||
227 | case CASE_TITLE: |
||
228 | return ucfirst(implode(' ', $words)); |
||
229 | case CASE_CAMEL: |
||
230 | case CASE_PASCAL: |
||
231 | $first = $case === CASE_CAMEL ? array_shift($words) : ''; |
||
232 | return preg_replace_callback( |
||
233 | '/([^a-zA-Z]+)([a-zA-Z]{1})([^a-zA-Z]+)/', |
||
234 | function (array $matches) { |
||
235 | return $matches[1] . strtolower($matches[2]) . $matches[3]; |
||
236 | }, |
||
237 | $first . implode('', array_map('ucfirst', $words)) |
||
238 | ); |
||
239 | case CASE_LISP: |
||
240 | return implode('-', $words); |
||
241 | default: |
||
242 | return $key; |
||
243 | } |
||
244 | }; |
||
245 | |||
246 | $changed = []; |
||
247 | foreach ($array as $key => $value) { |
||
248 | $changed[$changeCase($key, $case)] = $value; |
||
249 | } |
||
250 | |||
251 | return $changed; |
||
252 | } |
||
253 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.