Conditions | 1 |
Paths | 1 |
Total Lines | 65 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
101 | private function addResourcesSection(ArrayNodeDefinition $node) |
||
102 | { |
||
103 | $node |
||
104 | ->children() |
||
105 | ->arrayNode('resources') |
||
106 | ->addDefaultsIfNotSet() |
||
107 | ->children() |
||
108 | ->arrayNode('user') |
||
109 | ->addDefaultsIfNotSet() |
||
110 | ->children() |
||
111 | ->scalarNode('templates')->defaultValue('SyliusUserBundle:User')->end() |
||
112 | ->variableNode('options')->end() |
||
113 | ->arrayNode('classes') |
||
114 | ->addDefaultsIfNotSet() |
||
115 | ->children() |
||
116 | ->scalarNode('model')->defaultValue(User::class)->cannotBeEmpty()->end() |
||
117 | ->scalarNode('interface')->defaultValue(UserInterface::class)->cannotBeEmpty()->end() |
||
118 | ->scalarNode('controller')->defaultValue(UserController::class)->cannotBeEmpty()->end() |
||
119 | ->scalarNode('repository')->cannotBeEmpty()->end() |
||
120 | ->scalarNode('factory')->defaultValue(Factory::class)->end() |
||
121 | ->arrayNode('form') |
||
122 | ->addDefaultsIfNotSet() |
||
123 | ->children() |
||
124 | ->scalarNode('default')->defaultValue(UserType::class)->cannotBeEmpty()->end() |
||
125 | ->scalarNode('registration')->defaultValue(UserRegistrationType::class)->cannotBeEmpty()->end() |
||
126 | ->end() |
||
127 | ->end() |
||
128 | ->end() |
||
129 | ->end() |
||
130 | ->arrayNode('validation_groups') |
||
131 | ->addDefaultsIfNotSet() |
||
132 | ->children() |
||
133 | ->arrayNode('default') |
||
134 | ->prototype('scalar')->end() |
||
135 | ->defaultValue(['sylius']) |
||
136 | ->end() |
||
137 | ->arrayNode('registration') |
||
138 | ->prototype('scalar')->end() |
||
139 | ->defaultValue(['sylius', 'sylius_user_registration']) |
||
140 | ->end() |
||
141 | ->end() |
||
142 | ->end() |
||
143 | ->end() |
||
144 | ->end() |
||
145 | ->arrayNode('user_oauth') |
||
146 | ->addDefaultsIfNotSet() |
||
147 | ->children() |
||
148 | ->variableNode('options')->end() |
||
149 | ->arrayNode('classes') |
||
150 | ->addDefaultsIfNotSet() |
||
151 | ->children() |
||
152 | ->scalarNode('model')->defaultValue(UserOAuth::class)->cannotBeEmpty()->end() |
||
153 | ->scalarNode('interface')->defaultValue(UserOAuthInterface::class)->cannotBeEmpty()->end() |
||
154 | ->scalarNode('controller')->defaultValue(ResourceController::class)->cannotBeEmpty()->end() |
||
155 | ->scalarNode('repository')->cannotBeEmpty()->end() |
||
156 | ->scalarNode('factory')->defaultValue(Factory::class)->end() |
||
157 | ->end() |
||
158 | ->end() |
||
159 | ->end() |
||
160 | ->end() |
||
161 | ->end() |
||
162 | ->end() |
||
163 | ->end() |
||
164 | ; |
||
165 | } |
||
166 | } |
||
167 |
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: