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 |
||
| 14 | class ViewComposerServiceProvider extends ServiceProvider |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * Media public storage. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | public $mediaPath = 'app/public/media'; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Bootstrap the application services. |
||
| 25 | * |
||
| 26 | * @return void |
||
| 27 | */ |
||
| 28 | public function boot() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Register administrator view composers. |
||
| 39 | */ |
||
| 40 | protected function bootAdministratorViewComposer() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Show all directories on media path. |
||
| 74 | * |
||
| 75 | * @return array |
||
| 76 | */ |
||
| 77 | View Code Duplication | protected function getFileDirectories() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Symfony file finder. |
||
| 91 | * Show all files in selected $path directory. |
||
| 92 | * Sort by file type. |
||
| 93 | * |
||
| 94 | * @param string $path |
||
| 95 | * @return \Symfony\Component\Finder\Finder |
||
| 96 | */ |
||
| 97 | protected function getFiles($path) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Register the application services. |
||
| 104 | * |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | public function register() |
||
| 111 | } |
||
| 112 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: