From 8c4d4fccbc4f6423a7f350c754cc186257e8f124 Mon Sep 17 00:00:00 2001 From: Guillaume Sainthillier Date: Mon, 10 Mar 2025 15:22:31 +0100 Subject: [PATCH 1/2] add typehint to every properties --- src/bref/bref/Context.php | 25 ++++------- src/bref/src/BrefRunner.php | 7 +--- src/bref/src/ConsoleApplicationHandler.php | 2 +- src/bref/src/ConsoleApplicationRunner.php | 6 +-- src/bref/src/Lambda/ContextBuilder.php | 12 ++---- src/bref/src/Lambda/LambdaClient.php | 9 +--- src/bref/src/LaravelHttpHandler.php | 2 +- src/bref/src/LocalRunner.php | 7 +--- src/bref/src/SymfonyHttpHandler.php | 6 +-- src/bref/tests/Lambda/LambdaClientTest.php | 3 +- src/google-cloud/google/CloudEvent.php | 42 +++++-------------- src/google-cloud/google/Context.php | 17 +------- src/google-cloud/google/LegacyEventMapper.php | 8 ++-- src/google-cloud/src/Runtime.php | 2 +- src/laravel/src/ConsoleApplicationRunner.php | 9 +--- src/laravel/src/HttpKernelRunner.php | 4 +- src/psr-17/src/Runtime.php | 5 +-- src/psr-laminas/src/Emitter.php | 9 ++-- src/psr-nyholm-laminas/src/LaminasEmitter.php | 9 ++-- src/psr-nyholm-laminas/src/Runtime.php | 5 +-- src/psr-nyholm/src/Emitter.php | 6 +-- src/psr-nyholm/src/Runtime.php | 5 +-- src/reactphp/src/Runner.php | 4 +- src/roadrunner-symfony-nyholm/src/Runner.php | 10 ++--- src/swoole-nyholm/src/CallableRunner.php | 5 +-- .../src/RequestHandlerRunner.php | 15 ++----- src/swoole-nyholm/src/Runtime.php | 3 +- src/swoole-nyholm/src/ServerFactory.php | 3 +- src/swoole/src/CallableRunner.php | 5 +-- src/swoole/src/LaravelRunner.php | 8 +--- src/swoole/src/Runtime.php | 3 +- src/swoole/src/ServerFactory.php | 3 +- src/swoole/src/SymfonyRunner.php | 9 +--- 33 files changed, 75 insertions(+), 193 deletions(-) diff --git a/src/bref/bref/Context.php b/src/bref/bref/Context.php index 678945e..0404efe 100644 --- a/src/bref/bref/Context.php +++ b/src/bref/bref/Context.php @@ -11,24 +11,13 @@ */ final class Context implements \JsonSerializable { - /** @var string */ - private $awsRequestId; - - /** @var int Holds the deadline Unix timestamp in millis */ - private $deadlineMs; - - /** @var string */ - private $invokedFunctionArn; - - /** @var string */ - private $traceId; - - public function __construct(string $awsRequestId, int $deadlineMs, string $invokedFunctionArn, string $traceId) - { - $this->awsRequestId = $awsRequestId; - $this->deadlineMs = $deadlineMs; - $this->invokedFunctionArn = $invokedFunctionArn; - $this->traceId = $traceId; + public function __construct( + private string $awsRequestId, + /** @var int Holds the deadline Unix timestamp in millis */ + private int $deadlineMs, + private string $invokedFunctionArn, + private string $traceId + ) { } /** diff --git a/src/bref/src/BrefRunner.php b/src/bref/src/BrefRunner.php index fa944bb..90ff6dd 100644 --- a/src/bref/src/BrefRunner.php +++ b/src/bref/src/BrefRunner.php @@ -13,13 +13,8 @@ */ class BrefRunner implements RunnerInterface { - private $handler; - private $loopMax; - - public function __construct(Handler $handler, int $loopMax) + public function __construct(private Handler $handler, private int $loopMax) { - $this->handler = $handler; - $this->loopMax = $loopMax; } public function run(): int diff --git a/src/bref/src/ConsoleApplicationHandler.php b/src/bref/src/ConsoleApplicationHandler.php index fe2af97..0b6f54a 100644 --- a/src/bref/src/ConsoleApplicationHandler.php +++ b/src/bref/src/ConsoleApplicationHandler.php @@ -15,7 +15,7 @@ */ class ConsoleApplicationHandler implements Handler { - private $application; + private Application $application; public function __construct(Application $application) { diff --git a/src/bref/src/ConsoleApplicationRunner.php b/src/bref/src/ConsoleApplicationRunner.php index 3a87837..8c977f8 100644 --- a/src/bref/src/ConsoleApplicationRunner.php +++ b/src/bref/src/ConsoleApplicationRunner.php @@ -13,13 +13,11 @@ */ class ConsoleApplicationRunner implements RunnerInterface { - private $handler; - private $loopMax; + private ConsoleApplicationHandler $handler; - public function __construct(Application $application, int $loopMax = 1) + public function __construct(Application $application, private int $loopMax = 1) { $this->handler = new ConsoleApplicationHandler($application); - $this->loopMax = $loopMax; } public function run(): int diff --git a/src/bref/src/Lambda/ContextBuilder.php b/src/bref/src/Lambda/ContextBuilder.php index b247a5c..9fa48a0 100644 --- a/src/bref/src/Lambda/ContextBuilder.php +++ b/src/bref/src/Lambda/ContextBuilder.php @@ -9,17 +9,13 @@ */ final class ContextBuilder { - /** @var string */ - private $awsRequestId; + private string $awsRequestId; - /** @var int */ - private $deadlineMs; + private int $deadlineMs; - /** @var string */ - private $invokedFunctionArn; + private string $invokedFunctionArn; - /** @var string */ - private $traceId; + private string $traceId; public function __construct() { diff --git a/src/bref/src/Lambda/LambdaClient.php b/src/bref/src/Lambda/LambdaClient.php index e973a7c..8a886c2 100755 --- a/src/bref/src/Lambda/LambdaClient.php +++ b/src/bref/src/Lambda/LambdaClient.php @@ -37,25 +37,20 @@ final class LambdaClient /** @var resource|\CurlHandle|null */ private $returnHandler; - /** @var string */ - private $apiUrl; - - /** @var string */ - private $layer; + private string $apiUrl; public static function fromEnvironmentVariable(string $layer): self { return new self((string) getenv('AWS_LAMBDA_RUNTIME_API'), $layer); } - public function __construct(string $apiUrl, string $layer) + public function __construct(string $apiUrl, private string $layer) { if ('' === $apiUrl) { exit('At the moment lambdas can only be executed in an Lambda environment'); } $this->apiUrl = $apiUrl; - $this->layer = $layer; } public function __destruct() diff --git a/src/bref/src/LaravelHttpHandler.php b/src/bref/src/LaravelHttpHandler.php index df702da..e0d6fdc 100644 --- a/src/bref/src/LaravelHttpHandler.php +++ b/src/bref/src/LaravelHttpHandler.php @@ -16,7 +16,7 @@ */ class LaravelHttpHandler extends HttpHandler { - private $kernel; + private Kernel $kernel; public function __construct(Kernel $kernel) { diff --git a/src/bref/src/LocalRunner.php b/src/bref/src/LocalRunner.php index e5e1bc0..cd62db7 100644 --- a/src/bref/src/LocalRunner.php +++ b/src/bref/src/LocalRunner.php @@ -13,13 +13,8 @@ */ class LocalRunner implements RunnerInterface { - private $handler; - private $data; - - public function __construct(Handler $handler, $data) + public function __construct(private Handler $handler, private mixed $data) { - $this->handler = $handler; - $this->data = $data; } public function run(): int diff --git a/src/bref/src/SymfonyHttpHandler.php b/src/bref/src/SymfonyHttpHandler.php index def4289..43742a9 100644 --- a/src/bref/src/SymfonyHttpHandler.php +++ b/src/bref/src/SymfonyHttpHandler.php @@ -17,12 +17,8 @@ */ class SymfonyHttpHandler extends HttpHandler { - private $kernel; - - public function __construct(HttpKernelInterface $kernel) + public function __construct(private HttpKernelInterface $kernel) { - $this->kernel = $kernel; - Request::setTrustedProxies(['127.0.0.1'], Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO); } diff --git a/src/bref/tests/Lambda/LambdaClientTest.php b/src/bref/tests/Lambda/LambdaClientTest.php index 37d80cd..87bc607 100644 --- a/src/bref/tests/Lambda/LambdaClientTest.php +++ b/src/bref/tests/Lambda/LambdaClientTest.php @@ -16,8 +16,7 @@ */ class LambdaClientTest extends TestCase { - /** @var LambdaClient */ - private $lambda; + private LambdaClient $lambda; protected function setUp(): void { diff --git a/src/google-cloud/google/CloudEvent.php b/src/google-cloud/google/CloudEvent.php index e2820c7..e9c8cd7 100644 --- a/src/google-cloud/google/CloudEvent.php +++ b/src/google-cloud/google/CloudEvent.php @@ -20,39 +20,19 @@ class CloudEvent implements \JsonSerializable { - // Required Fields - private $id; - private $source; - private $specversion; - private $type; - - // Optional Fields - private $datacontenttype; - private $dataschema; - private $subject; - private $time; - private $data; - final public function __construct( - string $id, - string $source, - string $specversion, - string $type, - ?string $datacontenttype, - ?string $dataschema, - ?string $subject, - ?string $time, - $data, + // Required Fields + private string $id, + private string $source, + private string $specversion, + private string $type, + // Optional Fields + private ?string $datacontenttype, + private ?string $dataschema, + private ?string $subject, + private ?string $time, + private mixed $data ) { - $this->id = $id; - $this->source = $source; - $this->specversion = $specversion; - $this->type = $type; - $this->datacontenttype = $datacontenttype; - $this->dataschema = $dataschema; - $this->subject = $subject; - $this->time = $time; - $this->data = $data; } public function getId(): string diff --git a/src/google-cloud/google/Context.php b/src/google-cloud/google/Context.php index a462fbc..88f65f6 100644 --- a/src/google-cloud/google/Context.php +++ b/src/google-cloud/google/Context.php @@ -20,21 +20,8 @@ class Context { - private $eventId; - private $timestamp; - private $eventType; - private $resource; - - final public function __construct( - ?string $eventId, - ?string $timestamp, - ?string $eventType, - ?array $resource, - ) { - $this->eventId = $eventId; - $this->timestamp = $timestamp; - $this->eventType = $eventType; - $this->resource = $resource; + final public function __construct(private ?string $eventId, private ?string $timestamp, private ?string $eventType, private ?array $resource) + { } public function getEventId(): ?string diff --git a/src/google-cloud/google/LegacyEventMapper.php b/src/google-cloud/google/LegacyEventMapper.php index 264819d..31b040e 100644 --- a/src/google-cloud/google/LegacyEventMapper.php +++ b/src/google-cloud/google/LegacyEventMapper.php @@ -23,7 +23,7 @@ class LegacyEventMapper // Maps background/legacy event types to their equivalent CloudEvent types. // For more info on event mappings see // https://github.com/GoogleCloudPlatform/functions-framework-conformance/blob/master/docs/mapping.md - private static $ceTypeMap = [ + private static array $ceTypeMap = [ 'google.pubsub.topic.publish' => 'google.cloud.pubsub.topic.v1.messagePublished', 'providers/cloud.pubsub/eventTypes/topic.publish' => 'google.cloud.pubsub.topic.v1.messagePublished', 'google.storage.object.finalize' => 'google.cloud.storage.object.v1.finalized', @@ -53,7 +53,7 @@ class LegacyEventMapper private const STORAGE_CE_SERVICE = 'storage.googleapis.com'; // Maps background event services to their equivalent CloudEvent services. - private static $ceServiceMap = [ + private static array $ceServiceMap = [ 'providers/cloud.firestore/' => self::FIRESTORE_CE_SERVICE, 'providers/google.firebase.analytics/' => self::FIREBASE_CE_SERVICE, 'providers/firebase.auth/' => self::FIREBASE_AUTH_CE_SERVICE, @@ -66,7 +66,7 @@ class LegacyEventMapper // event resource string into CloudEvent resource and subject strings. Each regex // must have exactly two capture groups: the first for the resource and the second // for the subject. - private static $ceResourceRegexMap = [ + private static array $ceResourceRegexMap = [ self::FIREBASE_CE_SERVICE => '#^(projects/[^/]+)/(events/[^/]+)$#', self::FIREBASE_DB_CE_SERVICE => '#^(projects/_/instances/[^/]+)/(refs/.+)$#', self::FIRESTORE_CE_SERVICE => '#^(projects/[^/]+/databases/\(default\))/(documents/.+)$#', @@ -75,7 +75,7 @@ class LegacyEventMapper // Maps Firebase Auth background event metadata field names to their equivalent // CloudEvent field names. - private static $firebaseAuthMetadataFieldMap = [ + private static array $firebaseAuthMetadataFieldMap = [ 'createdAt' => 'createTime', 'lastSignedInAt' => 'lastSignInTime', ]; diff --git a/src/google-cloud/src/Runtime.php b/src/google-cloud/src/Runtime.php index f8a0a91..850b5e4 100644 --- a/src/google-cloud/src/Runtime.php +++ b/src/google-cloud/src/Runtime.php @@ -18,7 +18,7 @@ class Runtime extends GenericRuntime // from this list because the header 'ce-datacontenttype' is not permitted; // that data comes from the 'Content-Type' header instead. For more info see // https://github.com/cloudevents/spec/blob/v1.0.1/http-protocol-binding.md#311-http-content-type - private static $binaryModeHeaderAttrs = [ + private static array $binaryModeHeaderAttrs = [ 'id', 'source', 'specversion', diff --git a/src/laravel/src/ConsoleApplicationRunner.php b/src/laravel/src/ConsoleApplicationRunner.php index 4fb8eb2..a9e3194 100644 --- a/src/laravel/src/ConsoleApplicationRunner.php +++ b/src/laravel/src/ConsoleApplicationRunner.php @@ -12,15 +12,8 @@ */ class ConsoleApplicationRunner implements RunnerInterface { - private $application; - private $input; - private $output; - - public function __construct(ConsoleKernel $application, InputInterface $input, ?OutputInterface $output = null) + public function __construct(private ConsoleKernel $application, private InputInterface $input, private ?OutputInterface $output = null) { - $this->application = $application; - $this->input = $input; - $this->output = $output; } public function run(): int diff --git a/src/laravel/src/HttpKernelRunner.php b/src/laravel/src/HttpKernelRunner.php index 214c8e1..1b1e92a 100644 --- a/src/laravel/src/HttpKernelRunner.php +++ b/src/laravel/src/HttpKernelRunner.php @@ -8,8 +8,8 @@ class HttpKernelRunner implements RunnerInterface { - private $kernel; - private $request; + private Kernel $kernel; + private Request $request; public function __construct(Kernel $kernel, Request $request) { diff --git a/src/psr-17/src/Runtime.php b/src/psr-17/src/Runtime.php index b812b36..fea17bf 100644 --- a/src/psr-17/src/Runtime.php +++ b/src/psr-17/src/Runtime.php @@ -16,10 +16,7 @@ */ class Runtime extends GenericRuntime { - /** - * @var ServerRequestCreator|null - */ - private $requestCreator; + private ?ServerRequestCreator $requestCreator = null; /** * @param array{ diff --git a/src/psr-laminas/src/Emitter.php b/src/psr-laminas/src/Emitter.php index ba20634..ab1fef9 100644 --- a/src/psr-laminas/src/Emitter.php +++ b/src/psr-laminas/src/Emitter.php @@ -2,6 +2,7 @@ namespace Runtime\PsrLaminas; +use Laminas\HttpHandlerRunner\Emitter\EmitterInterface; use Laminas\HttpHandlerRunner\Emitter\SapiEmitter; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -13,10 +14,10 @@ */ class Emitter implements RunnerInterface { - private $requestHandler; - private $response; - private $request; - private $emitter; + private ?RequestHandlerInterface $requestHandler = null; + private ?ResponseInterface $response = null; + private ?ServerRequestInterface $request = null; + private EmitterInterface $emitter; private function __construct(array $options) { diff --git a/src/psr-nyholm-laminas/src/LaminasEmitter.php b/src/psr-nyholm-laminas/src/LaminasEmitter.php index 8539197..43ef33b 100644 --- a/src/psr-nyholm-laminas/src/LaminasEmitter.php +++ b/src/psr-nyholm-laminas/src/LaminasEmitter.php @@ -2,6 +2,7 @@ namespace Runtime\PsrNyholmLaminas; +use Laminas\HttpHandlerRunner\Emitter\EmitterInterface; use Laminas\HttpHandlerRunner\Emitter\SapiEmitter; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -13,10 +14,10 @@ */ class LaminasEmitter implements RunnerInterface { - private $requestHandler; - private $response; - private $request; - private $emitter; + private ?RequestHandlerInterface $requestHandler = null; + private ?ResponseInterface $response = null; + private ?ServerRequestInterface $request = null; + private EmitterInterface $emitter; private function __construct(array $options) { diff --git a/src/psr-nyholm-laminas/src/Runtime.php b/src/psr-nyholm-laminas/src/Runtime.php index 2f4d197..90e5e0f 100644 --- a/src/psr-nyholm-laminas/src/Runtime.php +++ b/src/psr-nyholm-laminas/src/Runtime.php @@ -17,10 +17,7 @@ */ class Runtime extends GenericRuntime { - /** - * @var ServerRequestCreator|null - */ - private $requestCreator; + private ?ServerRequestCreator $requestCreator = null; /** * @param array{ diff --git a/src/psr-nyholm/src/Emitter.php b/src/psr-nyholm/src/Emitter.php index 908965c..f9706e7 100644 --- a/src/psr-nyholm/src/Emitter.php +++ b/src/psr-nyholm/src/Emitter.php @@ -12,9 +12,9 @@ */ class Emitter implements RunnerInterface { - private $requestHandler; - private $response; - private $request; + private ?RequestHandlerInterface $requestHandler = null; + private ?ResponseInterface $response = null; + private ?ServerRequestInterface $request = null; private function __construct() { diff --git a/src/psr-nyholm/src/Runtime.php b/src/psr-nyholm/src/Runtime.php index 1048910..ddb8827 100644 --- a/src/psr-nyholm/src/Runtime.php +++ b/src/psr-nyholm/src/Runtime.php @@ -17,10 +17,7 @@ */ class Runtime extends GenericRuntime { - /** - * @var ServerRequestCreator|null - */ - private $requestCreator; + private ?ServerRequestCreator $requestCreator = null; public function getRunner(?object $application): RunnerInterface { diff --git a/src/reactphp/src/Runner.php b/src/reactphp/src/Runner.php index 2143c12..3109c8d 100644 --- a/src/reactphp/src/Runner.php +++ b/src/reactphp/src/Runner.php @@ -8,11 +8,9 @@ class Runner implements RunnerInterface { private RequestHandlerInterface $application; - private ServerFactory $serverFactory; - public function __construct(ServerFactory $serverFactory, RequestHandlerInterface $application) + public function __construct(private ServerFactory $serverFactory, RequestHandlerInterface $application) { - $this->serverFactory = $serverFactory; $this->application = $application; } diff --git a/src/roadrunner-symfony-nyholm/src/Runner.php b/src/roadrunner-symfony-nyholm/src/Runner.php index 3ae36ca..31d8e3e 100644 --- a/src/roadrunner-symfony-nyholm/src/Runner.php +++ b/src/roadrunner-symfony-nyholm/src/Runner.php @@ -17,14 +17,12 @@ */ class Runner implements RunnerInterface { - private $kernel; - private $httpFoundationFactory; - private $httpMessageFactory; - private $psrFactory; + private HttpFoundationFactoryInterface $httpFoundationFactory; + private HttpMessageFactoryInterface $httpMessageFactory; + private Psr7\Factory\Psr17Factory $psrFactory; - public function __construct(HttpKernelInterface $kernel, ?HttpFoundationFactoryInterface $httpFoundationFactory = null, ?HttpMessageFactoryInterface $httpMessageFactory = null) + public function __construct(private HttpKernelInterface $kernel, ?HttpFoundationFactoryInterface $httpFoundationFactory = null, ?HttpMessageFactoryInterface $httpMessageFactory = null) { - $this->kernel = $kernel; $this->psrFactory = new Psr7\Factory\Psr17Factory(); $this->httpFoundationFactory = $httpFoundationFactory ?? new HttpFoundationFactory(); $this->httpMessageFactory = $httpMessageFactory ?? new PsrHttpFactory($this->psrFactory, $this->psrFactory, $this->psrFactory, $this->psrFactory); diff --git a/src/swoole-nyholm/src/CallableRunner.php b/src/swoole-nyholm/src/CallableRunner.php index d01bb59..e1cf517 100644 --- a/src/swoole-nyholm/src/CallableRunner.php +++ b/src/swoole-nyholm/src/CallableRunner.php @@ -11,14 +11,11 @@ */ class CallableRunner implements RunnerInterface { - /** @var ServerFactory */ - private $serverFactory; /** @var callable */ private $application; - public function __construct(ServerFactory $serverFactory, callable $application) + public function __construct(private ServerFactory $serverFactory, callable $application) { - $this->serverFactory = $serverFactory; $this->application = $application; } diff --git a/src/swoole-nyholm/src/RequestHandlerRunner.php b/src/swoole-nyholm/src/RequestHandlerRunner.php index 805470a..119efc5 100644 --- a/src/swoole-nyholm/src/RequestHandlerRunner.php +++ b/src/swoole-nyholm/src/RequestHandlerRunner.php @@ -9,21 +9,12 @@ class RequestHandlerRunner implements RunnerInterface { - private const CHUNK_SIZE = 2097152; // 2MB + private const CHUNK_SIZE = 2097152; - /** - * @var ServerFactory - */ - private $serverFactory; + private RequestHandlerInterface $application; - /** - * @var RequestHandlerInterface - */ - private $application; - - public function __construct(ServerFactory $serverFactory, RequestHandlerInterface $application) + public function __construct(private ServerFactory $serverFactory, RequestHandlerInterface $application) { - $this->serverFactory = $serverFactory; $this->application = $application; } diff --git a/src/swoole-nyholm/src/Runtime.php b/src/swoole-nyholm/src/Runtime.php index bef510d..63d570c 100644 --- a/src/swoole-nyholm/src/Runtime.php +++ b/src/swoole-nyholm/src/Runtime.php @@ -13,8 +13,7 @@ */ class Runtime extends SymfonyRuntime { - /** @var ?ServerFactory */ - private $serverFactory; + private ?ServerFactory $serverFactory; public function __construct(array $options, ?ServerFactory $serverFactory = null) { diff --git a/src/swoole-nyholm/src/ServerFactory.php b/src/swoole-nyholm/src/ServerFactory.php index d2c817b..ce0a8d9 100644 --- a/src/swoole-nyholm/src/ServerFactory.php +++ b/src/swoole-nyholm/src/ServerFactory.php @@ -19,8 +19,7 @@ class ServerFactory 'settings' => [], ]; - /** @var array */ - private $options; + private array $options; public static function getDefaultOptions(): array { diff --git a/src/swoole/src/CallableRunner.php b/src/swoole/src/CallableRunner.php index 8584d01..2ca9845 100644 --- a/src/swoole/src/CallableRunner.php +++ b/src/swoole/src/CallableRunner.php @@ -11,14 +11,11 @@ */ class CallableRunner implements RunnerInterface { - /** @var ServerFactory */ - private $serverFactory; /** @var callable */ private $application; - public function __construct(ServerFactory $serverFactory, callable $application) + public function __construct(private ServerFactory $serverFactory, callable $application) { - $this->serverFactory = $serverFactory; $this->application = $application; } diff --git a/src/swoole/src/LaravelRunner.php b/src/swoole/src/LaravelRunner.php index 12a0a3f..c79c2b5 100644 --- a/src/swoole/src/LaravelRunner.php +++ b/src/swoole/src/LaravelRunner.php @@ -15,14 +15,10 @@ */ class LaravelRunner implements RunnerInterface { - /** @var ServerFactory */ - private $serverFactory; - /** @var Kernel */ - private $application; + private Kernel $application; - public function __construct(ServerFactory $serverFactory, Kernel $application) + public function __construct(private ServerFactory $serverFactory, Kernel $application) { - $this->serverFactory = $serverFactory; $this->application = $application; } diff --git a/src/swoole/src/Runtime.php b/src/swoole/src/Runtime.php index 38c9aff..428515d 100644 --- a/src/swoole/src/Runtime.php +++ b/src/swoole/src/Runtime.php @@ -14,8 +14,7 @@ */ class Runtime extends SymfonyRuntime { - /** @var ?ServerFactory */ - private $serverFactory; + private ?ServerFactory $serverFactory; public function __construct(array $options, ?ServerFactory $serverFactory = null) { diff --git a/src/swoole/src/ServerFactory.php b/src/swoole/src/ServerFactory.php index 18c08ed..3b772b6 100644 --- a/src/swoole/src/ServerFactory.php +++ b/src/swoole/src/ServerFactory.php @@ -19,8 +19,7 @@ class ServerFactory 'settings' => [], ]; - /** @var array */ - private $options; + private array $options; public static function getDefaultOptions(): array { diff --git a/src/swoole/src/SymfonyRunner.php b/src/swoole/src/SymfonyRunner.php index 352ff75..514db22 100644 --- a/src/swoole/src/SymfonyRunner.php +++ b/src/swoole/src/SymfonyRunner.php @@ -15,15 +15,8 @@ */ class SymfonyRunner implements RunnerInterface { - /** @var ServerFactory */ - private $serverFactory; - /** @var HttpKernelInterface */ - private $application; - - public function __construct(ServerFactory $serverFactory, HttpKernelInterface $application) + public function __construct(private ServerFactory $serverFactory, private HttpKernelInterface $application) { - $this->serverFactory = $serverFactory; - $this->application = $application; } public function run(): int From 8277aac7d22f61f865f68ad608868d11b92f2f7f Mon Sep 17 00:00:00 2001 From: Guillaume Sainthillier Date: Mon, 10 Mar 2025 15:28:56 +0100 Subject: [PATCH 2/2] add return method value types --- src/bref/src/ConsoleApplicationHandler.php | 2 +- src/bref/src/Runtime.php | 2 +- src/bref/tests/Lambda/LambdaClientTest.php | 32 +++++++++---------- src/bref/tests/SymfonyRequestBridgeTest.php | 12 +++---- src/google-cloud/google/CloudEvent.php | 2 +- src/google-cloud/google/Context.php | 2 +- src/google-cloud/src/Runtime.php | 2 +- .../tests/google/CloudEventTest.php | 2 +- src/google-cloud/tests/google/ContextTest.php | 4 +-- .../tests/google/LegacyEventMapperTest.php | 10 +++--- .../tests/runtime/AutoDiscoverTest.php | 2 +- .../tests/runtime/RuntimeTest.php | 17 +++++----- src/psr-17/src/Runtime.php | 5 +-- src/psr-17/tests/AutoDiscoverTest.php | 2 +- src/psr-guzzle/tests/AutoDiscoverTest.php | 2 +- src/psr-laminas/tests/AutoDiscoverTest.php | 2 +- src/psr-nyholm-laminas/src/Runtime.php | 5 +-- .../tests/AutoDiscoverTest.php | 2 +- src/psr-nyholm/src/Runtime.php | 5 +-- src/psr-nyholm/tests/AutoDiscoverTest.php | 2 +- src/reactphp/tests/AutoDiscoverTest.php | 2 +- 21 files changed, 54 insertions(+), 62 deletions(-) diff --git a/src/bref/src/ConsoleApplicationHandler.php b/src/bref/src/ConsoleApplicationHandler.php index 0b6f54a..bae5f5b 100644 --- a/src/bref/src/ConsoleApplicationHandler.php +++ b/src/bref/src/ConsoleApplicationHandler.php @@ -23,7 +23,7 @@ public function __construct(Application $application) $this->application->setAutoExit(false); } - public function handle($event, Context $context) + public function handle($event, Context $context): array { $args = \Clue\Arguments\split((string) $event); array_unshift($args, 'command'); diff --git a/src/bref/src/Runtime.php b/src/bref/src/Runtime.php index 1e532f4..ef81521 100644 --- a/src/bref/src/Runtime.php +++ b/src/bref/src/Runtime.php @@ -46,7 +46,7 @@ public function getRunner(?object $application): RunnerInterface } } - private function tryToFindRunner(?object $application) + private function tryToFindRunner(?object $application): RunnerInterface { if ($application instanceof ContainerInterface) { $handler = explode(':', $_SERVER['_HANDLER']); diff --git a/src/bref/tests/Lambda/LambdaClientTest.php b/src/bref/tests/Lambda/LambdaClientTest.php index 87bc607..ecd689a 100644 --- a/src/bref/tests/Lambda/LambdaClientTest.php +++ b/src/bref/tests/Lambda/LambdaClientTest.php @@ -31,12 +31,12 @@ protected function tearDown(): void ob_end_clean(); } - public function test basic behavior() + public function test basic behavior(): void { $this->givenAnEvent(['Hello' => 'world!']); $output = $this->lambda->processNextEvent(new class implements Handler { - public function handle($event, Context $context) + public function handle($event, Context $context): array { return ['hello' => 'world']; } @@ -46,12 +46,12 @@ public function handle($event, Context $context) $this->assertInvocationResult(['hello' => 'world']); } - public function test handler receives context() + public function test handler receives context(): void { $this->givenAnEvent(['Hello' => 'world!']); $this->lambda->processNextEvent(new class implements Handler { - public function handle($event, Context $context) + public function handle($event, Context $context): array { return ['hello' => 'world', 'received-function-arn' => $context->getInvokedFunctionArn()]; } @@ -63,7 +63,7 @@ public function handle($event, Context $context) ]); } - public function test exceptions in the handler result in an invocation error() + public function test exceptions in the handler result in an invocation error(): void { $this->givenAnEvent(['Hello' => 'world!']); @@ -79,7 +79,7 @@ public function handle($event, Context $context) $this->assertErrorInLogs('RuntimeException', 'This is an exception'); } - public function test nested exceptions in the handler result in an invocation error() + public function test nested exceptions in the handler result in an invocation error(): void { $this->givenAnEvent(['Hello' => 'world!']); @@ -98,7 +98,7 @@ public function handle($event, Context $context) ]); } - public function test an error is thrown if the runtime API returns a wrong response() + public function test an error is thrown if the runtime API returns a wrong response(): void { $this->expectExceptionMessage('Failed to fetch next Lambda invocation: The requested URL returned error: 404'); Server::enqueue([ @@ -118,7 +118,7 @@ public function handle($event, Context $context) }); } - public function test an error is thrown if the invocation id is missing() + public function test an error is thrown if the invocation id is missing(): void { $this->expectExceptionMessage('Failed to determine the Lambda invocation ID'); Server::enqueue([ @@ -136,7 +136,7 @@ public function handle($event, Context $context) }); } - public function test an error is thrown if the invocation body is empty() + public function test an error is thrown if the invocation body is empty(): void { $this->expectExceptionMessage('Empty Lambda runtime API response'); Server::enqueue([ @@ -155,7 +155,7 @@ public function handle($event, Context $context) }); } - public function test a wrong response from the runtime API turns the invocation into an error() + public function test a wrong response from the runtime API turns the invocation into an error(): void { Server::enqueue([ new Response( // lambda event @@ -193,12 +193,12 @@ public function handle($event, Context $context) $this->assertErrorInLogs('Exception', 'Error while calling the Lambda runtime API: The requested URL returned error: 400'); } - public function test function results that cannot be encoded are reported as invocation errors() + public function test function results that cannot be encoded are reported as invocation errors(): void { $this->givenAnEvent(['hello' => 'world!']); $this->lambda->processNextEvent(new class implements Handler { - public function handle($event, Context $context) + public function handle($event, Context $context): string { return "\xB1\x31"; } @@ -213,7 +213,7 @@ public function handle($event, Context $context) $this->assertErrorInLogs('Exception', $message); } - public function test generic event handler() + public function test generic event handler(): void { $handler = new class implements Handler { public function handle($event, Context $context) @@ -244,7 +244,7 @@ private function givenAnEvent($event): void ]); } - private function assertInvocationResult($result) + private function assertInvocationResult($result): void { $requests = Server::received(); $this->assertCount(2, $requests); @@ -257,7 +257,7 @@ private function assertInvocationResult($result) $this->assertEquals($result, json_decode($eventResponse->getBody()->__toString(), true)); } - private function assertInvocationErrorResult(string $errorClass, string $errorMessage) + private function assertInvocationErrorResult(string $errorClass, string $errorMessage): void { $requests = Server::received(); $this->assertCount(2, $requests); @@ -304,7 +304,7 @@ private function assertErrorInLogs(string $errorClass, string $errorMessage): vo $this->assertIsArray($invocationResult['stack']); } - private function assertPreviousErrorsInLogs(array $previousErrors) + private function assertPreviousErrorsInLogs(array $previousErrors): void { // Decode the logs from stdout $stdout = $this->getActualOutput(); diff --git a/src/bref/tests/SymfonyRequestBridgeTest.php b/src/bref/tests/SymfonyRequestBridgeTest.php index 4943724..600b98a 100644 --- a/src/bref/tests/SymfonyRequestBridgeTest.php +++ b/src/bref/tests/SymfonyRequestBridgeTest.php @@ -11,7 +11,7 @@ class SymfonyRequestBridgeTest extends TestCase { - public function testClientIpFromForwardedFor() + public function testClientIpFromForwardedFor(): void { $request = SymfonyRequestBridge::convertRequest(new HttpRequestEvent([ 'requestContext' => ['http' => ['method' => 'GET']], @@ -23,7 +23,7 @@ public function testClientIpFromForwardedFor() /** * Raw content should only exist when there is no multipart content. */ - public function testRawContent() + public function testRawContent(): void { // No content type $request = SymfonyRequestBridge::convertRequest(new HttpRequestEvent([ @@ -67,7 +67,7 @@ public function testRawContent() $this->assertSame('', $request->getContent()); } - public function testUploadedFile() + public function testUploadedFile(): void { $request = SymfonyRequestBridge::convertRequest(new HttpRequestEvent([ 'requestContext' => ['http' => ['method' => 'POST']], @@ -107,7 +107,7 @@ public function testUploadedFile() $this->assertSame('bar', $post['foo']); } - public function testEmptyUploadedFile() + public function testEmptyUploadedFile(): void { $request = SymfonyRequestBridge::convertRequest(new HttpRequestEvent([ 'requestContext' => ['http' => ['method' => 'POST']], @@ -136,7 +136,7 @@ public function testEmptyUploadedFile() $this->assertSame('bar', $post['foo']); } - public function testLambdaContext() + public function testLambdaContext(): void { $requestContext = ['http' => ['method' => 'GET']]; $request = SymfonyRequestBridge::convertRequest(new HttpRequestEvent([ @@ -149,7 +149,7 @@ public function testLambdaContext() $this->assertSame(json_encode($requestContext), $request->server->get('LAMBDA_REQUEST_CONTEXT')); } - private function getContext() + private function getContext(): Context { // this is set in LaravelHttpHandler and SymfonyHttpHandler to allow overwrite of this value Request::setTrustedProxies(['127.0.0.1'], Request::HEADER_X_FORWARDED_FOR | Request::HEADER_X_FORWARDED_HOST | Request::HEADER_X_FORWARDED_PORT | Request::HEADER_X_FORWARDED_PROTO); diff --git a/src/google-cloud/google/CloudEvent.php b/src/google-cloud/google/CloudEvent.php index e9c8cd7..abfecf5 100644 --- a/src/google-cloud/google/CloudEvent.php +++ b/src/google-cloud/google/CloudEvent.php @@ -80,7 +80,7 @@ public function getData() return $this->data; } - public static function fromArray(array $arr) + public static function fromArray(array $arr): static { $args = []; $argKeys = [ diff --git a/src/google-cloud/google/Context.php b/src/google-cloud/google/Context.php index 88f65f6..0739b7b 100644 --- a/src/google-cloud/google/Context.php +++ b/src/google-cloud/google/Context.php @@ -54,7 +54,7 @@ public function getResourceName(): ?string return $this->resource['name'] ?? null; } - public static function fromArray(array $arr) + public static function fromArray(array $arr): static { // When "resource" is defined in the root (instead of in "context") it // is a string representing the resource name diff --git a/src/google-cloud/src/Runtime.php b/src/google-cloud/src/Runtime.php index 850b5e4..364ec5e 100644 --- a/src/google-cloud/src/Runtime.php +++ b/src/google-cloud/src/Runtime.php @@ -90,7 +90,7 @@ protected function createCloudEvent(): ?CloudEvent } } - protected function sendHttpResponseAndExit(int $status, string $body, array $headers) + protected function sendHttpResponseAndExit(int $status, string $body, array $headers): void { error_log($body); header('HTTP/1.1 '.$status); diff --git a/src/google-cloud/tests/google/CloudEventTest.php b/src/google-cloud/tests/google/CloudEventTest.php index 1124c3c..887f1af 100644 --- a/src/google-cloud/tests/google/CloudEventTest.php +++ b/src/google-cloud/tests/google/CloudEventTest.php @@ -26,7 +26,7 @@ */ class CloudEventTest extends TestCase { - public function testJsonSerialize() + public function testJsonSerialize(): void { $event = new CloudEvent( '1413058901901494', diff --git a/src/google-cloud/tests/google/ContextTest.php b/src/google-cloud/tests/google/ContextTest.php index 0d21f53..3b66187 100644 --- a/src/google-cloud/tests/google/ContextTest.php +++ b/src/google-cloud/tests/google/ContextTest.php @@ -26,7 +26,7 @@ */ class ContextTest extends TestCase { - public function testFromArray() + public function testFromArray(): void { $context = Context::fromArray([ 'eventId' => 'abc', @@ -45,7 +45,7 @@ public function testFromArray() $this->assertEquals('mno', $context->getService()); } - public function testFromEmptyArray() + public function testFromEmptyArray(): void { $context = Context::fromArray([]); $this->assertEquals(null, $context->getEventId()); diff --git a/src/google-cloud/tests/google/LegacyEventMapperTest.php b/src/google-cloud/tests/google/LegacyEventMapperTest.php index e0bbd15..a025f60 100644 --- a/src/google-cloud/tests/google/LegacyEventMapperTest.php +++ b/src/google-cloud/tests/google/LegacyEventMapperTest.php @@ -26,7 +26,7 @@ */ class LegacyEventMapperTest extends TestCase { - public function testWithContextProperty() + public function testWithContextProperty(): void { $mapper = new LegacyEventMapper(); $jsonData = [ @@ -61,7 +61,7 @@ public function testWithContextProperty() $this->assertEquals(['message' => 'foo'], $cloudevent->getData()); } - public function testWithoutContextProperty() + public function testWithoutContextProperty(): void { $mapper = new LegacyEventMapper(); $jsonData = [ @@ -95,7 +95,7 @@ public function testWithoutContextProperty() $this->assertEquals(['message' => 'foo'], $cloudevent->getData()); } - public function testResourceAsString() + public function testResourceAsString(): void { $mapper = new LegacyEventMapper(); $jsonData = [ @@ -126,7 +126,7 @@ public function testResourceAsString() $this->assertEquals(['message' => 'foo'], $cloudevent->getData()); } - public function testCloudStorage() + public function testCloudStorage(): void { $mapper = new LegacyEventMapper(); $jsonData = [ @@ -163,7 +163,7 @@ public function testCloudStorage() $this->assertEquals('foo', $cloudevent->getData()); } - public function testFirebaseAuth() + public function testFirebaseAuth(): void { $mapper = new LegacyEventMapper(); $jsonData = [ diff --git a/src/google-cloud/tests/runtime/AutoDiscoverTest.php b/src/google-cloud/tests/runtime/AutoDiscoverTest.php index 924f6fb..e4d4ef5 100644 --- a/src/google-cloud/tests/runtime/AutoDiscoverTest.php +++ b/src/google-cloud/tests/runtime/AutoDiscoverTest.php @@ -7,7 +7,7 @@ class AutoDiscoverTest extends TestCase { - public function testAutoDiscoverClasses() + public function testAutoDiscoverClasses(): void { $classes = [ 'Symfony\Runtime\Google\CloudFunctions\CloudEventRuntime', diff --git a/src/google-cloud/tests/runtime/RuntimeTest.php b/src/google-cloud/tests/runtime/RuntimeTest.php index 5d3782f..b11e3b1 100644 --- a/src/google-cloud/tests/runtime/RuntimeTest.php +++ b/src/google-cloud/tests/runtime/RuntimeTest.php @@ -3,13 +3,14 @@ namespace Runtime\GoogleCloud\Tests; use Google\CloudFunctions\CloudEvent; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Runtime\GoogleCloud\Runtime; use Symfony\Component\Runtime\RuntimeInterface; class RuntimeTest extends TestCase { - public function testStructuredType() + public function testStructuredType(): void { $input = [ 'id' => '1234567890', @@ -28,7 +29,7 @@ public function testStructuredType() $this->assertSame('com.google.cloud.pubsub.topic.publish', $output->getType()); } - public function testLegacyType() + public function testLegacyType(): void { $input = [ 'data' => 'foo', @@ -54,7 +55,7 @@ public function testLegacyType() $this->assertSame('google.cloud.pubsub.topic.v1.messagePublished', $output->getType()); } - public function testValidateJsonWithJsonContentType() + public function testValidateJsonWithJsonContentType(): void { $runtime = $this->getRuntimeMock(); $runtime->method('getBody')->willReturn('not json'); @@ -64,7 +65,7 @@ public function testValidateJsonWithJsonContentType() $this->invokeCreateCloudEvent($runtime); } - public function testValidateJsonWithStructuredType() + public function testValidateJsonWithStructuredType(): void { $runtime = $this->getRuntimeMock(); $runtime->method('getBody')->willReturn('not json'); @@ -74,7 +75,7 @@ public function testValidateJsonWithStructuredType() $this->invokeCreateCloudEvent($runtime); } - public function testValidateJsonWithLegacyType() + public function testValidateJsonWithLegacyType(): void { $runtime = $this->getRuntimeMock(); $runtime->method('getBody')->willReturn('not json'); @@ -84,7 +85,7 @@ public function testValidateJsonWithLegacyType() $this->invokeCreateCloudEvent($runtime); } - public function testNoValidateJsonWithBinaryType() + public function testNoValidateJsonWithBinaryType(): void { $runtime = $this->getRuntimeMock(); $runtime->method('getBody')->willReturn('not json'); @@ -110,9 +111,9 @@ private function invokeCreateCloudEvent(RuntimeInterface $runtime): ?CloudEvent } /** - * @return \PHPUnit\Framework\MockObject\MockObject|Runtime + * @return MockObject&Runtime */ - private function getRuntimeMock() + private function getRuntimeMock(): mixed { $runtime = $this->getMockBuilder(Runtime::class) ->disableOriginalConstructor() diff --git a/src/psr-17/src/Runtime.php b/src/psr-17/src/Runtime.php index fea17bf..ec55a60 100644 --- a/src/psr-17/src/Runtime.php +++ b/src/psr-17/src/Runtime.php @@ -68,10 +68,7 @@ protected static function register(GenericRuntime $runtime): GenericRuntime return $self; } - /** - * @return ServerRequestInterface - */ - private function createRequest() + private function createRequest(): ServerRequestInterface { if (null === $this->requestCreator) { $creatorClass = $this->options['server_request_creator'] ?? ServerRequestCreator::class; diff --git a/src/psr-17/tests/AutoDiscoverTest.php b/src/psr-17/tests/AutoDiscoverTest.php index 915d28f..57fda06 100644 --- a/src/psr-17/tests/AutoDiscoverTest.php +++ b/src/psr-17/tests/AutoDiscoverTest.php @@ -7,7 +7,7 @@ class AutoDiscoverTest extends TestCase { - public function testAutoDiscoverClasses() + public function testAutoDiscoverClasses(): void { $classes = [ 'Symfony\Runtime\Psr\Http\Message\ResponseInterfaceRuntime', diff --git a/src/psr-guzzle/tests/AutoDiscoverTest.php b/src/psr-guzzle/tests/AutoDiscoverTest.php index 6088048..7d87910 100644 --- a/src/psr-guzzle/tests/AutoDiscoverTest.php +++ b/src/psr-guzzle/tests/AutoDiscoverTest.php @@ -7,7 +7,7 @@ class AutoDiscoverTest extends TestCase { - public function testAutoDiscoverClasses() + public function testAutoDiscoverClasses(): void { $classes = [ 'Symfony\Runtime\Psr\Http\Message\ResponseInterfaceRuntime', diff --git a/src/psr-laminas/tests/AutoDiscoverTest.php b/src/psr-laminas/tests/AutoDiscoverTest.php index d5f6e92..5ad5484 100644 --- a/src/psr-laminas/tests/AutoDiscoverTest.php +++ b/src/psr-laminas/tests/AutoDiscoverTest.php @@ -7,7 +7,7 @@ class AutoDiscoverTest extends TestCase { - public function testAutoDiscoverClasses() + public function testAutoDiscoverClasses(): void { $classes = [ 'Symfony\Runtime\Psr\Http\Message\ResponseInterfaceRuntime', diff --git a/src/psr-nyholm-laminas/src/Runtime.php b/src/psr-nyholm-laminas/src/Runtime.php index 90e5e0f..db0027b 100644 --- a/src/psr-nyholm-laminas/src/Runtime.php +++ b/src/psr-nyholm-laminas/src/Runtime.php @@ -70,10 +70,7 @@ protected static function register(GenericRuntime $runtime): GenericRuntime return $self; } - /** - * @return ServerRequestInterface - */ - private function createRequest() + private function createRequest(): ServerRequestInterface { if (null === $this->requestCreator) { $creatorClass = $this->options['server_request_creator'] ?? ServerRequestCreator::class; diff --git a/src/psr-nyholm-laminas/tests/AutoDiscoverTest.php b/src/psr-nyholm-laminas/tests/AutoDiscoverTest.php index 330bd11..6c34efd 100644 --- a/src/psr-nyholm-laminas/tests/AutoDiscoverTest.php +++ b/src/psr-nyholm-laminas/tests/AutoDiscoverTest.php @@ -7,7 +7,7 @@ class AutoDiscoverTest extends TestCase { - public function testAutoDiscoverClasses() + public function testAutoDiscoverClasses(): void { $classes = [ 'Symfony\Runtime\Psr\Http\Message\ResponseInterfaceRuntime', diff --git a/src/psr-nyholm/src/Runtime.php b/src/psr-nyholm/src/Runtime.php index ddb8827..8d0d26d 100644 --- a/src/psr-nyholm/src/Runtime.php +++ b/src/psr-nyholm/src/Runtime.php @@ -54,10 +54,7 @@ protected static function register(GenericRuntime $runtime): GenericRuntime return $self; } - /** - * @return ServerRequestInterface - */ - private function createRequest() + private function createRequest(): ServerRequestInterface { if (null === $this->requestCreator) { $psr17Factory = new Psr17Factory(); diff --git a/src/psr-nyholm/tests/AutoDiscoverTest.php b/src/psr-nyholm/tests/AutoDiscoverTest.php index f46d447..db359a6 100644 --- a/src/psr-nyholm/tests/AutoDiscoverTest.php +++ b/src/psr-nyholm/tests/AutoDiscoverTest.php @@ -7,7 +7,7 @@ class AutoDiscoverTest extends TestCase { - public function testAutoDiscoverClasses() + public function testAutoDiscoverClasses(): void { $classes = [ 'Symfony\Runtime\Psr\Http\Message\ResponseInterfaceRuntime', diff --git a/src/reactphp/tests/AutoDiscoverTest.php b/src/reactphp/tests/AutoDiscoverTest.php index 83c6924..d94f446 100644 --- a/src/reactphp/tests/AutoDiscoverTest.php +++ b/src/reactphp/tests/AutoDiscoverTest.php @@ -7,7 +7,7 @@ class AutoDiscoverTest extends TestCase { - public function testAutoDiscoverClasses() + public function testAutoDiscoverClasses(): void { $classes = [ 'Symfony\Runtime\Psr\Http\Server\RequestHandlerInterfaceRuntime',