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 |
||
29 | class ConfigManager { |
||
30 | const BUILD_PRIVATE = 1; |
||
31 | const BUILD_PUBLIC = 2; |
||
32 | |||
33 | /** @var \App\Satis\ConfigPersister $configPersister */ |
||
34 | protected $configPersister; |
||
35 | /** @var \App\Satis\ConfigBuilder $configBuilder */ |
||
36 | protected $configBuilder; |
||
37 | /** @var \JMS\Serializer\Serializer $serializer */ |
||
38 | protected $serializer; |
||
39 | /** @var \App\Satis\Model\Config $_satis */ |
||
40 | protected $_satis; |
||
41 | |||
42 | /** @var bool $disableBuild */ |
||
43 | protected $disableBuild = false; |
||
44 | |||
45 | /** @var string */ |
||
46 | protected $hiddenEntityPrefix = '_'; |
||
47 | |||
48 | /** @var array */ |
||
49 | protected $lazyLoadedProperties = [ |
||
50 | '_satis' => 'loadSatisConfig' |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * @return \App\Satis\Model\Config |
||
55 | * @throws \Illuminate\Contracts\Filesystem\FileNotFoundException |
||
56 | */ |
||
57 | protected function loadSatisConfig() { |
||
64 | |||
65 | /** |
||
66 | * @return \Monolog\Logger |
||
67 | */ |
||
68 | protected function getLogger() { |
||
77 | |||
78 | /** |
||
79 | * @param \App\Satis\Collections\RepositoryCollection $repositoryCollection |
||
80 | * @param string $repositoryId |
||
81 | * @param \Illuminate\Support\Collection $input |
||
82 | * @return \App\Satis\Collections\RepositoryCollection |
||
83 | * @throws \App\Satis\Exceptions\RepositoryNotFoundException |
||
84 | */ |
||
85 | protected function _addOrUpdateRepository(RepositoryCollection $repositoryCollection, $repositoryId, |
||
119 | |||
120 | /** |
||
121 | * @param \App\Satis\Collections\PackageCollection $packageCollection |
||
122 | * @param string $packageId |
||
123 | * @param \Illuminate\Support\Collection $input |
||
124 | * @return \App\Satis\Collections\RepositoryCollection |
||
125 | * @throws \App\Satis\Exceptions\PackageNotFoundException |
||
126 | */ |
||
127 | protected function _addOrUpdatePackage(PackageCollection $packageCollection, $packageId, |
||
160 | |||
161 | /** |
||
162 | * @param \App\Satis\Collections\RepositoryCollection $repositoryCollection |
||
163 | * @param string $repositoryId |
||
164 | * @return \App\Satis\Collections\RepositoryCollection |
||
165 | * @throws \App\Satis\Exceptions\RepositoryNotFoundException |
||
166 | */ |
||
167 | protected function _deleteRepository(RepositoryCollection $repositoryCollection, $repositoryId) { |
||
178 | |||
179 | /** |
||
180 | * @param \App\Satis\Collections\PackageCollection $packageCollection |
||
181 | * @param string $packageId |
||
182 | * @return \App\Satis\Collections\RepositoryCollection |
||
183 | * @throws \App\Satis\Exceptions\RepositoryNotFoundException |
||
184 | */ |
||
185 | protected function _deletePackage(PackageCollection $packageCollection, $packageId) { |
||
196 | |||
197 | /** |
||
198 | * @param string $repositoryUrl |
||
199 | * @return string |
||
200 | */ |
||
201 | public static function nameToId($repositoryUrl) { |
||
204 | |||
205 | /** |
||
206 | * @param \App\Satis\ConfigPersister $configPersister |
||
207 | * @param ConfigBuilder $configBuilder |
||
208 | * @param \JMS\Serializer\Serializer $serializer |
||
209 | */ |
||
210 | public function __construct(ConfigPersister $configPersister, ConfigBuilder $configBuilder, |
||
217 | |||
218 | /** |
||
219 | * @param string $method |
||
220 | * @param array $arguments |
||
221 | * |
||
222 | * @return mixed |
||
223 | * |
||
224 | * @throws \Exception |
||
225 | */ |
||
226 | public function __call($method, array $arguments) { |
||
255 | |||
256 | /** |
||
257 | * @param string $property |
||
258 | * @return mixed |
||
259 | */ |
||
260 | public function __get($property) { |
||
271 | |||
272 | /** |
||
273 | * @param boolean $disableBuild |
||
274 | * @return ConfigManager |
||
275 | */ |
||
276 | public function setDisableBuild($disableBuild) { |
||
281 | |||
282 | /** |
||
283 | * @param string|null $repositoryId |
||
284 | * @return string |
||
285 | * @throws \App\Satis\Exceptions\RepositoryNotFoundException |
||
286 | */ |
||
287 | View Code Duplication | public function getRepositories($repositoryId = null) { |
|
302 | |||
303 | /** |
||
304 | * @param string|null $packageId |
||
305 | * @return string |
||
306 | * @throws \App\Satis\Exceptions\PackageNotFoundException |
||
307 | */ |
||
308 | View Code Duplication | public function getPackages($packageId = null) { |
|
323 | |||
324 | /** |
||
325 | * @param \App\Satis\BuildContext $buildContext |
||
326 | */ |
||
327 | public function forceBuild(BuildContext $buildContext) { |
||
336 | |||
337 | /** |
||
338 | * @return bool |
||
339 | */ |
||
340 | public function isBuilding() { |
||
343 | |||
344 | /** |
||
345 | * @return \App\Satis\Model\Config |
||
346 | */ |
||
347 | public function getDefinition() { |
||
350 | |||
351 | /** |
||
352 | * @return string |
||
353 | */ |
||
354 | public function save() { |
||
365 | } |
||
366 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.