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 |
||
| 9 | class UrlValidatorTest extends TestCase |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var URLValidator|MockObject |
||
| 13 | */ |
||
| 14 | private $validatorMock; |
||
| 15 | |||
| 16 | protected function setUp() |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @dataProvider emailAddressProvider |
||
| 23 | */ |
||
| 24 | public function testIsEmailAddress($email, $result) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @dataProvider internalLinkProvider |
||
| 31 | */ |
||
| 32 | public function testIsInternalLink($link, $result) |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @dataProvider internalMediaLinkProvider |
||
| 39 | */ |
||
| 40 | public function testIsInternalMediaLink($link, $result) |
||
| 44 | |||
| 45 | public function emailAddressProvider() |
||
| 53 | |||
| 54 | View Code Duplication | public function internalLinkProvider() |
|
| 66 | |||
| 67 | View Code Duplication | public function internalMediaLinkProvider() |
|
| 79 | } |
||
| 80 |
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: