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 |
||
19 | class StaticWebServer |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * @var Server |
||
24 | */ |
||
25 | private $httpServer; |
||
26 | |||
27 | /** |
||
28 | * @var string the absolute path to the directory where files are served from |
||
29 | */ |
||
30 | private $webroot; |
||
31 | |||
32 | /** |
||
33 | * @var HandlerInterface |
||
34 | */ |
||
35 | private $authenticationHandler; |
||
36 | |||
37 | /** |
||
38 | * @var LoggerInterface |
||
39 | */ |
||
40 | private $logger; |
||
41 | |||
42 | /** |
||
43 | * @var ContentTypeParser |
||
44 | */ |
||
45 | private $contentTypeParser; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | private $indexFiles = [ |
||
51 | 'index.htm', |
||
52 | 'index.html', |
||
53 | ]; |
||
54 | |||
55 | |||
56 | /** |
||
57 | * StaticWebServer constructor. |
||
58 | * |
||
59 | * @param Server $httpServer |
||
60 | * @param string $webroot |
||
61 | * @param LoggerInterface|null $logger |
||
62 | */ |
||
63 | public function __construct(Server $httpServer, $webroot, LoggerInterface $logger = null) |
||
79 | |||
80 | |||
81 | /** |
||
82 | * @return string |
||
83 | */ |
||
84 | public function getWebroot() |
||
88 | |||
89 | |||
90 | /** |
||
91 | * @param string $webroot |
||
92 | * |
||
93 | * @return StaticWebServer |
||
94 | */ |
||
95 | public function setWebroot($webroot) |
||
101 | |||
102 | |||
103 | /** |
||
104 | * @return LoggerInterface |
||
105 | */ |
||
106 | public function getLogger() |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @param LoggerInterface $logger |
||
114 | * |
||
115 | * @return StaticWebServer |
||
116 | */ |
||
117 | public function setLogger($logger) |
||
123 | |||
124 | |||
125 | /** |
||
126 | * @return array |
||
127 | */ |
||
128 | public function getIndexFiles() |
||
132 | |||
133 | |||
134 | /** |
||
135 | * @param array $indexFiles |
||
136 | * |
||
137 | * @return StaticWebServer |
||
138 | */ |
||
139 | public function setIndexFiles($indexFiles) |
||
145 | |||
146 | |||
147 | /** |
||
148 | * @param HandlerInterface|null $authenticationHandler |
||
149 | * |
||
150 | * @return StaticWebServer |
||
151 | */ |
||
152 | public function setAuthenticationHandler($authenticationHandler) |
||
158 | |||
159 | |||
160 | /** |
||
161 | * @param Request $request |
||
162 | * @param Response $response |
||
163 | */ |
||
164 | public function handleRequest(Request $request, Response $response) |
||
216 | |||
217 | |||
218 | /** |
||
219 | * @param string $requestPath |
||
220 | * |
||
221 | * @return bool|string |
||
222 | */ |
||
223 | public function resolvePath($requestPath) |
||
241 | |||
242 | |||
243 | /** |
||
244 | * @param string $filePath |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | public function getContentType($filePath) |
||
267 | |||
268 | } |
||
269 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.