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 namespace Limoncello\Application\Authorization; |
||
27 | trait AuthorizationRulesTrait |
||
28 | { |
||
29 | /** |
||
30 | * @param ContextInterface $context |
||
31 | * |
||
32 | * @return bool |
||
33 | */ |
||
34 | protected static function reqHasAction(ContextInterface $context): bool |
||
35 | { |
||
36 | return $context->has(RequestProperties::REQ_ACTION); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param ContextInterface $context |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | protected static function reqGetAction(ContextInterface $context): string |
||
45 | { |
||
46 | assert(static::reqHasAction($context)); |
||
47 | |||
48 | $value = $context->get(RequestProperties::REQ_ACTION); |
||
49 | |||
50 | return $value; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @param ContextInterface $context |
||
55 | * |
||
56 | * @return bool |
||
57 | */ |
||
58 | protected static function reqHasResourceType(ContextInterface $context): bool |
||
59 | { |
||
60 | return $context->has(RequestProperties::REQ_RESOURCE_TYPE); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param ContextInterface $context |
||
65 | * |
||
66 | * @return string|null |
||
67 | */ |
||
68 | protected static function reqGetResourceType(ContextInterface $context) |
||
69 | { |
||
70 | assert(static::reqHasResourceType($context)); |
||
71 | |||
72 | $value = $context->get(RequestProperties::REQ_RESOURCE_TYPE); |
||
73 | |||
74 | assert($value === null || is_string($value)); |
||
75 | |||
76 | return $value; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param ContextInterface $context |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | protected static function reqHasResourceIdentity(ContextInterface $context): bool |
||
85 | { |
||
86 | return $context->has(RequestProperties::REQ_RESOURCE_IDENTITY); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param ContextInterface $context |
||
91 | * |
||
92 | * @return string|int|null |
||
93 | */ |
||
94 | protected static function reqGetResourceIdentity(ContextInterface $context) |
||
95 | { |
||
96 | assert(static::reqHasResourceIdentity($context)); |
||
97 | |||
98 | $value = $context->get(RequestProperties::REQ_RESOURCE_IDENTITY); |
||
99 | |||
100 | assert($value === null || is_string($value) || is_int($value)); |
||
101 | |||
102 | return $value; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param ContextInterface $context |
||
107 | * |
||
108 | * @return bool |
||
109 | */ |
||
110 | View Code Duplication | protected static function ctxHasCurrentAccount(ContextInterface $context): bool |
|
119 | |||
120 | /** |
||
121 | * @param ContextInterface $context |
||
122 | * |
||
123 | * @return AccountInterface |
||
124 | */ |
||
125 | View Code Duplication | protected static function ctxGetCurrentAccount(ContextInterface $context): AccountInterface |
|
136 | |||
137 | /** |
||
138 | * @param ContextInterface $context |
||
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | protected static function ctxHasContainer(ContextInterface $context): bool |
||
146 | |||
147 | /** |
||
148 | * @param ContextInterface $context |
||
149 | * |
||
150 | * @return ContainerInterface |
||
151 | */ |
||
152 | protected static function ctxGetContainer(ContextInterface $context): ContainerInterface |
||
158 | } |
||
159 |
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.