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 | abstract class TranslatableResource extends Resource |
||
| 23 | { |
||
| 24 | use TabsOnEdit; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | public function availablePanelsForDetail($request) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | */ |
||
| 41 | public function availablePanelsForCreate($request) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * {@inheritdoc} |
||
| 52 | */ |
||
| 53 | public function availablePanelsForUpdate($request) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Return configured translations panel. |
||
| 64 | * |
||
| 65 | * @param string $component |
||
| 66 | * @return \Laravel\Nova\Panel |
||
| 67 | */ |
||
| 68 | protected function translationsPanel(string $component = 'detail-tabs') |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Translations panel name. |
||
| 83 | * |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | protected function translationsPanelName() |
||
| 90 | |||
| 91 | // -------------------------------------------------------------------------------- |
||
| 92 | // -------------------------------------------------------------------------------- |
||
| 93 | // -------------------------------------------------------------------------------- |
||
| 94 | |||
| 95 | /** |
||
| 96 | * {@inheritdoc} |
||
| 97 | */ |
||
| 98 | public function detailFields(NovaRequest $request) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | View Code Duplication | public function creationFields(NovaRequest $request) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | View Code Duplication | public function updateFields(NovaRequest $request) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | protected function removeNonUpdateFields(NovaRequest $request, FieldCollection $fields) |
||
| 153 | |||
| 154 | // -------------------------------------------------------------------------------- |
||
| 155 | // -------------------------------------------------------------------------------- |
||
| 156 | // -------------------------------------------------------------------------------- |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Transform fields to handle translation system. |
||
| 160 | * |
||
| 161 | * @param \Laravel\Nova\Fields\FieldCollection $fields |
||
| 162 | * @param bool $forceReadonlyIds |
||
| 163 | * @return \Laravel\Nova\Fields\FieldCollection |
||
| 164 | * @throws \Exception |
||
| 165 | */ |
||
| 166 | protected function translationsFields(FieldCollection $fields, $forceReadonlyIds = false) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Override field to handle translation system. |
||
| 194 | * |
||
| 195 | * @param \Laravel\Nova\Fields\Field $field |
||
| 196 | * @param \BBSLab\NovaTranslation\Models\Locale $locale |
||
| 197 | * @param \Illuminate\Database\Eloquent\Model $localeResource |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | protected function translationField(Field $field, Locale $locale, Model $localeResource) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Return resource translations. |
||
| 222 | * |
||
| 223 | * @param \Illuminate\Database\Eloquent\Collection $locales |
||
| 224 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 225 | */ |
||
| 226 | protected function getResourceTranslations(Collection $locales) |
||
| 250 | |||
| 251 | // -------------------------------------------------------------------------------- |
||
| 252 | // -------------------------------------------------------------------------------- |
||
| 253 | // -------------------------------------------------------------------------------- |
||
| 254 | |||
| 255 | /** |
||
| 256 | * {@inheritdoc} |
||
| 257 | */ |
||
| 258 | public static function indexQuery(NovaRequest $request, $query) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * {@inheritdoc} |
||
| 265 | */ |
||
| 266 | public static function scoutQuery(NovaRequest $request, $query) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * {@inheritdoc} |
||
| 273 | */ |
||
| 274 | public static function detailQuery(NovaRequest $request, $query) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * {@inheritdoc} |
||
| 281 | */ |
||
| 282 | public static function relatableQuery(NovaRequest $request, $query) |
||
| 286 | } |
||
| 287 |
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: