| Conditions | 1 |
| Paths | 1 |
| Total Lines | 63 |
| Code Lines | 37 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 62 | public function resultProvider() { |
||
| 63 | |||
| 64 | #0 |
||
| 65 | $provider[] = array( |
||
|
|
|||
| 66 | array( 'query' => array() ), |
||
| 67 | array(), |
||
| 68 | false, |
||
| 69 | null |
||
| 70 | ); |
||
| 71 | |||
| 72 | #1 |
||
| 73 | $provider[] = array( |
||
| 74 | array( |
||
| 75 | 'query-continue-offset' => 3, |
||
| 76 | 'query' => array() |
||
| 77 | ), |
||
| 78 | array(), |
||
| 79 | true, |
||
| 80 | null |
||
| 81 | ); |
||
| 82 | |||
| 83 | #2 |
||
| 84 | $provider[] = array( |
||
| 85 | array( |
||
| 86 | 'query-continue-offset' => 3, |
||
| 87 | 'query' => array( |
||
| 88 | 'printrequests' => array( |
||
| 89 | array( 'label' => 'Category', 'mode' => 0 ) |
||
| 90 | ) |
||
| 91 | ) |
||
| 92 | ), |
||
| 93 | array( |
||
| 94 | 'printrequests' => array( |
||
| 95 | array( 'label' => 'Category', 'mode' => 0 ) |
||
| 96 | ) |
||
| 97 | ), |
||
| 98 | true, |
||
| 99 | new DIProperty( '_INST' ) |
||
| 100 | ); |
||
| 101 | |||
| 102 | #3 |
||
| 103 | $provider[] = array( |
||
| 104 | array( |
||
| 105 | 'query' => array( |
||
| 106 | 'printrequests' => array( |
||
| 107 | array( 'label' => 'Category', 'mode' => 0 ) |
||
| 108 | ), |
||
| 109 | 'results' => array() |
||
| 110 | ), |
||
| 111 | ), |
||
| 112 | array( |
||
| 113 | 'printrequests' => array( |
||
| 114 | array( 'label' => 'Category', 'mode' => 0 ) |
||
| 115 | ), |
||
| 116 | 'results' => array() |
||
| 117 | ), |
||
| 118 | false, |
||
| 119 | null |
||
| 120 | ); |
||
| 121 | |||
| 122 | |||
| 123 | return $provider; |
||
| 124 | } |
||
| 125 | |||
| 127 |
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.