Conditions | 15 |
Paths | 7 |
Total Lines | 35 |
Code Lines | 27 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
31 | public static function encodeJsVar( $value ) { |
||
32 | if ( is_bool( $value ) ) { |
||
33 | $s = $value ? 'true' : 'false'; |
||
34 | } elseif ( is_null( $value ) ) { |
||
35 | $s = 'null'; |
||
36 | } elseif ( is_int( $value ) || is_float( $value ) ) { |
||
37 | $s = $value; |
||
38 | } elseif ( is_array( $value ) && // Make sure it's not associative. |
||
39 | array_keys($value) === range( 0, count($value) - 1 ) || |
||
40 | count($value) == 0 |
||
41 | ) { |
||
42 | $s = '['; |
||
43 | foreach ( $value as $elt ) { |
||
|
|||
44 | if ( $s != '[' ) { |
||
45 | $s .= ', '; |
||
46 | } |
||
47 | $s .= self::encodeJsVar( $elt ); |
||
48 | } |
||
49 | $s .= ']'; |
||
50 | } elseif ( is_object( $value ) || is_array( $value ) ) { |
||
51 | // Objects and associative arrays |
||
52 | $s = '{'; |
||
53 | foreach ( (array)$value as $name => $elt ) { |
||
54 | if ( $s != '{' ) { |
||
55 | $s .= ', '; |
||
56 | } |
||
57 | $s .= '"' . Xml::encodeJsVar( $name ) . '": ' . |
||
58 | self::encodeJsVar( $elt ); |
||
59 | } |
||
60 | $s .= '}'; |
||
61 | } else { |
||
62 | $s = '"' . Xml::encodeJsVar( $value ) . '"'; |
||
63 | } |
||
64 | return $s; |
||
65 | } |
||
66 | |||
176 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.