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 |
||
15 | class Projector extends RoleProjector |
||
16 | { |
||
17 | /** |
||
18 | * @param RoleCreated $roleCreated |
||
19 | */ |
||
20 | protected function applyRoleCreated(RoleCreated $roleCreated) |
||
32 | |||
33 | /** |
||
34 | * @param RoleRenamed $roleRenamed |
||
35 | */ |
||
36 | protected function applyRoleRenamed(RoleRenamed $roleRenamed) |
||
37 | { |
||
38 | $document = $this->loadDocumentFromRepositoryByUuid( |
||
39 | $roleRenamed->getUuid()->toNative() |
||
40 | ); |
||
41 | |||
42 | $json = $document->getBody(); |
||
43 | $json->name = $roleRenamed->getName()->toNative(); |
||
44 | |||
45 | $this->repository->save($document->withBody($json)); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param RoleDeleted $roleDeleted |
||
50 | */ |
||
51 | protected function applyRoleDeleted(RoleDeleted $roleDeleted) |
||
55 | |||
56 | /** |
||
57 | * @param ConstraintAdded $constraintAdded |
||
58 | */ |
||
59 | View Code Duplication | protected function applyConstraintAdded( |
|
|
|||
60 | ConstraintAdded $constraintAdded |
||
61 | ) { |
||
62 | $document = $this->loadDocumentFromRepositoryByUuid( |
||
63 | $constraintAdded->getUuid()->toNative() |
||
64 | ); |
||
65 | |||
66 | $json = $document->getBody(); |
||
67 | $json->constraint = $constraintAdded->getQuery()->toNative(); |
||
68 | |||
69 | $json->constraints = new \stdClass(); |
||
70 | $json->constraints->{$constraintAdded->getSapiVersion()->toNative()} = $constraintAdded->getQuery()->toNative(); |
||
71 | |||
72 | $this->repository->save($document->withBody($json)); |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param ConstraintUpdated $constraintUpdated |
||
77 | */ |
||
78 | View Code Duplication | protected function applyConstraintUpdated( |
|
79 | ConstraintUpdated $constraintUpdated |
||
80 | ) { |
||
81 | $document = $this->loadDocumentFromRepositoryByUuid( |
||
82 | $constraintUpdated->getUuid()->toNative() |
||
83 | ); |
||
84 | |||
85 | $json = $document->getBody(); |
||
86 | $json->constraint = $constraintUpdated->getQuery()->toNative(); |
||
87 | $json->constraints->{$constraintUpdated->getSapiVersion()->toNative()} = $constraintUpdated->getQuery()->toNative(); |
||
88 | |||
89 | $this->repository->save($document->withBody($json)); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param ConstraintRemoved $constraintRemoved |
||
94 | */ |
||
95 | protected function applyConstraintRemoved( |
||
96 | ConstraintRemoved $constraintRemoved |
||
97 | ) { |
||
98 | $document = $this->loadDocumentFromRepositoryByUuid( |
||
99 | $constraintRemoved->getUuid()->toNative() |
||
100 | ); |
||
101 | |||
102 | $json = $document->getBody(); |
||
103 | $json->constraint = null; |
||
104 | $json->constraints->{$constraintRemoved->getSapiVersion()->toNative()} = null; |
||
105 | |||
106 | $this->repository->save($document->withBody($json)); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param PermissionAdded $permissionAdded |
||
111 | */ |
||
112 | public function applyPermissionAdded(PermissionAdded $permissionAdded) |
||
129 | |||
130 | /** |
||
131 | * @param PermissionRemoved $permissionRemoved |
||
132 | */ |
||
133 | public function applyPermissionRemoved(PermissionRemoved $permissionRemoved) |
||
154 | } |
||
155 |
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.