Completed
Push — master ( c93703...7f6bfb )
by Mikołaj
02:30
created

DebugFriendlyHandler::includeFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Rudolf\Component\ErrorHandler\Handler;
4
5
class DebugFriendlyHandler extends Handler implements IHandler
6
{
7
    /**
8
     * @var string
9
     */
10
    private $pageTitle;
11
12
    /**
13
     * @var string
14
     */
15
    private $pageStyle;
16
17
    /**
18
     * @var string
19
     */
20
    private $pageScript;
21
22
    /**
23
     * @var string
24
     */
25
    private $message;
26
27
    /**
28
     * @var array
29
     */
30
    private $description;
31
32
    /**
33
     * @var array
34
     */
35
    private $trace;
36
37
    public function handle()
38
    {
39
        $this->pageTitle = 'Oh no! Error occurred!';
40
        $this->pageStyle = $this->getResource(['css/reset.css', 'css/style.css']);
41
        $this->pageScript = $this->getResource(['js/checkargs.js']);
42
        $this->message = $this->getException()->getMessage();
43
        $this->description = $this->getDescription();
44
        $this->trace = $this->getTrace();
45
46
        echo $this->includeFile('templates/layout.html.php');
47
    }
48
49
    /**
50
     * Get resource.
51
     *
52
     * @param string|array $name
53
     * @param bool         $include
0 ignored issues
show
Bug introduced by
There is no parameter named $include. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
54
     *
55
     * @return string
56
     */
57
    private function getResource($name)
58
    {
59
        $t = [];
60
61
        foreach ($name as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $name of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
62
            $t[] = file_get_contents(__DIR__.'/../Resources/'.$value);
63
        }
64
65
        return implode('', $t);
66
    }
67
68
    /**
69
     * Include template file.
70
     *
71
     * @param string $file
72
     *
73
     * @return string
74
     */
75
    private function includeFile($file) {
76
        ob_start();
77
78
        include __DIR__.'/../Resources/'.$file;
79
80
        return ob_get_clean();
81
    }
82
}
83