Conditions | 1 |
Paths | 1 |
Total Lines | 103 |
Code Lines | 93 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
92 | private function getManagersNode() |
||
93 | { |
||
94 | $builder = new TreeBuilder(); |
||
95 | $node = $builder->root('managers'); |
||
96 | |||
97 | $node |
||
98 | ->isRequired() |
||
99 | ->requiresAtLeastOneElement() |
||
100 | ->useAttributeAsKey('name') |
||
101 | ->info('Maps managers to connections and bundles') |
||
102 | ->prototype('array') |
||
103 | ->children() |
||
104 | ->arrayNode('index') |
||
105 | ->children() |
||
106 | ->scalarNode('index_name') |
||
107 | ->isRequired() |
||
108 | ->info('Sets index name for connection.') |
||
109 | ->end() |
||
110 | ->arrayNode('hosts') |
||
111 | ->info('Defines hosts to connect to.') |
||
112 | ->defaultValue(['127.0.0.1:9200']) |
||
113 | ->prototype('scalar') |
||
114 | ->end() |
||
115 | ->end() |
||
116 | ->arrayNode('settings') |
||
117 | ->defaultValue([]) |
||
118 | ->info('Sets index settings for connection.') |
||
119 | ->prototype('variable')->end() |
||
120 | ->end() |
||
121 | ->arrayNode('analysis') |
||
122 | ->addDefaultsIfNotSet() |
||
123 | ->info('Sets index analysis settings for connection.') |
||
124 | ->children() |
||
125 | ->arrayNode('tokenizer')->prototype('scalar')->defaultValue([])->end()->end() |
||
126 | ->arrayNode('filter')->prototype('scalar')->defaultValue([])->end()->end() |
||
127 | ->arrayNode('analyzer')->prototype('scalar')->defaultValue([])->end()->end() |
||
128 | ->arrayNode('char_filter')->prototype('scalar')->defaultValue([])->end()->end() |
||
129 | ->end() |
||
130 | ->end() |
||
131 | ->end() |
||
132 | ->end() |
||
133 | ->integerNode('bulk_size') |
||
134 | ->min(0) |
||
135 | ->defaultValue(100) |
||
136 | ->info( |
||
137 | 'Maximum documents size in the bulk container. ' . |
||
138 | 'When the limit is reached it will auto-commit.' |
||
139 | ) |
||
140 | ->end() |
||
141 | ->enumNode('commit_mode') |
||
142 | ->values(['refresh', 'flush', 'none']) |
||
143 | ->defaultValue('refresh') |
||
144 | ->info( |
||
145 | 'The default type of commit for bulk queries.' |
||
146 | ) |
||
147 | ->end() |
||
148 | ->arrayNode('logger') |
||
149 | ->info('Enables elasticsearch queries logging') |
||
150 | ->addDefaultsIfNotSet() |
||
151 | ->beforeNormalization() |
||
152 | ->ifTrue( |
||
153 | function ($v) { |
||
154 | return is_bool($v); |
||
155 | } |
||
156 | ) |
||
157 | ->then( |
||
158 | function ($v) { |
||
159 | return ['enabled' => $v]; |
||
160 | } |
||
161 | ) |
||
162 | ->end() |
||
163 | ->children() |
||
164 | ->booleanNode('enabled') |
||
165 | ->info('enables logging') |
||
166 | ->defaultFalse() |
||
167 | ->end() |
||
168 | ->scalarNode('level') |
||
169 | ->info('Sets PSR logging level.') |
||
170 | ->defaultValue(LogLevel::WARNING) |
||
171 | ->validate() |
||
172 | ->ifNotInArray((new \ReflectionClass('Psr\Log\LogLevel'))->getConstants()) |
||
173 | ->thenInvalid('Invalid PSR log level.') |
||
174 | ->end() |
||
175 | ->end() |
||
176 | ->scalarNode('log_file_name') |
||
177 | ->info('Log filename. By default it is a manager name.') |
||
178 | ->defaultValue(null) |
||
179 | ->end() |
||
180 | ->end() |
||
181 | ->end() |
||
182 | ->arrayNode('mappings') |
||
183 | ->info('Maps manager to the bundles. f.e. AppBundle') |
||
184 | ->prototype('scalar')->end() |
||
185 | ->end() |
||
186 | ->booleanNode('force_commit') |
||
187 | ->info('Forces commit to the elasticsearch on kernel terminate event.') |
||
188 | ->defaultTrue() |
||
189 | ->end() |
||
190 | ->end() |
||
191 | ->end(); |
||
192 | |||
193 | return $node; |
||
194 | } |
||
195 | } |
||
196 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: