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

Mission::checkMissionCondition()   C

Complexity

Conditions 19
Paths 49

Size

Total Lines 66
Code Lines 32

Duplication

Lines 66
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 66
loc 66
rs 5.8086
cc 19
eloc 32
nc 49
nop 4

How to fix   Long Method    Complexity   

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\Service;
4
5
use Zend\ServiceManager\ServiceManagerAwareInterface;
6
use Zend\ServiceManager\ServiceManager;
7
use DoctrineModule\Validator\NoObjectExists as NoObjectExistsValidator;
8
use PlaygroundGame\Service\Game;
9
use Zend\Db\Sql\Sql;
10
use Zend\Db\Adapter\Adapter;
11
12
class Mission extends Game implements ServiceManagerAwareInterface
13
{
14
15
    /**
16
     * @var MissionMapperInterface
17
     */
18
    protected $missionMapper;
19
    
20
    /**
21
     * @var MissionGameMapperInterface
22
     */
23
    protected $missionGameMapper;
24
25
    protected $options;
26
    
27
    /**
28
     * Find games associated to a mission and add the last entry of the user if it exists
29
     * @param unknown $mission
30
     * @return multitype:NULL
0 ignored issues
show
Documentation introduced by
The doc-type multitype:NULL could not be parsed: Unknown type name "multitype:NULL" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
31
     */
32
    public function getMissionGames($mission, $user)
33
    {
34
        $games = array();
35
        $missionGames = $this->findGamesByMission($mission);
36
        foreach ($missionGames as $missionGame) {
37
            $entry = $this->checkExistingEntry($missionGame->getGame(), $user);
38
            $games[$missionGame->getGame()->getIdentifier()]['game'] = $missionGame;
39
            $games[$missionGame->getGame()->getIdentifier()]['entry'] = $entry;
40
        }
41
    
42
        return $games;
43
    }
44
    
45
    /**
46
     * findMissionGameByMission : find associated games to a mission
47
     * @param Mission $mission
48
     *
49
     * @return Collection de MissionGame $missionGames
50
     */
51
    public function findGamesByMission($mission){
52
        return $this->getMissionGameMapper()->findBy(array('mission'=>$mission));
53
    }
54
    
55 View Code Duplication
    public function checkMissionCondition($game, $winner, $prediction, $entry)
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...
56
    {
57
        $missionGame = $this->findMissionGameByGame($game);
0 ignored issues
show
Bug introduced by
The method findMissionGameByGame() does not seem to exist on object<PlaygroundGame\Service\Mission>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
        if(empty($missionGame)){
59
            return false;
60
        }
61
    
62
        if($missionGame->getMission()->getActive() === false){
63
            return false;
64
        }
65
    
66
        $nextMissionGame = $this->getMissionGameMapper()->getNextGame($missionGame->getMission(), $missionGame->getPosition());
67
    
68
        if(empty($nextMissionGame)){
69
            return false;
70
        }
71
    
72
        $missionGameConditions = $this->findMissionGameConditionByMissionGame($nextMissionGame);
0 ignored issues
show
Bug introduced by
The method findMissionGameConditionByMissionGame() does not seem to exist on object<PlaygroundGame\Service\Mission>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
    
74
        if(empty($missionGameConditions)){
75
            return false;
76
        }
77
    
78
        foreach ($missionGameConditions as $missionGameCondition) {
79
    
80
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::NONE) {
81
                continue;
82
            }
83
    
84
            // On passe au suivant si on a gagné
85
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::VICTORY) {
86
                if(!($winner || $prediction)){
87
                    return false;
88
                }
89
            }
90
    
91
            // On passe au suivant si on a perdu
92
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::DEFEAT) {
93
                if($winner || $prediction){
94
                    return false;
95
                }
96
            }
97
    
98
            // On passe au suivant si on a perdu
99
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::GREATER) {
100
                if (!$entry) {
101
                    return false;
102
                }
103
                if(!($entry->getPoints() > $missionGameCondition->getValue())){
104
                    return false;
105
                }
106
            }
107
    
108
            // On passe au suivant si on a perdu
109
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::LESS) {
110
                if (!$entry) {
111
                    return false;
112
                }
113
                if(!($entry->getPoints() < $missionGameCondition->getValue())){
114
                    return false;
115
                }
116
            }
117
        }
118
    
119
        return $nextMissionGame->getGame();
120
    }
121
122
    public function getGameEntity()
123
    {
124
        return new \PlaygroundGame\Entity\Mission;
125
    }
126
127
    /**
128
     * getMissionMapper
129
     *
130
     * @return MissionMapperInterface
131
     */
132
    public function getMissionMapper()
133
    {
134
        if (null === $this->missionMapper) {
135
            $this->missionMapper = $this->getServiceManager()->get('playgroundgame_mission_mapper');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceManager...ndgame_mission_mapper') can also be of type array. However, the property $missionMapper is declared as type object<PlaygroundGame\Se...MissionMapperInterface>. 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...
136
        }
137
138
        return $this->missionMapper;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->missionMapper; of type object|array adds the type array to the return on line 138 which is incompatible with the return type documented by PlaygroundGame\Service\Mission::getMissionMapper of type PlaygroundGame\Service\MissionMapperInterface.
Loading history...
139
    }
140
141
    /**
142
     * setMissionMapper
143
     *
144
     * @param  MissionMapperInterface $missionMapper
145
     * @return User
146
     */
147
    public function setMissionMapper($missionMapper)
148
    {
149
        $this->missionMapper = $missionMapper;
150
151
        return $this;
152
    }
153
    
154
    /**
155
     * getMissionGameMapper
156
     *
157
     * @return MissionGameMapperInterface
158
     */
159 View Code Duplication
    public function getMissionGameMapper()
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...
160
    {
161
        if (null === $this->missionGameMapper) {
162
            $this->missionGameMapper = $this->getServiceManager()->get('playgroundgame_mission_game_mapper');
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getServiceManager...e_mission_game_mapper') can also be of type array. However, the property $missionGameMapper is declared as type object<PlaygroundGame\Se...ionGameMapperInterface>. 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...
163
        }
164
    
165
        return $this->missionGameMapper;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->missionGameMapper; of type object|array adds the type array to the return on line 165 which is incompatible with the return type documented by PlaygroundGame\Service\M...n::getMissionGameMapper of type PlaygroundGame\Service\MissionGameMapperInterface.
Loading history...
166
    }
167
    
168
    /**
169
     * setMissionMapper
170
     *
171
     * @param  MissionMapperInterface $missionMapper
0 ignored issues
show
Bug introduced by
There is no parameter named $missionMapper. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
172
     * @return User
173
     */
174
    public function setMissionGameMapper($missionGameMapper)
175
    {
176
        $this->missionGameMapper = $missionGameMapper;
177
    
178
        return $this;
179
    }
180
}
181