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