| Conditions | 1 |
| Paths | 1 |
| Total Lines | 100 |
| Code Lines | 92 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | 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 |
||
| 13 | { |
||
| 14 | $treeBuilder = new TreeBuilder(); |
||
| 15 | $rootNode = $treeBuilder->root('lag_admin'); |
||
| 16 | |||
| 17 | $rootNode |
||
| 18 | ->children() |
||
| 19 | // application configuration |
||
| 20 | ->append($this->getApplicationNode()) |
||
| 21 | // admins configuration |
||
| 22 | ->append($this->getAdminsConfigurationNode()) |
||
| 23 | // menus configurations |
||
| 24 | ->arrayNode('menus') |
||
| 25 | ->prototype('array') |
||
| 26 | ->children() |
||
| 27 | ->scalarNode('template')->defaultValue('LAGAdminBundle:Menu:main_menu.html.twig')->end() |
||
| 28 | ->arrayNode('main_item') |
||
| 29 | ->children() |
||
| 30 | ->scalarNode('route')->end() |
||
| 31 | ->scalarNode('title')->end() |
||
| 32 | ->end() |
||
| 33 | ->end() |
||
| 34 | ->arrayNode('items') |
||
| 35 | ->prototype('array') |
||
| 36 | ->children() |
||
| 37 | ->scalarNode('admin')->end() |
||
| 38 | ->scalarNode('action')->end() |
||
| 39 | ->scalarNode('title')->end() |
||
| 40 | ->arrayNode('permissions') |
||
| 41 | ->defaultValue(['ROLE_USER']) |
||
| 42 | ->prototype('scalar') |
||
| 43 | ->end() |
||
| 44 | ->end() |
||
| 45 | ->end() |
||
| 46 | ->end() |
||
| 47 | ->end() |
||
| 48 | ->end() |
||
| 49 | ->end() |
||
| 50 | ->end() |
||
| 51 | ->end() |
||
| 52 | ->end(); |
||
| 53 | |||
| 54 | return $treeBuilder; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return ArrayNodeDefinition|NodeDefinition |
||
| 59 | */ |
||
| 60 | public function getAdminsConfigurationNode() |
||
| 61 | { |
||
| 62 | $builder = new TreeBuilder(); |
||
| 63 | $node = $builder->root('admins'); |
||
| 64 | |||
| 65 | $node |
||
|
1 ignored issue
–
show
|
|||
| 66 | ->prototype('array') |
||
| 67 | ->children() |
||
| 68 | ->scalarNode('entity')->end() |
||
| 69 | ->scalarNode('data_provider')->end() |
||
| 70 | ->scalarNode('form')->end() |
||
| 71 | ->scalarNode('max_per_page')->end() |
||
| 72 | ->scalarNode('controller')->defaultValue('LAGAdminBundle:CRUD')->end() |
||
| 73 | // actions configurations |
||
| 74 | ->arrayNode('actions') |
||
| 75 | ->useAttributeAsKey('name') |
||
| 76 | ->prototype('array') |
||
| 77 | ->children() |
||
| 78 | ->scalarNode('title')->end() |
||
| 79 | ->arrayNode('permissions') |
||
| 80 | ->defaultValue(['ROLE_USER']) |
||
| 81 | ->prototype('scalar')->end() |
||
| 82 | ->end() |
||
| 83 | ->arrayNode('export') |
||
| 84 | ->prototype('scalar')->end() |
||
| 85 | ->end() |
||
| 86 | ->arrayNode('order') |
||
| 87 | ->prototype('array') |
||
| 88 | ->children() |
||
| 89 | ->scalarNode('field')->end() |
||
| 90 | ->scalarNode('order')->end() |
||
| 91 | ->end() |
||
| 92 | ->end() |
||
| 93 | ->end() |
||
| 94 | ->arrayNode('fields') |
||
| 95 | ->prototype('array') |
||
| 96 | ->children() |
||
| 97 | ->scalarNode('type')->end() |
||
| 98 | ->arrayNode('options') |
||
| 99 | ->prototype('variable')->end() |
||
| 100 | ->end() |
||
| 101 | ->end() |
||
| 102 | ->end() |
||
| 103 | ->end() |
||
| 104 | ->arrayNode('filters') |
||
| 105 | ->prototype('scalar')->end() |
||
| 106 | ->end() |
||
| 107 | ->arrayNode('batch') |
||
| 108 | ->prototype('variable')->end() |
||
| 109 | ->end() |
||
| 110 | ->arrayNode('actions') |
||
| 111 | ->prototype('array') |
||
| 112 | ->children() |
||
| 113 | ->scalarNode('title')->end() |
||
| 179 |
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: