Conditions | 1 |
Paths | 1 |
Total Lines | 155 |
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('sonata_notification'); |
||
30 | |||
31 | $rootNode = $treeBuilder->getRootNode()->children(); |
||
|
|||
32 | |||
33 | $backendInfo = <<<'EOF' |
||
34 | Other backends you can use: |
||
35 | |||
36 | sonata.notification.backend.postpone |
||
37 | sonata.notification.backend.doctrine |
||
38 | sonata.notification.backend.rabbitmq |
||
39 | EOF; |
||
40 | |||
41 | $iterationListenersInfo = <<<EOF |
||
42 | Listeners attached to the IterateEvent |
||
43 | Iterate event is thrown on each command iteration |
||
44 | |||
45 | Iteration listener class must implement Sonata\NotificationBundle\Event\IterationListener |
||
46 | EOF; |
||
47 | |||
48 | $rootNode |
||
49 | ->scalarNode('backend') |
||
50 | ->info($backendInfo) |
||
51 | ->defaultValue('sonata.notification.backend.runtime') |
||
52 | ->end() |
||
53 | ->append($this->getQueueNode()) |
||
54 | ->arrayNode('backends') |
||
55 | ->children() |
||
56 | ->arrayNode('doctrine') |
||
57 | ->children() |
||
58 | ->scalarNode('message_manager') |
||
59 | ->defaultValue('sonata.notification.manager.message.default') |
||
60 | ->end() |
||
61 | ->scalarNode('max_age') |
||
62 | ->info('The max age in seconds') |
||
63 | ->defaultValue(86400) |
||
64 | ->end() |
||
65 | ->scalarNode('pause') |
||
66 | ->info('The delay in microseconds') |
||
67 | ->defaultValue(500000) |
||
68 | ->end() |
||
69 | ->scalarNode('batch_size') |
||
70 | ->info('The number of items on each iteration') |
||
71 | ->defaultValue(10) |
||
72 | ->end() |
||
73 | ->arrayNode('states') |
||
74 | ->info('Raising errors level') |
||
75 | ->addDefaultsIfNotSet() |
||
76 | ->children() |
||
77 | ->integerNode('in_progress') |
||
78 | ->defaultValue(10) |
||
79 | ->end() |
||
80 | ->integerNode('error') |
||
81 | ->defaultValue(20) |
||
82 | ->end() |
||
83 | ->integerNode('open') |
||
84 | ->defaultValue(100) |
||
85 | ->end() |
||
86 | ->integerNode('done') |
||
87 | ->defaultValue(10000) |
||
88 | ->end() |
||
89 | ->end() |
||
90 | ->end() |
||
91 | ->end() |
||
92 | ->end() |
||
93 | ->arrayNode('rabbitmq') |
||
94 | ->children() |
||
95 | ->scalarNode('exchange') |
||
96 | ->cannotBeEmpty() |
||
97 | ->isRequired() |
||
98 | ->end() |
||
99 | ->arrayNode('connection') |
||
100 | ->addDefaultsIfNotSet() |
||
101 | ->children() |
||
102 | ->scalarNode('host') |
||
103 | ->defaultValue('localhost') |
||
104 | ->end() |
||
105 | ->scalarNode('port') |
||
106 | ->defaultValue(5672) |
||
107 | ->end() |
||
108 | ->scalarNode('user') |
||
109 | ->defaultValue('guest') |
||
110 | ->end() |
||
111 | ->scalarNode('pass') |
||
112 | ->defaultValue('guest') |
||
113 | ->end() |
||
114 | ->scalarNode('vhost') |
||
115 | ->defaultValue('guest') |
||
116 | ->end() |
||
117 | ->scalarNode('console_url') |
||
118 | ->defaultValue('http://localhost:55672/api') |
||
119 | ->end() |
||
120 | ->scalarNode('factory_class') |
||
121 | ->cannotBeEmpty() |
||
122 | ->defaultValue(AmqpConnectionFactory::class) |
||
123 | ->info('This option defines an AMQP connection factory to be used to establish a connection with RabbitMQ.') |
||
124 | ->end() |
||
125 | ->end() |
||
126 | ->end() |
||
127 | ->end() |
||
128 | ->end() |
||
129 | ->end() |
||
130 | ->end() |
||
131 | ->arrayNode('consumers') |
||
132 | ->addDefaultsIfNotSet() |
||
133 | ->children() |
||
134 | ->booleanNode('register_default') |
||
135 | ->info('If set to true, SwiftMailerConsumer and LoggerConsumer will be registered as services') |
||
136 | ->defaultTrue() |
||
137 | ->end() |
||
138 | ->end() |
||
139 | ->end() |
||
140 | ->arrayNode('iteration_listeners') |
||
141 | ->info($iterationListenersInfo) |
||
142 | ->defaultValue([]) |
||
143 | ->prototype('scalar')->end() |
||
144 | ->end() |
||
145 | ->arrayNode('class') |
||
146 | ->addDefaultsIfNotSet() |
||
147 | ->children() |
||
148 | ->scalarNode('message') |
||
149 | ->defaultValue('App\\Entity\\Message') |
||
150 | ->end() |
||
151 | ->end() |
||
152 | ->end() |
||
153 | ->arrayNode('admin') |
||
154 | ->addDefaultsIfNotSet() |
||
155 | ->children() |
||
156 | ->booleanNode('enabled') |
||
157 | ->defaultFalse() |
||
158 | ->end() |
||
159 | ->arrayNode('message') |
||
160 | ->addDefaultsIfNotSet() |
||
161 | ->children() |
||
162 | ->scalarNode('class') |
||
163 | ->cannotBeEmpty() |
||
164 | ->defaultValue('Sonata\\NotificationBundle\\Admin\\MessageAdmin') |
||
165 | ->end() |
||
166 | ->scalarNode('controller') |
||
167 | ->cannotBeEmpty() |
||
168 | ->defaultValue('SonataNotificationBundle:MessageAdmin') |
||
169 | ->end() |
||
170 | ->scalarNode('translation') |
||
171 | ->cannotBeEmpty() |
||
172 | ->defaultValue('SonataNotificationBundle') |
||
173 | ->end() |
||
174 | ->end() |
||
175 | ->end() |
||
176 | ->end() |
||
177 | ->end() |
||
178 | ; |
||
179 | |||
180 | return $treeBuilder; |
||
181 | } |
||
182 | |||
306 |
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: