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 |
||
22 | class UrlBuilder |
||
23 | { |
||
24 | /** |
||
25 | * @var \League\Flysystem\FilesystemInterface|null |
||
26 | */ |
||
27 | private $fileSystem; |
||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | private $config; |
||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | private $adapterUrlEncoders = []; |
||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $adapterUrlMappings = []; |
||
40 | |||
41 | /** |
||
42 | * @return array |
||
43 | */ |
||
44 | public function getAdapterUrlMappings($adapterType = '') |
||
52 | |||
53 | public function setAdapterUrlMappings($adapterType, $data) |
||
63 | |||
64 | /** |
||
65 | * UrlBuilder constructor. |
||
66 | * |
||
67 | * @param PrimaryFileSystemWrapper|null $PrimaryFileSystemWrapper |
||
68 | * @param array|null $config |
||
69 | */ |
||
70 | public function __construct(PrimaryFileSystemWrapper $PrimaryFileSystemWrapper = null, array $config = null) |
||
77 | |||
78 | /** |
||
79 | * @param $key |
||
80 | * @param UrlEncoderInterface $encoder |
||
81 | */ |
||
82 | public function setAdapterUrlEncoder($key, UrlEncoderInterface $encoder) |
||
86 | |||
87 | /** |
||
88 | * @param $relativeFilePath |
||
89 | * @param string $adapterUrlData |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | public function filePublicUrl($relativeFilePath, array $urlData = []) |
||
127 | |||
128 | private function getAdapterTypeFromFilesystem() |
||
134 | |||
135 | /** |
||
136 | * @param $base |
||
137 | * @param $path |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | protected function formatAsUrl($base, $path) |
||
170 | |||
171 | /** |
||
172 | * @param array $data |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | protected function getUrlFromFileSystem() |
||
189 | |||
190 | /** |
||
191 | * @param AdapterInterface $adapter |
||
192 | * |
||
193 | * @return mixed |
||
194 | */ |
||
195 | View Code Duplication | protected function getAdapterType(AdapterInterface $adapter) |
|
202 | } |
Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.
To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.
The function can be called with either null or an array for the parameter
$needle
but will only accept an array as$haystack
.