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 PdoDelete |
||
11 | { |
||
12 | |||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | public $table = "auth_logins"; |
||
17 | |||
18 | /** |
||
19 | * @var PDOStatement |
||
20 | */ |
||
21 | public $stmt; |
||
22 | |||
23 | |||
24 | /** |
||
25 | * @var LoggerInterface |
||
26 | */ |
||
27 | public $logger; |
||
28 | |||
29 | |||
30 | |||
31 | /** |
||
32 | * @param \PDO $pdo PDO instance |
||
33 | * @param LoggerInterface $logger Optional: PSR-3 Logger |
||
34 | * @param string $table Optional: Custom table name, default: `auth_logins` |
||
35 | */ |
||
36 | 10 | View Code Duplication | public function __construct( \PDO $pdo, LoggerInterface $logger = null, $table = null ) |
51 | |||
52 | |||
53 | /** |
||
54 | * Deletes all logins for a given User ID. |
||
55 | * |
||
56 | * @param int $user_id User ID |
||
57 | * @return int Number of deleted rows |
||
58 | */ |
||
59 | 10 | public function __invoke($user_id) |
|
72 | |||
73 | |||
74 | } |
||
75 |
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.