Completed
Push — develop ( 0bc71f...f6430f )
by greg
41:42
created

InstantWinController   C

Complexity

Total Complexity 30

Size/Duplication

Total Lines 381
Duplicated Lines 20.47 %

Coupling/Cohesion

Components 1
Dependencies 20

Importance

Changes 8
Bugs 0 Features 0
Metric Value
wmc 30
c 8
b 0
f 0
lcom 1
cbo 20
dl 78
loc 381
rs 6.4706

11 Methods

Rating   Name   Duplication   Size   Complexity  
A removeAction() 0 8 1
B createInstantWinAction() 0 45 4
B editInstantWinAction() 62 62 6
A listOccurrenceAction() 0 21 1
A addOccurrenceAction() 0 51 3
B importOccurrencesAction() 0 63 4
A editOccurrenceAction() 16 55 4
B removeOccurrenceAction() 0 24 3
A exportOccurrencesAction() 0 19 1
A getAdminGameService() 0 8 2
A setAdminGameService() 0 6 1

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\InstantWin;
6
use PlaygroundGame\Entity\InstantWinOccurrence;
7
use Zend\InputFilter;
8
use Zend\Validator;
9
use PlaygroundGame\Controller\Admin\GameController;
10
use Zend\View\Model\ViewModel;
11
use Zend\Paginator\Paginator;
12
use PlaygroundCore\ORM\Pagination\LargeTablePaginator;
13
use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as DoctrineAdapter;
14
15
class InstantWinController extends GameController
16
{
17
    /**
18
     * @var GameService
19
     */
20
    protected $adminGameService;
21
22
    public function removeAction()
23
    {
24
        $this->checkGame();
25
        $service->getGameMapper()->remove($this->game);
0 ignored issues
show
Bug introduced by
The variable $service does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
26
        $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game has been removed');
27
28
        return $this->redirect()->toRoute('admin/playgroundgame/list');
29
    }
30
31
    public function createInstantWinAction()
32
    {
33
        $service = $this->getAdminGameService();
34
        $viewModel = new ViewModel();
35
        $viewModel->setTemplate('playground-game/instant-win/instantwin');
36
37
        $gameForm = new ViewModel();
38
        $gameForm->setTemplate('playground-game/game/game-form');
39
40
        $instantwin = new InstantWin();
41
42
        $form = $this->getServiceLocator()->get('playgroundgame_instantwin_form');
43
        $form->bind($instantwin);
44
        $form->setAttribute(
45
            'action',
46
            $this->url()->fromRoute('admin/playgroundgame/create-instantwin', array('gameId' => 0))
47
        );
48
        $form->setAttribute('method', 'post');
49
50
        $request = $this->getRequest();
51
        if ($request->isPost()) {
52
            $data = array_replace_recursive(
53
                $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...
54
                $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...
55
            );
56
            if (empty($data['prizes'])) {
57
                $data['prizes'] = array();
58
            }
59
            $game = $service->create($data, $instantwin, 'playgroundgame_instantwin_form');
60
            if ($game) {
61
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The game was created');
62
63
                return $this->redirect()->toRoute('admin/playgroundgame/list');
64
            }
65
        }
66
        $gameForm->setVariables(array('form' => $form, 'game' => $instantwin));
67
        $viewModel->addChild($gameForm, 'game_form');
68
69
        return $viewModel->setVariables(
70
            array(
71
                'form' => $form,
72
                'title' => 'Create instant win',
73
            )
74
        );
75
    }
76
77 View Code Duplication
    public function editInstantWinAction()
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...
78
    {
79
        $this->checkGame();
80
81
        $viewModel = new ViewModel();
82
        $viewModel->setTemplate('playground-game/instant-win/instantwin');
83
84
        $gameForm = new ViewModel();
85
        $gameForm->setTemplate('playground-game/game/game-form');
86
87
        $form   = $this->getServiceLocator()->get('playgroundgame_instantwin_form');
88
        $form->setAttribute(
89
            'action',
90
            $this->url()->fromRoute(
91
                'admin/playgroundgame/edit-instantwin',
92
                array('gameId' => $this->game->getId())
93
            )
94
        );
95
        $form->setAttribute('method', 'post');
96
97
        if ($this->game->getFbAppId()) {
98
            $appIds = $form->get('fbAppId')->getOption('value_options');
99
            $appIds[$this->game->getFbAppId()] = $this->game->getFbAppId();
100
            $form->get('fbAppId')->setAttribute('options', $appIds);
101
        }
102
103
        $gameOptions = $this->getAdminGameService()->getOptions();
104
        $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $this->game->getId(). '.css';
105
        if (is_file($gameStylesheet)) {
106
            $values = $form->get('stylesheet')->getValueOptions();
107
            $values[$gameStylesheet] = 'Style personnalisé de ce jeu';
108
109
            $form->get('stylesheet')->setAttribute('options', $values);
110
        }
111
112
        $form->bind($this->game);
113
114
        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...
115
            $data = array_replace_recursive(
116
                $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...
117
                $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...
118
            );
119
            if (empty($data['prizes'])) {
120
                $data['prizes'] = array();
121
            }
122
            $result = $service->edit($data, $this->game, 'playgroundgame_instantwin_form');
0 ignored issues
show
Bug introduced by
The variable $service does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
123
124
            if ($result) {
125
                return $this->redirect()->toRoute('admin/playgroundgame/list');
126
            }
127
        }
128
129
        $gameForm->setVariables(array('form' => $form, 'game' => $this->game));
130
        $viewModel->addChild($gameForm, 'game_form');
131
132
        return $viewModel->setVariables(
133
            array(
134
                'form' => $form,
135
                'title' => 'Edit instant win',
136
            )
137
        );
138
    }
