Skip to content

Commit

Permalink
add phpstan level 8 (#10)
Browse files Browse the repository at this point in the history
* wip

* wip

* add level 8

* remove ?
  • Loading branch information
mehedimi authored May 9, 2024
1 parent dcee8f8 commit 074167e
Show file tree
Hide file tree
Showing 21 changed files with 350 additions and 379 deletions.
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
"name": "mehedimi/wp-query-builder",
"description": "A database query builder for WordPress",
"license": "MIT",
"config": {
"platform": {
"php": "5.6"
}
},
"autoload": {
"psr-4": {
"Mehedi\\WPQueryBuilder\\": "src/"
Expand All @@ -27,7 +32,8 @@
"vlucas/phpdotenv": "^5.4",
"fakerphp/faker": "^1.20",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-mockery": "^1.1"
"phpstan/phpstan-mockery": "^1.1",
"laravel/pint": "^1.15"
},
"require": {
"ext-mysqli": "*",
Expand All @@ -38,6 +44,7 @@
"test:units": "phpunit --filter=Units --testdox",
"test:features": "RUN_FEATURE_TEST=on phpunit --filter=Features --testdox",
"check": "vendor/bin/phpstan analyse -c phpstan.neon",
"pre-commit": "composer check && composer test"
"pre-commit": "composer fmt && composer check && composer test",
"fmt": "./vendor/bin/pint"
}
}
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 6
level: 8
paths:
- src
includes:
Expand Down
10 changes: 5 additions & 5 deletions src/Concerns/ForwardsCalls.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ trait ForwardsCalls
/**
* Forward a method call to the given object.
*
* @param mixed $object
* @param string $method
* @param array<int, mixed> $parameters
* @param mixed $object
* @param string $method
* @param array<int, mixed> $parameters
* @return mixed
*
* @throws BadMethodCallException
Expand All @@ -23,7 +23,7 @@ protected static function forwardCallTo($object, $method, $parameters)
} catch (BadMethodCallException $e) {
$pattern = '~^Call to undefined method (?P<class>[^:]+)::(?P<method>[^\(]+)\(\)$~';

if (!preg_match($pattern, $e->getMessage(), $matches)) {
if (! preg_match($pattern, $e->getMessage(), $matches)) {
throw $e;
}

Expand All @@ -39,7 +39,7 @@ protected static function forwardCallTo($object, $method, $parameters)
/**
* Throw a bad method call exception for the given method.
*
* @param string $method
* @param string $method
* @return void
*
* @throws BadMethodCallException
Expand Down
77 changes: 38 additions & 39 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Connection
/**
* Query logs
*
* @var array<int, array<string, string>>|null
* @var array<int, array<string, array<int, int|string>|float|int|string>>|null
*/
protected $queryLogs;

Expand All @@ -43,8 +43,6 @@ class Connection

/**
* Create a new connection instance using mysqli connection from $wpdb
*
* @param mysqli $mysqli
*/
public function __construct(mysqli $mysqli)
{
Expand All @@ -54,8 +52,8 @@ public function __construct(mysqli $mysqli)
/**
* Run a select statement against the database.
*
* @param string $query
* @param array<int, mixed> $bindings
* @param string $query
* @param array<int, mixed> $bindings
* @return array<int, object>
*/
public function select($query, $bindings = [])
Expand All @@ -70,7 +68,7 @@ public function select($query, $bindings = [])
throw new QueryException($e->getMessage(), $e->getCode());
}

if (false === $statement) {
if ($statement === false) {
throw new QueryException($this->mysqli->error);
}

Expand All @@ -91,9 +89,8 @@ public function select($query, $bindings = [])
/**
* Run a SQL statement and log its execution context.
*
* @param string $query
* @param array<int, mixed> $bindings
* @param Closure $callback
* @param string $query
* @param array<int, mixed> $bindings
* @return mixed
*/
protected function run($query, $bindings, Closure $callback)
Expand All @@ -110,9 +107,9 @@ protected function run($query, $bindings, Closure $callback)
/**
* Log a query in the connection's query log.
*
* @param string $query
* @param array<int, mixed> $bindings
* @param float $time
* @param string $query
* @param array<int, string|int> $bindings
* @param float $time
* @return void
*/
protected function logQuery($query, $bindings, $time)
Expand All @@ -125,7 +122,7 @@ protected function logQuery($query, $bindings, $time)
/**
* Get the elapsed time since a given starting point.
*
* @param float $start
* @param float $start
* @return float
*/
protected function getElapsedTime($start)
Expand All @@ -136,8 +133,8 @@ protected function getElapsedTime($start)
/**
* Bind values to their parameters in the given statement.
*
* @param mysqli_stmt $statement
* @param array<int, mixed> $bindings
* @param mysqli_stmt $statement
* @param array<int, mixed> $bindings
* @return void
*/
protected function bindValues($statement, $bindings)
Expand All @@ -148,13 +145,13 @@ protected function bindValues($statement, $bindings)

$types = array_reduce($bindings, function ($carry, $value) {
if (is_int($value)) {
return $carry . 'i';
return $carry.'i';
}
if (is_float($value)) {
return $carry . 'd';
return $carry.'d';
}

return $carry . 's';
return $carry.'s';
}, '');

$statement->bind_param($types, ...$bindings);
Expand All @@ -163,21 +160,20 @@ protected function bindValues($statement, $bindings)
/**
* Get rows from mysqli_result
*
* @param mysqli_result $result
* @return array<int, object>
*/
protected function getRowsFromResult(mysqli_result $result)
{
return array_map(function ($row) {
return (object)$row;
return (object) $row;
}, $result->fetch_all(MYSQLI_ASSOC));
}

/**
* Run an insert statement against the database.
*
* @param string $query
* @param array<int, mixed> $bindings
* @param string $query
* @param array<int, mixed> $bindings
* @return bool
*/
public function insert($query, $bindings = [])
Expand All @@ -188,8 +184,8 @@ public function insert($query, $bindings = [])
/**
* Execute an SQL statement and return the boolean result.
*
* @param string $query
* @param array<int, mixed> $bindings
* @param string $query
* @param array<int, mixed> $bindings
* @return bool
*/
public function statement($query, $bindings = [])
Expand All @@ -201,7 +197,7 @@ public function statement($query, $bindings = [])
throw new QueryException($e->getMessage());
}

if (false === $statement) {
if ($statement === false) {
throw new QueryException($this->mysqli->error);
}

Expand All @@ -214,8 +210,8 @@ public function statement($query, $bindings = [])
/**
* Run update query with affected rows
*
* @param string $query
* @param array<int, mixed> $bindings
* @param string $query
* @param array<int, mixed> $bindings
* @return int
*/
public function update($query, $bindings = [])
Expand All @@ -226,8 +222,8 @@ public function update($query, $bindings = [])
/**
* Run an SQL statement and get the number of rows affected.
*
* @param string $query
* @param array<int, mixed> $bindings
* @param string $query
* @param array<int, mixed> $bindings
* @return int
*/
public function affectingStatement($query, $bindings = [])
Expand All @@ -242,7 +238,7 @@ public function affectingStatement($query, $bindings = [])
throw new QueryException($e->getMessage());
}

if (false === $statement) {
if ($statement === false) {
throw new QueryException($this->mysqli->error);
}

Expand All @@ -257,8 +253,8 @@ public function affectingStatement($query, $bindings = [])
/**
* Run delete query with affected rows
*
* @param string $query
* @param array<int, mixed> $bindings
* @param string $query
* @param array<int, mixed> $bindings
* @return int
*/
public function delete($query, $bindings = [])
Expand Down Expand Up @@ -289,7 +285,7 @@ public function disableQueryLog()
/**
* Get the connection query logs.
*
* @return array<int, array<string, string>>|null
* @return array<int, array<string, array<int, int|string>|float|int|string>>|null
*/
public function getQueryLog()
{
Expand All @@ -299,36 +295,39 @@ public function getQueryLog()
/**
* Execute a callback within a transaction
*
* @param callable $callback
* @param int $flags
* @param string|null $name
* @param int $flags
* @param string|null $name
* @return false|mixed
*/
public function transaction(callable $callback, $flags = 0, $name = null)
{
try {
$result = call_user_func($callback);
$this->commit($flags, $name);

return $result;
} catch (Exception $e) {
$this->rollback($flags, $name);

return false;
}
}

/**
* @param string $name
* @param array<int, string> $arguments
* @param string $name
* @param array<int, string> $arguments
* @return bool
*/
public function __call($name, $arguments)
{
if (!preg_match('/^(beginTransaction|commit|rollback)$/', $name)) {
if (! preg_match('/^(beginTransaction|commit|rollback)$/', $name)) {
self::throwBadMethodCallException($name);
}

// @phpstan-ignore-next-line
$name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name));

// @phpstan-ignore-next-line
return call_user_func_array([$this->mysqli, $name], $arguments);
}
}
1 change: 0 additions & 1 deletion src/Contracts/Pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ interface Pluggable
/**
* Apply that plugin
*
* @param Builder $builder
* @return mixed
*/
public function apply(Builder $builder);
Expand Down
7 changes: 3 additions & 4 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class DB
/**
* Set the table which the query is targeting.
*
* @param string $table
* @param string $table
* @return Builder
*/
public static function table($table)
Expand Down Expand Up @@ -65,7 +65,6 @@ public static function getConnection()
/**
* Apply a mixin to builder class
*
* @param Pluggable $plugin
* @return Builder
*/
public static function plugin(Pluggable $plugin)
Expand All @@ -76,8 +75,8 @@ public static function plugin(Pluggable $plugin)
/**
* Handle dynamic method calling
*
* @param string $name
* @param array<int> $arguments
* @param string $name
* @param array<int> $arguments
* @return Builder
*/
public static function __callStatic($name, $arguments)
Expand Down
Loading

0 comments on commit 074167e

Please sign in to comment.