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 |
||
21 | class UserPreferenceService implements UserPreferenceServiceInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var \eZ\Publish\API\Repository\Repository |
||
25 | */ |
||
26 | private $repository; |
||
27 | |||
28 | /** |
||
29 | * @var \eZ\Publish\SPI\Persistence\UserPreference\Handler |
||
30 | */ |
||
31 | private $userPreferenceHandler; |
||
32 | |||
33 | /** |
||
34 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
35 | * @param \eZ\Publish\SPI\Persistence\UserPreference\Handler $userPreferenceHandler |
||
36 | */ |
||
37 | public function __construct(RepositoryInterface $repository, UserPreferenceHandler $userPreferenceHandler) |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | View Code Duplication | public function loadUserPreferences(int $offset = 0, int $limit = 25): UserPreferenceList |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | public function setUserPreference(array $userPreferenceSetStructs): void |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function getUserPreference(string $userPreferenceName): APIUserPreference |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function getUserPreferenceCount(): int |
||
124 | |||
125 | /** |
||
126 | * Builds UserPreference domain object from ValueObject returned by Persistence API. |
||
127 | * |
||
128 | * @param \eZ\Publish\SPI\Persistence\UserPreference\UserPreference $spiUserPreference |
||
129 | * |
||
130 | * @return \eZ\Publish\API\Repository\Values\UserPreference\UserPreference |
||
131 | */ |
||
132 | protected function buildDomainObject(UserPreference $spiUserPreference): APIUserPreference |
||
139 | |||
140 | private function getCurrentUserId(): int |
||
147 | } |
||
148 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.