Completed
Push — develop ( 95e034...2642c0 )
by Victor
09:51 queued 02:58
created

TagController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 21.43%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 4
c 1
b 0
f 1
lcom 1
cbo 8
dl 0
loc 61
ccs 6
cts 28
cp 0.2143
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A roleAction() 0 10 1
B editRoleAction() 0 32 3
1
<?php
2
3
namespace AppBundle\Controller\Admin;
4
5
use AppBundle\Entity\Tag;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
11
use Symfony\Component\Form\Extension\Core\Type\TextType;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * Class AdminController
16
 * @package AppBundle\Controller\Admin
17
 * @Route("/admin")
18
 */
19
class TagController extends Controller
20
{
21
    /**
22
     * @Method("GET")
23
     * @Route("/tags", name="tagsAdmin")
24
     * @Template("AppBundle:admin:tags.html.twig")
25
     *
26
     * @return Response
27
     */
28 1
    public function roleAction()
29
    {
30 1
        $em = $this->getDoctrine()->getManager();
31 1
        $tags = $em->getRepository('AppBundle:Tag')
32 1
            ->findAll();
33
34
        return [
35 1
            'tags'  => $tags,
36 1
        ];
37
    }
38
39
    /**
40
     * @param Request $request
41
     * @Route("/tag/new", name="tagNew")
42
     * @Method({"GET", "POST"})
43
     * @Template("AppBundle:admin/form:tag.html.twig")
44
     *
45
     * @return Response
46
     */
47
    public function editRoleAction(Request $request)
48
    {
49
        $em = $this->getDoctrine()->getManager();
50
51
        $tag = new Tag();
52
        $title = 'Create new tag';
53
54
        $form = $this->createFormBuilder($tag)
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class Symfony\Component\Form\FormConfigBuilder as the method add() does only exist in the following sub-classes of Symfony\Component\Form\FormConfigBuilder: Symfony\Component\Form\FormBuilder. 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...
55
            ->setAction($this->generateUrl('tagNew'))
56
            ->setMethod('POST')
57
            ->add('name', TextType::class, array(
58
                    'attr' => array('placeholder' => '* Tag name'),
59
                )
60
            )
61
            ->add('save', SubmitType::class, array('label' => 'Save'))
62
            ->getForm();
63
64
        if ($request->getMethod() == 'POST') {
65
            $form->handleRequest($request);
66
            if ($form->isValid()) {
67
                $em->persist($tag);
68
                $em->flush();
69
70
                return $this->redirectToRoute('tagsAdmin');
71
            }
72
        }
73
74
        return [
75
            'title' => $title,
76
            'form'  => $form->createView(),
77
        ];
78
    }
79
}
80