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 nyx\auth\id\protocols\oauth1\middlewares; |
||
41 | class Authorization |
||
42 | { |
||
43 | /** |
||
44 | * @see http://oauth.net/core/1.0/#consumer_req_param |
||
45 | * Note: Passing the protocol parameters with the body of a Request is currently not implemented by this |
||
46 | * middleware, despite the spec allowing it. |
||
47 | */ |
||
48 | const METHOD_HEADER = 'header'; |
||
49 | const METHOD_QUERY = 'query'; |
||
50 | |||
51 | /** |
||
52 | * @var auth\id\credentials\Client The default Client Credentials to use when those are not passed with the options. |
||
53 | */ |
||
54 | protected $client; |
||
55 | |||
56 | /** |
||
57 | * @var oauth1\interfaces\Signer The default Signer to use when it is not passed with the options. |
||
58 | */ |
||
59 | protected $signer; |
||
60 | |||
61 | /** |
||
62 | * @var string The method with which the protocol parameters will be added to the Request being handled. |
||
63 | */ |
||
64 | private $method = self::METHOD_HEADER; |
||
65 | |||
66 | /** |
||
67 | * Constructs a new OAuth 1.0a Request Authorization Middleware instance. |
||
68 | * |
||
69 | * Note: In a scenario where this handler is used universally in a HTTP Client handler stack that may communicate |
||
70 | * with several different OAuth 1.0a providers, to facilitate reuse of the instance and to avoid potential |
||
71 | * confusion in case of authorization errors, it is suggested to avoid using a default set of client |
||
72 | * credentials and a signer, and instead pass them in along with each Request's $options array. |
||
73 | * |
||
74 | * @param auth\id\credentials\Client $client The default client credentials to sign requests with. |
||
75 | * @param oauth1\interfaces\Signer $signer The default Signer to generate the request's signature with. |
||
76 | */ |
||
77 | public function __construct(auth\id\credentials\Client $client = null, oauth1\interfaces\Signer $signer = null) |
||
82 | |||
83 | /** |
||
84 | * Sets the method by which the protocol parameters will be added to the Request. |
||
85 | * |
||
86 | * @param string $method One of the METHOD_* class constants. |
||
87 | * @return $this |
||
88 | * @throws \InvalidArgumentException When attempting to set an unsupported method. |
||
89 | */ |
||
90 | public function setMethod(string $method) : Authorization |
||
101 | |||
102 | /** |
||
103 | * Invokes the middleware if the $options passed along the Request contain an 'oauth1' key. |
||
104 | * See the class description for which kind of additional options are supported/mandatory. |
||
105 | * |
||
106 | * @param callable $handler |
||
107 | * @return callable |
||
108 | */ |
||
109 | public function __invoke(callable $handler) : callable |
||
132 | |||
133 | /** |
||
134 | * Merges and populates the base protocol parameters with any optional protocol parameters passed in the options. |
||
135 | * |
||
136 | * @param array $options A reference to the options passed along with the Request. |
||
137 | * @throws \InvalidArgumentException When no default signer/credentials are available and no valid |
||
138 | * signer/credentials have been given along with the $options. |
||
139 | * @throws \InvalidArgumentException When the 'token' key is given but is not a auth\interfaces\Credentials instance. |
||
140 | */ |
||
141 | protected function parseOptions(array& $options) |
||
174 | |||
175 | /** |
||
176 | * Merges and populates the base protocol parameters with any optional protocol parameters passed in the options. |
||
177 | * |
||
178 | * @param array $options A reference to the options passed along with the Request. |
||
179 | */ |
||
180 | protected function gatherAuthorizationParams(array& $options) |
||
196 | |||
197 | /** |
||
198 | * Generates the Request's signature based on the defined parameters. |
||
199 | * |
||
200 | * @param Request $request The Request to sign. |
||
201 | * @param array $options A reference to the options passed along with the Request. |
||
202 | */ |
||
203 | protected function sign(Request $request, array& $options) |
||
214 | |||
215 | /** |
||
216 | * Handles the Request. |
||
217 | * |
||
218 | * @param Request $request The request. |
||
219 | * @param array $options The options passed along with the Request. |
||
220 | * @return Request |
||
221 | * @throws \InvalidArgumentException When an invalid authorization method is set. |
||
222 | */ |
||
223 | protected function handle(Request $request, array $options) : Request |
||
239 | |||
240 | /** |
||
241 | * Creates a new Request with the Authorization protocol header applied to it. |
||
242 | * |
||
243 | * @param Request $request The base Request to add the header to. |
||
244 | * @param array $params The protocol parameters. |
||
245 | * @return Request |
||
246 | */ |
||
247 | protected function withHeader(Request $request, array $params) : Request |
||
256 | |||
257 | /** |
||
258 | * Creates a new Request with the protocol parameters appended to its query string. |
||
259 | * |
||
260 | * @param Request $request The base Request to add the query string to. |
||
261 | * @param array $params The protocol parameters. |
||
262 | * @return Request |
||
263 | */ |
||
264 | protected function withQuery(Request $request, array $params) : Request |
||
272 | } |
||
273 |
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.