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 TypeToken implements TokenInterface |
||
22 | { |
||
23 | private $type; |
||
24 | |||
25 | /** |
||
26 | * @param string $type |
||
27 | */ |
||
28 | public function __construct($type) |
||
29 | { |
||
30 | $checker = "is_{$type}"; |
||
31 | View Code Duplication | if (!function_exists($checker) && !interface_exists($type) && !class_exists($type)) { |
|
|
|||
32 | throw new InvalidArgumentException(sprintf( |
||
33 | 'Type or class name expected as an argument to TypeToken, but got %s.', $type |
||
34 | )); |
||
35 | } |
||
36 | |||
37 | $this->type = $type; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Scores 5 if argument has the same type this token was constructed with. |
||
42 | * |
||
43 | * @param $argument |
||
44 | * |
||
45 | * @return bool|int |
||
46 | */ |
||
47 | public function scoreArgument($argument) |
||
56 | |||
57 | /** |
||
58 | * Returns false. |
||
59 | * |
||
60 | * @return bool |
||
61 | */ |
||
62 | public function isLast() |
||
66 | |||
67 | /** |
||
68 | * Returns string representation for token. |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function __toString() |
||
76 | } |
||
77 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.