Skip to content

Upgrade codebase to php 8 (part 1) #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions src/bref/bref/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/bref/src/BrefRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/bref/src/ConsoleApplicationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
*/
class ConsoleApplicationHandler implements Handler
{
private $application;
private Application $application;

public function __construct(Application $application)
{
$this->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');
Expand Down
6 changes: 2 additions & 4 deletions src/bref/src/ConsoleApplicationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions src/bref/src/Lambda/ContextBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
9 changes: 2 additions & 7 deletions src/bref/src/Lambda/LambdaClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/bref/src/LaravelHttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class LaravelHttpHandler extends HttpHandler
{
private $kernel;
private Kernel $kernel;

public function __construct(Kernel $kernel)
{
Expand Down
7 changes: 1 addition & 6 deletions src/bref/src/LocalRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/bref/src/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
6 changes: 1 addition & 5 deletions src/bref/src/SymfonyHttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
35 changes: 17 additions & 18 deletions src/bref/tests/Lambda/LambdaClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
*/
class LambdaClientTest extends TestCase
{
/** @var LambdaClient */
private $lambda;
private LambdaClient $lambda;

protected function setUp(): void
{
Expand All @@ -32,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'];
}
Expand All @@ -47,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()];
}
Expand All @@ -64,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!']);

Expand All @@ -80,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!']);

Expand All @@ -99,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([
Expand All @@ -119,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([
Expand All @@ -137,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([
Expand All @@ -156,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
Expand Down Expand Up @@ -194,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";
}
Expand All @@ -214,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)
Expand Down Expand Up @@ -245,7 +244,7 @@ private function givenAnEvent($event): void
]);
}

private function assertInvocationResult($result)
private function assertInvocationResult($result): void
{
$requests = Server::received();
$this->assertCount(2, $requests);
Expand All @@ -258,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);
Expand Down Expand Up @@ -305,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();
Expand Down
12 changes: 6 additions & 6 deletions src/bref/tests/SymfonyRequestBridgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class SymfonyRequestBridgeTest extends TestCase
{
public function testClientIpFromForwardedFor()
public function testClientIpFromForwardedFor(): void
{
$request = SymfonyRequestBridge::convertRequest(new HttpRequestEvent([
'requestContext' => ['http' => ['method' => 'GET']],
Expand All @@ -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([
Expand Down Expand Up @@ -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']],
Expand Down Expand Up @@ -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']],
Expand Down Expand Up @@ -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([
Expand All @@ -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);
Expand Down
Loading
Loading