Issues (85)

src/Builder/QueryStatement.php (5 issues)

1
<?php
2
namespace Kir\MySQL\Builder;
3
4
use Kir\MySQL\Database\DatabaseStatement;
5
use Kir\MySQL\Databases\MySQL\MySQLExceptionInterpreter;
6
use Kir\MySQL\Exceptions\SqlException;
7
use Kir\MySQL\QueryLogger\QueryLoggers;
8
use PDO;
9
use PDOException;
10
use PDOStatement;
11
12
class QueryStatement implements DatabaseStatement {
13
	/**
14
	 * @param PDOStatement<mixed> $stmt
15
	 * @param string $query
16
	 * @param MySQLExceptionInterpreter $exceptionInterpreter
17
	 * @param QueryLoggers $queryLoggers
18
	 */
19
	public function __construct(
20
		private PDOStatement $stmt,
21
		private string $query,
22
		private MySQLExceptionInterpreter $exceptionInterpreter,
23
		private QueryLoggers $queryLoggers
24
	) {}
25
26
	/**
27
	 * @return PDOStatement<mixed>
28
	 */
29
	public function getStatement(): PDOStatement {
30
		return $this->stmt;
31
	}
32
33
	/**
34
	 * @param int $mode
35
	 * @param mixed $arg0
36
	 * @param array<mixed, mixed>|null $arg1
37
	 * @return $this
38
	 */
39
	public function setFetchMode(int $mode = PDO::FETCH_ASSOC, $arg0 = null, ?array $arg1 = null) {
40
		$args = [$mode];
41
		if($arg0 !== null) {
42
			$args[] = $arg0;
43
		}
44
		if($arg1 !== null) {
45
			$args[] = $arg1;
46
		}
47
		$this->stmt->setFetchMode(...$args);
0 ignored issues
show
$args is expanded, but the parameter $mode of PDOStatement::setFetchMode() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
		$this->stmt->setFetchMode(/** @scrutinizer ignore-type */ ...$args);
Loading history...
48
		return $this;
49
	}
50
51
	/**
52
	 * @param array<string, mixed> $params
53
	 * @throws SqlException
54
	 * @return $this
55
	 */
56
	public function execute(array $params = []) {
57
		$this->exceptionHandler(function() use ($params) {
58
			$this->queryLoggers->logRegion($this->query, function() use ($params) {
59
				$response = $this->stmt->execute($params);
60
				if(!$response) {
61
					throw new SqlException('Execution returned with "false".');
62
				}
63
			});
64
		});
65
		return $this;
66
	}
67
68
	/**
69
	 * @param int $fetchStyle
70
	 * @param mixed|null $fetchArgument
71
	 * @param mixed[] $ctorArgs
72
	 * @return array<mixed, mixed>
73
	 */
74
	public function fetchAll($fetchStyle = PDO::FETCH_ASSOC, $fetchArgument = null, array $ctorArgs = []): array {
75
		$result = $x = $this->exceptionHandler(function() use ($fetchStyle, $fetchArgument, $ctorArgs) {
76
			if($fetchArgument !== null) {
77
				return $this->stmt->fetchAll($fetchStyle, $fetchArgument, ...$ctorArgs);
78
			}
79
			return $this->stmt->fetchAll($fetchStyle);
80
		});
81
		/** @var array<mixed, mixed>|false $x */
82
		if($x === false) {
83
			return [];
84
		}
85
		return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type Kir\MySQL\Builder\T which is incompatible with the type-hinted return array.
Loading history...
86
	}
87
88
	/**
89
	 * @param int $fetchStyle
90
	 * @param int $cursorOrientation
91
	 * @param int $cursorOffset
92
	 * @return mixed
93
	 */
94
	public function fetch($fetchStyle = PDO::FETCH_ASSOC, $cursorOrientation = PDO::FETCH_ORI_NEXT, $cursorOffset = 0) {
95
		return $this->exceptionHandler(fn() => $this->stmt->fetch($fetchStyle, $cursorOrientation, $cursorOffset));
96
	}
97
98
	/**
99
	 * @param int $columnNo
100
	 * @return mixed
101
	 */
102
	public function fetchColumn($columnNo = 0) {
103
		return $this->exceptionHandler(fn() => $this->stmt->fetchColumn($columnNo));
104
	}
105
106
	/**
107
	 * @return bool
108
	 */
109
	public function closeCursor(): bool {
110
		return $this->exceptionHandler(fn() => $this->stmt->closeCursor());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->exceptionH...ion(...) { /* ... */ }) returns the type Kir\MySQL\Builder\T which is incompatible with the type-hinted return boolean.
Loading history...
111
	}
112
113
	/**
114
	 * @return int
115
	 */
116
	public function columnCount(): int {
117
		return $this->exceptionHandler(fn() => $this->stmt->columnCount());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->exceptionH...ion(...) { /* ... */ }) returns the type Kir\MySQL\Builder\T which is incompatible with the type-hinted return integer.
Loading history...
118
	}
119
120
	/**
121
	 * @param int $columnNo
122
	 * @return null|array<string, mixed>
123
	 */
124
	public function getColumnMeta(int $columnNo): ?array {
125
		return $this->exceptionHandler(function() use ($columnNo) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->exceptionH...ion(...) { /* ... */ }) returns the type Kir\MySQL\Builder\T which is incompatible with the type-hinted return array|null.
Loading history...
126
			$columnMeta = $this->stmt->getColumnMeta($columnNo);
127
			if($columnMeta === false) {
128
				return null;
129
			}
130
			return $columnMeta;
131
		});
132
	}
133
134
	/**
135
	 * @template T
136
	 * @param callable(): T $fn
137
	 * @return T
138
	 */
139
	private function exceptionHandler(callable $fn) {
140
		try {
141
			return $fn();
142
		} catch (PDOException $exception) {
143
			throw $this->exceptionInterpreter->getMoreConcreteException($exception);
144
		}
145
	}
146
}
147