From 59c9c0330fa22bf2aecbf898e341d7bc07cab0a5 Mon Sep 17 00:00:00 2001 From: fadrian06 Date: Wed, 12 Mar 2025 20:18:02 -0400 Subject: [PATCH] improve ::registerContainerHandler docblock --- flight/Engine.php | 5 +- flight/Flight.php | 5 +- flight/commands/RouteCommand.php | 2 +- flight/core/Dispatcher.php | 4 +- flight/net/Request.php | 4 +- flight/util/ReturnTypeWillChange.php | 1 + tests/EngineTest.php | 2 - tests/named-arguments/ExampleClass.php | 3 + tests/named-arguments/FlightTest.php | 79 +++++++++++++++++++++----- 9 files changed, 83 insertions(+), 22 deletions(-) diff --git a/flight/Engine.php b/flight/Engine.php index 63fd05d..31b9b8e 100644 --- a/flight/Engine.php +++ b/flight/Engine.php @@ -16,6 +16,7 @@ use flight\net\Router; use flight\template\View; use Throwable; use flight\net\Route; +use Psr\Container\ContainerInterface; /** * The Engine class contains the core functionality of the framework. @@ -265,9 +266,9 @@ class Engine /** * Registers the container handler * - * @param callable|object $containerHandler Callback function or PSR-11 Container object that sets the container and how it will inject classes + * @template T of object * - * @return void + * @param ContainerInterface|callable(class-string $id, array $params): ?T $containerHandler Callback function or PSR-11 Container object that sets the container and how it will inject classes */ public function registerContainerHandler($containerHandler): void { diff --git a/flight/Flight.php b/flight/Flight.php index c9c4e7e..f79253e 100644 --- a/flight/Flight.php +++ b/flight/Flight.php @@ -9,6 +9,7 @@ use flight\net\Router; use flight\template\View; use flight\net\Route; use flight\core\EventDispatcher; +use Psr\Container\ContainerInterface; require_once __DIR__ . '/autoload.php'; @@ -18,6 +19,8 @@ require_once __DIR__ . '/autoload.php'; * @license MIT, http://flightphp.com/license * @copyright Copyright (c) 2011, Mike Cao * + * @template T of object + * * # Core methods * @method static void start() Starts the framework. * @method static void path(string $dir) Adds a path for autoloading classes. @@ -28,7 +31,7 @@ require_once __DIR__ . '/autoload.php'; * Registers a class to a framework method. * @method static void unregister(string $methodName) * Unregisters a class to a framework method. - * @method static void registerContainerHandler(callable|object $containerHandler) Registers a container handler. + * @method static void registerContainerHandler(ContainerInterface|callable(class-string $id, array $params): ?T $containerHandler) Registers a container handler. * * # Class registration * @method EventDispatcher eventDispatcher() Gets event dispatcher diff --git a/flight/commands/RouteCommand.php b/flight/commands/RouteCommand.php index a34b821..8b5408e 100644 --- a/flight/commands/RouteCommand.php +++ b/flight/commands/RouteCommand.php @@ -106,7 +106,7 @@ class RouteCommand extends AbstractBaseCommand if ($showAll === true) { $boolval = true; } else { - $methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH' ]; + $methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']; foreach ($methods as $method) { $lowercaseMethod = strtolower($method); if ( diff --git a/flight/core/Dispatcher.php b/flight/core/Dispatcher.php index 769aeef..dde5ae8 100644 --- a/flight/core/Dispatcher.php +++ b/flight/core/Dispatcher.php @@ -52,7 +52,9 @@ class Dispatcher /** * Sets the dependency injection container handler. * - * @param ContainerInterface|(callable(string $classString, array $params): (null|object)) $containerHandler + * @template T of object + * + * @param ContainerInterface|(callable(class-string $classString, array $params): ?T) $containerHandler * Dependency injection container. * * @throws InvalidArgumentException If $containerHandler is not a `callable` or instance of `Psr\Container\ContainerInterface`. diff --git a/flight/net/Request.php b/flight/net/Request.php index 5164b11..be6f3f9 100644 --- a/flight/net/Request.php +++ b/flight/net/Request.php @@ -211,8 +211,8 @@ class Request $this->data->setData($data); } } - // Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data - } elseif (in_array($this->method, [ 'PUT', 'DELETE', 'PATCH' ], true) === true) { + // Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data + } elseif (in_array($this->method, ['PUT', 'DELETE', 'PATCH'], true) === true) { $body = $this->getBody(); if ($body !== '') { $data = []; diff --git a/flight/util/ReturnTypeWillChange.php b/flight/util/ReturnTypeWillChange.php index 1eba39e..4e27e1c 100644 --- a/flight/util/ReturnTypeWillChange.php +++ b/flight/util/ReturnTypeWillChange.php @@ -5,4 +5,5 @@ declare(strict_types=1); // This file is only here so that the PHP8 attribute for doesn't throw an error in files class ReturnTypeWillChange { + // } diff --git a/tests/EngineTest.php b/tests/EngineTest.php index 737f38d..553d7d7 100644 --- a/tests/EngineTest.php +++ b/tests/EngineTest.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace tests; -use ErrorException; use Exception; use flight\database\PdoWrapper; use flight\Engine; @@ -13,7 +12,6 @@ use flight\net\Response; use flight\util\Collection; use InvalidArgumentException; use JsonException; -use PDOException; use PHPUnit\Framework\TestCase; use tests\classes\Container; use tests\classes\ContainerDefault; diff --git a/tests/named-arguments/ExampleClass.php b/tests/named-arguments/ExampleClass.php index 581551d..d91dd64 100644 --- a/tests/named-arguments/ExampleClass.php +++ b/tests/named-arguments/ExampleClass.php @@ -1,5 +1,8 @@ engine = new Engine; Flight::init(); - Flight::setEngine(new Engine()); - } - - protected function tearDown(): void - { - unset($_REQUEST); - unset($_SERVER); - Flight::clear(); + Flight::setEngine($this->engine); } ////////////////// @@ -30,7 +29,7 @@ class FlightTest extends TestCase ////////////////// public function test_path(): void { - Flight::path(path: __DIR__); + Flight::path(dir: __DIR__); $exampleObject = new ExampleClass(); self::assertInstanceOf(ExampleClass::class, $exampleObject); @@ -46,12 +45,66 @@ class FlightTest extends TestCase public function test_halt(): void { - Flight::halt(500, actuallyExit: false, message: 'Test'); + Flight::halt(actuallyExit: false, code: 500, message: 'Test'); self::expectOutputString('Test'); self::assertSame(500, Flight::response()->status()); } + public function test_register(): void + { + Flight::register( + class: stdClass::class, + name: 'customClass', + callback: static function (stdClass $object): void { + $object->property = 'value'; + }, + params: [] + ); + + $object = Flight::customClass(); + + self::assertInstanceOf(stdClass::class, $object); + self::assertObjectHasProperty('property', $object); + self::assertSame('value', $object->property); + + Flight::unregister(methodName: 'customClass'); + } + + public function test_register_container(): void + { + $dateTime = new DateTimeImmutable(); + + $controller = new class($dateTime) { + public function __construct(private DateTimeImmutable $dateTime) + { + // + } + + public function test(): void + { + echo $this->dateTime->format('Y-m-d'); + } + }; + + Flight::registerContainerHandler( + containerHandler: new Container() + ); + + Flight::request()->url = '/test'; + + Flight::route( + pass_route: true, + alias: 'testRoute', + callback: [$controller::class, 'test'], + pattern: '/test' + ); + + self::expectOutputString($dateTime->format('Y-m-d')); + + Flight::start(); + } + ///////////////////// // ROUTING METHODS // /////////////////////