Passed
Push — master ( 477c3d...43cf80 )
by Paul
05:05
created

Controller::initProfiler()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 12
nop 0
dl 0
loc 15
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\BlackBar;
4
5
class Controller
6
{
7
    /**
8
     * @var Application
9
     */
10
    protected $app;
11
12
    public function __construct(Application $app)
13
    {
14
        $this->app = $app;
15
    }
16
17
    /**
18
     * @action admin_enqueue_scripts
19
     * @action wp_enqueue_scripts
20
     */
21
    public function enqueueAssets(): void
22
    {
23
        wp_enqueue_script(Application::ID, $this->app->url('assets/main.js'));
24
        wp_enqueue_style(Application::ID, $this->app->url('assets/main.css'), ['dashicons']);
25
        wp_enqueue_style(Application::ID.'-syntax', $this->app->url('assets/syntax.css'));
26
    }
27
28
    /**
29
     * @param string $classes
30
     * @action admin_body_class
31
     */
32
    public function filterBodyClasses($classes): string
33
    {
34
        return trim((string) $classes.' '.Application::ID);
35
    }
36
37
    /**
38
     * @action all
39
     */
40
    public function initActions(): void
41
    {
42
        if (!class_exists('Debug_Bar_Slow_Actions')) {
43
            $this->app->actions->startTimer();
44
        }
45
    }
46
47
    /**
48
     * @action all
49
     */
50
    public function initConsole(): void
51
    {
52
        if (Application::CONSOLE_HOOK !== func_get_arg(0)) {
53
            return;
54
        }
55
        $args = array_pad(func_get_args(), 4, '');
56
        $args = array_combine(['hook', 'message', 'errno', 'location'], $args);
57
        $args = array_map('sanitize_textarea_field', $args);
58
        $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4);
59
        $entry = array_pop($backtrace); // get the fourth backtrace entry
60
        if (empty(trim($args['location'])) && array_key_exists('file', $entry)) {
61
            $path = explode(ABSPATH, $entry['file']);
62
            $args['location'] = sprintf('%s:%s', array_pop($path), $entry['line']);
63
        }
64
        $this->app->console->store($args['message'], $args['errno'], $args['location']);
65
    }
66
67
    /**
68
     * @action all
69
     */
70
    public function initProfiler(): void
71
    {
72
        $hook = func_get_arg(0);
73
        $name = func_num_args() > 1 ? func_get_arg(1) : 'Timer';
74
        $microtime = microtime(true);
75
        if ('timer:start' === $hook) {
76
            $this->app->profiler->start($name);
77
        } elseif ('timer:stop' === $hook) {
78
            $this->app->profiler->stop($name);
79
        } elseif ('blackbar/profiler/noise' === $hook) {
80
            $this->app->profiler->setNoise($microtime);
1 ignored issue
show
Bug introduced by
It seems like $microtime can also be of type string; however, parameter $microtime of GeminiLabs\BlackBar\Modules\Profiler::setNoise() does only seem to accept double, 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

80
            $this->app->profiler->setNoise(/** @scrutinizer ignore-type */ $microtime);
Loading history...
81
        } elseif ('blackbar/profiler/start' === $hook) {
82
            $this->app->profiler->setStart($microtime);
1 ignored issue
show
Bug introduced by
It seems like $microtime can also be of type string; however, parameter $microtime of GeminiLabs\BlackBar\Modules\Profiler::setStart() does only seem to accept double, 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

82
            $this->app->profiler->setStart(/** @scrutinizer ignore-type */ $microtime);
Loading history...
83
        } elseif ('blackbar/profiler/stop' === $hook) {
84
            $this->app->profiler->setStop($microtime);
1 ignored issue
show
Bug introduced by
It seems like $microtime can also be of type string; however, parameter $microtime of GeminiLabs\BlackBar\Modules\Profiler::setStop() does only seem to accept double, 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

84
            $this->app->profiler->setStop(/** @scrutinizer ignore-type */ $microtime);
Loading history...
85
        }
86
    }
87
88
    /**
89
     * @action plugins_loaded
90
     */
91
    public function registerLanguages(): void
92
    {
93
        load_plugin_textdomain(Application::ID, false,
94
            plugin_basename($this->app->path()).'/languages/'
95
        );
96
    }
97
98
    /**
99
     * @action admin_footer
100
     * @action wp_footer
101
     */
102
    public function renderBar(): void
103
    {
104
        do_action('blackbar/profiler/stop'); // stop profiler
105
        $this->app->render('debug-bar', [
106
            'modules' => [ // order is intentional
107
                $this->app->console,
108
                $this->app->profiler,
109
                $this->app->queries,
110
                $this->app->actions,
111
                $this->app->templates,
112
                $this->app->globals,
113
            ],
114
        ]);
115
    }
116
}
117