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 | /** |
||
| 38 | * VotingAssembly constructor. |
||
| 39 | * @param string $subjectName |
||
| 40 | * @param string $actionName |
||
| 41 | * @param string $strategy |
||
| 42 | * @param \SpareParts\Overseer\Voter\IVoter[] $voters |
||
| 43 | */ |
||
| 44 | public function __construct( |
||
| 55 | |||
| 56 | |||
| 57 | 4 | /** |
|
| 58 | 4 | * @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
|
| 59 | 4 | * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
|
| 60 | 4 | * @return null|\SpareParts\Overseer\IVotingResult |
|
| 61 | 4 | * @throws \SpareParts\Overseer\InvalidVotingResultException |
|
| 62 | 4 | */ |
|
| 63 | public function commenceVote(IVotingSubject $votingSubject, IVotingContext $votingContext) |
||
| 80 | |||
| 81 | |||
| 82 | /** |
||
| 83 | * @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
||
| 84 | * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
||
| 85 | * @return \SpareParts\Overseer\IVotingResult |
||
| 86 | * @throws \SpareParts\Overseer\InvalidVotingResultException |
||
| 87 | */ |
||
| 88 | private function strategyFirstVoteDecides(IVotingSubject $votingSubject, IVotingContext $votingContext) |
||
| 97 | |||
| 98 | |||
| 99 | /** |
||
| 100 | * @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
||
| 101 | * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
||
| 102 | * @return \SpareParts\Overseer\IVotingResult |
||
| 103 | */ |
||
| 104 | View Code Duplication | private function strategyAllowUnlessDenied($votingSubject, $votingContext) |
|
| 117 | |||
| 118 | |||
| 119 | /** |
||
| 120 | * @param \SpareParts\Overseer\Voter\IVotingSubject $votingSubject |
||
| 121 | * @param \SpareParts\Overseer\Context\IVotingContext $votingContext |
||
| 122 | * @return \SpareParts\Overseer\IVotingResult |
||
| 123 | */ |
||
| 124 | View Code Duplication | private function strategyDenyUnlessAllowed($votingSubject, $votingContext) |
|
| 137 | |||
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $actionName |
||
| 141 | * @param \SpareParts\Overseer\Voter\IVotingSubject $subject |
||
| 142 | * @param \SpareParts\Overseer\Context\IVotingContext $context |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | public function canVoteOn($actionName, IVotingSubject $subject, IVotingContext $context) |
||
| 152 | } |
||
| 153 |
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.