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 |
||
8 | class FieldFactory implements FieldFactoryInterface |
||
9 | { |
||
10 | protected $namespaces = ['SimpleCrud\\Fields\\']; |
||
11 | protected $defaultType = 'Field'; |
||
12 | |||
13 | protected $nameMap = [ |
||
14 | 'id' => 'Integer', |
||
15 | 'active' => 'Boolean', |
||
16 | 'pubdate' => 'Datetime', |
||
17 | 'file' => 'File', |
||
18 | ]; |
||
19 | |||
20 | protected $regexMap = [ |
||
21 | //relation fields (post_id) |
||
22 | '/_id$/' => 'Integer', |
||
23 | |||
24 | //flags (isActive, inHome) |
||
|
|||
25 | '/^(is|has)[A-Z]/' => 'Boolean', |
||
26 | |||
27 | //time related (createdAt, publishedAt) |
||
28 | '/[a-z]At$/' => 'Datetime', |
||
29 | |||
30 | //time related (createdAt, publishedAt) |
||
31 | '/[a-z]File$/' => 'File', |
||
32 | ]; |
||
33 | |||
34 | protected $typeMap = [ |
||
35 | 'bigint' => 'Integer', |
||
36 | 'boolean' => 'Boolean', |
||
37 | 'date' => 'Date', |
||
38 | 'datetime' => 'Datetime', |
||
39 | 'float' => 'Decimal', |
||
40 | 'mediumint' => 'Integer', |
||
41 | 'set' => 'Set', |
||
42 | 'smallint' => 'Integer', |
||
43 | 'tinyint' => 'Integer', |
||
44 | 'year' => 'Integer', |
||
45 | ]; |
||
46 | |||
47 | /** |
||
48 | * Set the namespace for the fields classes. |
||
49 | * |
||
50 | * @param string $namespace |
||
51 | * |
||
52 | * @return self |
||
53 | */ |
||
54 | public function addNamespace($namespace) |
||
60 | |||
61 | /** |
||
62 | * Map names with field types. |
||
63 | * |
||
64 | * @param array $map |
||
65 | * |
||
66 | * @return self |
||
67 | */ |
||
68 | public function mapNames(array $map) |
||
74 | |||
75 | /** |
||
76 | * Map names with field types using regexp. |
||
77 | * |
||
78 | * @param array $map |
||
79 | * |
||
80 | * @return self |
||
81 | */ |
||
82 | public function mapRegex(array $map) |
||
88 | |||
89 | /** |
||
90 | * Map db field types with classes. |
||
91 | * |
||
92 | * @param array $map |
||
93 | * |
||
94 | * @return self |
||
95 | */ |
||
96 | public function mapTypes(array $map) |
||
102 | |||
103 | /** |
||
104 | * @see FieldFactoryInterface |
||
105 | * |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | public function get(Table $table, $name) |
||
129 | |||
130 | /** |
||
131 | * Get the field class name. |
||
132 | * |
||
133 | * @param string $name |
||
134 | * @param string $type |
||
135 | * |
||
136 | * @return string|null |
||
137 | */ |
||
138 | protected function getClassName($name, $type) |
||
154 | } |
||
155 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.