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 |
||
7 | class AssignUserToRetailer |
||
8 | { |
||
9 | |||
10 | /** |
||
11 | * Database table |
||
12 | * @var string |
||
13 | */ |
||
14 | public $table = "users_retailers"; |
||
15 | |||
16 | /** |
||
17 | * @var \PDOStatement |
||
18 | */ |
||
19 | public $stmt; |
||
20 | |||
21 | /** |
||
22 | * @var LoggerInterface |
||
23 | */ |
||
24 | public $logger; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * @param PDO $pdo PDO instance |
||
29 | * @param LoggerInterface $logger Optional: PSR-3 Logger |
||
30 | * @param string $table Optional: Database table name |
||
31 | */ |
||
32 | 8 | View Code Duplication | public function __construct(\PDO $pdo, LoggerInterface $logger = null, $table = null) |
46 | |||
47 | |||
48 | /** |
||
49 | * @param int $user_id |
||
50 | * @param int $retailer_number |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | 4 | public function __invoke( $user_id, $retailer_number ) |
|
89 | |||
90 | |||
91 | } |
||
92 |
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.