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.

UserTopicController::createAction()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 20
rs 9.4285
cc 1
eloc 15
nc 1
nop 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\UserTopicResponseEvent;
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 UserTopicController extends UserTopicBaseController
33
{
34
    /**
35
     *
36
     * @access public
37
     * @param  string                          $forumName
38
     * @param  int                             $topicId
39
     * @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...
40
     */
41
    public function showAction($forumName, $topicId)
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($topic = $this->getTopicModel()->findOneTopicByIdWithBoardAndCategory($topicId, true));
0 ignored issues
show
Documentation introduced by
$topic = $this->getTopic...ategory($topicId, true) is of type object<CCDNForum\ForumBundle\Entity\Topic>, 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()->canShowTopic($topic, $forum));
46
        $postsPager = $this->getPostModel()->findAllPostsPaginatedByTopicId($topicId, $this->getQuery('page', 1), $this->getPageHelper()->getPostsPerPageOnTopics(), true);
47
48
        if ($this->isGranted('ROLE_USER')) {
49
            if ($subscription = $this->getSubscriptionModel()->findOneSubscriptionForTopicByIdAndUserById($topicId, $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...
50
                $this->getSubscriptionModel()->markAsRead($subscription);
51
            }
52
        } else {
53
            $subscription = null;
54
        }
55
56
        $subscriberCount = $this->getSubscriptionModel()->countSubscriptionsForTopicById($topicId);
57
        $this->getTopicModel()->incrementViewCounter($topic);
58
        $response = $this->renderResponse('CCDNForumForumBundle:User:Topic/show.html.', array(
59
            'crumbs' => $this->getCrumbs()->addUserTopicShow($forum, $topic), 'forum' => $forum, 'topic' => $topic,
60
            'forumName' => $forumName,
61
            'pager' => $postsPager, 'subscription' => $subscription, 'subscription_count' => $subscriberCount,
62
        ));
63
64
        return $response;
65
    }
66
67
    /**
68
     *
69
     * @access public
70
     * @param  string                          $forumName
71
     * @param  int                             $boardId
72
     * @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...
73
     */
74
    public function createAction($forumName, $boardId)
75
    {
76
        $this->isAuthorised('ROLE_USER');
77
        $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...
78
        $this->isFound($board = $this->getBoardModel()->findOneBoardByIdWithCategory($boardId));
0 ignored issues
show
Documentation introduced by
$board = $this->getBoard...dWithCategory($boardId) is of type object<CCDNForum\ForumBundle\Entity\Board>, 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...
79
        $this->isAuthorised($this->getAuthorizer()->canCreateTopicOnBoard($board, $forum));
80
        $formHandler = $this->getFormHandlerToCreateTopic($forum, $board);
81
82
        $response = $this->renderResponse('CCDNForumForumBundle:User:Topic/create.html.', array(
83
            'crumbs' => $this->getCrumbs()->addUserTopicCreate($forum, $board),
84
            'forum' => $forum,
85
            'forumName' => $forumName,
86
            'board' => $board,
87
            'preview' => $formHandler->getForm()->getData(),
88
            'form' => $formHandler->getForm()->createView(),
89
        ));
90
        $this->dispatch(ForumEvents::USER_TOPIC_CREATE_RESPONSE, new UserTopicResponseEvent($this->getRequest(), $response, $formHandler->getForm()->getData()->getTopic()));
91
92
        return $response;
93
    }
94
95
    /**
96
     *
97
     * @access public
98
     * @param  string                          $forumName
99
     * @param  int                             $boardId
100
     * @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...
101
     */
102
    public function createProcessAction($forumName, $boardId)
