From 03f346f67df7c04a1bd7c0e8e4a48cae3dd8d8e3 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Sat, 23 Sep 2023 12:05:41 +0000 Subject: [PATCH 1/2] formatting --- src/Container/Action.php | 17 ++++++++--------- src/Container/Capsule.php | 16 ++++++++-------- src/Database/QueryBuilder.php | 1 - src/Http/Request.php | 6 ++++-- src/Http/Response.php | 6 +++--- src/Http/ServerAccessControl.php | 6 +++--- src/Http/UploadFile.php | 4 ++-- src/Mail/Driver/SmtpDriver.php | 6 +++--- src/Mail/Mail.php | 11 ++++++++++- src/Middleware/AuthMiddleware.php | 2 +- src/Middleware/CsrfMiddleware.php | 10 +++++++--- src/Session/Driver/DatabaseDriver.php | 1 - src/Session/Driver/FilesystemDriver.php | 2 +- src/Session/Session.php | 13 +++++++++---- src/Storage/Service/DiskFilesystemService.php | 2 +- src/Storage/Service/FTPService.php | 6 +++--- src/Storage/Service/S3Service.php | 12 ++++++------ src/Storage/Storage.php | 4 +++- src/Support/Collection.php | 2 +- src/Support/Str.php | 14 +++++++------- src/Testing/Features/SeedingHelper.php | 2 +- src/Translate/TranslatorConfiguration.php | 2 +- src/Validation/RequestValidation.php | 12 +++++++----- 23 files changed, 89 insertions(+), 68 deletions(-) diff --git a/src/Container/Action.php b/src/Container/Action.php index 7b8b759d..496ac82c 100644 --- a/src/Container/Action.php +++ b/src/Container/Action.php @@ -214,7 +214,6 @@ public function call(callable|string|array $actions, ?array $param = null): mixe if (!array_key_exists($middleware, $this->middlewares)) { throw new RouterException( sprintf('%s is not define middleware.', $middleware), - E_ERROR ); } @@ -222,7 +221,6 @@ public function call(callable|string|array $actions, ?array $param = null): mixe if (!class_exists($this->middlewares[$middleware])) { throw new RouterException( sprintf('%s is not a middleware class.', $middleware), - E_ERROR ); } @@ -243,7 +241,8 @@ public function call(callable|string|array $actions, ?array $param = null): mixe case is_string($response): case is_array($response): case is_object($response): - case $response instanceof \Iterable: + case is_iterable($response): + case $response instanceof \Iterator: case $response instanceof ResponseInterface: return $response; case $response instanceof Model || $response instanceof Collection: @@ -315,19 +314,19 @@ private function dispatchControllers(array $functions, array $params): mixed /** * Successively launches a function list. * - * @param array|callable $function - * @param array $arg + * @param array|callable|string $function + * @param array $arguments * @return mixed * @throws ReflectionException */ - public function execute(array|callable $function, array $arg): mixed + public function execute(array|callable|string $function, array $arguments): mixed { if (is_callable($function)) { - return call_user_func_array($function, $arg); + return call_user_func_array($function, $arguments); } if (is_array($function)) { - return call_user_func_array($function, $arg); + return call_user_func_array($function, $arguments); } // We launch the controller loader if $cb is a String @@ -340,7 +339,7 @@ public function execute(array|callable $function, array $arg): mixed if (is_array($controller)) { return call_user_func_array( $controller['action'], - array_merge($controller['injection'], $arg) + array_merge($controller['injection'], $arguments) ); } diff --git a/src/Container/Capsule.php b/src/Container/Capsule.php index cd832307..3c146174 100644 --- a/src/Container/Capsule.php +++ b/src/Container/Capsule.php @@ -145,11 +145,10 @@ public function bind(string $key, callable $value) * Register the instance of a class * * @param string $key - * @param Closure $value - * + * @param Closure|callable $value * @return void */ - public function factory($key, \Closure $value) + public function factory($key, Closure|callable $value) { $this->factories[$key] = $value; } @@ -157,15 +156,16 @@ public function factory($key, \Closure $value) /** * Saves the instance of a class * - * @param string $key + * @param string $key * @param mixed $instance - * * @return void */ - public function instance($key, $instance) + public function instance(string $key, mixed $instance): void { if (!is_object($instance)) { - throw new InvalidArgumentException('Parameter [2] is invalid'); + throw new InvalidArgumentException( + "The parameter $instance must be an object." + ); } $this->instances[$key] = $instance; @@ -178,7 +178,7 @@ public function instance($key, $instance) * @return mixed * @throws */ - private function resolve($key) + private function resolve($key): mixed { $reflection = new ReflectionClass($key); diff --git a/src/Database/QueryBuilder.php b/src/Database/QueryBuilder.php index 458017a9..04d0bceb 100644 --- a/src/Database/QueryBuilder.php +++ b/src/Database/QueryBuilder.php @@ -6,7 +6,6 @@ use Bow\Database\Connection\AbstractConnection; use PDO; -use stdClass; use PDOStatement; use Bow\Support\Str; use Bow\Support\Util; diff --git a/src/Http/Request.php b/src/Http/Request.php index c148ef4f..4aec3469 100644 --- a/src/Http/Request.php +++ b/src/Http/Request.php @@ -56,7 +56,9 @@ private function __construct() try { $data = json_decode(file_get_contents("php://input"), true, 1024, JSON_THROW_ON_ERROR); } catch (\Throwable $e) { - throw new BadRequestException("Invalid JSON syntax"); + throw new BadRequestException( + "The request json payload is invalid: " . $e->getMessage(), + ); } $this->input = array_merge((array) $data, $_GET); } else { @@ -649,7 +651,7 @@ public function getBag(string $name) /** * Set the shared value in request bags * - * @param Array $bags + * @param array $bags * @return mixed */ public function setBags(array $bags) diff --git a/src/Http/Response.php b/src/Http/Response.php index 1c8019ac..a7637004 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -237,9 +237,9 @@ public function addHeaders(array $headers): Response /** * Download the given file as an argument * - * @param string $file - * @param null $filename - * @param array $headers + * @param string $file + * @param ?string $filename + * @param array $headers * @return string */ public function download( diff --git a/src/Http/ServerAccessControl.php b/src/Http/ServerAccessControl.php index 1ce4a4b5..8380488a 100644 --- a/src/Http/ServerAccessControl.php +++ b/src/Http/ServerAccessControl.php @@ -110,11 +110,11 @@ public function allowCredentials(): ServerAccessControl /** * Active Access-control-Max-Age * - * @param string $excepted + * @param float|int $excepted * @return ServerAccessControl * @throws ServerAccessControlException */ - public function maxAge(string $excepted): ServerAccessControl + public function maxAge(float|int $excepted): ServerAccessControl { if (!is_numeric($excepted)) { throw new ServerAccessControlException( @@ -123,7 +123,7 @@ public function maxAge(string $excepted): ServerAccessControl ); } - return $this->push('Access-Control-Max-Age', $excepted); + return $this->push('Access-Control-Max-Age', (string) $excepted); } /** diff --git a/src/Http/UploadFile.php b/src/Http/UploadFile.php index f50d1241..7618e0af 100644 --- a/src/Http/UploadFile.php +++ b/src/Http/UploadFile.php @@ -24,9 +24,9 @@ public function __construct(array $file) /** * Get the file extension * - * @return string + * @return ?string */ - public function getExtension(): string + public function getExtension(): ?string { if (!isset($this->file['name'])) { return null; diff --git a/src/Mail/Driver/SmtpDriver.php b/src/Mail/Driver/SmtpDriver.php index 7f196c83..0e4d8001 100644 --- a/src/Mail/Driver/SmtpDriver.php +++ b/src/Mail/Driver/SmtpDriver.php @@ -226,9 +226,9 @@ private function disconnect() /** * Read the current connection stream. * - * @return string + * @return int */ - private function read() + private function read(): int { $s = null; @@ -255,7 +255,7 @@ private function read() * @param ?string $message * * @throws SmtpException - * @return string + * @return string|int|null */ private function write(string $command, ?int $code = null, ?string $message = null) { diff --git a/src/Mail/Mail.php b/src/Mail/Mail.php index ea621916..3dc0f7db 100644 --- a/src/Mail/Mail.php +++ b/src/Mail/Mail.php @@ -64,8 +64,17 @@ public static function configure(array $config = []): MailDriverInterface static::$config = $config; } + if (!isset($config['driver'])) { + throw new MailException( + "The driver is not defined.", E_USER_ERROR + ); + + } + if (!in_array($config['driver'], array_keys(static::$drivers))) { - throw new MailException("The type is not known.", E_USER_ERROR); + throw new MailException( + "The driver is not defined.", E_USER_ERROR + ); } $name = $config['driver']; diff --git a/src/Middleware/AuthMiddleware.php b/src/Middleware/AuthMiddleware.php index 7c53ddd1..3a73bf79 100644 --- a/src/Middleware/AuthMiddleware.php +++ b/src/Middleware/AuthMiddleware.php @@ -15,7 +15,7 @@ class AuthMiddleware implements BaseMiddleware * Handle an incoming request. * * @param Request $request - * @param Callable $next + * @param callable $next * @param array $args * @return Redirect */ diff --git a/src/Middleware/CsrfMiddleware.php b/src/Middleware/CsrfMiddleware.php index 8eba888b..72eaf001 100644 --- a/src/Middleware/CsrfMiddleware.php +++ b/src/Middleware/CsrfMiddleware.php @@ -14,7 +14,7 @@ class CsrfMiddleware implements BaseMiddleware * Handle an incoming request. * * @param Request $request - * @param Callable $next + * @param callable $next * @param array $args * @throws */ @@ -33,14 +33,18 @@ public function process(Request $request, callable $next, array $args = []): mix response()->status(401); - throw new TokenMismatch('Token Mismatch'); + throw new TokenMismatch( + 'The request csrf token mismatch' + ); } if ($request->get('_token') == $request->session()->get('_token')) { return $next($request); } - throw new TokenMismatch('Token Mismatch'); + throw new TokenMismatch( + 'The request csrf token mismatch' + ); } /** diff --git a/src/Session/Driver/DatabaseDriver.php b/src/Session/Driver/DatabaseDriver.php index 9324ecb4..06299e40 100644 --- a/src/Session/Driver/DatabaseDriver.php +++ b/src/Session/Driver/DatabaseDriver.php @@ -4,7 +4,6 @@ namespace Bow\Session\Driver; -use Bow\Support\Capsule; use Bow\Database\QueryBuilder; use Bow\Database\Database as DB; diff --git a/src/Session/Driver/FilesystemDriver.php b/src/Session/Driver/FilesystemDriver.php index 50e240f4..79f0a1c5 100644 --- a/src/Session/Driver/FilesystemDriver.php +++ b/src/Session/Driver/FilesystemDriver.php @@ -56,7 +56,7 @@ public function destroy(string $session_id): bool * @param int $maxlifetime * @return bool */ - public function gc(int $maxlifetime): int + public function gc(int $maxlifetime): bool { foreach (glob($this->save_path . "/*") as $file) { if (filemtime($file) + $maxlifetime < $this->createTimestamp() && file_exists($file)) { diff --git a/src/Session/Session.php b/src/Session/Session.php index 205c1336..f4950062 100644 --- a/src/Session/Session.php +++ b/src/Session/Session.php @@ -159,7 +159,9 @@ private function initializeDriver(): void $driver = $this->driver[$this->config['driver']] ?? null; if (is_null($driver)) { - throw new SessionException('The driver ' . $this->config['driver'] . ' is not valid'); + throw new SessionException( + 'The driver ' . $this->config['driver'] . ' is not valid' + ); } switch ($this->config['driver']) { @@ -174,13 +176,16 @@ private function initializeDriver(): void $handler = new $driver(); break; default: - throw new SessionException('Cannot set the session driver'); - break; + throw new SessionException( + 'Cannot set the session driver' + ); } // Set the session driver if (!@session_set_save_handler($handler, true)) { - throw new SessionException('Cannot set the session driver'); + throw new SessionException( + 'Cannot set the session driver' + ); } } diff --git a/src/Storage/Service/DiskFilesystemService.php b/src/Storage/Service/DiskFilesystemService.php index 4320fcc1..14928935 100644 --- a/src/Storage/Service/DiskFilesystemService.php +++ b/src/Storage/Service/DiskFilesystemService.php @@ -237,7 +237,7 @@ public function copy(string $target, string $source): bool } if (!$this->exists($source)) { - $this->makeDirectory(dirname($source), true); + $this->makeDirectory(dirname($source)); } return (bool) file_put_contents($source, $this->get($target)); diff --git a/src/Storage/Service/FTPService.php b/src/Storage/Service/FTPService.php index 523fc1f0..9b45c3db 100644 --- a/src/Storage/Service/FTPService.php +++ b/src/Storage/Service/FTPService.php @@ -232,7 +232,7 @@ public function store(UploadFile $file, ?string $location = null, array $option rewind($stream); // - $result = $this->writeStream($location, $stream, $option); + $result = $this->writeStream($location, $stream); fclose($stream); if ($result === false) { @@ -358,7 +358,7 @@ public function makeDirectory(string $dirname, int $mode = 0777): bool $directories = explode('/', $dirname); foreach ($directories as $directory) { - if (false === $this->makeActualDirectory($directory, $mode)) { + if (false === $this->makeActualDirectory($directory)) { $this->setConnectionRoot(); return false; } @@ -405,7 +405,7 @@ protected function makeActualDirectory(string $directory): bool public function get(string $filename): ?string { if (!$stream = $this->readStream($filename)) { - return false; + return null; } $contents = stream_get_contents($stream); diff --git a/src/Storage/Service/S3Service.php b/src/Storage/Service/S3Service.php index 80ef84bb..85b9ffc2 100644 --- a/src/Storage/Service/S3Service.php +++ b/src/Storage/Service/S3Service.php @@ -127,22 +127,22 @@ public function prepend(string $filename, string $content): bool * @param string $content * @param array $options * - * @return bool + * @return mixed */ - public function put(string $file, string $content, array $options = []): bool + public function put(string $file, string $content, array $options = []): mixed { $options = is_string($options) ? ['visibility' => $options] : (array) $options; - $this->client->putObject([ + $result = $this->client->putObject([ 'Bucket' => $this->config['bucket'], 'Key' => $file, 'Body' => $content, "Visibility" => $options["visibility"] ?? 'public' ]); - return true; + return $result; } /** @@ -221,9 +221,9 @@ public function makeDirectory(string $bucket, int $mode = 0777, array $option = * Recover the contents of the file * * @param string $filename - * @return null|string + * @return ?string */ - public function get(string $filename): string + public function get(string $filename): ?string { $result = $this->client->getObject([ 'Bucket' => $this->config['bucket'], diff --git a/src/Storage/Storage.php b/src/Storage/Storage.php index 631eb283..b401b8e1 100644 --- a/src/Storage/Storage.php +++ b/src/Storage/Storage.php @@ -185,6 +185,8 @@ public static function __callStatic($name, array $arguments) return call_user_func_array([static::$disk, $name], $arguments); } - throw new BadMethodCallException("unkdown $name method"); + throw new BadMethodCallException( + "The method $name is not defined" + ); } } diff --git a/src/Support/Collection.php b/src/Support/Collection.php index d8840a89..3f664c54 100644 --- a/src/Support/Collection.php +++ b/src/Support/Collection.php @@ -695,7 +695,7 @@ public function __toString() /** * jsonSerialize * - * @return string + * @return mixed */ public function jsonSerialize(): mixed { diff --git a/src/Support/Str.php b/src/Support/Str.php index c887c599..5dd97ea9 100644 --- a/src/Support/Str.php +++ b/src/Support/Str.php @@ -100,21 +100,21 @@ public static function plurial(string $str): string * slice * * @param string $str - * @param string $start - * @param string|null $end + * @param int $start + * @param int $length * @return string */ - public static function slice(string $str, int $start, ?int $end = null) + public static function slice(string $str, int $start, ?int $length = null) { $sliceStr = ''; if (is_string($str)) { - if ($end === null) { - $end = static::len($str); + if ($length === null) { + $length = static::len($str); } - if ($start < $end) { - $sliceStr = mb_substr($str, $start, $end, 'UTF-8'); + if ($start < $length) { + $sliceStr = mb_substr($str, $start, $length, 'UTF-8'); } } diff --git a/src/Testing/Features/SeedingHelper.php b/src/Testing/Features/SeedingHelper.php index 0031cac4..557db6fc 100644 --- a/src/Testing/Features/SeedingHelper.php +++ b/src/Testing/Features/SeedingHelper.php @@ -15,6 +15,6 @@ trait SeedingHelper */ public function seed(string $seeder, array $data = []): int { - return seed($seeder, $data); + return db_seed($seeder, $data); } } diff --git a/src/Translate/TranslatorConfiguration.php b/src/Translate/TranslatorConfiguration.php index cdf28e25..8130d656 100644 --- a/src/Translate/TranslatorConfiguration.php +++ b/src/Translate/TranslatorConfiguration.php @@ -28,7 +28,7 @@ public function create(Loader $config): void } } - return Translator::configure($lang, $dictionary, $auto_detected); + return Translator::configure($lang, $dictionary); }); } diff --git a/src/Validation/RequestValidation.php b/src/Validation/RequestValidation.php index b1e68b0c..c8c09351 100644 --- a/src/Validation/RequestValidation.php +++ b/src/Validation/RequestValidation.php @@ -4,8 +4,9 @@ namespace Bow\Validation; -use BadMethodCallException; use Bow\Http\Request; +use BadMethodCallException; +use Bow\Validation\Exception\ValidationException; use Bow\Validation\Exception\AuthorizationException; abstract class RequestValidation @@ -51,7 +52,7 @@ public function __construct() if ((count($keys) == 1 && $keys[0] === '*') || count($keys) == 0) { $this->data = $this->request->all(); } else { - $this->data = $this->request->excepts($keys); + $this->data = $this->request->only($keys); } $this->validate = Validator::make($this->data, $this->rules(), $this->messages()); @@ -109,10 +110,9 @@ protected function messages() /** * Send fails authorization * - * @param mixed $response * @throws AuthorizationException */ - private function sendFailAuthorization($response = null) + private function sendFailAuthorization() { throw new AuthorizationException( 'You do not have permission to make a request' @@ -225,7 +225,9 @@ public function __call($name, array $arguments) return call_user_func_array([$this->request, $name], $arguments); } - throw new BadMethodCallException('The method ' . $name . ' does not define.'); + throw new BadMethodCallException( + 'The method ' . $name . ' does not defined.' + ); } /** From dcf099f8af2d1c67e96fc42d8df796a5a2843a30 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Sat, 23 Sep 2023 12:06:21 +0000 Subject: [PATCH 2/2] lint --- src/Mail/Mail.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Mail/Mail.php b/src/Mail/Mail.php index 3dc0f7db..920fb689 100644 --- a/src/Mail/Mail.php +++ b/src/Mail/Mail.php @@ -66,14 +66,15 @@ public static function configure(array $config = []): MailDriverInterface if (!isset($config['driver'])) { throw new MailException( - "The driver is not defined.", E_USER_ERROR + "The driver is not defined.", + E_USER_ERROR ); - } if (!in_array($config['driver'], array_keys(static::$drivers))) { throw new MailException( - "The driver is not defined.", E_USER_ERROR + "The driver is not defined.", + E_USER_ERROR ); }