Completed
Push — master ( bc3898...bd8c0c )
by Mihail
04:18
created

Manager::startMeasure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Ffcms\Core\Debug;
4
5
use DebugBar\DataCollector\ConfigCollector;
6
use DebugBar\StandardDebugBar;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Helper\Type\Obj;
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
17
    public $bar;
18
    public $render;
19
20
    /**
21
     * Manager constructor. Construct debug manager - build debug bar, javascripts and initialize configs
22
     */
23
    public function __construct()
24
    {
25
        $this->bar = new StandardDebugBar();
26
        $this->render = $this->bar->getJavascriptRenderer();
27
28
        $this->bar->addCollector(new ConfigCollector());
29
    }
30
31
    /**
32
     * Render debug bar header
33
     * @return string
34
     */
35
    public function renderHead()
36
    {
37
        return $this->render->renderHead();
38
    }
39
40
    /**
41
     * Render debug bar code
42
     * @return string
43
     * @throws \DebugBar\DebugBarException
44
     */
45
    public function renderOut()
46
    {
47
        if (!$this->bar->hasCollector('queries')) {
48
            $timeCollector = null;
49
            $log = App::$Database->connection()->getQueryLog();
50
            if ($this->bar->hasCollector('time')) {
51
                $timeCollector = $this->bar->getCollector('time');
52
            }
53
            $queryCollector = new LaravelDatabaseCollector($timeCollector, $log);
0 ignored issues
show
Bug introduced by
It seems like $timeCollector defined by $this->bar->getCollector('time') on line 51 can also be of type object<DebugBar\DataColl...DataCollectorInterface>; however, Ffcms\Core\Debug\Laravel...ollector::__construct() does only seem to accept null|object<DebugBar\Dat...ctor\TimeDataCollector>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
54
            $this->bar->addCollector($queryCollector);
55
        }
56
        return $this->render->render();
57
    }
58
59
    /**
60
     * Add exception into debug bar and stop execute
61
     * @param $e
62
     * @throws \DebugBar\DebugBarException
63
     */
64
    public function addException($e)
65
    {
66
        if ($e instanceof \Exception) {
67
            $this->bar->getCollector('exceptions')->addException($e);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface DebugBar\DataCollector\DataCollectorInterface as the method addException() does only exist in the following implementations of said interface: DebugBar\DataCollector\ExceptionsCollector.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
68
        }
69
    }
70
71
    /**
72
     * Add message into debug bar
73
     * @param string $m
74
     * @param string $type
75
     * @throws \DebugBar\DebugBarException
76
     */
77
    public function addMessage($m, $type = 'info')
78
    {
79
        if (!Obj::isString($m) || !Obj::isString($type)) {
80
            return;
81
        }
82
        $m = App::$Security->secureHtml($m);
83
        $mCollector = $this->bar->getCollector('messages');
84
85
        if (method_exists($mCollector, $type)) {
86
            $this->bar->getCollector('messages')->{$type}($m);
87
        }
88
    }
89
90
    /**
91
     * Add message debug data to bar
92
     * @param $data
93
     * @throws \DebugBar\DebugBarException
94
     */
95
    public function vardump($data)
96
    {
97
        $this->bar->getCollector('messages')->info($data);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface DebugBar\DataCollector\DataCollectorInterface as the method info() does only exist in the following implementations of said interface: DebugBar\Bridge\PropelCollector, DebugBar\Bridge\SlimCollector, DebugBar\Bridge\SwiftMailer\SwiftLogCollector, DebugBar\DataCollector\MessagesCollector.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
98
    }
99
100
    /**
101
     * Start timeline measure
102
     * @param string $key
103
     */
104
    public function startMeasure(string $key): void
105
    {
106
        $this->bar['time']->startMeasure($key);
107
    }
108
109
    /**
110
     * Stop timeline measure
111
     * @param string $key
112
     */
113
    public function stopMeasure(string $key): void
114
    {
115
        $this->bar['time']->stopMeasure($key);
116
    }
117
118
    /**
119
     * Check if debug bar is enabled. Method called before __construct() is initiated!!
120
     * @return bool
121
     */
122
    public static function isEnabled()
123
    {
124
        $property = App::$Properties->get('debug');
125
        // $_COOKIE used insted of symfony request, cuz debug initialize early
126
        return ($property['all'] === true || $_COOKIE[$property['cookie']['key']] === $property['cookie']['value']);
127
    }
128
}