Completed
Push — develop ( 54744b...29957b )
by greg
03:57
created

QuizController   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 271
Duplicated Lines 48.34 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 31
c 4
b 0
f 0
lcom 1
cbo 11
dl 131
loc 271
rs 9.8

10 Methods

Rating   Name   Duplication   Size   Complexity  
B listQuestionAction() 0 25 3
B addQuestionAction() 18 44 4
B editQuestionAction() 18 45 4
A removeQuestionAction() 0 15 2
B createQuizAction() 38 38 4
B editQuizAction() 57 57 7
A getAdminGameService() 0 8 2
A setAdminGameService() 0 6 1
A getQuizReplyAnswerMapper() 0 7 2
A getQuizReplyMapper() 0 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace PlaygroundGame\Controller\Admin;
4
5
use PlaygroundGame\Entity\Quiz;
6
use PlaygroundGame\Entity\QuizQuestion;
7
use PlaygroundGame\Controller\Admin\GameController;
8
use PlaygroundGame\Service\Game as AdminGameService;
9
use Zend\View\Model\ViewModel;
10
use Zend\Paginator\Paginator;
11
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter;
12
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
13
14
class QuizController extends GameController
15
{
16
    /**
17
     * @var GameService
18
     */
19
    protected $adminGameService;
20
    protected $quizReplyAnswerMapper;
21
    protected $quizReplyMapper;
22
23
    public function listQuestionAction()
24
    {
25
        $service = $this->getAdminGameService();
26
        $quizId = $this->getEvent()->getRouteMatch()->getParam('quizId');
27
        if (!$quizId) {
28
            return $this->redirect()->toRoute('admin/playgroundgame/list');
29
        }
30
        $quiz = $service->getGameMapper()->findById($quizId);
31
        $questions = $service->getQuizQuestionMapper()->findByGameId($quizId);
32
33
        if (is_array($questions)) {
34
            $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($questions));
35
        } else {
36
            $paginator = $questions;
37
        }
38
39
        $paginator->setItemCountPerPage(10);
40
        $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
41
42
        return array(
43
            'questions' => $paginator,
44
            'quiz_id' => $quizId,
45
            'quiz' => $quiz,
46
        );
47
    }
48
49
    public function addQuestionAction()
50
    {
51
        $viewModel = new ViewModel();
52
        $viewModel->setTemplate('playground-game/quiz/question');
53
        $service = $this->getAdminGameService();
54
        $quizId = $this->getEvent()->getRouteMatch()->getParam('quizId');
55
56
        if (!$quizId) {
57
            return $this->redirect()->toRoute('admin/playgroundgame/list');
58
        }
59
60
        $form = $this->getServiceLocator()->get('playgroundgame_quizquestion_form');
61
        $form->get('submit')->setAttribute('label', 'Ajouter');
62
        $form->get('quiz_id')->setAttribute('value', $quizId);
63
        $form->setAttribute(
64
            'action',
65
            $this->url()->fromRoute('admin/playgroundgame/quiz-question-add', array('quizId' => $quizId))
66
        );
67
        $form->setAttribute('method', 'post');
68
69
        $question = new QuizQuestion();
70
        $form->bind($question);
71
72 View Code Duplication
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method isPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
            $data = array_replace_recursive(
74
                $this->getRequest()->getPost()->toArray(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
75
                $this->getRequest()->getFiles()->toArray()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getFiles() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
76
            );
77
78
            $question = $service->createQuestion($data);
79
            if ($question) {
80
                // Redirect to list of games
81
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was created');
82
83
                return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId));
84
            } else { // Creation failed
85
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
86
                    'The question was not updated - create at least one good answer'
87
                );
88
            }
89
        }
90
91
        return $viewModel->setVariables(array('form' => $form, 'quiz_id' => $quizId, 'question_id' => 0));
92
    }
93
94
    public function editQuestionAction()
95
    {
96
        $service = $this->getAdminGameService();
97
        $viewModel = new ViewModel();
98
        $viewModel->setTemplate('playground-game/quiz/question');
99
100
        $questionId = $this->getEvent()->getRouteMatch()->getParam('questionId');
101
        if (!$questionId) {
102
            return $this->redirect()->toRoute('admin/playgroundgame/list');
103
        }
104
        $question   = $service->getQuizQuestionMapper()->findById($questionId);
105
        $quizId     = $question->getQuiz()->getId();
106
107
        $form = $this->getServiceLocator()->get('playgroundgame_quizquestion_form');
108
        $form->get('submit')->setAttribute('label', 'Mettre à jour');
109
        $form->get('quiz_id')->setAttribute('value', $quizId);
110
        $form->setAttribute(
111
            'action',
112
            $this->url()->fromRoute('admin/playgroundgame/quiz-question-edit', array('questionId' => $questionId))
113
        );
114
        $form->setAttribute('method', 'post');
115
116
        $form->bind($question);
117
118 View Code Duplication
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method isPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
            $data = array_replace_recursive(
120
                $this->getRequest()->getPost()->toArray(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
121
                $this->getRequest()->getFiles()->toArray()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getFiles() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
122
            );
123
124
            $question = $service->updateQuestion($data, $question);
125
            if ($question) {
126
                // Redirect to list of games
127
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was updated');
128
129
                return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId));
130
            } else {
131
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
132
                    'The question was not updated - create at least one good answer'
133
                );
134
            }
135
        }
136
137
        return $viewModel->setVariables(array('form' => $form, 'quiz_id' => $quizId, 'question_id' => $questionId));
138
    }
139
140
    public function removeQuestionAction()
141
    {
142
        $service = $this->getAdminGameService();
143
        $questionId = $this->getEvent()->getRouteMatch()->getParam('questionId');
144
        if (!$questionId) {
145
            return $this->redirect()->toRoute('admin/playgroundgame/list');
146
        }
147
        $question   = $service->getQuizQuestionMapper()->findById($questionId);
148
        $quizId     = $question->getQuiz()->getId();
149
150
        $service->getQuizQuestionMapper()->remove($question);
151
        $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The question was created');
152
153
        return $this->redirect()->toRoute('admin/playgroundgame/quiz-question-list', array('quizId'=>$quizId));
154
    }
155
156 View Code Duplication
    public function createQuizAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
