Conditions | 1 |
Paths | 1 |
Total Lines | 71 |
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 |
||
27 | public function getConfigTreeBuilder() |
||
28 | { |
||
29 | $treeBuilder = new TreeBuilder('takeit_amp_html'); |
||
30 | $treeBuilder->getRootNode() |
||
|
|||
31 | ->canBeDisabled() |
||
32 | ->info('Enable or disable Google AMP HTML support.') |
||
33 | ->addDefaultsIfNotSet() |
||
34 | ->children() |
||
35 | ->arrayNode('theme') |
||
36 | ->addDefaultsIfNotSet() |
||
37 | ->children() |
||
38 | ->scalarNode('loader') |
||
39 | ->defaultValue('takeit_amp_html.loader.theme.default') |
||
40 | ->cannotBeEmpty() |
||
41 | ->end() |
||
42 | ->scalarNode('theme_path') |
||
43 | ->defaultValue('%kernel.root_dir%/Resources/amp/amp-theme') |
||
44 | ->end() |
||
45 | ->end() |
||
46 | ->end() |
||
47 | ->scalarNode('checker') |
||
48 | ->defaultValue('takeit_amp_html.checker.default') |
||
49 | ->cannotBeEmpty() |
||
50 | ->end() |
||
51 | ->arrayNode('routing') |
||
52 | ->isRequired() |
||
53 | ->addDefaultsIfNotSet() |
||
54 | ->children() |
||
55 | ->arrayNode('route_strategy') |
||
56 | ->canBeEnabled() |
||
57 | ->addDefaultsIfNotSet() |
||
58 | ->children() |
||
59 | ->scalarNode('prefix') |
||
60 | ->defaultValue('/platform/amp') |
||
61 | ->end() |
||
62 | ->scalarNode('pattern') |
||
63 | ->isRequired() |
||
64 | ->cannotBeEmpty() |
||
65 | ->end() |
||
66 | ->scalarNode('parameter') |
||
67 | ->cannotBeEmpty() |
||
68 | ->defaultValue('slug') |
||
69 | ->end() |
||
70 | ->scalarNode('parameterRegex') |
||
71 | ->defaultValue('.+') |
||
72 | ->info('Parameter\'s regular expression.') |
||
73 | ->end() |
||
74 | ->end() |
||
75 | ->end() |
||
76 | ->arrayNode('parameter_strategy') |
||
77 | ->canBeEnabled() |
||
78 | ->end() |
||
79 | ->scalarNode('controller') |
||
80 | ->defaultValue('takeit_amp_html.amp_controller:viewAction') |
||
81 | ->end() |
||
82 | ->end() |
||
83 | ->end() |
||
84 | ->scalarNode('model') |
||
85 | ->cannotBeEmpty() |
||
86 | ->isRequired() |
||
87 | ->info('Fully qualified class name of your model.') |
||
88 | ->end() |
||
89 | ->scalarNode('converter') |
||
90 | ->defaultValue('takeit_amp_html.amp_converter') |
||
91 | ->info('Custom converter service id.') |
||
92 | ->end() |
||
93 | ->end() |
||
94 | ; |
||
95 | |||
96 | return $treeBuilder; |
||
97 | } |
||
98 | } |
||
99 |
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: