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 |
||
5 | class ExpirationVerifierTest extends \PHPUnit_Framework_TestCase |
||
6 | { |
||
7 | /** |
||
8 | * @var \PHPUnit_Framework_MockObject_MockObject|\Emarref\Jwt\Token\Payload |
||
9 | */ |
||
10 | private $payload; |
||
11 | |||
12 | /** |
||
13 | * @var \PHPUnit_Framework_MockObject_MockObject|\Emarref\Jwt\Token |
||
14 | */ |
||
15 | private $token; |
||
16 | |||
17 | /** |
||
18 | * @var ExpirationVerifier |
||
19 | */ |
||
20 | private $verifier; |
||
21 | |||
22 | View Code Duplication | public function setUp() |
|
34 | |||
35 | View Code Duplication | public function testMissingExpiry() |
|
43 | |||
44 | /** |
||
45 | * @expectedException \Emarref\Jwt\Exception\ExpiredException |
||
46 | * @expectedExceptionMessage Token expired at "Sat, 08 Nov 2014 00:00:00 +0000" |
||
47 | */ |
||
48 | View Code Duplication | public function testExpired() |
|
64 | |||
65 | /** |
||
66 | * @expectedException \InvalidArgumentException |
||
67 | * @expectedExceptionMessage Invalid expiration timestamp "foobar" |
||
68 | */ |
||
69 | View Code Duplication | public function testUnexpectedValue() |
|
83 | |||
84 | View Code Duplication | public function testValid() |
|
100 | } |
||
101 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: