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 |
||
12 | class GuardCoverageToolCommandHandlerTest extends PhpUnitUnitTestCase |
||
13 | { |
||
14 | /** |
||
15 | * @var GuardCoverageToolCommandHandler |
||
16 | */ |
||
17 | private $guardCoverageToolCommandHandler; |
||
18 | |||
19 | protected function setUp() |
||
30 | |||
31 | /** |
||
32 | * @test |
||
33 | */ |
||
34 | View Code Duplication | public function itShouldShowWarningMessage() |
|
35 | { |
||
36 | $outputMessage = new PreCommitOutputWriter(GuardCoverageTool::CHECKING_MESSAGE); |
||
37 | $currentCoverage = 60.00; |
||
38 | $previousCoverage = 70.00; |
||
39 | |||
40 | $this->shouldProcessStrictCoverage($currentCoverage); |
||
41 | $this->shouldWriteOutput($outputMessage->getMessage()); |
||
42 | $this->shouldReadGuardCoverage($previousCoverage); |
||
43 | $this->shouldWriteLnOutput( |
||
44 | sprintf( |
||
45 | "\n<bg=yellow;options=bold>%s Previous coverage %s, current coverage %s.</>", |
||
46 | HookQuestions::PHPUNIT_GUARD_COVERAGE_MESSAGE_DEFAULT, |
||
47 | $previousCoverage, |
||
48 | $currentCoverage |
||
49 | ) |
||
50 | ); |
||
51 | $this->shouldWriteGuardCoverage($currentCoverage); |
||
52 | |||
53 | $this->guardCoverageToolCommandHandler->handle( |
||
54 | new GuardCoverageCommand(HookQuestions::PHPUNIT_GUARD_COVERAGE_MESSAGE_DEFAULT) |
||
55 | ); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @test |
||
60 | */ |
||
61 | View Code Duplication | public function itShouldWorksFine() |
|
77 | } |
||
78 |
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.