Completed
Push — master ( 7d3d6e...e2570f )
by Mohamed
01:26
created

ViewResolverTrait::buildVariables()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 17
nop 1
dl 0
loc 34
rs 8.7537
c 0
b 0
f 0
1
<?php
2
3
namespace Microboard\Foundations\Traits;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Str;
7
8
trait ViewResolverTrait
9
{
10
    /**
11
     * @param $file
12
     * @return string
13
     */
14
    protected function getViewPathFor($file)
15
    {
16
        $name = Str::lower($this->baseName);
0 ignored issues
show
Bug introduced by
The property baseName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
        if (view()->exists($view = "{$name}.{$file}")) {
0 ignored issues
show
Bug introduced by
The method exists does only exist in Illuminate\Contracts\View\Factory, but not in Illuminate\View\View.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
18
            return $view;
19
        }
20
21
        return "microboard::resource.{$file}";
22
    }
23
24
    /**
25
     * @param $view
26
     * @param Model|null $model
27
     * @return array
28
     */
29
    protected function getResourceVariables($view, ?Model $model = null): array
30
    {
31
        return array_merge([
32
            'widgets' => $this->getWidgetsFor($view)
0 ignored issues
show
Bug introduced by
It seems like getWidgetsFor() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
33
        ], $this->buildVariables($model));
34
    }
35
36
    protected function buildVariables(?Model $model = null) {
37
        $routePrefix = 'microboard';
38
        $translationsPrefix = '';
39
        $viewsPrefix = '';
40
        $viewsPath = 'admin';
41
42
        if (property_exists($this, 'attributes')) {
43
            if (isset($this->attributes['routes_prefix'])) {
44
                $routePrefix = $this->attributes['routes_prefix'];
0 ignored issues
show
Bug introduced by
The property attributes does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
            }
46
            if (isset($this->attributes['translations_prefix'])) {
47
                $translationsPrefix = $this->attributes['translations_prefix'];
48
            }
49
            if (isset($this->attributes['views_prefix'])) {
50
                $viewsPrefix = $this->attributes['views_prefix'];
51
            }
52
            if (isset($this->attributes['views_path'])) {
53
                $viewsPath = $this->attributes['views_path'];
54
            }
55
        }
56
57
        return [
58
            'resource' => $this->model,
0 ignored issues
show
Bug introduced by
The property model does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
            'model' => $model,
60
            'resourceName' => $name = Str::of($this->baseName)->lower()->plural(),
61
            'resourceVariable' => Str::of($this->baseName)->lower(),
62
            'routePrefix' => $this->getRightPrefixFor($routePrefix, '.', $name),
63
            'translationsPrefix' => $this->getRightPrefixFor($translationsPrefix, '::', $name),
64
            'viewsPrefix' => $this->getRightPrefixFor(
65
                $viewsPrefix, '::',
66
                $this->getRightPrefixFor($viewsPath, '.', $name)
67
            ),
68
        ];
69
    }
70
71
    /**
72
     * Join given prefix and $resource with the delimiter.
73
     *
74
     * @param string $prefix
75
     * @param string $delimiter
76
     * @param string $resource
77
     * @return string
78
     */
79
    protected function getRightPrefixFor($prefix = '', $delimiter = '.', $resource = '') {
80
        return (
81
            $prefix ? $prefix . $delimiter : ''
82
        ) . $resource;
83
    }
84
}
85