Skip to content

Commit 22570b9

Browse files
authored
Merge pull request #548 from flightphp/php8-named-arguments-support
PHP 8 named arguments support
2 parents a2c74dd + 5425955 commit 22570b9

File tree

15 files changed

+192
-32
lines changed

15 files changed

+192
-32
lines changed

composer.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@
3333
},
3434
"autoload-dev": {
3535
"classmap": [
36-
"tests/classes/User.php",
37-
"tests/classes/Hello.php",
38-
"tests/classes/Factory.php",
39-
"tests/classes/TesterClass.php"
40-
]
36+
"tests/classes/"
37+
],
38+
"psr-4": {
39+
"Tests\\PHP8\\": [
40+
"tests/named-arguments"
41+
]
42+
}
4143
},
4244
"require-dev": {
4345
"ext-pdo_sqlite": "*",
46+
"flightphp/container": "^1.0",
4447
"flightphp/runway": "^0.2.3 || ^1.0",
4548
"league/container": "^4.2",
4649
"level-2/dice": "^4.0",

flight/Engine.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use flight\template\View;
1717
use Throwable;
1818
use flight\net\Route;
19+
use Psr\Container\ContainerInterface;
1920

2021
/**
2122
* The Engine class contains the core functionality of the framework.
@@ -265,9 +266,9 @@ public function handleException(Throwable $e): void
265266
/**
266267
* Registers the container handler
267268
*
268-
* @param callable|object $containerHandler Callback function or PSR-11 Container object that sets the container and how it will inject classes
269+
* @template T of object
269270
*
270-
* @return void
271+
* @param ContainerInterface|callable(class-string<T> $id, array<int|string, mixed> $params): ?T $containerHandler Callback function or PSR-11 Container object that sets the container and how it will inject classes
271272
*/
272273
public function registerContainerHandler($containerHandler): void
273274
{

flight/Flight.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use flight\template\View;
1010
use flight\net\Route;
1111
use flight\core\EventDispatcher;
12+
use Psr\Container\ContainerInterface;
1213

1314
require_once __DIR__ . '/autoload.php';
1415

@@ -18,17 +19,22 @@
1819
* @license MIT, http://flightphp.com/license
1920
* @copyright Copyright (c) 2011, Mike Cao <[email protected]>
2021
*
22+
* @template T of object
23+
*
2124
* # Core methods
2225
* @method static void start() Starts the framework.
23-
* @method static void path(string $path) Adds a path for autoloading classes.
26+
* @method static void path(string $dir) Adds a path for autoloading classes.
2427
* @method static void stop(?int $code = null) Stops the framework and sends a response.
2528
* @method static void halt(int $code = 200, string $message = '', bool $actuallyExit = true)
2629
* Stop the framework with an optional status code and message.
2730
* @method static void register(string $name, string $class, array<int, mixed> $params = [], ?callable $callback = null)
2831
* Registers a class to a framework method.
2932
* @method static void unregister(string $methodName)
3033
* Unregisters a class to a framework method.
31-
* @method static void registerContainerHandler(callable|object $containerHandler) Registers a container handler.
34+
* @method static void registerContainerHandler(ContainerInterface|callable(class-string<T> $id, array<int|string, mixed> $params): ?T $containerHandler) Registers a container handler.
35+
*
36+
* # Class registration
37+
* @method EventDispatcher eventDispatcher() Gets event dispatcher
3238
*
3339
* # Class registration
3440
* @method EventDispatcher eventDispatcher() Gets event dispatcher
@@ -104,6 +110,7 @@ class Flight
104110
*/
105111
private function __construct()
106112
{
113+
//
107114
}
108115

109116
/**
@@ -114,6 +121,7 @@ private function __construct()
114121
*/
115122
private function __clone()
116123
{
124+
//
117125
}
118126

119127
/**

flight/commands/RouteCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function shouldAddRoute(Route $route)
106106
if ($showAll === true) {
107107
$boolval = true;
108108
} else {
109-
$methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH' ];
109+
$methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
110110
foreach ($methods as $method) {
111111
$lowercaseMethod = strtolower($method);
112112
if (

flight/core/Dispatcher.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ class Dispatcher
5252
/**
5353
* Sets the dependency injection container handler.
5454
*
55-
* @param ContainerInterface|(callable(string $classString, array<int, mixed> $params): (null|object)) $containerHandler
55+
* @template T of object
56+
*
57+
* @param ContainerInterface|(callable(class-string<T> $classString, array<int, mixed> $params): ?T) $containerHandler
5658
* Dependency injection container.
5759
*
5860
* @throws InvalidArgumentException If $containerHandler is not a `callable` or instance of `Psr\Container\ContainerInterface`.
@@ -231,7 +233,7 @@ public function hook(string $name, string $type, callable $callback): self
231233

232234
if ($parametersNumber === 1) {
233235
/** @disregard &$params in after filters are deprecated. */
234-
$callback = fn (array &$params, &$output) => $callback($output);
236+
$callback = fn(array &$params, &$output) => $callback($output);
235237
}
236238
}
237239

@@ -437,11 +439,12 @@ protected function verifyValidClassCallable($class, $method, $resolvedClass): vo
437439
public function resolveContainerClass(string $class, array &$params)
438440
{
439441
// PSR-11
440-
if (
441-
is_a($this->containerHandler, '\Psr\Container\ContainerInterface')
442-
&& $this->containerHandler->has($class)
443-
) {
444-
return $this->containerHandler->get($class);
442+
if (is_a($this->containerHandler, '\Psr\Container\ContainerInterface')) {
443+
try {
444+
return $this->containerHandler->get($class);
445+
} catch (Throwable $exception) {
446+
return null;
447+
}
445448
}
446449

447450
// Just a callable where you configure the behavior (Dice, PHP-DI, etc.)

flight/net/Request.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ public function init(array $properties = []): self
211211
$this->data->setData($data);
212212
}
213213
}
214-
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data
215-
} elseif (in_array($this->method, [ 'PUT', 'DELETE', 'PATCH' ], true) === true) {
214+
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data
215+
} elseif (in_array($this->method, ['PUT', 'DELETE', 'PATCH'], true) === true) {
216216
$body = $this->getBody();
217217
if ($body !== '') {
218218
$data = [];

flight/util/ReturnTypeWillChange.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
// This file is only here so that the PHP8 attribute for doesn't throw an error in files
66
class ReturnTypeWillChange
77
{
8+
//
89
}

phpstan.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parameters:
77
excludePaths:
88
- vendor
99
- flight/util/ReturnTypeWillChange.php
10+
- tests/named-arguments
1011
paths:
1112
- flight
1213
- index.php

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<testsuites>
2323
<testsuite name="default">
2424
<directory>tests/</directory>
25+
<exclude>tests/named-arguments/</exclude>
2526
</testsuite>
2627
</testsuites>
2728
<logging />

tests/EngineTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace tests;
66

7-
use ErrorException;
87
use Exception;
98
use flight\database\PdoWrapper;
109
use flight\Engine;

0 commit comments

Comments
 (0)