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 |
||
21 | abstract class ACLModel extends Model |
||
22 | { |
||
23 | const ERROR_NO_PERMISSION = 'no_permission'; |
||
24 | |||
25 | const LISTENER_PRIORITY = 1000; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private $permissionsCache = []; |
||
31 | |||
32 | /** |
||
33 | * @var bool |
||
34 | */ |
||
35 | private $permissionsDisabled = false; |
||
36 | |||
37 | /** |
||
38 | * Checks if the requesting model has a specific permission |
||
39 | * on this object. |
||
40 | * |
||
41 | * @param string $permission |
||
42 | */ |
||
43 | public function can($permission, Model $requester): bool |
||
57 | |||
58 | abstract protected function hasPermission($permission, Model $requester); |
||
59 | |||
60 | /** |
||
61 | * Disables all permissions checking in can() for this object |
||
62 | * DANGER: this should only be used when objects are mutated from application code |
||
63 | * Granting all permissions to anyone else, i.e. HTTP requests is dangerous. |
||
64 | * |
||
65 | * @return $this |
||
66 | */ |
||
67 | public function grantAllPermissions() |
||
73 | |||
74 | /** |
||
75 | * Ensures that permissions are enforced for this object. |
||
76 | * |
||
77 | * @return $this |
||
78 | */ |
||
79 | public function enforcePermissions() |
||
85 | |||
86 | protected function initialize() |
||
102 | |||
103 | View Code Duplication | public static function checkCreatePermission(ModelCreating $event): void |
|
113 | |||
114 | View Code Duplication | public static function checkUpdatePermission(ModelUpdating $event): void |
|
124 | |||
125 | View Code Duplication | public static function checkDeletePermission(ModelDeleting $event): void |
|
135 | } |
||
136 |
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.