Conditions | 1 |
Paths | 1 |
Total Lines | 53 |
Code Lines | 37 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
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 |
||
99 | public function locationProvider() { |
||
100 | |||
101 | $urlComponent = array ( |
||
102 | 'scheme' => 'http', |
||
103 | 'host' => 'example.com', |
||
104 | 'port' => 80, |
||
105 | 'path' => '' |
||
106 | ); |
||
107 | |||
108 | $provider[] = array( |
||
|
|||
109 | 'http://example.com', |
||
110 | $urlComponent, |
||
111 | 'http://abc.com', |
||
112 | 'http://abc.com' |
||
113 | ); |
||
114 | |||
115 | $provider[] = array( |
||
116 | 'http://example.com', |
||
117 | $urlComponent, |
||
118 | '/foo', |
||
119 | 'http://example.com/foo' |
||
120 | ); |
||
121 | |||
122 | $urlComponent = array ( |
||
123 | 'scheme' => 'https', |
||
124 | 'host' => 'tls://example.com', |
||
125 | 'port' => 443, |
||
126 | 'path' => '' |
||
127 | ); |
||
128 | |||
129 | $provider[] = array( |
||
130 | 'https://example.com', |
||
131 | $urlComponent, |
||
132 | '/foo', |
||
133 | 'https://example.com/foo' |
||
134 | ); |
||
135 | |||
136 | $urlComponent = array ( |
||
137 | 'scheme' => 'https', |
||
138 | 'host' => 'tls://example.com', |
||
139 | 'port' => 4443, |
||
140 | 'path' => '' |
||
141 | ); |
||
142 | |||
143 | $provider[] = array( |
||
144 | 'https://example.com:4443', |
||
145 | $urlComponent, |
||
146 | '/foo', |
||
147 | 'https://example.com/foo' |
||
148 | ); |
||
149 | |||
150 | return $provider; |
||
151 | } |
||
152 | |||
154 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.