Conditions | 1 |
Paths | 1 |
Total Lines | 73 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 1 |
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 |
||
82 | public function stringValueProvider() { |
||
83 | |||
84 | $provider[] = array( |
||
|
|||
85 | 'foo', |
||
86 | StringValueFormatter::VALUE, |
||
87 | null, |
||
88 | 'foo' |
||
89 | ); |
||
90 | |||
91 | $provider[] = array( |
||
92 | 'foo', |
||
93 | StringValueFormatter::WIKI_SHORT, |
||
94 | null, |
||
95 | 'foo' |
||
96 | ); |
||
97 | |||
98 | $provider[] = array( |
||
99 | 'foo', |
||
100 | StringValueFormatter::HTML_SHORT, |
||
101 | null, |
||
102 | 'foo' |
||
103 | ); |
||
104 | |||
105 | $provider[] = array( |
||
106 | 'foo', |
||
107 | StringValueFormatter::WIKI_LONG, |
||
108 | null, |
||
109 | 'foo' |
||
110 | ); |
||
111 | |||
112 | $provider[] = array( |
||
113 | 'foo', |
||
114 | StringValueFormatter::HTML_LONG, |
||
115 | null, |
||
116 | 'foo' |
||
117 | ); |
||
118 | |||
119 | // > 255 |
||
120 | $text = 'Lorem ipsum dolor sit amet consectetuer justo Nam quis lobortis vel. Sapien nulla enim Lorem enim pede ' . |
||
121 | 'lorem nulla justo diam wisi. Libero Nam turpis neque leo scelerisque nec habitasse a lacus mattis. Accumsan ' . |
||
122 | 'tincidunt Sed adipiscing nec facilisis tortor Nunc Sed ipsum tellus'; |
||
123 | |||
124 | $provider[] = array( |
||
125 | $text, |
||
126 | StringValueFormatter::HTML_LONG, |
||
127 | null, |
||
128 | 'Lorem ipsum dolor sit amet consectetuer ju <span class="smwwarning">…</span> nec facilisis tortor Nunc Sed ipsum tellus' |
||
129 | ); |
||
130 | |||
131 | $provider[] = array( |
||
132 | $text, |
||
133 | StringValueFormatter::WIKI_LONG, |
||
134 | null, |
||
135 | 'Lorem ipsum dolor sit amet consectetuer ju <span class="smwwarning">…</span> nec facilisis tortor Nunc Sed ipsum tellus' |
||
136 | ); |
||
137 | |||
138 | // XMLContentEncode |
||
139 | $provider[] = array( |
||
140 | '<foo>', |
||
141 | StringValueFormatter::HTML_LONG, |
||
142 | null, |
||
143 | '<foo>' |
||
144 | ); |
||
145 | |||
146 | $provider[] = array( |
||
147 | '<foo>', |
||
148 | StringValueFormatter::HTML_SHORT, |
||
149 | null, |
||
150 | '<foo>' |
||
151 | ); |
||
152 | |||
153 | return $provider; |
||
154 | } |
||
155 | |||
157 |
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.