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 |
||
13 | class EmailExceptionMiddleware implements MiddlewareInterface |
||
14 | { |
||
15 | /** |
||
16 | * @var callable |
||
17 | */ |
||
18 | public $mailer_factory; |
||
19 | |||
20 | |||
21 | /** |
||
22 | * @var callable |
||
23 | */ |
||
24 | public $message_factory; |
||
25 | |||
26 | |||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | public $app_name; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | public $include_file; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * @param string $app_name Name of application (used in email subject) |
||
41 | * @param callable $mailer_factory Factory that returns Swift_Mailer instance |
||
42 | * @param callable $message_factory Factory that returns Swift_Message instance |
||
43 | */ |
||
44 | 12 | public function __construct($app_name, callable $mailer_factory, callable $message_factory) |
|
45 | { |
||
46 | 12 | $this->app_name = $app_name; |
|
47 | |||
48 | 12 | $this->mailer_factory = $mailer_factory; |
|
49 | 12 | $this->message_factory = $message_factory; |
|
50 | |||
51 | 12 | $include_path = realpath(__DIR__.'/../includes'); |
|
52 | 12 | $this->include_file = $include_path.'/exception.php'; |
|
53 | 12 | } |
|
54 | |||
55 | |||
56 | |||
57 | |||
58 | /** |
||
59 | * PSR-15 Single Pass |
||
60 | * |
||
61 | * @param ServerRequestInterface $request Server reuest instance |
||
62 | * @param RequestHandlerInterface $handler Request handler |
||
63 | * @return ResponseInterface |
||
64 | */ |
||
65 | 4 | View Code Duplication | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface |
|
|||
66 | { |
||
67 | try { |
||
68 | 4 | $response = $handler->handle($request); |
|
69 | 4 | return $response; |
|
70 | } |
||
71 | // Executed only in PHP 7, will not match in PHP 5.x |
||
72 | catch (\Throwable $e) { |
||
73 | $this->handleThrowable($e); |
||
74 | throw $e; |
||
75 | } |
||
76 | |||
77 | // Executed only in PHP 5.x, will not be reached in PHP 7 |
||
78 | catch (\Exception $e) { |
||
79 | $this->handleThrowable($e); |
||
80 | throw $e; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | |||
85 | |||
86 | /** |
||
87 | * PSR-7 Double Pass |
||
88 | * |
||
89 | * Wrap $next callable in a try-catch block. |
||
90 | * When an exception is caught, an email will be sent, and the execption will be re-thrown. |
||
91 | * |
||
92 | * @param RequestInterface $request |
||
93 | * @param ResponseInterface $response |
||
94 | * @param callable $next |
||
95 | * |
||
96 | * @return ResponseInterface |
||
97 | */ |
||
98 | 4 | View Code Duplication | public function __invoke(RequestInterface $request, ResponseInterface $response, callable $next) |
99 | { |
||
100 | try { |
||
101 | // Try to do business as usual... |
||
102 | 4 | return $next($request, $response); |
|
103 | } |
||
104 | |||
105 | // Executed only in PHP 7, will not match in PHP 5.x |
||
106 | 4 | catch (\Throwable $e) { |
|
107 | 4 | $this->handleThrowable($e); |
|
108 | throw $e; |
||
109 | } |
||
110 | |||
111 | // Executed only in PHP 5.x, will not be reached in PHP 7 |
||
112 | catch (\Exception $e) { |
||
113 | $this->handleThrowable($e); |
||
114 | throw $e; |
||
115 | } |
||
116 | |||
117 | // Anything else NOT caught here will bubble up. |
||
118 | } |
||
119 | |||
120 | |||
121 | /** |
||
122 | * @param \Exception|\Throwable $e |
||
123 | */ |
||
124 | 4 | public function handleThrowable($e) |
|
125 | { |
||
126 | // Render email body, prepare some things |
||
127 | 4 | $text = $this->render($e, [ |
|
128 | 3 | 'package' => array( |
|
129 | 1 | 'title' => 'Germania KG · Middleware', |
|
130 | 'name' => 'germania-kg/middleware', |
||
131 | 'packagist' => 'https://packagist.org/packages/germania-kg/middleware', |
||
132 | 'middleware' => 'EmailExceptionMiddleware' |
||
133 | ) |
||
134 | ]); |
||
135 | 4 | $format = 'text/html'; |
|
136 | 4 | $subject = sprintf("[%s] Exception %s", $this->app_name, get_class($e)); |
|
137 | |||
138 | // Create email message instance |
||
139 | 4 | $message = $this->getMessage(); |
|
140 | $message->setContentType($format) |
||
141 | ->setSubject($subject) |
||
142 | ->setBody($text); |
||
143 | |||
144 | // Create emailer instance + send |
||
145 | $mailer = $this->getMailer(); |
||
146 | $mailer->send($message); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Creates the email body. |
||
151 | * |
||
152 | * In this class, an include file creates a basic information table. |
||
153 | * Override this method to use your own method. |
||
154 | * |
||
155 | * @param Exception $e |
||
156 | * |
||
157 | * @return string Exception explanation |
||
158 | */ |
||
159 | 4 | public function render($e, array $context = array()) |
|
160 | { |
||
161 | 4 | extract($context); |
|
162 | 4 | return require $this->include_file; |
|
163 | } |
||
164 | |||
165 | |||
166 | /** |
||
167 | * @return Swift_Mailer |
||
168 | * |
||
169 | * @throws FactoryException |
||
170 | */ |
||
171 | public function getMailer() |
||
181 | |||
182 | |||
183 | /** |
||
184 | * @return Swift_Message |
||
185 | * |
||
186 | * @throws FactoryException |
||
187 | */ |
||
188 | 4 | public function getMessage() |
|
198 | } |
||
199 |
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.