139
140
    public function listOccurrenceAction()
141
    {
142
        $this->checkGame();
143
144
        $adapter = new DoctrineAdapter(
145
            new LargeTablePaginator(
146
                $service->getInstantWinOccurrenceMapper()->queryByGame($this->game)
0 ignored issues
show
Bug introduced by
The variable $service does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
147
            )
148
        );
149
        $paginator = new Paginator($adapter);
150
        $paginator->setItemCountPerPage(25);
151
        $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
152
153
        return new ViewModel(
154
            array(
155
                'occurrences' => $paginator,
156
                'gameId'      => $this->game->getId(),
157
                'game'        => $this->game,
158
            )
159
        );
160
    }
161
162
    public function addOccurrenceAction()
163
    {
164
        $this->checkGame();
165
166
        $viewModel = new ViewModel();
167
        $viewModel->setTemplate('playground-game/instant-win/occurrence');
168
169
        $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form');
170
        $form->get('submit')->setAttribute('label', 'Add');
171
172
        $form->setAttribute(
173
            'action',
174
            $this->url()->fromRoute(
175
                'admin/playgroundgame/instantwin-occurrence-add',
176
                array('gameId' => $this->game->getId())
177
            )
178
        );
179
        $form->setAttribute('method', 'post');
180
        $form->get('instant_win_id')->setAttribute('value', $this->game->getId());
181
        $occurrence = new InstantWinOccurrence();
182
        $form->bind($occurrence);
183
184
        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...
185
            $data = array_merge(
186
                $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...
187
                $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...
188
            );
189
190
            // Change the format of the date
191
            $value = \DateTime::createFromFormat('d/m/Y H:i', $data['value']);
192
            $data['value'] = $value->format('Y-m-d H:i:s');
193
            
194
            $occurrence = $service->updateOccurrence($data, $occurrence->getId());
0 ignored issues
show
Bug introduced by
The variable $service does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
195
            if ($occurrence) {
196
                // Redirect to list of games
197
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was created');
198
                return $this->redirect()->toRoute(
199
                    'admin/playgroundgame/instantwin-occurrence-list',
200
                    array('gameId'=>$this->game->getId())
201
                );
202
            }
203
        }
204
        return $viewModel->setVariables(
205
            array(
206
                'form' => $form,
207
                'game' => $this->game,
208
                'occurrence_id' => 0,
209
                'title' => 'Add occurrence',
210
            )
211
        );
212
    }
213
214
    public function importOccurrencesAction()
215
    {
216
        $this->checkGame();
217
218
        $viewModel = new ViewModel();
219
        $viewModel->setTemplate('playground-game/instant-win/import-occurrences');
220
221
        $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrenceimport_form');
222
223
        $form->get('submit')->setAttribute('label', 'Import');
224
        $form->setAttribute(
225
            'action',
226
            $this->url()->fromRoute(
227
                'admin/playgroundgame/instantwin-occurrences-import',
228
                array('gameId' => $this->game->getId())
229
            )
230
        );
231
        $form->get('instant_win_id')->setAttribute('value', $this->game->getId());
232
233
        // File validator
234
        $inputFilter = new InputFilter\InputFilter();
235
        $fileFilter = new InputFilter\FileInput('file');
236
        $validatorChain = new Validator\ValidatorChain();
237
        $validatorChain->attach(new Validator\File\Exists());
238
        $validatorChain->attach(new Validator\File\Extension('csv'));
239
        $fileFilter->setValidatorChain($validatorChain);
240
        $fileFilter->setRequired(true);
241
242
        $prizeFilter = new InputFilter\Input('prize');
243
        $prizeFilter->setRequired(false);
244
245
        $inputFilter->add($fileFilter);
246
        $inputFilter->add($prizeFilter);
247
        $form->setInputFilter($inputFilter);
248
249
        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...
250
            $data = array_merge_recursive(
251
                $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...
252
                $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...
253
            );
254
            $form->setData($data);
255
            if ($form->isValid()) {
256
                $data = $form->getData();
257
                $created = $this->getAdminGameService()->importOccurrences($data);
258
                if ($created) {
259
                    $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
260
                        $created.' occurrences were created !'
261
                    );
262
                    return $this->redirect()->toRoute(
263
                        'admin/playgroundgame/instantwin-occurrence-list',
264
                        array('gameId'=>$this->game->getId())
265
                    );
266
                }
267
            }
268
        }
269
270
        return $viewModel->setVariables(
271
            array(
272
                'form' => $form,
273
                'title' => 'Import occurrences',
274
            )
275
        );
276
    }
277
278
    public function editOccurrenceAction()
279
    {
280
        $this->checkGame();
281
282
        $viewModel = new ViewModel();
283
        $viewModel->setTemplate('playground-game/instant-win/occurrence');
284
        $service = $this->getAdminGameService();
285
286
        $occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId');
287
        $occurrence = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId);
288
        // Si l'occurrence a été utilisée, on ne peut plus la modifier
289
        if ($occurrence->getUser()) {
290
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
291
                'This occurrence has a winner, you can not update it.'
292
            );
293
            return $this->redirect()->toRoute(
294
                'admin/playgroundgame/instantwin-occurrence-list',
295
                array('gameId'=>$this->game->getId())
296
            );
297
        }
298
        $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form');
299
        $form->remove('occurrences_file');
300
301
        $form->get('submit')->setAttribute('label', 'Edit');
302
        $form->setAttribute('action', '');
303
304
        $form->get('instant_win_id')->setAttribute('value', $this->game->getId());
305
306
        $form->bind($occurrence);
307
308 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...
309
            $data = array_merge(
310
                $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...
311
                $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...
312
            );
313
            $occurrence = $service->updateOccurrence($data, $occurrence->getId());
314
315
            if ($occurrence) {
316
                // Redirect to list of games
317
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was edited');
318
                return $this->redirect()->toRoute(
319
                    'admin/playgroundgame/instantwin-occurrence-list',
320
                    array('gameId'=>$this->game->getId())
321
                );
322
            }
323
        }
324
        return $viewModel->setVariables(
325
            array(
326
                'form' => $form,
327
                'game' => $this->game,
328
                'occurrence_id' => $occurrenceId,
329
                'title' => 'Edit occurrence',
330
            )
331
        );
332
    }
333
334
335
    public function removeOccurrenceAction()
336
    {
337
        $service = $this->getAdminGameService();
338
        $occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId');
339
        if (!$occurrenceId) {
340
            return $this->redirect()->toRoute('admin/playgroundgame/list');
341
        }
342
        $occurrence   = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId);
343
        $instantwinId = $occurrence->getInstantWin()->getId();
344
345
        if ($occurrence->getActive()) {
346
            $service->getInstantWinOccurrenceMapper()->remove($occurrence);
347
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was deleted');
348
        } else {
349
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
350
                'Il y a un participant à cet instant gagnant. Vous ne pouvez plus le supprimer'
351
            );
352
        }
353
354
        return $this->redirect()->toRoute(
355
            'admin/playgroundgame/instantwin-occurrence-list',
356
            array('gameId'=>$instantwinId)
357
        );
358
    }
359
360
    public function exportOccurrencesAction()
361
    {
362
        $this->checkGame();
363
364
        $file = $this->getAdminGameService()->setOccurencesToCSV($this->game);
365
366
        $response = new \Zend\Http\Response\Stream();
367
        $response->setStream(fopen($file, 'r'));
368
        $response->setStatusCode(200);
369
370
        $headers = new \Zend\Http\Headers();
371
        $headers->addHeaderLine('Content-Type', 'text/csv')
372
                ->addHeaderLine('Content-Disposition', 'attachment; filename="' . $file . '"')
373
                ->addHeaderLine('Content-Length', filesize($file));
374
375
        $response->setHeaders($headers);
376
        unlink($file);
377
        return $response;
378
    }
379
380
    public function getAdminGameService()
381
    {
382
        if (!$this->adminGameService) {
383
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_instantwin_service');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceLocator...me_instantwin_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...
384
        }
385
386
        return $this->adminGameService;
387
    }
388
389
    public function setAdminGameService(\PlaygroundGame\Service\Game $adminGameService)
390
    {
391
        $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...
392
393
        return $this;
394
    }
395
}
396