Completed
Push — develop ( 89108e...7bb4ed )
by greg
03:09
created

InstantWinController::createInstantWinAction()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 45
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 45
rs 8.5806
cc 4
eloc 30
nc 5
nop 0
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
    public function editInstantWinAction()
78
    {
79
        $this->checkGame();
80
81
        return $this->editGame(
82
            'playground-game/instant-win/instantwin',
83
            'playgroundgame_instantwin_form'
84
        );
85
    }
86
87
    public function listOccurrenceAction()
88
    {
89
        $this->checkGame();
90
91
        $adapter = new DoctrineAdapter(
92
            new LargeTablePaginator(
93
                $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...
94
            )
95
        );
96
        $paginator = new Paginator($adapter);
97
        $paginator->setItemCountPerPage(25);
98
        $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p'));
99
100
        return new ViewModel(
101
            array(
102
                'occurrences' => $paginator,
103
                'gameId'      => $this->game->getId(),
104
                'game'        => $this->game,
105
            )
106
        );
107
    }
108
109
    public function addOccurrenceAction()
110
    {
111
        $this->checkGame();
112
113
        $viewModel = new ViewModel();
114
        $viewModel->setTemplate('playground-game/instant-win/occurrence');
115
116
        $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form');
117
        $form->get('submit')->setAttribute('label', 'Add');
118
119
        $form->setAttribute(
120
            'action',
121
            $this->url()->fromRoute(
122
                'admin/playgroundgame/instantwin-occurrence-add',
123
                array('gameId' => $this->game->getId())
124
            )
125
        );
126
        $form->setAttribute('method', 'post');
127
        $form->get('instant_win_id')->setAttribute('value', $this->game->getId());
128
        $occurrence = new InstantWinOccurrence();
129
        $form->bind($occurrence);
130
131
        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...
132
            $data = array_merge(
133
                $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...
134
                $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...
135
            );
136
137
            // Change the format of the date
138
            $value = \DateTime::createFromFormat('d/m/Y H:i', $data['value']);
139
            $data['value'] = $value->format('Y-m-d H:i:s');
140
            
141
            $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...
142
            if ($occurrence) {
143
                // Redirect to list of games
144
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was created');
145
                return $this->redirect()->toRoute(
146
                    'admin/playgroundgame/instantwin-occurrence-list',
147
                    array('gameId'=>$this->game->getId())
148
                );
149
            }
150
        }
151
        return $viewModel->setVariables(
152
            array(
153
                'form' => $form,
154
                'game' => $this->game,
155
                'occurrence_id' => 0,
156
                'title' => 'Add occurrence',
157
            )
158
        );
159
    }
160
161
    public function importOccurrencesAction()
162
    {
163
        $this->checkGame();
164
165
        $viewModel = new ViewModel();
166
        $viewModel->setTemplate('playground-game/instant-win/import-occurrences');
167
168
        $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrenceimport_form');
169
170
        $form->get('submit')->setAttribute('label', 'Import');
171
        $form->setAttribute(
172
            'action',
173
            $this->url()->fromRoute(
174
                'admin/playgroundgame/instantwin-occurrences-import',
175
                array('gameId' => $this->game->getId())
176
            )
177
        );
178
        $form->get('instant_win_id')->setAttribute('value', $this->game->getId());
179
180
        // File validator
181
        $inputFilter = new InputFilter\InputFilter();
182
        $fileFilter = new InputFilter\FileInput('file');
183
        $validatorChain = new Validator\ValidatorChain();
184
        $validatorChain->attach(new Validator\File\Exists());
185
        $validatorChain->attach(new Validator\File\Extension('csv'));
186
        $fileFilter->setValidatorChain($validatorChain);
187
        $fileFilter->setRequired(true);
188
189
        $prizeFilter = new InputFilter\Input('prize');
190
        $prizeFilter->setRequired(false);
191
192
        $inputFilter->add($fileFilter);
193
        $inputFilter->add($prizeFilter);
194
        $form->setInputFilter($inputFilter);
195
196
        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...
197
            $data = array_merge_recursive(
198
                $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...
199
                $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...
200
            );
201
            $form->setData($data);
202
            if ($form->isValid()) {
203
                $data = $form->getData();
204
                $created = $this->getAdminGameService()->importOccurrences($data);
205
                if ($created) {
206
                    $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
207
                        $created.' occurrences were created !'
208
                    );
209
                    return $this->redirect()->toRoute(
210
                        'admin/playgroundgame/instantwin-occurrence-list',
211
                        array('gameId'=>$this->game->getId())
212
                    );
213
                }
214
            }
215
        }
216
217
        return $viewModel->setVariables(
218
            array(
219
                'form' => $form,
220
                'title' => 'Import occurrences',
221
            )
222
        );
223
    }
224
225
    public function editOccurrenceAction()
226
    {
227
        $this->checkGame();
228
229
        $viewModel = new ViewModel();
230
        $viewModel->setTemplate('playground-game/instant-win/occurrence');
231
        $service = $this->getAdminGameService();
232
233
        $occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId');
234
        $occurrence = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId);
235
        // Si l'occurrence a été utilisée, on ne peut plus la modifier
236
        if ($occurrence->getUser()) {
237
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
238
                'This occurrence has a winner, you can not update it.'
239
            );
240
            return $this->redirect()->toRoute(
241
                'admin/playgroundgame/instantwin-occurrence-list',
242
                array('gameId'=>$this->game->getId())
243
            );
244
        }
245
        $form = $this->getServiceLocator()->get('playgroundgame_instantwinoccurrence_form');
246
        $form->remove('occurrences_file');
247
248
        $form->get('submit')->setAttribute('label', 'Edit');
249
        $form->setAttribute('action', '');
250
251
        $form->get('instant_win_id')->setAttribute('value', $this->game->getId());
252
253
        $form->bind($occurrence);
254
255 View Code Duplication
        if ($this->getRequest()->isPost()) {
0 ignored issues
show
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...
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...
256
            $data = array_merge(
257
                $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...
258
                $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...
259
            );
260
            $occurrence = $service->updateOccurrence($data, $occurrence->getId());
261
262
            if ($occurrence) {
263
                // Redirect to list of games
264
                $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was edited');
265
                return $this->redirect()->toRoute(
266
                    'admin/playgroundgame/instantwin-occurrence-list',
267
                    array('gameId'=>$this->game->getId())
268
                );
269
            }
270
        }
271
        return $viewModel->setVariables(
272
            array(
273
                'form' => $form,
274
                'game' => $this->game,
275
                'occurrence_id' => $occurrenceId,
276
                'title' => 'Edit occurrence',
277
            )
278
        );
279
    }
280
281
282
    public function removeOccurrenceAction()
283
    {
284
        $service = $this->getAdminGameService();
285
        $occurrenceId = $this->getEvent()->getRouteMatch()->getParam('occurrenceId');
286
        if (!$occurrenceId) {
287
            return $this->redirect()->toRoute('admin/playgroundgame/list');
288
        }
289
        $occurrence   = $service->getInstantWinOccurrenceMapper()->findById($occurrenceId);
290
        $instantwinId = $occurrence->getInstantWin()->getId();
291
292
        if ($occurrence->getActive()) {
293
            $service->getInstantWinOccurrenceMapper()->remove($occurrence);
294
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage('The occurrence was deleted');
295
        } else {
296
            $this->flashMessenger()->setNamespace('playgroundgame')->addMessage(
297
                'Il y a un participant à cet instant gagnant. Vous ne pouvez plus le supprimer'
298
            );
299
        }
300
301
        return $this->redirect()->toRoute(
302
            'admin/playgroundgame/instantwin-occurrence-list',
303
            array('gameId'=>$instantwinId)
304
        );
305
    }
306
307
    public function exportOccurrencesAction()
308
    {
309
        $this->checkGame();
310
311
        $file = $this->getAdminGameService()->setOccurencesToCSV($this->game);
312
313
        $response = new \Zend\Http\Response\Stream();
314
        $response->setStream(fopen($file, 'r'));
315
        $response->setStatusCode(200);
316
317
        $headers = new \Zend\Http\Headers();
318
        $headers->addHeaderLine('Content-Type', 'text/csv')
319
                ->addHeaderLine('Content-Disposition', 'attachment; filename="' . $file . '"')
320
                ->addHeaderLine('Content-Length', filesize($file));
321
322
        $response->setHeaders($headers);
323
        unlink($file);
324
        return $response;
325
    }
326
327
    public function getAdminGameService()
328
    {
329
        if (!$this->adminGameService) {
330
            $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...
331
        }
332
333
        return $this->adminGameService;
334
    }
335
336
    public function setAdminGameService(\PlaygroundGame\Service\Game $adminGameService)
337
    {
338
        $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...
339
340
        return $this;
341
    }
342
}
343