Conditions | 2 |
Paths | 2 |
Total Lines | 55 |
Code Lines | 31 |
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 |
||
19 | public function init($params) |
||
20 | { |
||
21 | $nbObjects = $params['nb_objects']; |
||
22 | |||
23 | for ($i = 0; $i < $nbObjects; $i++) { |
||
24 | $objects[] = new Article('hello', $i); |
||
|
|||
25 | } |
||
26 | |||
27 | $container = $this->createContainer([ |
||
28 | 'mapping' => [ |
||
29 | Article::class => [ |
||
30 | 'grids' => [ |
||
31 | 'main' => [ |
||
32 | 'columns' => [ |
||
33 | 'title' => [ |
||
34 | 'type' => 'property', |
||
35 | ], |
||
36 | 'number' => [ |
||
37 | 'type' => 'property', |
||
38 | ], |
||
39 | ], |
||
40 | 'filters' => [ |
||
41 | 'title' => [ |
||
42 | 'type' => 'string', |
||
43 | ], |
||
44 | ], |
||
45 | ], |
||
46 | 'form' => [ |
||
47 | 'columns' => [ |
||
48 | 'select' => [ |
||
49 | 'type' => 'select', |
||
50 | ], |
||
51 | 'title' => [ |
||
52 | 'type' => 'property', |
||
53 | ], |
||
54 | 'number' => [ |
||
55 | 'type' => 'property', |
||
56 | ], |
||
57 | ], |
||
58 | 'filters' => [ |
||
59 | 'title' => [ |
||
60 | 'type' => 'string', |
||
61 | ], |
||
62 | ], |
||
63 | ], |
||
64 | ], |
||
65 | ], |
||
66 | ], |
||
67 | 'collections' => [ |
||
68 | Article::class => $objects, |
||
69 | ], |
||
70 | ]); |
||
71 | |||
72 | $this->factory = $container->get('grid.factory'); |
||
73 | } |
||
74 | |||
108 |
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.