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.

UserSubscriptionController::indexAction()   C
last analyzed

Complexity

Conditions 11
Paths 88

Size

Total Lines 76
Code Lines 52

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 76
rs 5.4429
cc 11
eloc 52
nc 88
nop 1

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\UserTopicEvent;
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 UserSubscriptionController extends BaseController
33
{
34
    /**
35
     *
36
     * @access public
37
     * @param  string         $forumName
38
     * @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...
39
     */
40
    public function indexAction($forumName)
41
    {
42
        $this->isAuthorised('ROLE_USER');
43
44
        if ($forumName != '~') {
45
            $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...
46
        } else {
47
            $forum = null;
48
        }
49
50
        $page = $this->getQuery('page', 1);
51
        $filter = $this->getQuery('filter', 'all');
52
        // Use this for the sidebar counters
53
        $subscriptionForums = $this->getSubscriptionModel()->findAllSubscriptionsForUserById($this->getUser()->getId(), true);
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...
54
        $forumsSubscribed = array();
55
        $totalForumsSubscribed = array('count_read' => 0, 'count_unread' => 0, 'count_total' => 0);
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $totalForumsSubscribed exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
56
        foreach ($subscriptionForums as $subscription) {
57
            $forumSubscribed = $subscription->getForum();
58
59
            if ($forumSubscribed) {
60
                $forumSubscribedId = $forumSubscribed->getId();
61
62
                if (! array_key_exists($forumSubscribedId, $forumsSubscribed)) {
63
                    $forumsSubscribed[$forumSubscribedId] = array(
64
                        'forum' => $forumSubscribed,
65
                        'count_read' => 0,
66
                        'count_unread' => 0,
67
                        'count_total' => 0,
68
                    );
69
                }
70
71
                $forumsSubscribed[$forumSubscribedId]['count_total']++;
72
                if ($subscription->isRead()) {
73
                    $forumsSubscribed[$forumSubscribedId]['count_read']++;
74
                } else {
75
                    $forumsSubscribed[$forumSubscribedId]['count_unread']++;
76
                }
77
78
                if ($forum) {
79
                    if ($forum->getId() == $forumSubscribedId) {
80
                        $totalForumsSubscribed['count_total']++;
81
                        if ($subscription->isRead()) {
82
                            $totalForumsSubscribed['count_read']++;
83
                        } else {
84
                            $totalForumsSubscribed['count_unread']++;
85
                        }
86
                    }
87
                } else {
88
                    $totalForumsSubscribed['count_total']++;
89
                    if ($subscription->isRead()) {
90
                        $totalForumsSubscribed['count_read']++;
91
                    } else {
92
                        $totalForumsSubscribed['count_unread']++;
93
                    }
94
                }
95
            }
96
        }
97
98
        // Use this for the ALL/READ/UNREAD tab
99
        $itemsPerPage = $this->getPageHelper()->getTopicsPerPageOnSubscriptions();
100
        if ($forumName == '~') {
101
            $subscriptionPager = $this->getSubscriptionModel()->findAllSubscriptionsPaginatedForUserById($this->getUser()->getId(), $page, $itemsPerPage, $filter, true);
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...
102
        } else {
103
            $subscriptionPager = $this->getSubscriptionModel()->findAllSubscriptionsPaginatedForUserByIdAndForumById($forum->getId(), $this->getUser()->getId(), $page, $itemsPerPage, $filter, true);
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...
104
        }
105
106
        return $this->renderResponse('CCDNForumForumBundle:User:Subscription/show.html.', array(
107
            'forum' => $forum,
108
            'forumName' => $forumName,
109
            'subscribed_forums' => $forumsSubscribed,
110
            'total_subscribed_forums' => $totalForumsSubscribed,
111
            'filter' => $filter,
112
            'pager' => $subscriptionPager,
113
            'posts_per_page' => $this->container->getParameter('ccdn_forum_forum.topic.user.show.posts_per_page')
114
        ));
115
    }
116
117
    /**
118
     *
119
     * @access public
120
     * @param  string           $forumName
121
     * @param  int              $topicId
122
     * @return RedirectResponse
123
     */
124
    public function subscribeAction($forumName, $topicId)
125
    {
126
        $this->isAuthorised('ROLE_USER');
127
128
        if ($forumName != '~') {
129
            $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...
130
        } else {
131
            $forum = null;
132
        }
133
134
        $this->isFound($topic = $this->getTopicModel()->findOneTopicByIdWithBoardAndCategory($topicId));
0 ignored issues
show
Documentation introduced by
$topic = $this->getTopic...rdAndCategory($topicId) 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...
135
        $this->isAuthorised($this->getAuthorizer()->canSubscribeToTopic($topic, $forum));
136
        $this->getSubscriptionModel()->subscribe($topic, $this->getUser())->flush();
137
        $this->dispatch(ForumEvents::USER_TOPIC_SUBSCRIBE_COMPLETE, new UserTopicEvent($this->getRequest(), $topic, true));
138
139
        return $this->redirectResponse($this->path('ccdn_forum_user_topic_show', array(
140
            'forumName' => $forumName,
141
            'topicId' => $topicId
142
        )));
143
    }
144
145
    /**
146
     *
147
     * @access public
148
     * @param  string           $forumName
149
     * @param  int              $topicId
150
     * @return RedirectResponse
151
     */
152
    public function unsubscribeAction($forumName, $topicId)
153
    {
154
        $this->isAuthorised('ROLE_USER');
155
156
        if ($forumName != '~') {
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
        } else {
159
            $forum = null;
160
        }
161
162
        $this->isFound($topic = $this->getTopicModel()->findOneTopicByIdWithBoardAndCategory($topicId));
0 ignored issues
show
Documentation introduced by
$topic = $this->getTopic...rdAndCategory($topicId) 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...
163
        $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...
164
        $this->isAuthorised($this->getAuthorizer()->canUnsubscribeFromTopic($topic, $forum, $subscription));
165
        $this->getSubscriptionModel()->unsubscribe($topic, $this->getUser()->getId())->flush();
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...
166
        $this->dispatch(ForumEvents::USER_TOPIC_UNSUBSCRIBE_COMPLETE, new UserTopicEvent($this->getRequest(), $topic, false));
167
168
        return $this->redirectResponse($this->path('ccdn_forum_user_topic_show', array(
169
            'forumName' => $forumName,
170
            'topicId' => $topicId
171
        )));
172
    }
173
}
174