Path::setContent()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
namespace JumpGate\ViewResolution\Resolvers;
4
5
use Illuminate\Routing\Router;
6
use Illuminate\View\Factory;
7
use Illuminate\View\View;
8
use JumpGate\ViewResolution\Models\View as ViewModel;
9
10
class Path
11
{
12
    /**
13
     * @var \JumpGate\ViewResolution\Models\View
14
     */
15
    public $viewModel;
16
17
    /**
18
     * @var \JumpGate\ViewResolution\Resolvers\Path
19
     */
20
    public $path;
21
22
    /**
23
     * @var \JumpGate\ViewResolution\Resolvers\Layout|\Illuminate\View\View
24
     */
25
    public $layout;
26
27
    /**
28
     * @var \Illuminate\Routing\Router
29
     */
30
    protected $route;
31
32
    /**
33
     * @var \Illuminate\View\Factory
34
     */
35
    protected $view;
36
37
    /**
38
     * @param \Illuminate\Routing\Router $route
39
     * @param \Illuminate\View\Factory   $view
40
     */
41
    public function __construct(Router $route, Factory $view)
42
    {
43
        $this->route = $route;
44
        $this->view  = $view;
45
    }
46
47
    /**
48
     * Set up the needed details for the view.
49
     *
50
     * @param \Illuminate\View\View $layout
51
     * @param null|string           $view
52
     *
53
     * @return \Illuminate\View\View
54
     */
55
    public function setUp(View $layout, $view = null)
56
    {
57
        $this->layout = $layout;
58
        $this->setPath($view);
59
        $this->setContent();
60
61
        return $this->layout;
62
    }
63
64
    /**
65
     * Get a valid view path save it.
66
     *
67
     * @param $view
68
     */
69
    protected function setPath($view)
70
    {
71
        $this->viewModel = new ViewModel([], $this->layout->getName());
0 ignored issues
show
Bug introduced by
The method getName does only exist in Illuminate\View\View, but not in JumpGate\ViewResolution\Resolvers\Layout.

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...
72
73
        if ($view == null) {
74
            $view = $this->findView();
75
        }
76
77
        $this->viewModel->view = $view;
78
79
        viewResolver()->collectDetails($this->viewModel);
80
81
        $this->path = $view;
82
    }
83
84
    /**
85
     * Put the found view inside the layout.
86
     */
87
    protected function setContent()
88
    {
89
        if (stripos($this->path, 'missingmethod') === false && $this->view->exists($this->path)) {
0 ignored issues
show
Documentation introduced by
$this->path is of type object<JumpGate\ViewResolution\Resolvers\Path>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
90
            try {
91
                $this->layout->content = $this->view->make($this->path);
0 ignored issues
show
Documentation introduced by
$this->path is of type object<JumpGate\ViewResolution\Resolvers\Path>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
            } catch (\Exception $e) {
93
                $this->layout->content = null;
94
            }
95
        }
96
    }
97
98
    /**
99
     * Try to figure out a view based on the called action.
100
     *
101
     * @param $layout
102
     * @param $parameters
103
     *
104
     * @return \Illuminate\View\View
105
     */
106
    public function missingMethod($layout, $parameters)
107
    {
108
        $view = $this->findView();
109
110
        if (count($parameters) == 1) {
111
            $view = str_ireplace('missingMethod', $parameters[0], $view);
112
        } elseif ($parameters[0] == null && $parameters[1] == null) {
113
            $view = str_ireplace('missingMethod', 'index', $view);
114
        } else {
115
            $view = implode('.', $parameters);
116
        }
117
118
        return $this->setUp($layout, $view);
119
    }
120
121
    /**
122
     * Find a view based on the available details.
123
     *
124
     * This will look in the config and the route details to
125
     * find a sensible place a view may be.
126
     *
127
     * @return string
128
     */
129
    protected function findView()
130
    {
131
        // Get the overall route name (SomeController@someMethod)
132
        // Break it up into it's component parts
133
        $route      = $this->route->currentRouteAction();
134
        $routeParts = explode('@', $route);
135
136
        if (count(array_filter($routeParts)) > 0) {
137
            $this->viewModel = new ViewModel($routeParts, $this->layout->getName());
0 ignored issues
show
Bug introduced by
The method getName does only exist in Illuminate\View\View, but not in JumpGate\ViewResolution\Resolvers\Layout.

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...
138
139
            // Check for a configured view route.
140
            if (! is_null($configView = $this->viewModel->checkConfig())) {
141
                $this->viewModel->type = 'config';
142
143
                return $configView;
144
            }
145
146
            $this->viewModel->type = 'auto';
147
148
            return $this->viewModel->getView();
149
        }
150
151
        return null;
152
    }
153
}
154