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 |
||
17 | class GenericWriter |
||
18 | { |
||
19 | /** @var \samsoncms\api\generator\analyzer\GenericAnalyzer[string] Collection of entity analyzers */ |
||
20 | protected $analyzers = []; |
||
21 | |||
22 | /** @var \samsoncms\api\generator\Generic[string] Collection of entity generators */ |
||
23 | protected $generators = []; |
||
24 | |||
25 | /** @var array Analyzers metadata */ |
||
26 | protected $metadata = []; |
||
27 | |||
28 | /** @var Generator Code generator */ |
||
29 | protected $codeGenerator; |
||
30 | |||
31 | /** @var string Path to generated entities */ |
||
32 | protected $path; |
||
33 | |||
34 | /** |
||
35 | * Writer constructor. |
||
36 | * |
||
37 | * @param DatabaseInterface $db |
||
38 | * @param Generator $codeGenerator |
||
39 | * @param string $namespace |
||
40 | * @param array $analyzers Collection of analyzer class names |
||
41 | * @param string $path Path to generated entities |
||
42 | * |
||
43 | * @throws \Exception |
||
44 | */ |
||
45 | public function __construct(DatabaseInterface $db, Generator $codeGenerator, $namespace, array $analyzers, $path) |
||
72 | |||
73 | /** |
||
74 | * Analyze, generate and write class files. |
||
75 | */ |
||
76 | public function write() |
||
107 | } |
||
108 | //[PHPCOMPRESSOR(remove,end)] |
||
109 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: