GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

UserPostController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7
Metric Value
wmc 8
lcom 1
cbo 7
dl 0
loc 145
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A showAction() 0 22 2
A editAction() 0 20 1
A editProcessAction() 0 21 2
A deleteAction() 0 18 1
A deleteProcessAction() 0 22 2
1
<?php
2
3
/*
4
 * This file is part of the CCDNForum ForumBundle
5
 *
6
 * (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
7
 *
8
 * Available on github <http://www.github.com/codeconsortium/>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace CCDNForum\ForumBundle\Controller;
15
16
use Symfony\Component\HttpFoundation\RedirectResponse;
17
18
use CCDNForum\ForumBundle\Component\Dispatcher\ForumEvents;
19
use CCDNForum\ForumBundle\Component\Dispatcher\Event\UserPostResponseEvent;
20
21
/**
22
 *
23
 * @category CCDNForum
24
 * @package  ForumBundle
25
 *
26
 * @author   Reece Fowell <[email protected]>
27
 * @license  http://opensource.org/licenses/MIT MIT
28
 * @version  Release: 2.0
29
 * @link     https://github.com/codeconsortium/CCDNForumForumBundle
30
 *
31
 */
32
class UserPostController extends UserPostBaseController
33
{
34
    /**
35
     *
36
     * @access public
37
     * @param  string         $forumName
38
     * @param  int            $postId
39
     * @return RenderResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Symfony\Component\HttpFoundation\Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
40
     */
41
    public function showAction($forumName, $postId)
42
    {
43
        $this->isFound($forum = $this->getForumModel()->findOneForumByName($forumName));
0 ignored issues
show
Documentation introduced by
$forum = $this->getForum...ForumByName($forumName) is of type object<CCDNForum\ForumBundle\Entity\Forum>, but the function expects a object<Object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
        $this->isFound($post = $this->getPostModel()->findOnePostByIdWithTopicAndBoard($postId, true));
0 ignored issues
show
Documentation introduced by
$post = $this->getPostMo...AndBoard($postId, true) is of type object<CCDNForum\ForumBundle\Entity\Post>, but the function expects a object<Object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
45
        $this->isAuthorised($this->getAuthorizer()->canShowPost($post, $forum));
46
47
        if ($this->isGranted('ROLE_USER')) { // get the topic subscriptions.
48
            $subscription = $this->getSubscriptionModel()->findOneSubscriptionForTopicByIdAndUserById($post->getTopic()->getId(), $this->getUser()->getId());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Security\Core\User\UserInterface as the method getId() does only exist in the following implementations of said interface: CCDNForum\ForumBundle\Te...ctional\src\Entity\User.

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...
49
        } else {
50
            $subscription = null;
51
        }
52
        $subscriberCount = $this->getSubscriptionModel()->countSubscriptionsForTopicById($post->getTopic()->getId());
53
54
        return $this->renderResponse('CCDNForumForumBundle:User:Post/show.html.', array(
55
            'crumbs' => $this->getCrumbs()->addUserPostShow($forum, $post),
56
            'forum' => $forum,
57
            'forumName' => $forumName,
58
            'topic' => $post->getTopic(), 'post' => $post,
59
            'subscription' => $subscription,
60
            'subscription_count' => $subscriberCount,
61
        ));
62
    }
63
64
    /**
65
     *
66
     * @access public
67
     * @param  string                          $forumName
68
     * @param  int                             $postId
69
     * @return RedirectResponse|RenderResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Symfony\Component\HttpFoundation\Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
70
     */
71
    public function editAction($forumName, $postId)
72
    {
73
        $this->isAuthorised('ROLE_USER');
74
        $this->isFound($forum = $this->getForumModel()->findOneForumByName($forumName));
0 ignored issues
show
Documentation introduced by
$forum = $this->getForum...ForumByName($forumName) is of type object<CCDNForum\ForumBundle\Entity\Forum>, but the function expects a object<Object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75
        $this->isFound($post = $this->getPostModel()->findOnePostByIdWithTopicAndBoard($postId, true));
0 ignored issues
show
Documentation introduced by
$post = $this->getPostMo...AndBoard($postId, true) is of type object<CCDNForum\ForumBundle\Entity\Post>, but the function expects a object<Object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
        $this->isAuthorised($this->getAuthorizer()->canEditPost($post, $forum));
77
        $formHandler = $this->getFormHandlerToEditPost($post);
78
79
        $response = $this->renderResponse('CCDNForumForumBundle:User:Post/edit.html.', array(
80
            'crumbs' => $this->getCrumbs()->addUserPostShow($forum, $post),
81
            'forum' => $forum,
82
            'forumName' => $forumName,
83
            'post' => $post,
84
            'preview' => $formHandler->getForm()->getData(),
85
            'form' => $formHandler->getForm()->createView(),
86
        ));
87
        $this->dispatch(ForumEvents::USER_POST_EDIT_RESPONSE, new UserPostResponseEvent($this->getRequest(), $response, $formHandler->getForm()->getData()));
88
89
        return $response;
90
    }
91
92
    /**
93
     *
94
     * @access public
95
     * @param  string                          $forumName
96
     * @param  int                             $postId
97
     * @return RedirectResponse|RenderResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Symfony\Component\HttpFoundation\Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
98
     */
99
    public function editProcessAction($forumName, $postId)
100
    {
101
        $this->isAuthorised('ROLE_USER');
102
        $this->isFound($forum = $this->getForumModel()->findOneForumByName($forumName));
0 ignored issues
show
Documentation introduced by
$forum = $this->getForum...ForumByName($forumName) is of type object<CCDNForum\ForumBundle\Entity\Forum>, but the function expects a object<Object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
103
        $this->isFound($post = $this->getPostModel()->findOnePostByIdWithTopicAndBoard($postId, true));
0 ignored issues
show
Documentation introduced by
$post = $this->getPostMo...AndBoard($postId, true) is of type object<CCDNForum\ForumBundle\Entity\Post>, but the function expects a object<Object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
104
        $this->isAuthorised($this->getAuthorizer()->canEditPost($post, $forum));
105
        $formHandler = $this->getFormHandlerToEditPost($post);
106
107
        if ($formHandler->process()) {
108
            $response = $this->redirectResponseForTopicOnPageFromPost($forumName, $formHandler->getForm()->getData()->getTopic(), $formHandler->getForm()->getData());
109
        } else {
110
            $response = $this->renderResponse('CCDNForumForumBundle:User:Post/edit.html.', array(
111
                'crumbs' => $this->getCrumbs()->addUserPostShow($forum, $post), 'forum' => $forum, 'post' => $post,
112
                'forumName' => $forumName,
113
                'preview' => $formHandler->getForm()->getData(), 'form' => $formHandler->getForm()->createView(),
114
            ));
115
        }
116
        $this->dispatch(ForumEvents::USER_POST_EDIT_RESPONSE, new UserPostResponseEvent($this->getRequest(), $response, $formHandler->getForm()->getData()));
117
118
        return $response;
119
    }
120
121
    /**
122
     *
123
     * @access public
124
     * @param  string                          $forumName
125
     * @param  int                             $postId
126
     * @return RedirectResponse|RenderResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Symfony\Component\HttpFoundation\Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
127
     */
128
    public function deleteAction($forumName, $postId)
129
    {
130
        $this->isAuthorised('ROLE_USER');
131
        $this->isFound($forum = $this->getForumModel()->findOneForumByName($forumName));
0 ignored issues
show
Documentation introduced by
$forum = $this->getForum...ForumByName($forumName) is of type object<CCDNForum\ForumBundle\Entity\Forum>, but the function expects a object<Object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
132
        $this->isFound($post = $this->getPostModel()->findOnePostByIdWithTopicAndBoard($postId, true));
0 ignored issues
show
Documentation introduced by
$post = $this->getPostMo...AndBoard($postId, true) is of type object<CCDNForum\ForumBundle\Entity\Post>, but the function expects a object<Object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
133
        $this->isAuthorised($this->getAuthorizer()->canDeletePost($post, $forum));
134
        $formHandler = $this->getFormHandlerToDeletePost($post);
135
        $response = $this->renderResponse('CCDNForumForumBundle:User:Post/delete.html.', array(
136
            'crumbs' => $this->getCrumbs()->addUserPostDelete($forum, $post),
137
            'forum' => $forum,
138
            'forumName' => $forumName,
139
            'post' => $post,
140
            'form' => $formHandler->getForm()->createView(),
141
        ));
142
        $this->dispatch(ForumEvents::USER_POST_SOFT_DELETE_RESPONSE, new UserPostResponseEvent($this->getRequest(), $response, $formHandler->getForm()->getData()));
143
144
        return $response;
145
    }
146
147
    /**
148
     *
149
     * @access public
150
     * @param  string                          $forumName
151
     * @param  int                             $postId
152
     * @return RedirectResponse|RenderResponse
0 ignored issues
show
Documentation introduced by
Should the return type not be \Symfony\Component\HttpFoundation\Response?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
153
     */
154
    public function deleteProcessAction($forumName, $postId)
155
    {
156
        $this->isAuthorised('ROLE_USER');
157
        $this->isFound($forum = $this->getForumModel()->findOneForumByName($forumName));
0 ignored issues
show
Documentation introduced by
$forum = $this->getForum...ForumByName($forumName) is of type object<CCDNForum\ForumBundle\Entity\Forum>, but the function expects a object<Object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
158
        $this->isFound($post = $this->getPostModel()->findOnePostByIdWithTopicAndBoard($postId, true));
0 ignored issues
show
Documentation introduced by
$post = $this->getPostMo...AndBoard($postId, true) is of type object<CCDNForum\ForumBundle\Entity\Post>, but the function expects a object<Object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
159
        $this->isAuthorised($this->getAuthorizer()->canDeletePost($post, $forum));
160
        $formHandler = $this->getFormHandlerToDeletePost($post);
161
162
        if ($formHandler->process()) {
163
            $response = $this->redirectResponseForTopicOnPageFromPost($forumName, $post->getTopic(), $post);
0 ignored issues
show
Compatibility introduced by
$post->getTopic() of type object<CCDNForum\ForumBundle\Entity\Model\Topic> is not a sub-type of object<CCDNForum\ForumBundle\Entity\Topic>. It seems like you assume a child class of the class CCDNForum\ForumBundle\Entity\Model\Topic to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
164
        } else {
165
            $response = $this->renderResponse('CCDNForumForumBundle:User:Post/delete.html.', array(
166
                'crumbs' => $this->getCrumbs()->addUserPostShow($forum, $post),
167
                'forum' => $forum, 'post' => $post,
168
                'forumName' => $forumName,
169
                'form' => $formHandler->getForm()->createView(),
170
            ));
171
        }
172
        $this->dispatch(ForumEvents::USER_POST_SOFT_DELETE_RESPONSE, new UserPostResponseEvent($this->getRequest(), $response, $formHandler->getForm()->getData()));
173
174
        return $response;
175
    }
176
}
177