carno-php /
mysql
| 1 | <?php |
||||||
| 2 | /** |
||||||
| 3 | * Aggregate functions |
||||||
| 4 | * User: moyo |
||||||
| 5 | * Date: 2018/4/19 |
||||||
| 6 | * Time: 11:13 AM |
||||||
| 7 | */ |
||||||
| 8 | |||||||
| 9 | namespace Carno\Database\SQL\Actions; |
||||||
| 10 | |||||||
| 11 | trait Aggregate |
||||||
| 12 | { |
||||||
| 13 | /** |
||||||
| 14 | * @var string |
||||||
| 15 | */ |
||||||
| 16 | private $ark = 'A_R_K'; |
||||||
| 17 | |||||||
| 18 | /** |
||||||
| 19 | * @param mixed ...$conditions |
||||||
| 20 | * @return bool |
||||||
| 21 | */ |
||||||
| 22 | public function exists(...$conditions) |
||||||
| 23 | { |
||||||
| 24 | $conditions && $this->where(...$conditions); |
||||||
|
0 ignored issues
–
show
It seems like
where() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 25 | |||||||
| 26 | return (yield $this->count()) > 0 ? true : false; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 27 | } |
||||||
| 28 | |||||||
| 29 | /** |
||||||
| 30 | * @param string $expr |
||||||
| 31 | * @return int |
||||||
| 32 | */ |
||||||
| 33 | public function count(string $expr = '1') |
||||||
| 34 | { |
||||||
| 35 | return (int) yield $this->calc(sprintf('COUNT(%s)', $expr)); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 36 | } |
||||||
| 37 | |||||||
| 38 | /** |
||||||
| 39 | * @param string $expr |
||||||
| 40 | * @return float |
||||||
| 41 | */ |
||||||
| 42 | public function sum(string $expr) |
||||||
| 43 | { |
||||||
| 44 | return (float) yield $this->calc(sprintf('SUM(%s)', $expr)); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 45 | } |
||||||
| 46 | |||||||
| 47 | /** |
||||||
| 48 | * @param string $expr |
||||||
| 49 | * @return float |
||||||
| 50 | */ |
||||||
| 51 | public function max(string $expr) |
||||||
| 52 | { |
||||||
| 53 | return (float) yield $this->calc(sprintf('MAX(%s)', $expr)); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 54 | } |
||||||
| 55 | |||||||
| 56 | /** |
||||||
| 57 | * @param string $expr |
||||||
| 58 | * @return float |
||||||
| 59 | */ |
||||||
| 60 | public function min(string $expr) |
||||||
| 61 | { |
||||||
| 62 | return (float) yield $this->calc(sprintf('MIN(%s)', $expr)); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 63 | } |
||||||
| 64 | |||||||
| 65 | /** |
||||||
| 66 | * @param string $expr |
||||||
| 67 | * @return float |
||||||
| 68 | */ |
||||||
| 69 | public function avg(string $expr) |
||||||
| 70 | { |
||||||
| 71 | return (float) yield $this->calc(sprintf('AVG(%s)', $expr)); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 72 | } |
||||||
| 73 | |||||||
| 74 | /** |
||||||
| 75 | * @param string $expr |
||||||
| 76 | * @return float |
||||||
| 77 | */ |
||||||
| 78 | private function calc(string $expr) |
||||||
| 79 | { |
||||||
| 80 | $stmt = $this->backup('select', 'orders'); |
||||||
| 81 | |||||||
| 82 | $this->select(sprintf('%s AS %s', $expr, $this->ark)); |
||||||
|
0 ignored issues
–
show
It seems like
select() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 83 | |||||||
| 84 | $result = (yield $this->get())[$this->ark] ?? 0; |
||||||
|
0 ignored issues
–
show
It seems like
get() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 85 | |||||||
| 86 | $this->restore($stmt); |
||||||
| 87 | |||||||
| 88 | return $result; |
||||||
| 89 | } |
||||||
| 90 | |||||||
| 91 | /** |
||||||
| 92 | * @param string ...$builds |
||||||
| 93 | * @return array |
||||||
| 94 | */ |
||||||
| 95 | private function backup(string ...$builds) : array |
||||||
| 96 | { |
||||||
| 97 | $saved = []; |
||||||
| 98 | |||||||
| 99 | foreach ($builds as $build) { |
||||||
| 100 | if (property_exists($this, $pt = sprintf('b%s', ucfirst($build)))) { |
||||||
| 101 | $saved[$build] = $this->$pt; |
||||||
| 102 | $this->$pt = null; |
||||||
| 103 | } |
||||||
| 104 | } |
||||||
| 105 | |||||||
| 106 | return $saved; |
||||||
| 107 | } |
||||||
| 108 | |||||||
| 109 | /** |
||||||
| 110 | * @param array $previous |
||||||
| 111 | */ |
||||||
| 112 | private function restore(array $previous) : void |
||||||
| 113 | { |
||||||
| 114 | foreach ($previous as $build => $saved) { |
||||||
| 115 | if (property_exists($this, $pt = sprintf('b%s', ucfirst($build)))) { |
||||||
| 116 | $this->$pt = $saved; |
||||||
| 117 | } |
||||||
| 118 | } |
||||||
| 119 | } |
||||||
| 120 | } |
||||||
| 121 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.