Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
13 | class VotingAssembly implements IVotingAssembly |
||
14 | { |
||
15 | |||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | private $strategy; |
||
20 | |||
21 | /** |
||
22 | * @var IVoter[] |
||
23 | */ |
||
24 | private $voters; |
||
25 | |||
26 | /** |
||
27 | * @var string |
||
28 | */ |
||
29 | private $subjectName; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | private $actionName; |
||
35 | |||
36 | /** |
||
37 | * @var null|string |
||
38 | */ |
||
39 | private $contextClassname; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * VotingAssembly constructor. |
||
44 | * @param string $subjectName |
||
45 | * @param string $actionName |
||
46 | * @param string $strategy |
||
47 | * @param \SpareParts\Overseer\Voter\IVoter[] $voters |
||
48 | * @param string|null $contextClassname If present, this assembly will require specific context class |
||
49 | */ |
||
50 | 4 | public function __construct( |
|
63 | |||
64 | |||
65 | /** |
||
66 | * @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
||
67 | * @param \SpareParts\Overseer\Identity\IVotingContext $votingContext |
||
68 | * @return null|\SpareParts\Overseer\IVotingResult |
||
69 | * @throws \SpareParts\Overseer\InvalidVotingResultException |
||
70 | */ |
||
71 | public function commenceVote(IVotingSubject $votingSubject, IVotingContext $votingContext) |
||
85 | |||
86 | |||
87 | /** |
||
88 | * @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
||
89 | * @param \SpareParts\Overseer\Identity\IVotingContext $votingContext |
||
90 | * @return \SpareParts\Overseer\IVotingResult |
||
91 | * @throws \SpareParts\Overseer\InvalidVotingResultException |
||
92 | */ |
||
93 | private function strategyFirstVoteDecides(IVotingSubject $votingSubject, IVotingContext $votingContext) |
||
102 | |||
103 | |||
104 | /** |
||
105 | * @param string $voterName |
||
106 | * @param string|IVotingResult $partialResult |
||
107 | * @return \SpareParts\Overseer\VotingResult|null |
||
108 | * @throws \SpareParts\Overseer\InvalidVotingResultException |
||
109 | */ |
||
110 | protected function prepareResult($voterName, $partialResult) |
||
123 | |||
124 | |||
125 | private function strategyAllowUnlessDenied($votingSubject, $votingContext) |
||
134 | |||
135 | |||
136 | /** |
||
137 | * @param string $actionName |
||
138 | * @param \SpareParts\Overseer\Voter\IVotingSubject $subject |
||
139 | * @param \SpareParts\Overseer\Identity\IVotingContext $context |
||
140 | * @return bool |
||
141 | */ |
||
142 | 4 | public function canVoteOn($actionName, IVotingSubject $subject, IVotingContext $context) |
|
153 | } |
||
154 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.