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:
Complex classes like Detector often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Detector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class Detector |
||
11 | { |
||
12 | const STATE_UNCHECKED = 'unckecked'; |
||
13 | const STATE_PASSED = 'checkPassed'; |
||
14 | const STATE_FAILED = 'checkFailed'; |
||
15 | |||
16 | private $state = self::STATE_UNCHECKED; |
||
17 | |||
18 | /** |
||
19 | * |
||
20 | * @var mixed key to identify unique user |
||
21 | */ |
||
22 | private $key; |
||
23 | |||
24 | /** |
||
25 | * |
||
26 | * @var \Sokil\DataType\PriorityList |
||
27 | */ |
||
28 | private $processorDeclarationList; |
||
29 | |||
30 | private $processorList = array(); |
||
31 | |||
32 | private $processorNamespaces = array( |
||
33 | '\Sokil\FraudDetector\Processor', |
||
34 | ); |
||
35 | |||
36 | private $collectorNamespaces = array( |
||
37 | '\Sokil\FraudDetector\Collector', |
||
38 | ); |
||
39 | |||
40 | private $storageNamespaces = array( |
||
41 | '\Sokil\FraudDetector\Storage', |
||
42 | ); |
||
43 | |||
44 | /** |
||
45 | * |
||
46 | * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
||
47 | */ |
||
48 | private $eventDispatcher; |
||
49 | |||
50 | public function __construct() |
||
55 | |||
56 | /** |
||
57 | * Key that uniquely identify user |
||
58 | * @param type $key |
||
59 | * @return \Sokil\FraudDetector\Detector |
||
60 | */ |
||
61 | public function setKey($key) |
||
66 | |||
67 | public function getKey() |
||
75 | |||
76 | /** |
||
77 | * Check if request is not fraud |
||
78 | */ |
||
79 | public function check() |
||
104 | |||
105 | public function registerProcessorNamespace($namespace) |
||
110 | |||
111 | /** |
||
112 | * Add processor identified by its name. |
||
113 | * If processor already added, it will be replaced by new instance. |
||
114 | * |
||
115 | * @param string $name name of processor |
||
116 | * @param callable $callable configurator callable |
||
117 | * @return \Sokil\FraudDetector\Detector |
||
118 | */ |
||
119 | public function declareProcessor($name, $callable = null, $priority = 0) |
||
124 | |||
125 | public function addProcssor($name, ProcessorInterface $processor, $priority = 0) |
||
132 | |||
133 | public function isProcessorDeclared($name) |
||
137 | |||
138 | /** |
||
139 | * Factory method to create new check condition |
||
140 | * |
||
141 | * @param string $name name of check condition |
||
142 | * @return \Sokil\FraudDetector\ProcessorInterface |
||
143 | * @throws \Exception |
||
144 | */ |
||
145 | private function getProcessorClassName($name) |
||
158 | |||
159 | public function getProcessor($processorName) |
||
183 | |||
184 | public function registerCollectorNamespace($namespace) |
||
189 | |||
190 | View Code Duplication | private function getCollectorClassName($type) |
|
209 | |||
210 | /* |
||
211 | * @param int $requestNumber maximum number of allowed requests |
||
212 | * @param int $timeInterval time interval in seconds |
||
213 | */ |
||
214 | public function createCollector( |
||
241 | |||
242 | View Code Duplication | private function getStorageClassName($type) |
|
261 | |||
262 | public function createStorage($type, $configuratorCallable = null) |
||
276 | |||
277 | public function registerStorageNamespace($namespace) |
||
282 | |||
283 | private function on($stateName, $callable) |
||
293 | |||
294 | public function onCheckPassed($callable) |
||
300 | |||
301 | public function onCheckFailed($callable) |
||
307 | |||
308 | public function isUnchecked() |
||
312 | |||
313 | public function isPassed() |
||
317 | |||
318 | public function isFailed() |
||
322 | |||
323 | private function hasState($state) |
||
327 | |||
328 | public function subscribe($eventName, $callable, $priority = 0) |
||
333 | |||
334 | /** |
||
335 | * |
||
336 | * @param string $eventName |
||
337 | * @param mixed $target |
||
338 | * @return \Sokil\FraudDetector\Event |
||
339 | */ |
||
340 | public function trigger($eventName, $target = null) |
||
350 | } |
||
351 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: