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 |
||
20 | class Repository |
||
21 | { |
||
22 | /** |
||
23 | * Language/Locale. |
||
24 | * |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $lang; |
||
28 | |||
29 | /** |
||
30 | * The instance of the cache. |
||
31 | * |
||
32 | * @var \Illuminate\Cache\CacheManager |
||
33 | */ |
||
34 | protected $cache; |
||
35 | |||
36 | /** |
||
37 | * Config. |
||
38 | * |
||
39 | * @var array |
||
40 | */ |
||
41 | protected $config; |
||
42 | |||
43 | /** |
||
44 | * The instance of the database. |
||
45 | * |
||
46 | * @var \Illuminate\Database\DatabaseManager |
||
47 | */ |
||
48 | protected $db; |
||
49 | |||
50 | /** |
||
51 | * Name of the cache. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $cache_name; |
||
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * Create a new MultiLang instance. |
||
61 | * |
||
62 | * @param string $environment |
||
|
|||
63 | * @param array $config |
||
64 | * @param \Illuminate\Cache\CacheManager $cache |
||
65 | * @param \Illuminate\Database\DatabaseManager $db |
||
66 | */ |
||
67 | public function __construct(Config $config, Cache $cache, Database $db) |
||
74 | |||
75 | |||
76 | View Code Duplication | public function loadFromDatabase($lang) |
|
88 | |||
89 | /** |
||
90 | * Get a database connection instance. |
||
91 | * |
||
92 | * @return \Illuminate\Database\Connection |
||
93 | */ |
||
94 | View Code Duplication | public function getDb() |
|
102 | |||
103 | /** |
||
104 | * Get a cache driver instance. |
||
105 | * |
||
106 | * @return \Illuminate\Contracts\Cache\Repository |
||
107 | */ |
||
108 | View Code Duplication | public function getCache() |
|
119 | |||
120 | |||
121 | public function save($texts) |
||
150 | |||
151 | |||
152 | protected function getTableName() |
||
157 | |||
158 | |||
159 | } |
||
160 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.