Completed
Push — develop ( 29e92f...f35b7e )
by greg
05:15
created

MissionController::editMissionAction()   C

Complexity

Conditions 7
Paths 21

Size

Total Lines 79
Code Lines 36

Duplication

Lines 6
Ratio 7.59 %

Importance

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