| Conditions | 1 |
| Paths | 1 |
| Total Lines | 91 |
| 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 |
||
| 65 | public function titleProvider() { |
||
| 66 | |||
| 67 | #0 |
||
| 68 | $provider[] = [ |
||
|
|
|||
| 69 | 'Foo', |
||
| 70 | 0, |
||
| 71 | [] |
||
| 72 | ]; |
||
| 73 | |||
| 74 | #1 |
||
| 75 | $provider[] = [ |
||
| 76 | 'Foo/', |
||
| 77 | 1, |
||
| 78 | [ |
||
| 79 | new DIWikiPage( 'Foo', NS_MAIN ) |
||
| 80 | ] |
||
| 81 | ]; |
||
| 82 | |||
| 83 | #2 |
||
| 84 | $provider[] = [ |
||
| 85 | 'Foo/Bar/Baz', |
||
| 86 | 2, |
||
| 87 | [ |
||
| 88 | new DIWikiPage( 'Foo', NS_MAIN ), |
||
| 89 | new DIWikiPage( 'Foo/Bar', NS_MAIN ) |
||
| 90 | ] |
||
| 91 | ]; |
||
| 92 | |||
| 93 | #3 |
||
| 94 | $provider[] = [ |
||
| 95 | 'Foo/Bar/Baz/Yin/Yan', |
||
| 96 | 4, |
||
| 97 | [ |
||
| 98 | new DIWikiPage( 'Foo', NS_MAIN ), |
||
| 99 | new DIWikiPage( 'Foo/Bar', NS_MAIN ), |
||
| 100 | new DIWikiPage( 'Foo/Bar/Baz', NS_MAIN ), |
||
| 101 | new DIWikiPage( 'Foo/Bar/Baz/Yin', NS_MAIN ) |
||
| 102 | ] |
||
| 103 | ]; |
||
| 104 | |||
| 105 | #4 /a/b |
||
| 106 | $provider[] = [ |
||
| 107 | '/a/b', |
||
| 108 | 1, |
||
| 109 | [ |
||
| 110 | new DIWikiPage( '/a', NS_MAIN ) |
||
| 111 | ] |
||
| 112 | ]; |
||
| 113 | |||
| 114 | #5 /a//b/c |
||
| 115 | $provider[] = [ |
||
| 116 | '/a//b/c', |
||
| 117 | 2, |
||
| 118 | [ |
||
| 119 | new DIWikiPage( '/a', NS_MAIN ), |
||
| 120 | new DIWikiPage( '/a//b', NS_MAIN ) |
||
| 121 | ] |
||
| 122 | ]; |
||
| 123 | |||
| 124 | #6 (#23 issue) |
||
| 125 | $provider[] = [ |
||
| 126 | 'Foo / Bar', |
||
| 127 | 0, |
||
| 128 | [] |
||
| 129 | ]; |
||
| 130 | |||
| 131 | #7 (#23 issue) |
||
| 132 | $provider[] = [ |
||
| 133 | 'Foo /Bar', |
||
| 134 | 0, |
||
| 135 | [] |
||
| 136 | ]; |
||
| 137 | |||
| 138 | #8 (#23 issue) |
||
| 139 | $provider[] = [ |
||
| 140 | 'Foo /Bar /Foobar', |
||
| 141 | 0, |
||
| 142 | [] |
||
| 143 | ]; |
||
| 144 | |||
| 145 | #9 |
||
| 146 | $provider[] = [ |
||
| 147 | 'Help:Foo/Foobar', |
||
| 148 | 1, |
||
| 149 | [ |
||
| 150 | new DIWikiPage( 'Foo', NS_HELP ), |
||
| 151 | ] |
||
| 152 | ]; |
||
| 153 | |||
| 154 | return $provider; |
||
| 155 | } |
||
| 156 | |||
| 158 |
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.