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 |
||
| 25 | class Tokenizer extends Component implements SingletonInterface, TokenizerInterface, InjectorInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Required traits. |
||
| 29 | */ |
||
| 30 | use LoggerTrait, BenchmarkTrait, TokensTrait; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Declares to IoC that component instance should be treated as singleton. |
||
| 34 | */ |
||
| 35 | const SINGLETON = self::class; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Memory section. |
||
| 39 | */ |
||
| 40 | const MEMORY = 'tokenizer'; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Cache of already processed file reflections, used to speed up lookup. |
||
| 44 | * |
||
| 45 | * @invisible |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | private $cache = []; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var TokenizerConfig |
||
| 52 | */ |
||
| 53 | protected $config = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @invisible |
||
| 57 | * @var FilesInterface |
||
| 58 | */ |
||
| 59 | protected $files = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @invisible |
||
| 63 | * @var HippocampusInterface |
||
| 64 | */ |
||
| 65 | protected $memory = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Tokenizer constructor. |
||
| 69 | * |
||
| 70 | * @param FilesInterface $files |
||
| 71 | * @param TokenizerConfig $config |
||
| 72 | * @param HippocampusInterface $runtime |
||
| 73 | */ |
||
| 74 | public function __construct( |
||
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | * |
||
| 89 | * @deprecated this method creates looped dependencies, drop it |
||
| 90 | */ |
||
| 91 | public function fetchTokens($filename) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function fileReflection($filename) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get pre-configured class locator. |
||
| 119 | * |
||
| 120 | * @param array $directories |
||
| 121 | * @param array $exclude |
||
| 122 | * @param Finder $finder |
||
| 123 | * @return ClassLocator |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function classLocator( |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Get pre-configured invocation locator. |
||
| 149 | * |
||
| 150 | * @param array $directories |
||
| 151 | * @param array $exclude |
||
| 152 | * @param Finder $finder |
||
| 153 | * @return ClassLocator |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function invocationLocator( |
|
| 176 | |||
| 177 | /** |
||
| 178 | * {@inheritdoc} |
||
| 179 | */ |
||
| 180 | public function createInjection(\ReflectionClass $class, $context = null) |
||
| 188 | } |
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
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. 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.