Profiler_Core   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 258
Duplicated Lines 16.28 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 42
loc 258
rs 9.3999
wmc 33
lcom 1
cbo 4

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
A __call() 0 15 4
A disable() 0 5 1
C render() 0 41 7
B benchmarks() 0 32 4
B database() 0 26 3
B session() 0 25 5
A post() 21 21 4
A cookies() 21 21 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php defined('SYSPATH') or die('No direct access allowed.');
2
/**
3
 * Adds useful information to the bottom of the current page for debugging and optimization purposes.
4
 *
5
 * Benchmarks   - The times and memory usage of benchmarks run by the Benchmark library.
6
 * Database     - The raw SQL and number of affected rows of Database queries.
7
 * Session Data - Data stored in the current session if using the Session library.
8
 * POST Data    - The name and values of any POST data submitted to the current page.
9
 * Cookie Data  - All cookies sent for the current request.
10
 *
11
 * $Id: Profiler.php 4383 2009-06-03 00:17:24Z ixmatus $
12
 *
13
 * @package    Profiler
14
 * @author     Kohana Team
15
 * @copyright  (c) 2007-2008 Kohana Team
16
 * @license    http://kohanaphp.com/license.html
17
 */
18
class Profiler_Core
19
{
20
    protected $profiles = array();
21
    protected $show;
22
23
    public function __construct()
24
    {
25
        // Add all built in profiles to event
26
        Event::add('profiler.run', array($this, 'benchmarks'));
27
        Event::add('profiler.run', array($this, 'database'));
28
        Event::add('profiler.run', array($this, 'session'));
29
        Event::add('profiler.run', array($this, 'post'));
30
        Event::add('profiler.run', array($this, 'cookies'));
31
32
        // Add profiler to page output automatically
33
        Event::add('system.display', array($this, 'render'));
34
35
        Kohana::log('debug', 'Profiler Library initialized');
36
    }
37
38
    /**
39
     * Magic __call method. Creates a new profiler section object.
40
     *
41
     * @param   string   input type
42
     * @param   string   input name
43
     * @return  object
0 ignored issues
show
Documentation introduced by
Should the return type not be false|object?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
44
     */
45
    public function __call($method, $args)
46
    {
47
        if (! $this->show or (is_array($this->show) and ! in_array($args[0], $this->show))) {
48
            return false;
49
        }
50
51
        // Class name
52
        $class = 'Profiler_'.ucfirst($method);
53
54
        $class = new $class();
55
56
        $this->profiles[$args[0]] = $class;
57
58
        return $class;
59
    }
60
61
    /**
62
     * Disables the profiler for this page only.
63
     * Best used when profiler is autoloaded.
64
     *
65
     * @return  void
66
     */
67
    public function disable()
68
    {
69
        // Removes itself from the event queue
70
        Event::clear('system.display', array($this, 'render'));
0 ignored issues
show
Documentation introduced by
array($this, 'render') is of type array<integer,this<Profi...r_Core>","1":"string"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71
    }
72
73
    /**
74
     * Render the profiler. Output is added to the bottom of the page by default.
75
     *
76
     * @param   boolean  return the output if TRUE
77
     * @return  void|string
78
     */
79
    public function render($return = false)
0 ignored issues
show
Coding Style introduced by
render uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
80
    {
81
        $start = microtime(true);
82
83
        $get = isset($_GET['profiler']) ? explode(',', $_GET['profiler']) : array();
84
        $this->show = empty($get) ? Kohana::config('profiler.show') : $get;
85
86
        Event::run('profiler.run', $this);
87
88
        $styles = '';
89
        foreach ($this->profiles as $profile) {
90
            $styles .= $profile->styles();
91
        }
92
93
        // Don't display if there's no profiles
94
        if (empty($this->profiles)) {
95
            return;
96
        }
97
98
        // Load the profiler view
99
        $data = array(
100
            'profiles' => $this->profiles,
101
            'styles'   => $styles,
102
            'execution_time' => microtime(true) - $start
103
        );
104
        $view = new View('kohana_profiler', $data);
105
106
        // Return rendered view if $return is TRUE
107
        if ($return === true) {
108
            return $view->render();
109
        }
110
111
        // Add profiler data to the output
112
        if (stripos(Kohana::$output, '</body>') !== false) {
113
            // Closing body tag was found, insert the profiler data before it
114
            Kohana::$output = str_ireplace('</body>', $view->render().'</body>', Kohana::$output);
115
        } else {
116
            // Append the profiler data to the output
117
            Kohana::$output .= $view->render();
118
        }
119
    }
120
121
    /**
122
     * Benchmark times and memory usage from the Benchmark library.
123
     *
124
     * @return  void
125
     */
126
    public function benchmarks()
127
    {
128
        if (! $table = $this->table('benchmarks')) {
0 ignored issues
show
Documentation Bug introduced by
The method table does not exist on object<Profiler_Core>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
129
            return;
130
        }
131
132
        $table->add_column();
133
        $table->add_column('kp-column kp-data');
134
        $table->add_column('kp-column kp-data');
135
        $table->add_column('kp-column kp-data');
136
        $table->add_row(array('Benchmarks', 'Time', 'Count', 'Memory'), 'kp-title', 'background-color: #FFE0E0');
137
138
        $benchmarks = Benchmark::get(true);
139
140
        // Moves the first benchmark (total execution time) to the end of the array
141
        $benchmarks = array_slice($benchmarks, 1) + array_slice($benchmarks, 0, 1);
142
143
        text::alternate();
144
        foreach ($benchmarks as $name => $benchmark) {
145
            // Clean unique id from system benchmark names
146
            $name = ucwords(str_replace(array('_', '-'), ' ', str_replace(SYSTEM_BENCHMARK.'_', '', $name)));
147
148
            $data = array($name, number_format($benchmark['time'], 3), $benchmark['count'], number_format($benchmark['memory'] / 1024 / 1024, 2).'MB');
149
            $class = text::alternate('', 'kp-altrow');
150
151
            if ($name == 'Total Execution') {
152
                $class = 'kp-totalrow';
153
            }
154
155
            $table->add_row($data, $class);
156
        }
157
    }
158
159
    /**
160
     * Database query benchmarks.
161
     *
162
     * @return  void
163
     */
164
    public function database()
165
    {
166
        if (! $table = $this->table('database')) {
0 ignored issues
show
Documentation Bug introduced by
The method table does not exist on object<Profiler_Core>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
167
            return;
168
        }
169
170
        $table->add_column();
171
        $table->add_column('kp-column kp-data');
172
        $table->add_column('kp-column kp-data');
173
        $table->add_row(array('Queries', 'Time', 'Rows'), 'kp-title', 'background-color: #E0FFE0');
174
175
        $queries = Database::$benchmarks;
176
177
        text::alternate();
178
        $total_time = $total_rows = 0;
179
        foreach ($queries as $query) {
180
            $data = array($query['query'], number_format($query['time'], 3), $query['rows']);
181
            $class = text::alternate('', 'kp-altrow');
182
            $table->add_row($data, $class);
183
            $total_time += $query['time'];
184
            $total_rows += $query['rows'];
185
        }
186
187
        $data = array('Total: ' . count($queries), number_format($total_time, 3), $total_rows);
188
        $table->add_row($data, 'kp-totalrow');
189
    }
190
191
    /**
192
     * Session data.
193
     *
194
     * @return  void
195
     */
196
    public function session()
0 ignored issues
show
Coding Style introduced by
session uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
197
    {
198
        if (empty($_SESSION)) {
199
            return;
200
        }
201
202
        if (! $table = $this->table('session')) {
0 ignored issues
show
Documentation Bug introduced by
The method table does not exist on object<Profiler_Core>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
203
            return;
204
        }
205
206
        $table->add_column('kp-name');
207
        $table->add_column();
208
        $table->add_row(array('Session', 'Value'), 'kp-title', 'background-color: #CCE8FB');
209
210
        text::alternate();
211
        foreach ($_SESSION as $name => $value) {
212
            if (is_object($value)) {
213
                $value = get_class($value).' [object]';
214
            }
215
216
            $data = array($name, $value);
217
            $class = text::alternate('', 'kp-altrow');
218
            $table->add_row($data, $class);
219
        }
220
    }
221
222
    /**
223
     * POST data.
224
     *
225
     * @return  void
226
     */
227 View Code Duplication
    public function post()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
post uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
228
    {
229
        if (empty($_POST)) {
230
            return;
231
        }
232
233
        if (! $table = $this->table('post')) {
0 ignored issues
show
Documentation Bug introduced by
The method table does not exist on object<Profiler_Core>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
234
            return;
235
        }
236
237
        $table->add_column('kp-name');
238
        $table->add_column();
239
        $table->add_row(array('POST', 'Value'), 'kp-title', 'background-color: #E0E0FF');
240
241
        text::alternate();
242
        foreach ($_POST as $name => $value) {
243
            $data = array($name, $value);
244
            $class = text::alternate('', 'kp-altrow');
245
            $table->add_row($data, $class);
246
        }
247
    }
248
249
    /**
250
     * Cookie data.
251
     *
252
     * @return  void
253
     */
254 View Code Duplication
    public function cookies()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style introduced by
cookies uses the super-global variable $_COOKIE which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
255
    {
256
        if (empty($_COOKIE)) {
257
            return;
258
        }
259
260
        if (! $table = $this->table('cookies')) {
0 ignored issues
show
Documentation Bug introduced by
The method table does not exist on object<Profiler_Core>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
261
            return;
262
        }
263
264
        $table->add_column('kp-name');
265
        $table->add_column();
266
        $table->add_row(array('Cookies', 'Value'), 'kp-title', 'background-color: #FFF4D7');
267
268
        text::alternate();
269
        foreach ($_COOKIE as $name => $value) {
270
            $data = array($name, $value);
271
            $class = text::alternate('', 'kp-altrow');
272
            $table->add_row($data, $class);
273
        }
274
    }
275
}
276