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 |
||
| 23 | abstract class TranslatableResource extends Resource |
||
| 24 | { |
||
| 25 | use TabsOnEdit; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * {@inheritdoc} |
||
| 29 | */ |
||
| 30 | public function availablePanelsForDetail($request) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | public function availablePanelsForCreate($request) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public function availablePanelsForUpdate($request) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Return configured translations panel. |
||
| 65 | * |
||
| 66 | * @param string $component |
||
| 67 | * @return \Laravel\Nova\Panel |
||
| 68 | */ |
||
| 69 | protected function translationsPanel(string $component = 'detail-tabs') |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Translations panel name. |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | protected function translationsPanelName() |
||
| 91 | |||
| 92 | // -------------------------------------------------------------------------------- |
||
| 93 | // -------------------------------------------------------------------------------- |
||
| 94 | // -------------------------------------------------------------------------------- |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function detailFields(NovaRequest $request) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | View Code Duplication | public function creationFields(NovaRequest $request) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | View Code Duplication | public function updateFields(NovaRequest $request) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | protected function removeNonUpdateFields(NovaRequest $request, FieldCollection $fields) |
||
| 154 | |||
| 155 | // -------------------------------------------------------------------------------- |
||
| 156 | // -------------------------------------------------------------------------------- |
||
| 157 | // -------------------------------------------------------------------------------- |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Transform fields to handle translation system. |
||
| 161 | * |
||
| 162 | * @param \Laravel\Nova\Fields\FieldCollection $fields |
||
| 163 | * @param bool $forceReadonlyIds |
||
| 164 | * @return \Laravel\Nova\Fields\FieldCollection |
||
| 165 | * @throws \Exception |
||
| 166 | */ |
||
| 167 | protected function translationsFields(FieldCollection $fields, $forceReadonlyIds = false) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Override field to handle translation system. |
||
| 197 | * |
||
| 198 | * @param \Laravel\Nova\Fields\Field $field |
||
| 199 | * @param \BBSLab\NovaTranslation\Models\Locale $locale |
||
| 200 | * @param \Illuminate\Database\Eloquent\Model $localeResource |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | protected function translationField(Field $field, Locale $locale, Model $localeResource) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Return hidden translation ID field. |
||
| 225 | * |
||
| 226 | * @param \BBSLab\NovaTranslation\Models\Locale $locale |
||
| 227 | * @param \Illuminate\Database\Eloquent\Model $localeResource |
||
| 228 | * @return \Laravel\Nova\Fields\Text |
||
| 229 | */ |
||
| 230 | protected function translationIdField(Locale $locale, Model $localeResource) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Return resource translations. |
||
| 250 | * |
||
| 251 | * @param \Illuminate\Database\Eloquent\Collection $locales |
||
| 252 | * @return \Illuminate\Database\Eloquent\Collection |
||
| 253 | */ |
||
| 254 | protected function getResourceTranslations(Collection $locales) |
||
| 278 | |||
| 279 | // -------------------------------------------------------------------------------- |
||
| 280 | // -------------------------------------------------------------------------------- |
||
| 281 | // -------------------------------------------------------------------------------- |
||
| 282 | |||
| 283 | /** |
||
| 284 | * {@inheritdoc} |
||
| 285 | */ |
||
| 286 | public static function indexQuery(NovaRequest $request, $query) |
||
| 290 | |||
| 291 | /** |
||
| 292 | * {@inheritdoc} |
||
| 293 | */ |
||
| 294 | public static function scoutQuery(NovaRequest $request, $query) |
||
| 298 | |||
| 299 | /** |
||
| 300 | * {@inheritdoc} |
||
| 301 | */ |
||
| 302 | public static function detailQuery(NovaRequest $request, $query) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * {@inheritdoc} |
||
| 309 | */ |
||
| 310 | public static function relatableQuery(NovaRequest $request, $query) |
||
| 314 | } |
||
| 315 |
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: