HasViewResponseTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 7
c 1
b 0
f 0
dl 0
loc 40
rs 10
ccs 0
cts 8
cp 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setView() 0 3 1
A renderView() 0 6 2
A view() 0 3 1
1
<?php
2
3
namespace Nip\Controllers\Response\ResponseFactory;
4
5
use Nip\Http\Response\Response;
6
use Nip\View\View;
7
8
/**
9
 * Trait HasViewResponseTrait
10
 * @package Nip\Controllers\Response\ResponseFactory
11
 */
12
trait HasViewResponseTrait
13
{
14
    /**
15
     * @var null|View
16
     */
17
    protected $view = null;
18
19
    /**
20
     * Create a new response for a given view.
21
     *
22
     * @param string|array $view
23
     * @param array $data
24
     * @param int $status
25
     * @param array $headers
26
     * @return Response
27
     */
28
    public function view($view, $data = [], $status = 200, array $headers = [])
29
    {
30
        return $this->make($this->renderView($view, $data), $status, $headers);
0 ignored issues
show
Bug introduced by
It seems like $view can also be of type array; however, parameter $view of Nip\Controllers\Response...onseTrait::renderView() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

30
        return $this->make($this->renderView(/** @scrutinizer ignore-type */ $view, $data), $status, $headers);
Loading history...
Bug introduced by
It seems like make() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        return $this->/** @scrutinizer ignore-call */ make($this->renderView($view, $data), $status, $headers);
Loading history...
31
    }
32
33
    /**
34
     * @param string $view
35
     * @param array $data
36
     * @return bool|string|null
37
     */
38
    protected function renderView($view, $data = [])
39
    {
40
        foreach ($data as $key => $value) {
41
            $this->view->set($key, $value);
0 ignored issues
show
Bug introduced by
The method set() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            $this->view->/** @scrutinizer ignore-call */ 
42
                         set($key, $value);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
        }
43
        return $this->view->load($view, [], true);
44
    }
45
46
    /**
47
     * @param View|null $view
48
     */
49
    public function setView(?View $view): void
50
    {
51
        $this->view = $view;
52
    }
53
}
54