157
    {
158
        $service = $this->getAdminGameService();
159
        $viewModel = new ViewModel();
160
        $viewModel->setTemplate('playground-game/quiz/quiz');
161
162
        $gameForm = new ViewModel();
163
        $gameForm->setTemplate('playground-game/game/game-form');
164
165
        $quiz = new Quiz();
166
167
        $form = $this->getServiceLocator()->get('playgroundgame_quiz_form');
168
        $form->bind($quiz);
169
        $form->get('submit')->setAttribute('label', 'Add');
170
        $form->setAttribute('action', $this->url()->fromRoute('admin/playgroundgame/create-quiz', array('gameId' => 0)));
171
        $form->setAttribute('method', 'post');
172
173
        $request = $this->getRequest();
174
        if ($request->isPost()) {
175
            $data = array_replace_recursive(
176
                $this->getRequest()->getPost()->toArray(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
177
                $this->getRequest()->getFiles()->toArray()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getFiles() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
178
            );
179
            if (empty($data['prizes'])) {
180
                $data['prizes'] = array();
181
            }
182
            $game = $service->create($data, $quiz, 'playgroundgame_quiz_form');
183
            if ($game) {
184
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created');
185
186
                return $this->redirect()->toRoute('admin/playgroundgame/list');
187
            }
188
        }
189
        $gameForm->setVariables(array('form' => $form, 'game' => $quiz));
190
        $viewModel->addChild($gameForm, 'game_form');
191
192
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Create quiz'));
193
    }
194
195 View Code Duplication
    public function editQuizAction()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
    {
197
        $service = $this->getAdminGameService();
198
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
199
200
        if (!$gameId) {
201
            return $this->redirect()->toRoute('admin/playgroundgame/create-quiz');
202
        }
203
204
        $game = $service->getGameMapper()->findById($gameId);
205
        $viewModel = new ViewModel();
206
        $viewModel->setTemplate('playground-game/quiz/quiz');
207
208
        $gameForm = new ViewModel();
209
        $gameForm->setTemplate('playground-game/game/game-form');
210
211
        $form   = $this->getServiceLocator()->get('playgroundgame_quiz_form');
212
        $form->setAttribute('action', $this->url()->fromRoute('admin/playgroundgame/edit-quiz', array('gameId' => $gameId)));
213
        $form->setAttribute('method', 'post');
214
215
        if ($game->getFbAppId()) {
216
            $appIds = $form->get('fbAppId')->getOption('value_options');
217
            $appIds[$game->getFbAppId()] = $game->getFbAppId();
218
            $form->get('fbAppId')->setAttribute('options', $appIds);
219
        }
220
221
        $gameOptions = $this->getAdminGameService()->getOptions();
222
        $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $game->getId(). '.css';
223
        if (is_file($gameStylesheet)) {
224
            $values = $form->get('stylesheet')->getValueOptions();
225
            $values[$gameStylesheet] = 'Style personnalisé de ce jeu';
226
227
            $form->get('stylesheet')->setAttribute('options', $values);
228
        }
229
230
        $form->bind($game);
231
232
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method isPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
233
            $data = array_replace_recursive(
234
                $this->getRequest()->getPost()->toArray(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getPost() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
235
                $this->getRequest()->getFiles()->toArray()
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Stdlib\RequestInterface as the method getFiles() does only exist in the following implementations of said interface: Zend\Http\PhpEnvironment\Request, Zend\Http\Request.

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...
236
            );
237
            if (empty($data['prizes'])) {
238
                $data['prizes'] = array();
239
            }
240
            $result = $service->edit($data, $game, 'playgroundgame_quiz_form');
241
242
            if ($result) {
243
                return $this->redirect()->toRoute('admin/playgroundgame/list');
244
            }
245
        }
246
247
        $gameForm->setVariables(array('form' => $form, 'game' => $game));
248
        $viewModel->addChild($gameForm, 'game_form');
249
250
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Edit quiz'));
251
    }
252
253
    public function getAdminGameService()
254
    {
255
        if (!$this->adminGameService) {
256
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_quiz_service');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceLocator...oundgame_quiz_service') can also be of type array. However, the property $adminGameService is declared as type object<PlaygroundGame\Co...ller\Admin\GameService>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
257
        }
258
259
        return $this->adminGameService;
260
    }
261
262
    public function setAdminGameService(AdminGameService $adminGameService)
263
    {
264
        $this->adminGameService = $adminGameService;
0 ignored issues
show
Documentation Bug introduced by
It seems like $adminGameService of type object<PlaygroundGame\Service\Game> is incompatible with the declared type object<PlaygroundGame\Co...ller\Admin\GameService> of property $adminGameService.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
265
266
        return $this;
267
    }
268
269
    public function getQuizReplyAnswerMapper()
270
    {
271
        if (!$this->quizReplyAnswerMapper) {
272
            $this->quizReplyAnswerMapper = $this->getServiceLocator()->get('playgroundgame_quizreplyanswer_mapper');
273
        }
274
        return $this->quizReplyAnswerMapper;
275
    }
276
277
    public function getQuizReplyMapper()
278
    {
279
        if (!$this->quizReplyMapper) {
280
            $this->quizReplyMapper = $this->getServiceLocator()->get('playgroundgame_quizreply_mapper');
281
        }
282
        return $this->quizReplyMapper;
283
    }
284
}
285