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 AppConfig { |
||
18 | |||
19 | private $defaults = [ |
||
20 | 'wopi_url' => '', |
||
21 | 'watermark_text' => '{userId}', |
||
22 | 'watermark_allGroupsList' => [], |
||
23 | 'watermark_allTagsList' => [], |
||
24 | 'watermark_linkTagsList' => [], |
||
25 | |||
26 | ]; |
||
27 | |||
28 | const WATERMARK_APP_NAMESPACE = 'files'; |
||
29 | |||
30 | const APP_SETTING_TYPES = [ |
||
31 | 'watermark_allGroupsList' => 'array', |
||
32 | 'watermark_allTagsList' => 'array', |
||
33 | 'watermark_linkTagsList' => 'array' |
||
34 | ]; |
||
35 | |||
36 | /** @var IConfig */ |
||
37 | private $config; |
||
38 | |||
39 | public function __construct(IConfig $config) { |
||
42 | |||
43 | public function getAppNamespace($key) { |
||
49 | |||
50 | /** |
||
51 | * Get a value by key |
||
52 | * @param string $key |
||
53 | * @return string |
||
54 | */ |
||
55 | public function getAppValue($key) { |
||
62 | |||
63 | /** |
||
64 | * @param $key |
||
65 | * @return array |
||
66 | */ |
||
67 | public function getAppValueArray($key) { |
||
68 | $value = $this->config->getAppValue($this->getAppNamespace($key), $key, []); |
||
|
|||
69 | if (array_key_exists($key, self::APP_SETTING_TYPES) && self::APP_SETTING_TYPES[$key] === 'array') { |
||
70 | $value = $value !== '' ? explode(',', $value) : []; |
||
71 | } |
||
72 | return $value; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Set a value by key |
||
77 | * @param string $key |
||
78 | * @param string $value |
||
79 | * @return void |
||
80 | */ |
||
81 | public function setAppValue($key, $value) { |
||
84 | |||
85 | /** |
||
86 | * Get all app settings |
||
87 | * @return array |
||
88 | */ |
||
89 | public function getAppSettings() { |
||
108 | |||
109 | } |
||
110 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: