Completed
Push — master ( 6e39d0...b8063b )
by Paul
10s
created

PageSeoController::settingsAction()   C

Complexity

Conditions 8
Paths 24

Size

Total Lines 75
Code Lines 41

Duplication

Lines 8
Ratio 10.67 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 8
loc 75
rs 6.2413
cc 8
eloc 41
nc 24
nop 1

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace Victoire\Bundle\SeoBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessPage;
10
use Victoire\Bundle\BusinessPageBundle\Entity\BusinessTemplate;
11
use Victoire\Bundle\CoreBundle\Controller\VictoireAlertifyControllerTrait;
12
use Victoire\Bundle\CoreBundle\Entity\View;
13
use Victoire\Bundle\PageBundle\Entity\BasePage;
14
use Victoire\Bundle\SeoBundle\Entity\PageSeo;
15
use Victoire\Bundle\SeoBundle\Form\PageSeoType;
16
use Victoire\Bundle\ViewReferenceBundle\ViewReference\ViewReference;
17
18
/**
19
 * The Page seo controller.
20
 *
21
 * @Route("/victoire-dcms/seo")
22
 */
23
class PageSeoController extends Controller
24
{
25
    use VictoireAlertifyControllerTrait;
26
27
    /**
28
     * BasePage settings.
29
     *
30
     * @param BasePage $page
31
     *
32
     * @Route("/{id}", name="victoire_seo_pageSeo_settings")
33
     * @Template()
34
     *
35
     * @return JsonResponse
36
     */
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);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\CoreBundle\Entity\View as the method getSeo() does only exist in the following sub-classes of Victoire\Bundle\CoreBundle\Entity\View: Victoire\Bundle\BlogBundle\Entity\ArticleTemplate, Victoire\Bundle\BlogBundle\Entity\Blog, Victoire\Bundle\Business...dle\Entity\BusinessPage, Victoire\Bundle\Business...Entity\BusinessTemplate, Victoire\Bundle\Business...ity\VirtualBusinessPage, Victoire\Bundle\PageBundle\Entity\BasePage, Victoire\Bundle\PageBundle\Entity\Page. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
Unused Code introduced by
The call to PageSeo::__construct() has too many arguments starting with $page.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Victoire\Bundle\CoreBundle\Entity\View as the method setSeo() does only exist in the following sub-classes of Victoire\Bundle\CoreBundle\Entity\View: Victoire\Bundle\BlogBundle\Entity\ArticleTemplate, Victoire\Bundle\BlogBundle\Entity\Blog, Victoire\Bundle\Business...dle\Entity\BusinessPage, Victoire\Bundle\Business...Entity\BusinessTemplate, Victoire\Bundle\Business...ity\VirtualBusinessPage, Victoire\Bundle\PageBundle\Entity\BasePage, Victoire\Bundle\PageBundle\Entity\Page. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
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 {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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