Skip to content

Commit

Permalink
Merge pull request #310 from bowphp/refactor/code-base
Browse files Browse the repository at this point in the history
refactor: refactoring with phpstorm to deep debugs
  • Loading branch information
papac authored Jan 16, 2025
2 parents 8d263ef + 82b7d52 commit a6cf59f
Show file tree
Hide file tree
Showing 154 changed files with 1,555 additions and 1,259 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@
"php": "^8.1",
"bowphp/tintin": "^3.0",
"filp/whoops": "^2.1",
"nesbot/carbon": "^2.16",
"nesbot/carbon": "3.8.4",
"psy/psysh": "v0.12.*",
"fakerphp/faker": "^1.20",
"neitanod/forceutf8": "^2.0",
"ramsey/uuid": "^4.7"
"ramsey/uuid": "^4.7",
"ext-ftp": "*"
},
"require-dev": {
"pda/pheanstalk": "^4.0.5",
"phpunit/phpunit": "^9",
"pda/pheanstalk": "^5.0",
"phpunit/phpunit": "^9.6",
"monolog/monolog": "^1.22",
"twig/twig": "^3",
"squizlabs/php_codesniffer": "3.*",
Expand Down
31 changes: 18 additions & 13 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use Bow\Container\Action;
use Bow\Configuration\Loader;
use Bow\Contracts\ResponseInterface;
use Bow\Http\Exception\BadRequestException;
use Bow\Http\Exception\HttpException;
use Bow\Http\Request;
use Bow\Http\Response;
use Bow\Router\Exception\RouterException;
use Bow\Router\Resource;
use Bow\Router\Router;
use Bow\Router\Route;
use ReflectionException;

class Application extends Router
{
Expand All @@ -24,21 +26,21 @@ class Application extends Router
*
* @var Capsule
*/
private $capsule;
private Capsule $capsule;

/**
* The booting flag
*
* @var bool
*/
private $booted = false;
private bool $booted = false;

/**
* The Application instance
*
* @var Application
* @var ?Application
*/
private static $instance;
private static ?Application $instance = null;

/**
* The HTTP Request
Expand All @@ -62,7 +64,7 @@ class Application extends Router
private Loader $config;

/**
* This define if the X-powered-By header must be put in response
* This defines if the X-powered-By header must be put in response
*
* @var bool
*/
Expand All @@ -74,6 +76,7 @@ class Application extends Router
* @param Request $request
* @param Response $response
* @return void
* @throws BadRequestException
*/
public function __construct(Request $request, Response $response)
{
Expand All @@ -87,6 +90,7 @@ public function __construct(Request $request, Response $response)
$this->capsule->instance('app', $this);

$this->request->capture();

parent::__construct($request->method(), $request->get('_method'));
}

Expand Down Expand Up @@ -114,7 +118,7 @@ public function bind(Loader $config): void
$this->setBaseRoute($config['app']['root']);
}

// We active the auto csrf switcher
// We activate the auto csrf switcher
$this->setAutoCsrf($config['app']['auto_csrf'] ?? false);

$this->capsule->instance('config', $config);
Expand Down Expand Up @@ -144,6 +148,7 @@ private function boot(): void
* @param Request $request
* @param Response $response
* @return Application
* @throws BadRequestException
*/
public static function make(Request $request, Response $response): Application
{
Expand All @@ -155,7 +160,7 @@ public static function make(Request $request, Response $response): Application
}

/**
* Check if is running on php cli
* Check if it is running on php cli
*
* @return bool
*/
Expand All @@ -168,7 +173,7 @@ public function isRunningOnCli(): bool
* Launcher of the application
*
* @return ?bool
* @throws RouterException
* @throws RouterException|ReflectionException
*/
public function send(): ?bool
{
Expand Down Expand Up @@ -244,7 +249,7 @@ public function send(): ?bool
*
* @param mixed $response
* @param int $code
* @return null
* @return void
*/
private function sendResponse(mixed $response, int $code = 200): void
{
Expand All @@ -267,7 +272,7 @@ public function disablePoweredByMention(): void
}

/**
* Make the REST API base on route and ressource controller.
* Make the REST API base on route and resource controller.
*
* @param string $url
* @param string|array $controller_name
Expand Down Expand Up @@ -305,7 +310,7 @@ public function rest(string $url, string|array $controller_name, array $where =
}
}

if (is_null($controller) || !is_string($controller)) {
if (!is_string($controller)) {
throw new ApplicationException(
"[REST] No defined controller!",
E_ERROR
Expand Down Expand Up @@ -346,7 +351,7 @@ public function abort(int $code = 500, string $message = '', array $headers = []
}

/**
* Build dependance
* Build dependence
*
* @param ?string $name
* @param ?callable $callable
Expand Down Expand Up @@ -378,7 +383,7 @@ public function container(?string $name = null, ?callable $callable = null): mix
* This point method on the container system
*
* @param array $params
* @return Capsule
* @return mixed
* @throws ApplicationException
*/
public function __invoke(...$params): mixed
Expand Down
21 changes: 13 additions & 8 deletions src/Application/Exception/BaseErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Bow\Application\Exception;

use JetBrains\PhpStorm\NoReturn;
use PDOException;
use Bow\View\View;
use Bow\Http\Exception\HttpException;
Expand All @@ -20,19 +21,19 @@ class BaseErrorHandler
* @param array $data
* @return string
*/
protected function render($view, $data = []): string
protected function render(string $view, array $data = []): string
{
return View::parse($view, $data)->getContent();
}

/**
* Send the json as response
*
* @param string $data
* @param mixed $code
* @return mixed
* @param $exception
* @param mixed|null $code
* @return void
*/
protected function json($exception, $code = null)
#[NoReturn] protected function json($exception, mixed $code = null): void
{
if ($exception instanceof TokenInvalidException) {
$code = 'TOKEN_INVALID';
Expand All @@ -50,8 +51,12 @@ protected function json($exception, $code = null)
}
}

if (app_env("APP_ENV") == "production" && $exception instanceof PDOException) {
$message = 'An SQL error occurs. For security, we did not display the message.';
if ($exception instanceof PDOException) {
if (app_env("APP_ENV") == "production") {
$message = 'An SQL error occurs. For security, we did not display the message.';
} else {
$message = $exception->getMessage();
}
} else {
$message = $exception->getMessage();
}
Expand All @@ -78,6 +83,6 @@ protected function json($exception, $code = null)

response()->status($status);

return die(json_encode($response));
die(json_encode($response));
}
}
12 changes: 7 additions & 5 deletions src/Auth/Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Auth
/**
* The Auth instance
*
* @var GuardContract
* @var ?GuardContract
*/
private static ?GuardContract $instance = null;

Expand All @@ -29,17 +29,18 @@ class Auth
/**
* The current guard
*
* @var string
* @var ?string
*/
private static ?string $guard = null;

/**
* Configure Auth system
*
* @param array $config
* @return GuardContract
* @return ?GuardContract
* @throws AuthenticationException
*/
public static function configure(array $config)
public static function configure(array $config): ?GuardContract
{
if (!is_null(static::$instance)) {
return static::$instance;
Expand All @@ -61,7 +62,7 @@ public static function getInstance(): ?GuardContract
}

/**
* Check if user is authenticate
* Check if user is authenticated
*
* @param null|string $guard
* @return GuardContract
Expand Down Expand Up @@ -102,6 +103,7 @@ public static function guard(?string $guard = null): GuardContract
* @param string $method
* @param array $params
* @return ?GuardContract
* @throws ErrorException
*/
public static function __callStatic(string $method, array $params)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Auth/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class Authentication extends Model
*
* @return mixed
*/
public function getAuthenticateUserId()
public function getAuthenticateUserId(): mixed
{
return $this->attributes[$this->primary_key];
}

/**
* Define the additionals values
* Define the additional values
*
* @return array
*/
Expand Down
16 changes: 9 additions & 7 deletions src/Auth/Guards/GuardContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

use Bow\Auth\Auth;
use Bow\Auth\Authentication;
use Bow\Auth\Exception\AuthenticationException;

/**
* @method ?\Policier\Token getToken()
* @method ?\Bow\Policier\Token getToken()
*/
abstract class GuardContract
{
Expand All @@ -27,7 +28,7 @@ abstract class GuardContract
abstract public function id(): mixed;

/**
* Check if user is authenticate
* Check if user is authenticated
*
* @return bool
*/
Expand Down Expand Up @@ -58,12 +59,12 @@ abstract public function login(Authentication $user): bool;
/**
* Get authenticated user
*
* @return Authentication
* @return ?Authentication
*/
abstract public function user(): ?Authentication;

/**
* Check if user is authenticate
* Check if user is authenticated
*
* @param array $credentials
* @return bool
Expand All @@ -81,12 +82,13 @@ public function getName(): string
}

/**
* Load the a guard
* Load the guard
*
* @param string $guard
* @param string|null $guard
* @return GuardContract
* @throws AuthenticationException
*/
public function guard($guard = null): GuardContract
public function guard(string $guard = null): GuardContract
{
if ($guard) {
$this->guard = $guard;
Expand Down
Loading

0 comments on commit a6cf59f

Please sign in to comment.