| Conditions | 1 |
| Paths | 1 |
| Total Lines | 65 |
| 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 |
||
| 47 | public function provideSettingsTriples() { |
||
| 48 | $default = [ 'key' => 'x' ]; |
||
| 49 | $custom['key'] = 'y'; |
||
|
|
|||
| 50 | $expected = [ 'key' => 'y' ]; |
||
| 51 | yield "['key'], scalar" => [ $default, $custom, $expected ]; |
||
| 52 | unset( $custom ); |
||
| 53 | |||
| 54 | $default = [ 'key' => [] ]; |
||
| 55 | $custom['key'][] = 'one'; |
||
| 56 | $expected = [ 'key' => [ 'one' ] ]; |
||
| 57 | yield "['key'][], default empty" => [ $default, $custom, $expected ]; |
||
| 58 | unset( $custom ); |
||
| 59 | |||
| 60 | $default = [ 'key' => [ 'one', 'two' ] ]; |
||
| 61 | $custom['key'][] = 'three'; |
||
| 62 | $expected = [ 'key' => [ 'one', 'two', 'three' ] ]; |
||
| 63 | yield "['key'][], default nonempty" => [ $default, $custom, $expected ]; |
||
| 64 | unset( $custom ); |
||
| 65 | |||
| 66 | $default = [ 'key' => [ 'a' => 'A', 'b' => 'B' ] ]; |
||
| 67 | $custom['key']['a'] = 'Ä'; |
||
| 68 | $expected = [ 'key' => [ 'a' => 'Ä', 'b' => 'B' ] ]; |
||
| 69 | yield "['key']['a'], scalar" => [ $default, $custom, $expected ]; |
||
| 70 | unset( $custom ); |
||
| 71 | |||
| 72 | $default = [ 'key' => [ 'one' ] ]; |
||
| 73 | $custom['key'] = 'two'; |
||
| 74 | $expected = [ 'key' => 'two' ]; |
||
| 75 | yield "['key'], nonempty->scalar" => [ $default, $custom, $expected ]; |
||
| 76 | unset( $custom ); |
||
| 77 | |||
| 78 | $default = [ 'key' => 'value' ]; |
||
| 79 | $custom = []; |
||
| 80 | $expected = [ 'key' => 'value' ]; |
||
| 81 | yield 'no custom setting' => [ $default, $custom, $expected ]; |
||
| 82 | unset( $custom ); |
||
| 83 | |||
| 84 | $default = []; |
||
| 85 | $custom['key'] = 'value'; |
||
| 86 | $expected = [ 'key' => 'value' ]; |
||
| 87 | yield 'no default setting, scalar' => [ $default, $custom, $expected ]; |
||
| 88 | unset( $custom ); |
||
| 89 | |||
| 90 | $default = []; |
||
| 91 | $custom['key'] = [ 'one', 'two' ]; |
||
| 92 | $expected = [ 'key' => [ 'one', 'two' ] ]; |
||
| 93 | yield 'no default setting, array' => [ $default, $custom, $expected ]; |
||
| 94 | unset( $custom ); |
||
| 95 | |||
| 96 | $default = [ 'key' => function () { |
||
| 97 | return 'value'; |
||
| 98 | } ]; |
||
| 99 | $custom['key'] = 'custom value'; |
||
| 100 | $expected = [ 'key' => 'custom value' ]; |
||
| 101 | yield "['key'], callable->scalar" => [ $default, $custom, $expected ]; |
||
| 102 | unset( $custom ); |
||
| 103 | |||
| 104 | $default = [ 'key' => function () { |
||
| 105 | return [ 'one', 'two' ]; |
||
| 106 | } ]; |
||
| 107 | $custom['key'] = [ 'ONE', 'TWO' ]; |
||
| 108 | $expected = [ 'key' => [ 'ONE', 'TWO' ] ]; |
||
| 109 | yield "['key'], callable->array" => [ $default, $custom, $expected ]; |
||
| 110 | unset( $custom ); |
||
| 111 | } |
||
| 112 | |||
| 156 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.