Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
11 / 11
CRAP
100.00% covered (success)
100.00%
1 / 1
Step
100.00% covered (success)
100.00%
31 / 31
100.00% covered (success)
100.00%
11 / 11
19
100.00% covered (success)
100.00%
1 / 1
 setQuery
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasQuery
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getQuery
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addParam
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addParams
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 hasParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 addError
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 hasErrors
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getErrors
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
8
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-2024 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\Db\Adapter\Profiler;
15
16/**
17 * MySQL database adapter profiler step class
18 *
19 * @category   Pop
20 * @package    Pop\Db
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    6.5.0
25 */
26class Step extends AbstractProfiler
27{
28
29    /**
30     * Query SQL
31     * @var ?string
32     */
33    protected ?string $query = null;
34
35    /**
36     * Statement parameters
37     * @var array
38     */
39    protected array $params = [];
40
41    /**
42     * Errors
43     * @var array
44     */
45    protected array $errors = [];
46
47    /**
48     * Set query
49     *
50     * @param  string $sql
51     * @return Step
52     */
53    public function setQuery(string $sql): Step
54    {
55        $this->query = $sql;
56        return $this;
57    }
58
59    /**
60     * Determine if the profiler has query
61     *
62     * @return bool
63     */
64    public function hasQuery(): bool
65    {
66        return ($this->query !== null);
67    }
68
69    /**
70     * Get query
71     *
72     * @return ?string
73     */
74    public function getQuery(): ?string
75    {
76        return $this->query;
77    }
78
79    /**
80     * Add param
81     *
82     * @param  string $name
83     * @param  mixed  $value
84     * @return Step
85     */
86    public function addParam(string $name, mixed $value): Step
87    {
88        $this->params[$name] = $value;
89        return $this;
90    }
91
92    /**
93     * Add params
94     *
95     * @param  array $params
96     * @return Step
97     */
98    public function addParams(array $params): Step
99    {
100        foreach ($params as $name => $value) {
101            $this->addParam($name, $value);
102        }
103        return $this;
104    }
105
106    /**
107     * Determine if the profiler has params
108     *
109     * @return bool
110     */
111    public function hasParams(): bool
112    {
113        return (count($this->params) > 0);
114    }
115
116    /**
117     * Get params
118     *
119     * @return array
120     */
121    public function getParams(): array
122    {
123        return $this->params;
124    }
125
126    /**
127     * Add error
128     *
129     * @param  string $error
130     * @param  mixed  $number
131     * @return Step
132     */
133    public function addError(string $error, mixed $number = null): Step
134    {
135        $this->errors[(string)microtime(true)] = [
136            'error'  => $error,
137            'number' => $number
138        ];
139
140        return $this;
141    }
142
143    /**
144     * Determine if the profiler has errors
145     *
146     * @return bool
147     */
148    public function hasErrors(): bool
149    {
150        return (count($this->errors) > 0);
151    }
152
153    /**
154     * Get errors
155     *
156     * @return array
157     */
158    public function getErrors(): array
159    {
160        return $this->errors;
161    }
162
163    /**
164     * Magic method to support shorthand calls to certain values in the step
165     *
166     * @param  string $name
167     * @return mixed
168     */
169    public function __get(string $name): mixed
170    {
171        switch ($name) {
172            case 'query':
173                return $this->query;
174                break;
175            case 'params':
176                return $this->params;
177                break;
178            case 'errors':
179                return $this->errors;
180                break;
181            case 'start':
182                return $this->start;
183                break;
184            case 'finish':
185                return $this->finish;
186                break;
187            case 'elapsed':
188                return $this->getElapsed();
189                break;
190            default:
191                return null;
192        }
193    }
194
195}