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 |
||
24 | class Tokenizer extends Component implements SingletonInterface, TokenizerInterface, InjectorInterface |
||
25 | { |
||
26 | /** |
||
27 | * Required traits. |
||
28 | */ |
||
29 | use LoggerTrait, BenchmarkTrait; |
||
30 | |||
31 | /** |
||
32 | * Declares to IoC that component instance should be treated as singleton. |
||
33 | */ |
||
34 | const SINGLETON = self::class; |
||
35 | |||
36 | /** |
||
37 | * Memory section. |
||
38 | */ |
||
39 | const MEMORY = 'tokenizer'; |
||
40 | |||
41 | /** |
||
42 | * Cache of already processed file reflections, used to speed up lookup. |
||
43 | * |
||
44 | * @invisible |
||
45 | * @var array |
||
46 | */ |
||
47 | private $cache = []; |
||
48 | |||
49 | /** |
||
50 | * @var TokenizerConfig |
||
51 | */ |
||
52 | protected $config = null; |
||
53 | |||
54 | /** |
||
55 | * @invisible |
||
56 | * @var FilesInterface |
||
57 | */ |
||
58 | protected $files = null; |
||
59 | |||
60 | /** |
||
61 | * @invisible |
||
62 | * @var HippocampusInterface |
||
63 | */ |
||
64 | protected $memory = null; |
||
65 | |||
66 | /** |
||
67 | * Tokenizer constructor. |
||
68 | * |
||
69 | * @param FilesInterface $files |
||
70 | * @param TokenizerConfig $config |
||
71 | * @param HippocampusInterface $runtime |
||
72 | */ |
||
73 | public function __construct( |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | public function fetchTokens($filename) |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | public function fileReflection($filename) |
||
128 | |||
129 | /** |
||
130 | * Get pre-configured class locator. |
||
131 | * |
||
132 | * @param array $directories |
||
133 | * @param array $exclude |
||
134 | * @param Finder $finder |
||
135 | * @return ClassLocator |
||
136 | */ |
||
137 | View Code Duplication | public function classLocator( |
|
158 | |||
159 | /** |
||
160 | * Get pre-configured invocation locator. |
||
161 | * |
||
162 | * @param array $directories |
||
163 | * @param array $exclude |
||
164 | * @param Finder $finder |
||
165 | * @return ClassLocator |
||
166 | */ |
||
167 | View Code Duplication | public function invocationLocator( |
|
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | public function createInjection(\ReflectionClass $class, $context = null) |
||
200 | } |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.