CommentController::showAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/*
3
 * Copyright 2014-2016 Arnaud Bienvenu
4
 *
5
 * This file is part of Kyela.
6
7
 * Kyela is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
12
 * Kyela is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with Kyela.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace Abienvenu\KyelaBundle\Controller;
23
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\Routing\Annotation\Route;
26
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
27
use Abienvenu\KyelaBundle\Entity\Comment;
28
use Abienvenu\KyelaBundle\Form\Type\CommentType;
29
30
/**
31
 * Comment controller.
32
 *
33
 * @Route("/{pollUrl}/comment")
34
 */
35
class CommentController extends CRUDController
36
{
37
    protected $entityName = 'KyelaBundle:Comment';
38
    protected $cancelRoute = 'poll_show';
39
    protected $successRoute = 'poll_show';
40
    protected $deleteRoute = 'comment_delete';
41
    protected $deleteSuccessRoute = 'poll_show';
42
43
    /**
44
     * Displays latest comments
45
     *
46
     * @Route("/", methods="GET")
47
     * @Template()
48
     */
49
    public function showAction()
50
    {
51
        $em = $this->getDoctrine()->getManager();
52
        $comments = $em->getRepository('KyelaBundle:Comment')->getLatestComments($this->poll);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getLatestComments() does only exist in the following implementations of said interface: Abienvenu\KyelaBundle\Entity\CommentRepository.

Let’s take a look at an example:

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

class MyUser implements 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 implementation 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 interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
53
        return ['poll' => $this->poll, 'comments' => $comments];
54
    }
55
56
    /**
57
     * Displays a form to create a new Comment entity.
58
     *
59
     * @Route("/new", name="comment_new", methods={"GET", "POST"})
60
     * @Template()
61
     */
62
    public function newAction(Request $request)
63
    {
64
        $comment = new Comment();
65
        if ($request->isMethod('POST'))
66
        {
67
            $comment->setDatetime(new \DateTime());
68
        }
69
        return $this->doNewAction(CommentType::class, $comment, $request, null, ['authors' => $this->poll->getParticipantsAsArrayOfNames()]);
70
    }
71
72
    /**
73
     * Displays a form to edit an existing Comment entity.
74
     *
75
     * @Route("/{id}/edit", name="comment_edit", methods={"GET", "PUT"})
76
     * @Template()
77
     */
78
    public function editAction(Request $request, $id)
79
    {
80
        return $this->doEditAction(CommentType::class, $id, $request, ['authors' => $this->poll->getParticipantsAsArrayOfNames()]);
81
    }
82
83
    /**
84
     * Deletes a Comment entity.
85
     *
86
     * @Route("/{id}", name="comment_delete", methods="DELETE")
87
     */
88
    public function deleteAction(Request $request, $id)
89
    {
90
        return $this->doDeleteAction($request, $id);
91
    }
92
}
93