Conditions | 18 |
Paths | 2880 |
Total Lines | 49 |
Lines | 0 |
Ratio | 0 % |
Tests | 36 |
CRAP Score | 18.5883 |
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 |
||
13 | 9 | static public function cast($val, $type, $validate = true) |
|
14 | { |
||
15 | 9 | TypeHint::isScalarType($type) or \PhpBoot\abort(new \InvalidArgumentException("$type is not scalar type")); |
|
16 | |||
17 | 9 | if(is_bool($val)){ |
|
18 | 2 | $val = intval($val); |
|
19 | 9 | }else if($val === null){ |
|
20 | $map = [ |
||
21 | 1 | 'string'=>'', |
|
22 | 1 | 'bool'=>false, |
|
23 | 1 | 'int'=>0, |
|
24 | 1 | 'float'=>0, |
|
25 | 1 | ]; |
|
26 | 1 | if(isset($map[$type])){ |
|
27 | 1 | $val = $map[$type]; |
|
28 | 1 | } |
|
29 | 1 | } |
|
30 | 9 | if(is_object($val)){ |
|
31 | try{ |
||
32 | 1 | $val = (string)$val; |
|
33 | 1 | }catch (\Exception $e){ |
|
|
|||
34 | 1 | $className = get_class($val); |
|
35 | 1 | \PhpBoot\abort(new \InvalidArgumentException("could not cast value from class $className to {$type}")); |
|
36 | } |
||
37 | |||
38 | } |
||
39 | 9 | if(is_array($val)){ |
|
40 | 1 | $type == 'array' || $type =='mixed' || !$type or \PhpBoot\abort(new \InvalidArgumentException("could not cast value from resource to {$type}")); |
|
41 | } |
||
42 | 9 | if(is_resource($val)) { |
|
43 | \PhpBoot\abort(new \InvalidArgumentException("could not cast value from resource to {$type}")); |
||
44 | } |
||
45 | 9 | if(!$validate){ |
|
46 | 2 | settype($val, $type) or \PhpBoot\abort(new \InvalidArgumentException("cast value($val) to {$type} failed")); |
|
47 | 2 | }else{ |
|
48 | 8 | $ori = $val; |
|
49 | 8 | $oriType = gettype($val); |
|
50 | 8 | settype($val, $type) or \PhpBoot\abort(new \InvalidArgumentException("cast value($ori) to type {$type} failed")); |
|
51 | 8 | $newData = $val; |
|
52 | 8 | if(is_bool($newData)){ |
|
53 | 1 | $newData = intval($newData); |
|
54 | 1 | } |
|
55 | 8 | settype($newData, $oriType) or \PhpBoot\abort(new \InvalidArgumentException("cast value($ori) to type {$type} failed")); |
|
56 | 8 | if($ori != $newData){ |
|
57 | 3 | \PhpBoot\abort(new \InvalidArgumentException("could not cast value($ori) to type {$type}")); |
|
58 | } |
||
59 | } |
||
60 | 9 | return $val; |
|
61 | } |
||
62 | } |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.