Manager::addException()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Ffcms\Core\Debug;
4
5
use DebugBar\DataCollector\ConfigCollector;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Helper\Type\Any;
8
use Ffcms\Templex\Exceptions\Error;
0 ignored issues
show
Bug introduced by
The type Ffcms\Templex\Exceptions\Error was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
/**
11
 * Class Debug. Provide methods of display information about debug and collected data in debug bar
12
 * @package Ffcms\Core
13
 */
14
class Manager
15
{
16
    public $bar;
17
    public $render;
18
19
    /**
20
     * Manager constructor. Construct debug manager - build debug bar, javascripts and initialize config
21
     */
22
    public function __construct()
23
    {
24
        $this->bar = new FfcmsDebugBar();
25
        $this->render = $this->bar->getJavascriptRenderer();
26
        try {
27
            $config = App::$Properties->getAll();
28
            $config['database']['password'] = '***HIDDEN***';
29
            $config['mail']['password'] = '***HIDDEN***';
30
            $this->bar->addCollector(new ConfigCollector($config));
0 ignored issues
show
Bug introduced by
It seems like $config can also be of type null; however, parameter $data of DebugBar\DataCollector\C...ollector::__construct() does only seem to accept array, 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
            $this->bar->addCollector(new ConfigCollector(/** @scrutinizer ignore-type */ $config));
Loading history...
31
        } catch (\Exception $oe) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
32
        }
33
    }
34
35
    /**
36
     * Render debug bar header
37
     * @return string
38
     */
39
    public function renderHead()
40
    {
41
        return $this->render->renderHead();
42
    }
43
44
    /**
45
     * Render debug bar code
46
     * @return string
47
     */
48
    public function renderOut()
49
    {
50
        foreach (Error::all() as $file => $error) {
51
            foreach ($error as $line) {
52
                $this->addMessage('Template error: ' . $line . '(' . $file . ')', 'error');
53
            }
54
        }
55
        return $this->render->render();
56
    }
57
58
    /**
59
     * Add exception into debug bar and stop execute
60
     * @param \Exception $e
61
     */
62
    public function addException($e)
63
    {
64
        if ($e instanceof \Exception) {
0 ignored issues
show
introduced by
$e is always a sub-type of Exception.
Loading history...
65
            try {
66
                $this->bar->getCollector('exceptions')->addException($e);
0 ignored issues
show
Bug introduced by
The method addException() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as DebugBar\DataCollector\ExceptionsCollector. ( Ignorable by Annotation )

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

66
                $this->bar->getCollector('exceptions')->/** @scrutinizer ignore-call */ addException($e);
Loading history...
67
            } catch (\Exception $ie) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
68
            } // mute exceptions there
69
        }
70
    }
71
72
    /**
73
     * Add message into debug bar
74
     * @param string $m
75
     * @param string $type
76
     */
77
    public function addMessage($m, $type = 'info')
78
    {
79
        if (!Any::isStr($m) || !Any::isStr($type)) {
80
            return;
81
        }
82
83
        $m = App::$Security->secureHtml($m);
84
        try {
85
            $mCollector = $this->bar->getCollector('messages');
86
87
            if (method_exists($mCollector, $type)) {
88
                $this->bar->getCollector('messages')->{$type}($m);
89
            }
90
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
91
        } // mute exceptions there
92
    }
93
94
    /**
95
     * Add message debug data to bar
96
     * @param mixed $data
97
     */
98
    public function vardump($data)
99
    {
100
        try {
101
            $this->bar->getCollector('messages')->info($data);
0 ignored issues
show
Bug introduced by
The method info() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as DebugBar\DataCollector\MessagesCollector or DebugBar\Bridge\PropelCollector. ( Ignorable by Annotation )

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

101
            $this->bar->getCollector('messages')->/** @scrutinizer ignore-call */ info($data);
Loading history...
102
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
103
        }
104
    }
105
106
    /**
107
     * Start timeline measure
108
     * @param string $key
109
     */
110
    public function startMeasure(string $key): void
111
    {
112
        $this->bar['time']->startMeasure($key);
0 ignored issues
show
Bug introduced by
The method startMeasure() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as DebugBar\DataCollector\TimeDataCollector. ( Ignorable by Annotation )

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

112
        $this->bar['time']->/** @scrutinizer ignore-call */ 
113
                            startMeasure($key);
Loading history...
113
    }
114
115
    /**
116
     * Stop timeline measure
117
     * @param string $key
118
     */
119
    public function stopMeasure(string $key): void
120
    {
121
        $this->bar['time']->stopMeasure($key);
0 ignored issues
show
Bug introduced by
The method stopMeasure() does not exist on DebugBar\DataCollector\DataCollectorInterface. It seems like you code against a sub-type of DebugBar\DataCollector\DataCollectorInterface such as DebugBar\DataCollector\TimeDataCollector. ( Ignorable by Annotation )

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

121
        $this->bar['time']->/** @scrutinizer ignore-call */ 
122
                            stopMeasure($key);
Loading history...
122
    }
123
124
    /**
125
     * Check if debug bar is enabled. Method called before __construct() is initiated!!
126
     * @return bool
127
     */
128
    public static function isEnabled()
129
    {
130
        $property = App::$Properties->get('debug');
131
        // $_COOKIE used insted of symfony request, cuz debug initialize early
132
        return ($property['all'] === true || (isset($_COOKIE[$property['cookie']['key']]) && $_COOKIE[$property['cookie']['key']] === $property['cookie']['value']));
133
    }
134
}
135