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) |
||
| 88 | |||
| 89 | |||
| 90 | /** |
||
| 91 | * @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
||
| 92 | * @param \SpareParts\Overseer\Identity\IVotingContext $votingContext |
||
| 93 | * @return \SpareParts\Overseer\IVotingResult |
||
| 94 | * @throws \SpareParts\Overseer\InvalidVotingResultException |
||
| 95 | */ |
||
| 96 | private function strategyFirstVoteDecides(IVotingSubject $votingSubject, IVotingContext $votingContext) |
||
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
||
| 109 | * @param \SpareParts\Overseer\Identity\IVotingContext $votingContext |
||
| 110 | * @return \SpareParts\Overseer\IVotingResult |
||
| 111 | */ |
||
| 112 | View Code Duplication | private function strategyAllowUnlessDenied($votingSubject, $votingContext) |
|
| 125 | |||
| 126 | |||
| 127 | /** |
||
| 128 | * @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
||
| 129 | * @param \SpareParts\Overseer\Identity\IVotingContext $votingContext |
||
| 130 | * @return \SpareParts\Overseer\IVotingResult |
||
| 131 | */ |
||
| 132 | View Code Duplication | private function strategyDenyUnlessAllowed($votingSubject, $votingContext) |
|
| 145 | |||
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $voterName |
||
| 149 | * @param string|IVotingResult $partialResult |
||
| 150 | * @return \SpareParts\Overseer\IVotingResult|null |
||
| 151 | * @throws \SpareParts\Overseer\InvalidVotingResultException |
||
| 152 | */ |
||
| 153 | protected function prepareResult($voterName, $partialResult) |
||
| 166 | |||
| 167 | |||
| 168 | /** |
||
| 169 | * @param string $actionName |
||
| 170 | * @param \SpareParts\Overseer\Voter\IVotingSubject $subject |
||
| 171 | * @param \SpareParts\Overseer\Identity\IVotingContext $context |
||
| 172 | * @return bool |
||
| 173 | */ |
||
| 174 | 4 | public function canVoteOn($actionName, IVotingSubject $subject, IVotingContext $context) |
|
| 185 | } |
||
| 186 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.