| Conditions | 1 |
| Paths | 1 |
| Total Lines | 67 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 96 | private function addGridsSection(ArrayNodeDefinition $node) |
||
| 97 | { |
||
| 98 | $node |
||
| 99 | ->children() |
||
| 100 | ->arrayNode('grids') |
||
| 101 | ->useAttributeAsKey('code') |
||
| 102 | ->prototype('array') |
||
| 103 | ->children() |
||
| 104 | ->arrayNode('driver') |
||
| 105 | ->addDefaultsIfNotSet() |
||
| 106 | ->children() |
||
| 107 | ->scalarNode('name')->defaultValue(DoctrineORMDriver::NAME)->end() |
||
| 108 | ->arrayNode('options') |
||
| 109 | ->prototype('variable')->end() |
||
| 110 | ->defaultValue([]) |
||
| 111 | ->end() |
||
| 112 | ->end() |
||
| 113 | ->end() |
||
| 114 | ->arrayNode('sorting') |
||
| 115 | ->prototype('scalar')->end() |
||
| 116 | ->end() |
||
| 117 | ->arrayNode('fields') |
||
| 118 | ->useAttributeAsKey('name') |
||
| 119 | ->prototype('array') |
||
| 120 | ->children() |
||
| 121 | ->scalarNode('type')->isRequired()->cannotBeEmpty()->end() |
||
| 122 | ->scalarNode('label')->cannotBeEmpty()->end() |
||
| 123 | ->scalarNode('path')->cannotBeEmpty()->end() |
||
| 124 | ->arrayNode('options') |
||
| 125 | ->prototype('variable')->end() |
||
| 126 | ->end() |
||
| 127 | ->end() |
||
| 128 | ->end() |
||
| 129 | ->end() |
||
| 130 | ->arrayNode('filters') |
||
| 131 | ->useAttributeAsKey('name') |
||
| 132 | ->prototype('array') |
||
| 133 | ->children() |
||
| 134 | ->scalarNode('type')->isRequired()->cannotBeEmpty()->end() |
||
| 135 | ->scalarNode('label')->cannotBeEmpty()->end() |
||
| 136 | ->arrayNode('options') |
||
| 137 | ->prototype('variable')->end() |
||
| 138 | ->end() |
||
| 139 | ->end() |
||
| 140 | ->end() |
||
| 141 | ->end() |
||
| 142 | ->arrayNode('actions') |
||
| 143 | ->useAttributeAsKey('name') |
||
| 144 | ->prototype('array') |
||
| 145 | ->useAttributeAsKey('name') |
||
| 146 | ->prototype('array') |
||
| 147 | ->children() |
||
| 148 | ->scalarNode('type')->isRequired()->end() |
||
| 149 | ->scalarNode('label')->end() |
||
| 150 | ->arrayNode('options') |
||
| 151 | ->prototype('variable')->end() |
||
| 152 | ->end() |
||
| 153 | ->end() |
||
| 154 | ->end() |
||
| 155 | ->end() |
||
| 156 | ->end() |
||
| 157 | ->end() |
||
| 158 | ->end() |
||
| 159 | ->end() |
||
| 160 | ->end() |
||
| 161 | ; |
||
| 162 | } |
||
| 163 | } |
||
| 164 |
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.