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 |
||
8 | View Code Duplication | class PdoAssignRoleToPermission |
|
|
|||
9 | { |
||
10 | |||
11 | use LoggerAwareTrait; |
||
12 | |||
13 | /** |
||
14 | * @var \PDO |
||
15 | */ |
||
16 | public $pdo; |
||
17 | |||
18 | /** |
||
19 | * @var \PDOStatement |
||
20 | */ |
||
21 | public $stmt; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | public $permissions_roles_table = "permissions_roles"; |
||
27 | |||
28 | /** |
||
29 | * @var Seperator string for roles in SELECT statement |
||
30 | */ |
||
31 | protected $separator = ","; |
||
32 | |||
33 | |||
34 | /** |
||
35 | * @param \PDO $pdo PDO instance |
||
36 | * @param string $permissions_roles_table Permissions/Roles table name |
||
37 | * @param LoggerInterface|null $logger Optional: PSR-3 Logger |
||
38 | */ |
||
39 | public function __construct( \PDO $pdo, $permissions_roles_table, LoggerInterface $logger = null ) |
||
56 | |||
57 | |||
58 | /** |
||
59 | * @return int Last Insert ID or FALSE when something errored |
||
60 | * @throws PermissionNameExistsException On duplicate name |
||
61 | */ |
||
62 | public function __invoke( $permission_id, $role_id ) |
||
90 | } |
||
91 |
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.