Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
TimeHandler
100.00% covered (success)
100.00%
30 / 30
100.00% covered (success)
100.00%
11 / 11
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getStart
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasStarted
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getStop
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasStopped
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 start
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 stop
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getElapsed
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 prepare
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 prepareHeaderAsString
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 prepareAsString
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Debug\Handler;
15
16/**
17 * Debug time handler class
18 *
19 * @category   Pop
20 * @package    Pop\Debug
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    1.3.0
25 */
26class TimeHandler extends AbstractHandler
27{
28
29    /**
30     * Start time
31     * @var float
32     */
33    protected $start = null;
34
35    /**
36     * Stop time
37     * @var float
38     */
39    protected $stop = null;
40
41    /**
42     * Constructor
43     *
44     * Instantiate a time handler object
45     *
46     * @param string  $name
47     * @param boolean $start
48     */
49    public function __construct($name = null, $start = true)
50    {
51        parent::__construct($name);
52        if ($start) {
53            $this->start();
54        }
55    }
56
57    /**
58     * Get start value
59     *
60     * @return float
61     */
62    public function getStart()
63    {
64        return $this->start;
65    }
66
67    /**
68     * Determined if the timer has started
69     *
70     * @return boolean
71     */
72    public function hasStarted()
73    {
74        return (null !== $this->start);
75    }
76
77    /**
78     * Get stop value
79     *
80     * @return float
81     */
82    public function getStop()
83    {
84        return $this->stop;
85    }
86
87    /**
88     * Determined if the timer has stopped
89     *
90     * @return boolean
91     */
92    public function hasStopped()
93    {
94        return (null !== $this->stop);
95    }
96
97    /**
98     * Start timer
99     *
100     * @return self
101     */
102    public function start()
103    {
104        $this->start = microtime(true);
105        return $this;
106    }
107
108    /**
109     * Stop timer
110     *
111     * @return self
112     */
113    public function stop()
114    {
115        $this->stop = microtime(true);
116        return $this;
117    }
118
119    /**
120     * Get elapsed time
121     *
122     * @return string
123     */
124    public function getElapsed()
125    {
126        if (null === $this->stop) {
127            $this->stop();
128        }
129        return number_format(($this->stop - $this->start), 5, '.', '');
130    }
131
132    /**
133     * Prepare handler data for storage
134     *
135     * @return array
136     */
137    public function prepare()
138    {
139        if (null === $this->stop) {
140            $this->stop();
141        }
142
143        $data = [
144            'start'   => number_format($this->start, 5, '.', ''),
145            'stop'    => number_format($this->stop, 5, '.', ''),
146            'elapsed' => $this->getElapsed()
147        ];
148
149        return $data;
150    }
151
152    /**
153     * Prepare header string
154     *
155     * @return string
156     */
157    public function prepareHeaderAsString()
158    {
159        $string  = ((!empty($this->name)) ? $this->name . ' ' : '') . 'Time Handler';
160        $string .= PHP_EOL . str_repeat('=', strlen($string)) . PHP_EOL;
161
162        return $string;
163    }
164
165    /**
166     * Prepare handler data as string
167     *
168     * @return string
169     */
170    public function prepareAsString()
171    {
172        if (null === $this->stop) {
173            $this->stop();
174        }
175
176        $string  = "Start:\t\t\t" . number_format($this->start, 5, '.', '') . PHP_EOL;
177        $string .= "Stop:\t\t\t" . number_format($this->stop, 5, '.', '') . PHP_EOL;
178        $string .= "Elapsed:\t\t" . $this->getElapsed() . ' seconds' . PHP_EOL . PHP_EOL;
179
180        return $string;
181    }
182
183}