| Conditions | 8 |
| Paths | 24 |
| Total Lines | 75 |
| Code Lines | 41 |
| Lines | 8 |
| Ratio | 10.67 % |
| Changes | 3 | ||
| Bugs | 1 | 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 |
||
| 37 | public function settingsAction(View $page) |
||
| 38 | { |
||
| 39 | //services |
||
| 40 | $em = $this->getDoctrine()->getManager(); |
||
| 41 | |||
| 42 | $businessProperties = []; |
||
| 43 | |||
| 44 | //if the page is a business entity template page |
||
| 45 | if ($page instanceof BusinessPage || $page instanceof BusinessTemplate) { |
||
| 46 | //we can use the business entity properties on the seo |
||
| 47 | $businessEntity = $this->get('victoire_core.helper.business_entity_helper')->findById($page->getBusinessEntityId()); |
||
| 48 | $businessProperties = $businessEntity->getBusinessPropertiesByType('seoable'); |
||
| 49 | } |
||
| 50 | |||
| 51 | $pageSeo = $page->getSeo() ? $page->getSeo() : new PageSeo($page); |
||
| 52 | |||
| 53 | //url for the form |
||
| 54 | $formUrl = $this->get('router')->generate('victoire_seo_pageSeo_settings', |
||
| 55 | [ |
||
| 56 | 'id' => $page->getId(), |
||
| 57 | ] |
||
| 58 | ); |
||
| 59 | //create the form |
||
| 60 | $form = $this->get('form.factory')->create(PageSeoType::class, $pageSeo, |
||
| 61 | [ |
||
| 62 | 'action' => $formUrl, |
||
| 63 | 'method' => 'POST', |
||
| 64 | ] |
||
| 65 | ); |
||
| 66 | |||
| 67 | $form->handleRequest($this->get('request')); |
||
| 68 | $novalidate = $this->get('request')->query->get('novalidate', false); |
||
| 69 | |||
| 70 | $template = 'VictoireSeoBundle:PageSeo:form.html.twig'; |
||
| 71 | if ($novalidate === false) { |
||
| 72 | $template = 'VictoireSeoBundle:PageSeo:settings.html.twig'; |
||
| 73 | } |
||
| 74 | if (false === $novalidate && $form->isValid()) { |
||
| 75 | $em->persist($pageSeo); |
||
| 76 | $page->setSeo($pageSeo); |
||
| 77 | $em->persist($page); |
||
| 78 | $em->flush(); |
||
| 79 | |||
| 80 | //redirect to the page url |
||
| 81 | if (!method_exists($page, 'getUrl')) { |
||
| 82 | $url = $this->generateUrl('victoire_business_template_show', ['id' => $page->getId()]); |
||
| 83 | View Code Duplication | } else { |
|
| 84 | /** @var ViewReference $viewReference */ |
||
| 85 | $viewReference = $this->container->get('victoire_view_reference.repository') |
||
| 86 | ->getOneReferenceByParameters(['viewId' => $page->getId()]); |
||
| 87 | |||
| 88 | $page->setReference($viewReference); |
||
| 89 | $url = $this->generateUrl('victoire_core_page_show', ['url' => $viewReference->getUrl()]); |
||
| 90 | } |
||
| 91 | $this->get('victoire_core.current_view')->setCurrentView($page); |
||
| 92 | $this->congrat('victoire_seo.save.success'); |
||
| 93 | |||
| 94 | return new JsonResponse([ |
||
| 95 | 'success' => true, |
||
| 96 | 'url' => $url, |
||
| 97 | ]); |
||
| 98 | } |
||
| 99 | |||
| 100 | return new JsonResponse([ |
||
| 101 | 'success' => !$form->isSubmitted(), |
||
| 102 | 'html' => $this->container->get('templating')->render( |
||
| 103 | $template, |
||
| 104 | [ |
||
| 105 | 'page' => $page, |
||
| 106 | 'form' => $form->createView(), |
||
| 107 | 'businessProperties' => $businessProperties, |
||
| 108 | ] |
||
| 109 | ), |
||
| 110 | ]); |
||
| 111 | } |
||
| 112 | } |
||
| 113 |
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: