Completed
Push — master ( bf12d7...5507e8 )
by
unknown
08:47
created

TranslatableResource::relatableQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace BBSLab\NovaTranslation\Resources;
4
5
use BBSLab\NovaTranslation\Models\Locale;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, BBSLab\NovaTranslation\Resources\Locale.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use BBSLab\NovaTranslation\Models\Translation;
7
use Eminiarts\Tabs\Tabs;
8
use Illuminate\Http\Request;
9
use Laravel\Nova\Fields\Text;
10
use Laravel\Nova\Http\Requests\NovaRequest;
11
use Laravel\Nova\Resource;
12
13
abstract class TranslatableResource extends Resource
14
{
15
    /**
16
     * Return tabs of translations for current resource.
17
     *
18
     * @param  \Illuminate\Http\Request  $request
19
     * @param  array  $fields
20
     * @return array
21
     */
22
    protected function translations(Request $request, array $fields)
23
    {
24
        $locales = Locale::query()->select('id', 'iso', 'label')->get();
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
25
26
        $baseTranslation = Translation::query()
0 ignored issues
show
Bug introduced by
The method select() does not exist on Illuminate\Database\Eloquent\Builder. Did you maybe mean createSelectWithConstraint()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
27
            ->select('translation_id')
28
            ->where('translatable_id', '=', $this->resource->getKey())
29
            ->where('translatable_type', '=', get_class($this->resource))
30
            ->first();
31
        $translationId = ! empty($baseTranslation) ? $baseTranslation->translation_id : $this->resource->freshTranslationId();
0 ignored issues
show
Unused Code introduced by
$translationId is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
32
33
        $tabs = [];
34
        $test = [Text::make('key')];
0 ignored issues
show
Unused Code introduced by
$test is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
        foreach ($locales as $locale) {
36
            $tabs[$locale->label] = [Text::make('key_' . $locale->iso)];
37
            /*
38
            $tabs[] = new Tabs($locale->label, array_merge([
39
                // Text::make('Translation ID', 'translation_id', $translationId)->withMeta(['type' => 'hidden']),
40
            ], $fields));
41
            */
42
        }
43
44
        return [
45
            new Tabs('Translations', $tabs),
46
        ];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public static function indexQuery(NovaRequest $request, $query)
53
    {
54
        return $query->inLocale();
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public static function scoutQuery(NovaRequest $request, $query)
61
    {
62
        return $query;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public static function detailQuery(NovaRequest $request, $query)
69
    {
70
        return parent::detailQuery($request, $query);
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public static function relatableQuery(NovaRequest $request, $query)
77
    {
78
        return parent::relatableQuery($request, $query);
79
    }
80
}
81