EventController::newAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Event;
28
use Abienvenu\KyelaBundle\Form\Type\EventType;
29
30
/**
31
 * Event controller.
32
 *
33
 * @Route("/{pollUrl}/event")
34
 */
35
class EventController extends CRUDController
36
{
37
    protected $entityName = 'KyelaBundle:Event';
38
    protected $cancelRoute = 'poll_show';
39
    protected $successRoute = 'poll_show';
40
    protected $deleteRoute = 'event_delete';
41
    protected $deleteSuccessRoute = 'poll_show';
42
43
    /**
44
     * Displays poll events
45
     *
46
     * @Route("", methods="GET")
47
     * @Template()
48
     */
49
    public function showAction($isFuture)
50
    {
51
        $em = $this->getDoctrine()->getManager();
52
        $events = $em->getRepository('KyelaBundle:Event')->getFutureOrPastEvents($this->poll, $isFuture);
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 getFutureOrPastEvents() does only exist in the following implementations of said interface: Abienvenu\KyelaBundle\Entity\EventRepository.

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 [
54
            'poll' => $this->poll,
55
            'events' => $events,
56
        ];
57
    }
58
59
    /**
60
     * Displays a form to create a new Event entity.
61
     *
62
     * @Route("/new", name="event_new", methods={"GET", "POST"})
63
     * @Template()
64
     */
65
    public function newAction(Request $request)
66
    {
67
        return $this->doNewAction(EventType::class, new Event(), $request);
68
    }
69
70
    /**
71
     * Displays a form to edit an existing Event entity.
72
     *
73
     * @Route("/{id}/edit", name="event_edit", methods={"GET", "PUT"})
74
     * @Template()
75
     */
76
    public function editAction(Request $request, $id)
77
    {
78
        return $this->doEditAction(EventType::class, $id, $request);
79
    }
80
81
    /**
82
     * Deletes a Event entity.
83
     *
84
     * @Route("/{id}", name="event_delete", methods="DELETE")
85
     */
86
    public function deleteAction(Request $request, $id)
87
    {
88
        return $this->doDeleteAction($request, $id);
89
    }
90
}
91