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 OAuthUserCreator implements OAuthUserCreatorInterface |
||
10 | { |
||
11 | /** @var EntityManagerInterface */ |
||
12 | private $em; |
||
13 | |||
14 | /** @var array */ |
||
15 | private $hostedDomains; |
||
16 | |||
17 | /** @var string */ |
||
18 | private $userClass; |
||
19 | |||
20 | /** @var OAuthUserFinderInterface */ |
||
21 | private $userFinder; |
||
22 | |||
23 | /** |
||
24 | * OAuthUserCreator constructor. |
||
25 | * |
||
26 | * @param EntityManagerInterface $em |
||
27 | * @param array $hostedDomains |
||
28 | * @param string $userClass |
||
29 | * @param OAuthUserFinderInterface $userFinder |
||
30 | */ |
||
31 | public function __construct(EntityManagerInterface $em, $hostedDomains, $userClass, OAuthUserFinderInterface $userFinder) |
||
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | public function getOrCreateUser($email, $googleId) |
||
75 | |||
76 | /** |
||
77 | * This method returns the access level coupled with the domain of the given email |
||
78 | * If the given domain name has not been configured this function will return null |
||
79 | * |
||
80 | * @param string $email |
||
81 | * |
||
82 | * @return string[]|null |
||
83 | */ |
||
84 | View Code Duplication | private function getAccessLevels($email) |
|
94 | |||
95 | /** |
||
96 | * This method returns wether a domain for the given email has been configured |
||
97 | * |
||
98 | * @param string $email |
||
99 | * |
||
100 | * @return bool |
||
101 | */ |
||
102 | View Code Duplication | private function isConfiguredDomain($email) |
|
112 | } |
||
113 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.