Completed
Pull Request — master (#268)
by greg
07:26 queued 04:08
created

MissionController::editMissionAction()   C

Complexity

Conditions 7
Paths 21

Size

Total Lines 85
Code Lines 40

Duplication

Lines 6
Ratio 7.06 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 6
loc 85
rs 6.5338
cc 7
eloc 40
nc 21
nop 0

How to fix   Long Method   

Long Method

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

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

Commonly applied refactorings include:

1
<?php
2
3
namespace PlaygroundGame\Controller\Admin;
4
5
use PlaygroundGame\Entity\Mission;
6
use PlaygroundGame\Controller\Admin\GameController;
7
use PlaygroundGame\Service\Game as AdminGameService;
8
use Zend\View\Model\ViewModel;
9
10
class MissionController extends GameController
11
{
12
    /**
13
     * @var GameService
14
     */
15
    protected $adminGameService;
16
17
    protected $mission;
18
19 View Code Duplication
    public function createMissionAction()
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...
20
    {
21
        $service = $this->getAdminGameService();
22
        $viewModel = new ViewModel();
23
        $viewModel->setTemplate('playground-game/game/mission');
24
25
        $gameForm = new ViewModel();
26
        $gameForm->setTemplate('playground-game/game/game-form');
27
28
        $mission = new Mission();
29
30
        $form = $this->getServiceLocator()->get('playgroundgame_mission_form');
31
        $form->bind($mission);
32
        $form->get('submit')->setAttribute('label', 'Add');
33
        $form->setAttribute(
34
            'action',
35
            $this->url()->fromRoute(
36
                'admin/playgroundgame/create-mission',
37
                array('gameId' => 0)
38
            )
39
        );
40
        $form->setAttribute('method', 'post');
41
42
        $request = $this->getRequest();
43
        if ($request->isPost()) {
44
            $data = array_replace_recursive(
45
                $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...
46
                $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...
47
            );
48
            if (empty($data['prizes'])) {
49
                $data['prizes'] = array();
50
            }
51
52
            $game = $service->createOrUpdate($data, $mission, 'playgroundgame_mission_form');
53
            if ($game) {
54
55
                $this->flashMessenger()->setNamespace('mission')->addMessage('The game was created');
56
57
                return $this->redirect()->toRoute('admin/playgroundgame/list');
58
            }
59
        }
60
        $gameForm->setVariables(array('form' => $form, 'game' => $mission));
61
        $viewModel->addChild($gameForm, 'game_form');
62
63
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Create mission', 'mission' => $mission));
64
    }
65
66
    public function editMissionAction()
67
    {
68
        $service = $this->getAdminGameService();
69
        $gameId = $this->getEvent()->getRouteMatch()->getParam('gameId');
70
71
        if (!$gameId) {
72
            return $this->redirect()->toRoute('admin/playgroundgame/createMission');
73
        }
74
75
        $game = $service->getGameMapper()->findById($gameId);
76
77
        $viewModel = new ViewModel();
78
        $viewModel->setTemplate('playground-game/mission/mission');
79
80
        $gameForm = new ViewModel();
81
        $gameForm->setTemplate('playground-game/game/game-form');
82
83
        $form   = $this->getServiceLocator()->get('playgroundgame_mission_form');
84
        $form->setAttribute(
85
            'action',
86
            $this->url()->fromRoute(
87
                'admin/playgroundgame/edit-mission',
88
                array('gameId' => $gameId)
89
            )
90
        );
91
        $form->setAttribute('method', 'post');
92
        if ($game->getFbAppId()) {
93
            $appIds = $form->get('fbAppId')->getOption('value_options');
94
            $appIds[$game->getFbAppId()] = $game->getFbAppId();
95
            $form->get('fbAppId')->setAttribute('options', $appIds);
96
        }
97
98
        $gameOptions = $this->getAdminGameService()->getOptions();
99
        $gameStylesheet = $gameOptions->getMediaPath() . '/' . 'stylesheet_'. $game->getId(). '.css';
100 View Code Duplication
        if (is_file($gameStylesheet)) {
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...
101
            $values = $form->get('stylesheet')->getValueOptions();
102
            $values[$gameStylesheet] = 'Style personnalisé de ce jeu';
103
104
            $form->get('stylesheet')->setAttribute('options', $values);
105
        }
106
107
        $form->bind($game);
108
109
        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...
110
            $data = array_replace_recursive(
111
                $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...
112
                $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...
113
            );
114
            if (empty($data['prizes'])) {
115
                $data['prizes'] = array();
116
            }
117
            
118
            $result = $service->createOrUpdate($data, $game, 'playgroundgame_mission_form');
119
120
            if ($result) {
121
122
                /**
123
                 *   The following work has to be done there because doctrine hydration with nested objects is a mess
124
                 *   I've been obliged to proceed this way... haven't found the bugfix.
125
                 */
126
                /*$cmg = count($result->getMissionGames());
127
                if( $cmg < count($data['missionGames']) ){
128
                    $i=0;
129
                    foreach($data['missionGames'] as $m){
130
                        if( $i >= $cmg ){
131
                            $g = $service->getGameMapper()->findById($m['game']);
132
                            $mg = new \PlaygroundGame\Entity\MissionGame();
133
                            $mg->setGame($g);
134
                            $mg->setPosition($m['position']);
135
                            $mg->setMission($result);
136
                            $result->addMissionGame($mg);
137
                        }
138
                        $i++;
139
                    }
140
                    $service->getGameMapper()->update($result);
141
                }*/
142
                return $this->redirect()->toRoute('admin/playgroundgame/list');
143
            }
144
        }
145
146
        $gameForm->setVariables(array('form' => $form, 'game' => $game));
147
        $viewModel->addChild($gameForm, 'game_form');
148
149
        return $viewModel->setVariables(array('form' => $form, 'title' => 'Edit mission', 'mission' => $game));
150
    }
151
152
    public function getAdminGameService()
153
    {
154
        if (!$this->adminGameService) {
155
            $this->adminGameService = $this->getServiceLocator()->get('playgroundgame_mission_service');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceLocator...dgame_mission_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...
156
        }
157
158
        return $this->adminGameService;
159
    }
160
161
    public function setAdminGameService(AdminGameService $adminGameService)
162
    {
163
        $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...
164
165
        return $this;
166
    }
167
}
168