103
    {
104
        $this->isAuthorised('ROLE_USER');
105
        $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...
106
        $this->isFound($board = $this->getBoardModel()->findOneBoardByIdWithCategory($boardId));
0 ignored issues
show
Documentation introduced by
$board = $this->getBoard...dWithCategory($boardId) is of type object<CCDNForum\ForumBundle\Entity\Board>, 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...
107
        $this->isAuthorised($this->getAuthorizer()->canCreateTopicOnBoard($board, $forum));
108
        $formHandler = $this->getFormHandlerToCreateTopic($forum, $board);
109
110
        if ($formHandler->process()) {
111
            $response = $this->redirectResponseForTopicOnPageFromPost($forumName, $formHandler->getForm()->getData()->getTopic(), $formHandler->getForm()->getData());
112
        } else {
113
            $response = $this->renderResponse('CCDNForumForumBundle:User:Topic/create.html.', array(
114
                'crumbs' => $this->getCrumbs()->addUserTopicCreate($forum, $board), 'forum' => $forum, 'board' => $board,
115
                'forumName' => $forumName,
116
                'preview' => $formHandler->getForm()->getData(), 'form' => $formHandler->getForm()->createView(),
117
            ));
118
        }
119
        $this->dispatch(ForumEvents::USER_TOPIC_CREATE_RESPONSE, new UserTopicResponseEvent($this->getRequest(), $response, $formHandler->getForm()->getData()->getTopic()));
120
121
        return $response;
122
    }
123
124
    /**
125
     *
126
     * @access public
127
     * @param  string                          $forumName
128
     * @param  int                             $topicId
129
     * @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...
130
     */
131
    public function replyAction($forumName, $topicId)
132
    {
133
        $this->isAuthorised('ROLE_USER');
134
        $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...
135
        $this->isFound($topic = $this->getTopicModel()->findOneTopicByIdWithPosts($topicId, true));
0 ignored issues
show
Documentation introduced by
$topic = $this->getTopic...thPosts($topicId, true) is of type object<CCDNForum\ForumBundle\Entity\Topic>, 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...
136
        $this->isAuthorised($this->getAuthorizer()->canReplyToTopic($topic, $forum));
137
        $formHandler = $this->getFormHandlerToReplyToTopic($topic);
138
139
        $response = $this->renderResponse('CCDNForumForumBundle:User:Topic/reply.html.', array(
140
            'crumbs' => $this->getCrumbs()->addUserTopicReply($forum, $topic),
141
            'forum' => $forum,
142
            'forumName' => $forumName,
143
            'topic' => $topic,
144
            'preview' => $formHandler->getForm()->getData(),
145
            'form' => $formHandler->getForm()->createView(),
146
        ));
147
        $this->dispatch(ForumEvents::USER_TOPIC_REPLY_RESPONSE, new UserTopicResponseEvent($this->getRequest(), $response, $formHandler->getForm()->getData()->getTopic()));
148
149
        return $response;
150
    }
151
152
    /**
153
     *
154
     * @access public
155
     * @param  string                          $forumName
156
     * @param  int                             $topicId
157
     * @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...
158
     */
159
    public function replyProcessAction($forumName, $topicId)
160
    {
161
        $this->isAuthorised('ROLE_USER');
162
        $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...
163
        $this->isFound($topic = $this->getTopicModel()->findOneTopicByIdWithPosts($topicId, true));
0 ignored issues
show
Documentation introduced by
$topic = $this->getTopic...thPosts($topicId, true) is of type object<CCDNForum\ForumBundle\Entity\Topic>, 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...
164
        $this->isAuthorised($this->getAuthorizer()->canReplyToTopic($topic, $forum));
165
        $formHandler = $this->getFormHandlerToReplyToTopic($topic);
166
167
        if ($formHandler->process()) {
168
            $response = $this->redirectResponseForTopicOnPageFromPost($forumName, $topic, $formHandler->getForm()->getData());
169
        } else {
170
            $response = $this->renderResponse('CCDNForumForumBundle:User:Topic/reply.html.', array(
171
                'crumbs' => $this->getCrumbs()->addUserTopicReply($forum, $topic), 'forum' => $forum, 'topic' => $topic,
172
                'forumName' => $forumName,
173
                'preview' => $formHandler->getForm()->getData(), 'form' => $formHandler->getForm()->createView(),
174
            ));
175
        }
176
        $this->dispatch(ForumEvents::USER_TOPIC_REPLY_RESPONSE, new UserTopicResponseEvent($this->getRequest(), $response, $formHandler->getForm()->getData()->getTopic()));
177
178
        return $response;
179
    }
180
}
181