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 |
||
10 | class Rauth implements RauthInterface |
||
11 | { |
||
12 | const REGEX = '/@auth-([\w-]+)\s?\s(.+)/'; |
||
13 | |||
14 | const MODE_OR = 'or'; |
||
15 | const MODE_AND = 'and'; |
||
16 | const MODE_NONE = 'none'; |
||
17 | |||
18 | const MODES = [ |
||
19 | self::MODE_AND, |
||
20 | self::MODE_NONE, |
||
21 | self::MODE_OR, |
||
22 | ]; |
||
23 | |||
24 | /** @var string */ |
||
25 | private $defaultMode = self::MODE_OR; |
||
26 | |||
27 | /** @var Cache */ |
||
28 | private $cache; |
||
29 | |||
30 | 78 | public function __construct(Cache $c = null) |
|
36 | |||
37 | /** |
||
38 | * Set a default mode for auth blocks without one defined. |
||
39 | * |
||
40 | * Default is MODE_OR |
||
41 | * |
||
42 | * @param string $mode |
||
43 | * @return RauthInterface |
||
44 | */ |
||
45 | 2 | public function setDefaultMode(string $mode = null) : RauthInterface |
|
56 | |||
57 | /** |
||
58 | * Inject Cache instance |
||
59 | * |
||
60 | * @param Cache $c |
||
61 | * @return RauthInterface |
||
62 | */ |
||
63 | 74 | public function setCache(Cache $c) : RauthInterface |
|
69 | |||
70 | /** |
||
71 | * Only used by the class. |
||
72 | * |
||
73 | * Could have user property directly, but having default cache is convenient |
||
74 | * |
||
75 | * @internal |
||
76 | * @return null|Cache |
||
77 | */ |
||
78 | 74 | private function getCache() |
|
86 | |||
87 | /** |
||
88 | * Used to extract the @auth blocks from a class or method |
||
89 | * |
||
90 | * The auth prefix is stripped, and the remaining values are saved as |
||
91 | * key => value pairs. |
||
92 | * |
||
93 | * @param $class |
||
94 | * @param string|null $method |
||
95 | * @return array |
||
96 | */ |
||
97 | 75 | public function extractAuth($class, string $method = null) : array |
|
126 | |||
127 | /** |
||
128 | * Turns a pregexed array of auth blocks into a decent array |
||
129 | * |
||
130 | * Internal use only - @see Rauth::extractAuth |
||
131 | * |
||
132 | * @internal |
||
133 | * @param array $matches |
||
134 | * @return array |
||
135 | */ |
||
136 | 74 | private function normalize(array $matches) : array |
|
161 | |||
162 | |||
163 | /** |
||
164 | * Either passes or fails an authorization attempt. |
||
165 | * |
||
166 | * The first two arguments are the class/method pair to inspect for @auth |
||
167 | * tags, and `$attr` are attributes to compare the @auths against. |
||
168 | * |
||
169 | * Depending on the currently selected mode (either default - for that |
||
170 | * you should @see Rauth::setDefaultMode, or defined in the @auths), it will |
||
171 | * evaluate the arrays against one another and come to a conclusion. |
||
172 | * |
||
173 | * @param $class |
||
174 | * @param string|null $method |
||
175 | * @param array $attr |
||
176 | * @return bool |
||
177 | * @throws \InvalidArgumentException |
||
178 | * @throws AuthException |
||
179 | */ |
||
180 | 68 | public function authorize( |
|
277 | |||
278 | 65 | private function handleBans(&$auth, $attr) |
|
300 | } |
||
301 |
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.