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 |
||
| 18 | class JwtUserProviderTest extends \PHPUnit_Framework_TestCase |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var JwtUserProvider |
||
| 22 | */ |
||
| 23 | private $provider; |
||
| 24 | |||
| 25 | protected function setUp() |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @test |
||
| 32 | */ |
||
| 33 | public function canLoadUserPopulatedFromToken() |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @test |
||
| 45 | */ |
||
| 46 | public function willAddGroupsFromAudienceClaims() |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @test |
||
| 55 | */ |
||
| 56 | public function onlySupportsJwtUser() |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @test |
||
| 64 | */ |
||
| 65 | public function refreshUserReturnsNewInstance() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @test |
||
| 74 | */ |
||
| 75 | public function canCallSuperfluousMethods() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @param array $claims |
||
| 85 | * @return JwtUser |
||
| 86 | */ |
||
| 87 | private function loadUser(array $claims) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param array $claims |
||
| 96 | * |
||
| 97 | * @return JwtToken |
||
| 98 | */ |
||
| 99 | View Code Duplication | private function createToken(array $claims) |
|
| 111 | } |
||
| 112 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call: