Completed
Push — master ( 8f08c7...7bd638 )
by Mohamed
02:25
created

ViewResolverTrait::getViewPathFor()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 9
rs 9.9666
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 = "microboard::{$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 Model|null $model
26
     * @return array
27
     */
28
    protected function getResourceVariables(?Model $model = null): array
29
    {
30
        $routePrefix = 'microboard';
31
        $translationsPrefix = '';
32
33
        if (property_exists($this, 'attributes')) {
34
            if (isset($this->attributes['routes_prefix'])) {
35
                $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...
36
            }
37
            if (isset($this->attributes['translations_prefix'])) {
38
                $translationsPrefix = $this->attributes['translations_prefix'];
39
            }
40
        }
41
42
        return [
43
            '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...
44
            'resourceName' => Str::of($this->baseName)->lower()->plural(),
45
            'resourceVariable' => Str::of($this->baseName)->lower(),
46
            'routePrefix' => $routePrefix ? $routePrefix . '.' : '',
47
            'translationsPrefix' => $translationsPrefix ? $translationsPrefix . '::' : '',
48
            'model' => $model
49
        ];
50
    }
51
}
52