Conditions | 1 |
Paths | 1 |
Total Lines | 54 |
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 |
||
39 | public function getConfigTreeBuilder() |
||
40 | { |
||
41 | $treeBuilder = new TreeBuilder('swp_user'); |
||
42 | $treeBuilder->getRootNode() |
||
|
|||
43 | ->children() |
||
44 | ->arrayNode('from_email') |
||
45 | ->isRequired() |
||
46 | ->children() |
||
47 | ->scalarNode('address')->isRequired()->cannotBeEmpty()->end() |
||
48 | ->scalarNode('sender_name')->isRequired()->cannotBeEmpty()->end() |
||
49 | ->end() |
||
50 | ->end() |
||
51 | ->arrayNode('persistence') |
||
52 | ->addDefaultsIfNotSet() |
||
53 | ->children() |
||
54 | ->arrayNode('orm') |
||
55 | ->addDefaultsIfNotSet() |
||
56 | ->canBeEnabled() |
||
57 | ->children() |
||
58 | ->arrayNode('classes') |
||
59 | ->addDefaultsIfNotSet() |
||
60 | ->children() |
||
61 | ->arrayNode('user') |
||
62 | ->addDefaultsIfNotSet() |
||
63 | ->children() |
||
64 | ->scalarNode('model')->cannotBeEmpty()->defaultValue(User::class)->end() |
||
65 | ->scalarNode('repository')->defaultValue(EntityRepository::class)->end() |
||
66 | ->scalarNode('factory')->defaultValue(Factory::class)->end() |
||
67 | ->scalarNode('interface')->defaultValue(UserInterface::class)->end() |
||
68 | ->scalarNode('object_manager_name')->defaultValue(null)->end() |
||
69 | ->end() |
||
70 | ->end() |
||
71 | ->arrayNode('reset_password_request') |
||
72 | ->addDefaultsIfNotSet() |
||
73 | ->children() |
||
74 | ->scalarNode('model')->cannotBeEmpty()->defaultValue(ResetPasswordRequest::class)->end() |
||
75 | ->scalarNode('repository')->defaultValue(EntityRepository::class)->end() |
||
76 | ->scalarNode('factory')->defaultValue(Factory::class)->end() |
||
77 | ->scalarNode('interface')->defaultValue(UserInterface::class)->end() |
||
78 | ->scalarNode('object_manager_name')->defaultValue(null)->end() |
||
79 | ->end() |
||
80 | ->end() |
||
81 | ->end() // classes |
||
82 | ->end() |
||
83 | ->end() |
||
84 | ->end() |
||
85 | ->end() |
||
86 | ->end() |
||
87 | ; |
||
88 | |||
89 | $this->addRegistrationSection($treeBuilder->getRootNode()); |
||
90 | |||
91 | return $treeBuilder; |
||
92 | } |
||
93 | |||
132 |
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: