Skip to content

Commit

Permalink
style: format code
Browse files Browse the repository at this point in the history
  • Loading branch information
tbreuss committed Feb 15, 2024
1 parent 7b3ac1f commit b189ba7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 55 deletions.
6 changes: 3 additions & 3 deletions src/PDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function __construct(string $dsn, ?string $username = NULL, ?string $pass
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
],
],
$options
);
$this->pdo = new \PDO($dsn, $username, $password, $options);
Expand Down Expand Up @@ -155,7 +155,7 @@ public static function __callStatic(string $name, array $arguments): mixed
return call_user_func_array([\PDO::class, $name], $arguments);
}

throw new \BadMethodCallException("Static method $name doesn't exist");
throw new \BadMethodCallException("Static method $name doesn't exist");
}

/**
Expand Down Expand Up @@ -219,7 +219,7 @@ public function run(string $sql, ?array $args = null): PDOStatement|false
$stmt = $this->pdo->query($sql);
return $stmt ? new PDOStatement($stmt) : false;
}

$isMultiArray = false;
foreach ($args as $arg) {
if (is_array($arg)) {
Expand Down
16 changes: 8 additions & 8 deletions src/PDOParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function prepareValuePlaceholders(array $subs): string

protected function prepareNumberedPlaceholder()
{
$this->num ++;
$this->num++;
if (array_key_exists($this->num, $this->values) === false) {
throw new PDOException("Parameter {$this->num} is missing from the bound values");
}
Expand All @@ -112,7 +112,7 @@ protected function prepareNumberedPlaceholder()
$values[] = null;
}
foreach ($values as $value) {
$count = ++ $this->count['__'];
$count = ++$this->count['__'];
$name = "__{$count}";
$expanded[] = ":{$name}";
$this->final_values[$name] = $value;
Expand Down Expand Up @@ -143,12 +143,12 @@ protected function prepareNamedPlaceholder(string $sub): string

protected function getPlaceholderName(string $orig): string
{
if (! isset($this->count[$orig])) {
if (!isset($this->count[$orig])) {
$this->count[$orig] = 0;
return $orig;
}

$count = ++ $this->count[$orig];
$count = ++$this->count[$orig];
return "{$orig}__{$count}";
}

Expand All @@ -160,7 +160,7 @@ protected function expandNamedPlaceholder(string $prefix, array $values): string
$name = "{$prefix}_{$i}";
$expanded[] = ":{$name}";
$this->final_values[$name] = $value;
$i ++;
$i++;
}
return implode(', ', $expanded);
}
Expand All @@ -179,14 +179,14 @@ protected function getParts(string $statement): array
protected function determineSplitRegex($driver): array
{
// see https://github.com/auraphp/Aura.Sql/tree/5.x/src/Parser
return match($driver) {
return match ($driver) {
'mysql' => [
// single-quoted string
"'(?:[^'\\\\]|\\\\'?)*'",
// double-quoted string
'"(?:[^"\\\\]|\\\\"?)*"',
// backtick-quoted string
'`(?:[^`\\\\]|\\\\`?)*`',
'`(?:[^`\\\\]|\\\\`?)*`',
],
'pgsql' => [
// single-quoted string
Expand Down Expand Up @@ -217,7 +217,7 @@ protected function determineSplitRegex($driver): array

protected function determineSkipRegex($driver): string
{
return match($driver) {
return match ($driver) {
'mysql' => '/^(\'|\"|\`)/um',
'pgsql' => '/^(\'|\"|\$|\:[^a-zA-Z_])/um',
'sqlite' => '/^(\'|"|`|\:[^a-zA-Z_])/um',
Expand Down
16 changes: 8 additions & 8 deletions src/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __call(string $name, array $arguments): mixed
'nextRowset',
'rowCount',
'setAttribute',
'setFetchMode',
'setFetchMode',
];

if (in_array($name, $methods)) {
Expand Down Expand Up @@ -114,15 +114,15 @@ public function fetchAffected(): int
{
return $this->stmt->rowCount();
}

/**
* Fetches the next row from the result set as an associative array
*/
public function fetchAssoc(): array|false
{
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}

/**
* Fetches the next row from the result set as an associative and indexed array
*/
Expand Down Expand Up @@ -151,15 +151,15 @@ public function fetchNamed(): array|false

/**
* Fetches the next row from the result set as an indexed array
*/
*/
public function fetchNumeric(): array|false
{
return $this->stmt->fetch(PDO::FETCH_NUM);
}

/**
* Fetches the next row from the result set as a key-value pair
*/
*/
public function fetchPair(): array|false
{
return $this->stmt->fetch(PDO::FETCH_KEY_PAIR);
Expand All @@ -172,7 +172,7 @@ public function fetchAllAssoc(): array
{
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}

/**
* Fetches all rows from the result set as an array of associative and indexed arrays
*/
Expand All @@ -191,7 +191,7 @@ public function fetchAllColumn(int $column = 0): array

/**
* Fetches all rows from the result set as an array by calling a function for each row
*/
*/
public function fetchAllFunction(callable $callable): array
{
return $this->stmt->fetchAll(PDO::FETCH_FUNC, $callable);
Expand Down Expand Up @@ -256,5 +256,5 @@ public function fetchAllUnique(int $style = 0): array
public function getIterator(): \Iterator
{
return $this->stmt->getIterator();
}
}
}
14 changes: 8 additions & 6 deletions tests/functional.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@ function _assert(mixed $assertion, string $message): void
$numberOfAssertions++;
}

function assert_equal(mixed $result, mixed $expected, string $message) {
function assert_equal(mixed $result, mixed $expected, string $message)
{
_assert($result === $expected, $message);
}

function assert_instanceof(mixed $result, string $class, string $message) {
function assert_instanceof(mixed $result, string $class, string $message)
{
_assert($result instanceof $class, $message);
}

function init_db(): \tebe\PDO
{
{
// set options that are later used for tests
$db = new \tebe\PDO('sqlite::memory:', options: [
\tebe\PDO::ATTR_ERRMODE => \tebe\PDO::ERRMODE_SILENT,
Expand All @@ -46,7 +48,7 @@ function init_db(): \tebe\PDO

$sql = "CREATE TABLE fruits (id int, name varchar(20), color varchar(20), calories int)";
$db->exec($sql);

$sql = "
INSERT INTO fruits VALUES
(1, 'Banana', 'yellow', 250),
Expand All @@ -63,10 +65,10 @@ function init_db(): \tebe\PDO
return $db;
}

(function() {
(function () {
global $numberOfAssertions;
$definedFunctions = get_defined_functions()['user'] ?? [];
$filterCallback = function(string $function): bool {
$filterCallback = function (string $function): bool {
$namespaceWithFunction = explode('\\', $function);
$functionName = end($namespaceWithFunction);
return str_starts_with($functionName, 'test_');
Expand Down
6 changes: 3 additions & 3 deletions tests/pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function test_pdo_begin_rollback_transaction(): void
function test_pdo_begin_commit_transaction(): void
{
$db = init_db();

$db->beginTransaction();
$db->exec("INSERT INTO fruits VALUES (999, 'XYZ', 'xyz', 999)");
$db->commit();
Expand All @@ -27,10 +27,10 @@ function test_pdo_begin_commit_transaction(): void
assert_equal($count, 9, 'Begin and commit back transaction');
}

function test_pdo_error_code(): void
function test_pdo_error_code(): void
{
$db = init_db();

$errorCode = $db->errorCode();
assert_equal($errorCode, '00000', 'Empty error code');

Expand Down

0 comments on commit b189ba7

Please sign in to comment.