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() |
||
100 | |||
101 | public function registerProcessorNamespace($namespace) |
||
106 | |||
107 | /** |
||
108 | * Add processor identified by its name. |
||
109 | * If processor already added, it will be replaced by new instance. |
||
110 | * |
||
111 | * @param string $name name of processor |
||
112 | * @param callable $callable configurator callable |
||
113 | * @return \Sokil\FraudDetector\Detector |
||
114 | */ |
||
115 | public function declareProcessor($name, $callable = null, $priority = 0) |
||
120 | |||
121 | public function addProcssor($name, ProcessorInterface $processor, $priority = 0) |
||
128 | |||
129 | public function isProcessorDeclared($name) |
||
133 | |||
134 | /** |
||
135 | * Factory method to create new check condition |
||
136 | * |
||
137 | * @param string $name name of check condition |
||
138 | * @return \Sokil\FraudDetector\ProcessorInterface |
||
139 | * @throws \Exception |
||
140 | */ |
||
141 | private function getProcessorClassName($name) |
||
154 | |||
155 | public function getProcessor($processorName) |
||
179 | |||
180 | public function registerCollectorNamespace($namespace) |
||
185 | |||
186 | View Code Duplication | private function getCollectorClassName($type) |
|
205 | |||
206 | /* |
||
207 | * @param int $requestNumber maximum number of allowed requests |
||
208 | * @param int $timeInterval time interval in seconds |
||
209 | */ |
||
210 | public function createCollector( |
||
237 | |||
238 | View Code Duplication | private function getStorageClassName($type) |
|
257 | |||
258 | public function createStorage($type, $configuratorCallable = null) |
||
272 | |||
273 | private function on($stateName, $callable) |
||
283 | |||
284 | public function onCheckPassed($callable) |
||
290 | |||
291 | public function onCheckFailed($callable) |
||
297 | |||
298 | public function isUnchecked() |
||
302 | |||
303 | public function isPassed() |
||
307 | |||
308 | public function isFailed() |
||
312 | |||
313 | private function hasState($state) |
||
317 | |||
318 | public function subscribe($eventName, $callable, $priority = 0) |
||
323 | |||
324 | /** |
||
325 | * |
||
326 | * @param string $eventName |
||
327 | * @param mixed $target |
||
328 | * @return \Sokil\FraudDetector\Event |
||
329 | */ |
||
330 | public function trigger($eventName, $target = null) |
||
340 | } |
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: