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 |
||
20 | class Middleware |
||
21 | { |
||
22 | /** |
||
23 | * @var Callable |
||
24 | */ |
||
25 | public $client_authenticator; |
||
26 | |||
27 | /** |
||
28 | * @var Callable |
||
29 | */ |
||
30 | public $client_auth_remover; |
||
31 | |||
32 | /** |
||
33 | * @var Callable |
||
34 | */ |
||
35 | public $server_authenticator; |
||
36 | |||
37 | /** |
||
38 | * @var LoggerInterface |
||
39 | */ |
||
40 | public $logger; |
||
41 | |||
42 | /** |
||
43 | * HTTP Status code to use on redirect after successful login |
||
44 | * @var int |
||
45 | */ |
||
46 | public $authenticated_status_code = 200; |
||
47 | |||
48 | /** |
||
49 | * @var AuthUserInterface |
||
50 | */ |
||
51 | public $user; |
||
52 | |||
53 | /** |
||
54 | * Reflects current working status |
||
55 | */ |
||
56 | public $status = 0; |
||
57 | |||
58 | /** |
||
59 | * @var int |
||
60 | */ |
||
61 | const ACTIVE = 1; |
||
62 | |||
63 | /** |
||
64 | * @var int |
||
65 | */ |
||
66 | const CLIENT_DATA = 2; |
||
67 | |||
68 | /** |
||
69 | * @var int |
||
70 | */ |
||
71 | const SERVER_MATCH = 4; |
||
72 | |||
73 | |||
74 | |||
75 | /** |
||
76 | * @param AuthUserInterface $user Authentication User Object |
||
77 | * @param Callable $client_authenticator Callable that returns selector and token sent with request |
||
78 | * @param Callable $client_auth_remover Callable that deletes errorneous Client Authentication |
||
79 | * @param Callable $server_authenticator Callable that accepts selector and token and returns User ID |
||
80 | * @param LoggerInterface|null $logger Optional: PSR-3 Logger |
||
81 | */ |
||
82 | 30 | public function __construct( AuthUserInterface $user, Callable $client_authenticator, Callable $client_auth_remover, Callable $server_authenticator, LoggerInterface $logger = null) |
|
91 | |||
92 | |||
93 | /** |
||
94 | * @param Psr\Http\Message\ServerRequestInterface $request PSR7 request |
||
95 | * @param Psr\Http\Message\ResponseInterface $response PSR7 response |
||
96 | * @param callable $next Next middleware |
||
97 | * |
||
98 | * @return Psr\Http\Message\ResponseInterface |
||
99 | * |
||
100 | * @throws RuntimeException if Request has no 'user' attribute with AuthUserInterface instance. |
||
101 | */ |
||
102 | 30 | public function __invoke(Request $request, Response $response, $next) |
|
172 | |||
173 | |||
174 | |||
175 | } |
||
176 |
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.