Conditions | 1 |
Paths | 1 |
Total Lines | 72 |
Code Lines | 69 |
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 |
||
59 | private function addResourcesSection(ArrayNodeDefinition $node) |
||
60 | { |
||
61 | $node |
||
62 | ->children() |
||
63 | ->arrayNode('resources') |
||
64 | ->useAttributeAsKey('name') |
||
65 | ->prototype('array') |
||
66 | ->children() |
||
67 | ->scalarNode('driver')->defaultValue(SyliusResourceBundle::DRIVER_DOCTRINE_ORM)->end() |
||
68 | ->variableNode('options')->end() |
||
69 | ->scalarNode('templates')->cannotBeEmpty()->end() |
||
70 | ->arrayNode('classes') |
||
71 | ->isRequired() |
||
72 | ->addDefaultsIfNotSet() |
||
73 | ->children() |
||
74 | ->scalarNode('model')->isRequired()->cannotBeEmpty()->end() |
||
75 | ->scalarNode('interface')->cannotBeEmpty()->end() |
||
76 | ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() |
||
77 | ->scalarNode('repository')->cannotBeEmpty()->end() |
||
78 | ->scalarNode('factory')->defaultValue(Factory::class)->end() |
||
79 | ->arrayNode('form') |
||
80 | ->prototype('scalar')->end() |
||
81 | ->end() |
||
82 | ->end() |
||
83 | ->end() |
||
84 | ->arrayNode('validation_groups') |
||
85 | ->addDefaultsIfNotSet() |
||
86 | ->children() |
||
87 | ->arrayNode('default') |
||
88 | ->prototype('scalar')->end() |
||
89 | ->defaultValue([]) |
||
90 | ->end() |
||
91 | ->end() |
||
92 | ->end() |
||
93 | ->arrayNode('translation') |
||
94 | ->children() |
||
95 | ->variableNode('options')->end() |
||
96 | ->arrayNode('classes') |
||
97 | ->isRequired() |
||
98 | ->addDefaultsIfNotSet() |
||
99 | ->children() |
||
100 | ->scalarNode('model')->isRequired()->cannotBeEmpty()->end() |
||
101 | ->scalarNode('interface')->cannotBeEmpty()->end() |
||
102 | ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() |
||
103 | ->scalarNode('repository')->cannotBeEmpty()->end() |
||
104 | ->scalarNode('factory')->defaultValue(Factory::class)->end() |
||
105 | ->arrayNode('form') |
||
106 | ->prototype('scalar')->end() |
||
107 | ->end() |
||
108 | ->end() |
||
109 | ->end() |
||
110 | ->arrayNode('validation_groups') |
||
111 | ->addDefaultsIfNotSet() |
||
112 | ->children() |
||
113 | ->arrayNode('default') |
||
114 | ->prototype('scalar')->end() |
||
115 | ->defaultValue([]) |
||
116 | ->end() |
||
117 | ->end() |
||
118 | ->end() |
||
119 | ->arrayNode('fields') |
||
120 | ->prototype('scalar')->end() |
||
121 | ->defaultValue([]) |
||
122 | ->end() |
||
123 | ->end() |
||
124 | ->end() |
||
125 | ->end() |
||
126 | ->end() |
||
127 | ->end() |
||
128 | ->end() |
||
129 | ; |
||
130 | } |
||
131 | |||
210 |
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.