Conditions | 4 |
Paths | 4 |
Total Lines | 119 |
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 |
||
174 | public function testBuildBreadcrumbs($action): void |
||
175 | { |
||
176 | $breadcrumbsBuilder = new BreadcrumbsBuilder(); |
||
177 | |||
178 | $menu = $this->prophesize(ItemInterface::class); |
||
179 | $menuFactory = $this->prophesize(MenuFactory::class); |
||
180 | $menuFactory->createItem('root')->willReturn($menu); |
||
181 | $admin = $this->prophesize(AbstractAdmin::class); |
||
182 | $admin->getMenuFactory()->willReturn($menuFactory); |
||
183 | $labelTranslatorStrategy = $this->prophesize(LabelTranslatorStrategyInterface::class); |
||
184 | |||
185 | $routeGenerator = $this->prophesize(RouteGeneratorInterface::class); |
||
186 | $routeGenerator->generate('sonata_admin_dashboard')->willReturn('/dashboard'); |
||
187 | $admin->getRouteGenerator()->willReturn($routeGenerator->reveal()); |
||
188 | $menu->addChild('link_breadcrumb_dashboard', [ |
||
189 | 'uri' => '/dashboard', |
||
190 | 'extras' => [ |
||
191 | 'translation_domain' => 'SonataAdminBundle', |
||
192 | ], |
||
193 | ])->willReturn( |
||
194 | $menu->reveal() |
||
195 | ); |
||
196 | $labelTranslatorStrategy->getLabel( |
||
197 | 'my_class_name_list', |
||
198 | 'breadcrumb', |
||
199 | 'link' |
||
200 | )->willReturn('My class'); |
||
201 | $labelTranslatorStrategy->getLabel( |
||
202 | 'my_child_class_name_list', |
||
203 | 'breadcrumb', |
||
204 | 'link' |
||
205 | )->willReturn('My child class'); |
||
206 | $labelTranslatorStrategy->getLabel( |
||
207 | 'my_child_class_name_my_action', |
||
208 | 'breadcrumb', |
||
209 | 'link' |
||
210 | )->willReturn('My action'); |
||
211 | if ('create' === $action) { |
||
212 | $labelTranslatorStrategy->getLabel( |
||
213 | 'my_class_name_create', |
||
214 | 'breadcrumb', |
||
215 | 'link' |
||
216 | )->willReturn('create my object'); |
||
217 | $menu->addChild('create my object', [ |
||
218 | 'extras' => [ |
||
219 | 'translation_domain' => 'FooBundle', |
||
220 | ], |
||
221 | ])->willReturn($menu); |
||
222 | } |
||
223 | $childAdmin = $this->prophesize(AbstractAdmin::class); |
||
224 | $childAdmin->getTranslationDomain()->willReturn('ChildBundle'); |
||
225 | $childAdmin->getLabelTranslatorStrategy()->willReturn($labelTranslatorStrategy->reveal()); |
||
226 | $childAdmin->getClassnameLabel()->willReturn('my_child_class_name'); |
||
227 | $childAdmin->hasRoute('list')->willReturn(false); |
||
228 | $childAdmin->getCurrentChildAdmin()->willReturn(null); |
||
229 | $childAdmin->hasSubject()->willReturn(false); |
||
230 | |||
231 | $admin->hasRoute('list')->willReturn(true); |
||
232 | $admin->hasAccess('list')->willReturn(true); |
||
233 | $admin->generateUrl('list')->willReturn('/myadmin/list'); |
||
234 | $admin->getCurrentChildAdmin()->willReturn( |
||
235 | 'my_action' === $action ? $childAdmin->reveal() : false |
||
236 | ); |
||
237 | if ('list' === $action) { |
||
238 | $admin->isChild()->willReturn(true); |
||
239 | $menu->setUri(false)->shouldBeCalled(); |
||
240 | } else { |
||
241 | $menu->setUri()->shouldNotBeCalled(); |
||
242 | } |
||
243 | $request = $this->prophesize(Request::class); |
||
244 | $request->get('slug')->willReturn('my-object'); |
||
245 | |||
246 | $admin->getIdParameter()->willReturn('slug'); |
||
247 | $admin->hasRoute('edit')->willReturn(false); |
||
248 | $admin->getRequest()->willReturn($request->reveal()); |
||
249 | $admin->hasSubject()->willReturn(true); |
||
250 | $admin->getSubject()->willReturn('my subject'); |
||
251 | $admin->toString('my subject')->willReturn('My subject'); |
||
252 | $admin->getTranslationDomain()->willReturn('FooBundle'); |
||
253 | $admin->getLabelTranslatorStrategy()->willReturn( |
||
254 | $labelTranslatorStrategy->reveal() |
||
255 | ); |
||
256 | $admin->getClassnameLabel()->willReturn('my_class_name'); |
||
257 | |||
258 | $menu->addChild('My class', [ |
||
259 | 'uri' => '/myadmin/list', |
||
260 | 'extras' => [ |
||
261 | 'translation_domain' => 'FooBundle', |
||
262 | ], |
||
263 | ])->willReturn($menu->reveal()); |
||
264 | $menu->addChild('My subject', [ |
||
265 | 'extras' => [ |
||
266 | 'translation_domain' => false, |
||
267 | ], |
||
268 | ])->willReturn($menu); |
||
269 | $menu->addChild('My subject', [ |
||
270 | 'uri' => null, |
||
271 | 'extras' => [ |
||
272 | 'translation_domain' => false, |
||
273 | ], |
||
274 | ])->willReturn($menu); |
||
275 | $menu->addChild('My child class', [ |
||
276 | 'extras' => [ |
||
277 | 'translation_domain' => 'ChildBundle', |
||
278 | ], |
||
279 | 'uri' => null, |
||
280 | ])->willReturn($menu); |
||
281 | $menu->setExtra('safe_label', false)->willReturn($menu); |
||
282 | $menu->addChild('My action', [ |
||
283 | 'extras' => [ |
||
284 | 'translation_domain' => 'ChildBundle', |
||
285 | ], |
||
286 | ])->willReturn($menu); |
||
287 | |||
288 | $reflection = new \ReflectionMethod('Sonata\AdminBundle\Admin\BreadcrumbsBuilder', 'buildBreadcrumbs'); |
||
289 | $reflection->setAccessible(true); |
||
290 | |||
291 | $reflection->invoke($breadcrumbsBuilder, $admin->reveal(), $action); |
||
292 | } |
||
293 | } |
||
294 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: