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 PdoValidator |
||
11 | { |
||
12 | /** |
||
13 | * @var LoggerInterface |
||
14 | */ |
||
15 | public $logger; |
||
16 | |||
17 | |||
18 | /** |
||
19 | * @var PDOStatement |
||
20 | */ |
||
21 | public $stmt; |
||
22 | |||
23 | /** |
||
24 | * @var Callable |
||
25 | */ |
||
26 | public $verifier; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | public $table = "auth_logins"; |
||
33 | |||
34 | |||
35 | /** |
||
36 | * @param \PDO $pdo PDO instance |
||
37 | * @param Callable $verifier Token authentication that accepts selector and token |
||
38 | * @param LoggerInterface|null $logger Optional: PSR-3 Logger |
||
39 | * @param string $table Optional: Custom table name, default: `auth_logins` |
||
40 | */ |
||
41 | 20 | View Code Duplication | public function __construct( \PDO $pdo, Callable $verifier, LoggerInterface $logger = null, $table = null) |
57 | |||
58 | |||
59 | /** |
||
60 | * Finds the user ID for a matching selector and token pair. |
||
61 | * |
||
62 | * @param string $selector Login selector |
||
63 | * @param string $token Login token |
||
64 | * @return mixed User ID or FALSE |
||
65 | */ |
||
66 | 15 | public function __invoke( $selector, $token ) |
|
88 | } |
||
89 |
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.