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

Mission::checkMissionCondition()   C

Complexity

Conditions 19
Paths 49

Size

Total Lines 69
Code Lines 34

Duplication

Lines 66
Ratio 95.65 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 66
loc 69
rs 5.6193
cc 19
eloc 34
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
    {
53
        return $this->getMissionGameMapper()->findBy(array('mission'=>$mission));
54
    }
55
    
56 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...
57
    {
58
        $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...
59
        if (empty($missionGame)) {
60
            return false;
61
        }
62
    
63
        if ($missionGame->getMission()->getActive() === false) {
64
            return false;
65
        }
66
    
67
        $nextMissionGame = $this->getMissionGameMapper()->getNextGame(
68
            $missionGame->getMission(),
69
            $missionGame->getPosition()
70
        );
71
    
72
        if (empty($nextMissionGame)) {
73
            return false;
74
        }
75
    
76
        $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...
77
    
78
        if (empty($missionGameConditions)) {
79
            return false;
80
        }
81
    
82
        foreach ($missionGameConditions as $missionGameCondition) {
83
    
84
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::NONE) {
85
                continue;
86
            }
87
    
88
            // On passe au suivant si on a gagné
89
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::VICTORY) {
90
                if (!($winner || $prediction)) {
91
                    return false;
92
                }
93
            }
94
    
95
            // On passe au suivant si on a perdu
96
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::DEFEAT) {
97
                if ($winner || $prediction) {
98
                    return false;
99
                }
100
            }
101
    
102
            // On passe au suivant si on a perdu
103
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::GREATER) {
104
                if (!$entry) {
105
                    return false;
106
                }
107
                if (!($entry->getPoints() > $missionGameCondition->getValue())) {
108
                    return false;
109
                }
110
            }
111
    
112
            // On passe au suivant si on a perdu
113
            if ($missionGameCondition->getAttribute() == MissionGameConditionEntity::LESS) {
114
                if (!$entry) {
115
                    return false;
116
                }
117
                if (!($entry->getPoints() < $missionGameCondition->getValue())) {
118
                    return false;
119
                }
120
            }
121
        }
122
    
123
        return $nextMissionGame->getGame();
124
    }
125
126
    public function getGameEntity()
127
    {
128
        return new \PlaygroundGame\Entity\Mission;
129
    }
130
131
    /**
132
     * getMissionMapper
133
     *
134
     * @return MissionMapperInterface
135
     */
136
    public function getMissionMapper()
137
    {
138
        if (null === $this->missionMapper) {
139
            $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...
140
        }
141
142
        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 142 which is incompatible with the return type documented by PlaygroundGame\Service\Mission::getMissionMapper of type PlaygroundGame\Service\MissionMapperInterface.
Loading history...
143
    }
144
145
    /**
146
     * setMissionMapper
147
     *
148
     * @param  MissionMapperInterface $missionMapper
149
     * @return Mission
150
     */
151
    public function setMissionMapper($missionMapper)
152
    {
153
        $this->missionMapper = $missionMapper;
154
155
        return $this;
156
    }
157
    
158
    /**
159
     * getMissionGameMapper
160
     *
161
     * @return MissionGameMapperInterface
162
     */
163 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...
164
    {
165
        if (null === $this->missionGameMapper) {
166
            $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...
167
        }
168
    
169
        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 169 which is incompatible with the return type documented by PlaygroundGame\Service\M...n::getMissionGameMapper of type PlaygroundGame\Service\MissionGameMapperInterface.
Loading history...
170
    }
171
    
172
    /**
173
     * setMissionMapper
174
     *
175
     * @param  MissionMapperInterface $missionGameMapper
176
     * @return Mission
177
     */
178
    public function setMissionGameMapper($missionGameMapper)
179
    {
180
        $this->missionGameMapper = $missionGameMapper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $missionGameMapper of type object<PlaygroundGame\Se...MissionMapperInterface> is incompatible with the declared type object<PlaygroundGame\Se...ionGameMapperInterface> of property $missionGameMapper.

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...
181
    
182
        return $this;
183
    }
184
}
185