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 |
||
10 | class InMemoryUserRepository implements UserRepository, PasswordResetRepository |
||
11 | { |
||
12 | /** @var array */ |
||
13 | private $users = []; |
||
14 | |||
15 | /** |
||
16 | * InMemoryUserRepository constructor. |
||
17 | */ |
||
18 | public function __construct() |
||
41 | |||
42 | /** |
||
43 | * {@inheritDoc} |
||
44 | */ |
||
45 | public function findByUsername($username) |
||
53 | |||
54 | /** |
||
55 | * {@inheritDoc} |
||
56 | */ |
||
57 | public function find($id) |
||
65 | |||
66 | /** |
||
67 | * {@inheritDoc} |
||
68 | */ |
||
69 | public function supportsClass($class) |
||
73 | |||
74 | /** |
||
75 | * @param string $token |
||
76 | * |
||
77 | * @return UserInterface|null |
||
78 | */ |
||
79 | public function findByPasswordResetToken($token) |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function getSupportedClass() |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function add(UserInterface $user) |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function save(UserInterface $user) |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | View Code Duplication | public function update(UserInterface $user) |
|
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | View Code Duplication | public function delete(UserInterface $user) |
|
132 | } |
||
133 |
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.