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 |
||
45 | class ThreadWorker extends \Thread implements WorkerInterface |
||
46 | { |
||
47 | /** |
||
48 | * Define's the default value for accept min count |
||
49 | * |
||
50 | * @var int |
||
51 | */ |
||
52 | const DEFAULT_ACCEPT_MIN = 8; |
||
53 | |||
54 | /** |
||
55 | * Define's the default value for accept max count |
||
56 | * |
||
57 | * @var int |
||
58 | */ |
||
59 | const DEFAULT_ACCEPT_MAX = 32; |
||
60 | |||
61 | /** |
||
62 | * Hold's the serer connection resource |
||
63 | * |
||
64 | * @var resource |
||
65 | */ |
||
66 | protected $serverConnectionResource; |
||
67 | |||
68 | /** |
||
69 | * Holds the server context object |
||
70 | * |
||
71 | * @var \AppserverIo\Server\Interfaces\ServerContextInterface |
||
72 | */ |
||
73 | protected $serverContext; |
||
74 | |||
75 | /** |
||
76 | * Hold's an array of connection handlers to use |
||
77 | * |
||
78 | * @var array |
||
79 | */ |
||
80 | protected $connectionHandlers; |
||
81 | |||
82 | /** |
||
83 | * Defines the minimum count of connections for the worker to accept |
||
84 | * |
||
85 | * @var int |
||
86 | */ |
||
87 | protected $acceptMin; |
||
88 | |||
89 | /** |
||
90 | * Defines the maximum count of connections for the worker to accept |
||
91 | * |
||
92 | * @var int |
||
93 | */ |
||
94 | protected $acceptMax; |
||
95 | |||
96 | /** |
||
97 | * Flag if worker should be restarted by server |
||
98 | * |
||
99 | * @var bool |
||
100 | */ |
||
101 | public $shouldRestart; |
||
102 | |||
103 | /** |
||
104 | * Constructs the worker by setting the server context |
||
105 | * |
||
106 | * @param resource $serverConnectionResource The server's file descriptor resource |
||
107 | * @param \AppserverIo\Server\Interfaces\ServerContextInterface $serverContext The server's context |
||
108 | * @param array $connectionHandlers An array of connection handlers to use |
||
109 | */ |
||
110 | public function __construct($serverConnectionResource, ServerContextInterface $serverContext, array $connectionHandlers) |
||
122 | |||
123 | /** |
||
124 | * Init's the worker before it runs |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | public function init() |
||
136 | |||
137 | /** |
||
138 | * Return's an array of connection handlers to use |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | public function getConnectionHandlers() |
||
146 | |||
147 | /** |
||
148 | * Return's the server context instance |
||
149 | * |
||
150 | * @return \AppserverIo\Server\Interfaces\ServerContextInterface The server's context |
||
151 | */ |
||
152 | public function getServerContext() |
||
156 | |||
157 | /** |
||
158 | * Return's the server's connection resource ref |
||
159 | * |
||
160 | * @return resource |
||
161 | */ |
||
162 | protected function getServerConnectionResource() |
||
166 | |||
167 | /** |
||
168 | * Starts the worker doing logic. |
||
169 | * |
||
170 | * @return void |
||
171 | */ |
||
172 | public function run() |
||
185 | |||
186 | /** |
||
187 | * Prepares the worker's in it's own context for upcoming work to do on things |
||
188 | * that can not be shared by using the init method in the parent's context. |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | public function prepare() |
||
205 | |||
206 | /** |
||
207 | * Implements the workers actual logic |
||
208 | * |
||
209 | * @return void |
||
210 | * |
||
211 | * @throws \AppserverIo\Server\Exceptions\ModuleNotFoundException |
||
212 | * @throws \AppserverIo\Server\Exceptions\ConnectionHandlerNotFoundException |
||
213 | */ |
||
214 | public function work() |
||
270 | |||
271 | /** |
||
272 | * Does shutdown logic for worker if something breaks in process. |
||
273 | * |
||
274 | * This shutdown function will be called from specific connection handler if an error occurs, so the connection |
||
275 | * handler can send an response in the correct protocol specifications and a new worker can be started |
||
276 | * |
||
277 | * @return void |
||
278 | */ |
||
279 | View Code Duplication | public function shutdown() |
|
289 | |||
290 | /** |
||
291 | * Return's if worker should be restarted by server |
||
292 | * |
||
293 | * @return bool |
||
294 | */ |
||
295 | public function shouldRestart() |
||
299 | |||
300 | /** |
||
301 | * Return's the max count for the worker to accept |
||
302 | * |
||
303 | * @return int |
||
304 | */ |
||
305 | public function getAcceptMax() |
||
312 | |||
313 | /** |
||
314 | * Return's the min count for the worker to accept |
||
315 | * |
||
316 | * @return int |
||
317 | */ |
||
318 | public function getAcceptMin() |
||
325 | } |
||
326 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.