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.

BoardManager   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2
Metric Value
wmc 19
lcom 2
cbo 2
dl 0
loc 178
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A createBoard() 0 4 1
A saveBoard() 0 9 1
A updateBoard() 0 6 1
A deleteBoard() 0 11 1
A reassignTopicsToBoard() 0 11 2
D reorderBoards() 0 91 13
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\Model\Component\Manager;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
18
use CCDNForum\ForumBundle\Model\Component\Manager\ManagerInterface;
19
use CCDNForum\ForumBundle\Model\Component\Manager\BaseManager;
20
21
use CCDNForum\ForumBundle\Entity\Board;
22
23
/**
24
 *
25
 * @category CCDNForum
26
 * @package  ForumBundle
27
 *
28
 * @author   Reece Fowell <[email protected]>
29
 * @license  http://opensource.org/licenses/MIT MIT
30
 * @version  Release: 2.0
31
 * @link     https://github.com/codeconsortium/CCDNForumForumBundle
32
 *
33
 */
34
class BoardManager extends BaseManager implements ManagerInterface
35
{
36
    /**
37
     *
38
     * @access public
39
     * @return \CCDNForum\ForumBundle\Entity\Board
40
     */
41
    public function createBoard()
42
    {
43
        return $this->gateway->createBoard();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CCDNForum\ForumBundle\Mo...ateway\GatewayInterface as the method createBoard() does only exist in the following implementations of said interface: CCDNForum\ForumBundle\Mo...nt\Gateway\BoardGateway.

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...
44
    }
45
46
    /**
47
     *
48
     * @access public
49
     * @param  \CCDNForum\ForumBundle\Entity\Board             $board
50
     * @return \CCDNForum\ForumBundle\Manager\ManagerInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be BoardManager?

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...
51
     */
52
    public function saveBoard(Board $board)
53
    {
54
        $boardCount = $this->model->getBoardCount();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CCDNForum\ForumBundle\Mo...ontModel\ModelInterface as the method getBoardCount() does only exist in the following implementations of said interface: CCDNForum\ForumBundle\Model\FrontModel\BoardModel.

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...
55
        $board->setListOrderPriority(++$boardCount);
56
57
        $this->gateway->saveBoard($board);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CCDNForum\ForumBundle\Mo...ateway\GatewayInterface as the method saveBoard() does only exist in the following implementations of said interface: CCDNForum\ForumBundle\Mo...nt\Gateway\BoardGateway.

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...
58
59
        return $this;
60
    }
61
62
    /**
63
     *
64
     * @access public
65
     * @param  \CCDNForum\ForumBundle\Entity\Board             $board
66
     * @return \CCDNForum\ForumBundle\Manager\ManagerInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be BoardManager?

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...
67
     */
68
    public function updateBoard(Board $board)
69
    {
70
        $this->gateway->updateBoard($board);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface CCDNForum\ForumBundle\Mo...ateway\GatewayInterface as the method updateBoard() does only exist in the following implementations of said interface: CCDNForum\ForumBundle\Mo...nt\Gateway\BoardGateway.

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...
71
72
        return $this;
73
    }
74
75
    /**
76
     *
77
     * @access public
78
     * @param  \CCDNForum\ForumBundle\Entity\Board             $board
79
     * @return \CCDNForum\ForumBundle\Manager\ManagerInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be BoardManager?

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...
80
     */
81
    public function deleteBoard(Board $board)
82
    {
83
        // If we do not refresh the board, AND we have reassigned the topics to null,
84
        // then its lazy-loaded topics are dirty, as the topics in memory will still
85
        // have the old board id set. Removing the board will cascade into deleting
86
        // topics aswell, even though in the db the relation has been set to null.
87
        $this->refresh($board);
88
        $this->remove($board)->flush();
89
90
        return $this;
91
    }
92
93
    /**
94
     *
95
     * @access public
96
     * @param  \Doctrine\Common\Collections\ArrayCollection    $topics
97
     * @param  \CCDNForum\ForumBundle\Entity\Board             $board
0 ignored issues
show
Documentation introduced by
Should the type for parameter $board not be null|Board?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
98
     * @return \CCDNForum\ForumBundle\Manager\ManagerInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be BoardManager?

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...
99
     */
100
    public function reassignTopicsToBoard(ArrayCollection $topics, Board $board = null)
101
    {
102
        foreach ($topics as $topic) {
103
            $topic->setBoard($board);
104
            $this->persist($topic);
105
        }
106
107
        $this->flush();
108
109
        return $this;
110
    }
111
112
    /**
113
     *
114
     * @access public
115
     * @param  Array                                           $boards
116
     * @param  \CCDNForum\ForumBundle\Entity\Board             $boardShift
117
     * @param  int                                             $direction
118
     * @return \CCDNForum\ForumBundle\Manager\ManagerInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be BoardManager?

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...
119
     */
120
    public function reorderBoards($boards, Board $boardShift, $direction)
121
    {
122
        $boardCount = (count($boards) - 1);
123
124
        // Find board in collection to shift and use list order as array key for easier editing.
125
        $sorted = array();
126
        $shiftIndex = null;
127
        foreach ($boards as $boardIndex => $board) {
128
            if ($boards[$boardIndex]->getId() == $boardShift->getId()) {
129
                $shiftIndex = $boardIndex;
130
            }
131
132
            $sorted[$boardIndex] = $board;
133
        }
134
135
        $incrementKeysAfterIndex = function ($index, $arr) {
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $incrementKeysAfterIndex 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...
136
            $hydrated = array();
137
138
            foreach ($arr as $key => $el) {
139
                if ($key > $index) {
140
                    $hydrated[$key + 1] = $el;
141
                } else {
142
                    $hydrated[$key] = $el;
143
                }
144
            }
145
146
            return $hydrated;
147
        };
148
149
        $decrementKeysBeforeIndex = function ($index, $arr) {
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $decrementKeysBeforeIndex 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...
150
            $hydrated = array();
151
152
            foreach ($arr as $key => $el) {
153
                if ($key < $index) {
154
                    $hydrated[$key - 1] = $el;
155
                } else {
156
                    $hydrated[$key] = $el;
157
                }
158
            }
159
160
            return $hydrated;
161
        };
162
163
        $shifted = array();
164
165
        // First Board needs reordering??
166
        if ($shiftIndex == 0) {
167
            if ($direction) { // Down (move down 1)
168
                $shifted = $sorted;
169
                $shifted[$shiftIndex] = $sorted[$shiftIndex + 1];
170
                $shifted[$shiftIndex + 1] = $sorted[$shiftIndex];
171
            } else { // Up (send to bottom)
172
                $shifted[$boardCount] = $sorted[0];
173
                unset($sorted[0]);
174
                $shifted = array_merge($decrementKeysBeforeIndex($boardCount + 1, $sorted), $shifted);
175
            }
176
        } else {
177
            // Last board needs reordering??
178
            if ($shiftIndex == $boardCount) {
179
                if ($direction) { // Down (send to top)
180
                    $shifted[0] = $sorted[$boardCount];
181
                    unset($sorted[$boardCount]);
182
                    $shifted = array_merge($shifted, $incrementKeysAfterIndex(-1, $sorted));
183
                } else { // Up (move up 1)
184
                    $shifted = $sorted;
185
                    $shifted[$shiftIndex] = $sorted[$shiftIndex - 1];
186
                    $shifted[$shiftIndex - 1] = $sorted[$shiftIndex];
187
                }
188
            } else {
189
                // Swap 2 boards not at beginning or end.
190
                $shifted = $sorted;
191
                if ($direction) { // Down (move down 1)
192
                    $shifted[$shiftIndex] = $sorted[$shiftIndex + 1];
193
                    $shifted[$shiftIndex + 1] = $sorted[$shiftIndex];
194
                } else { // Up (move up 1)
195
                    $shifted[$shiftIndex] = $sorted[$shiftIndex - 1];
196
                    $shifted[$shiftIndex - 1] = $sorted[$shiftIndex];
197
                }
198
            }
199
        }
200
201
        // Set the order from the $index arranged prior and persist.
202
        foreach ($shifted as $index => $board) {
203
            $board->setListOrderPriority($index);
204
            $this->persist($board);
205
        }
206
207
        $this->flush();
208
209
        return $this;
210
    }
211
}
212