diff --git a/rector.php b/rector.php index 7907d00..27716b7 100644 --- a/rector.php +++ b/rector.php @@ -6,6 +6,7 @@ return RectorConfig::configure() ->withPaths([ + __DIR__.'/src', __DIR__ . '/test', ]) ->withRootFiles() @@ -13,8 +14,12 @@ ->withComposerBased(phpunit: true) ->withPhpSets() ->withPreparedSets( + deadCode: true, codeQuality: true, typeDeclarations: true, typeDeclarationDocblocks: true, + phpunitCodeQuality: true, + phpunitNarrowAsserts: true, + phpunitMockToStub: true, ) ->withImportNames(); diff --git a/src/DefaultStackdriverLoggerProvider.php b/src/DefaultStackdriverLoggerProvider.php index 471108c..4a26813 100644 --- a/src/DefaultStackdriverLoggerProvider.php +++ b/src/DefaultStackdriverLoggerProvider.php @@ -20,7 +20,7 @@ public function getLogger(): LoggerInterface { if (!StackdriverApplicationLogger::isInitialised()) { StackdriverApplicationLogger::initialise( - fn () => new StackdriverApplicationLogger( + fn (): StackdriverApplicationLogger => new StackdriverApplicationLogger( $this->log_destination, ...$this->getMetadataProviders(), ) diff --git a/src/LoggerProvider.php b/src/LoggerProvider.php index 03298e5..aeaf938 100644 --- a/src/LoggerProvider.php +++ b/src/LoggerProvider.php @@ -1,5 +1,7 @@ emergency( sprintf( 'Uncaught %s: %s at %s:%s', - get_class($e), + $e::class, $e->getMessage(), $e->getFile(), $e->getFile(), diff --git a/src/RequestHandler.php b/src/RequestHandler.php index fe53d06..dd73b4c 100644 --- a/src/RequestHandler.php +++ b/src/RequestHandler.php @@ -1,5 +1,7 @@ > + * @return Iterator> */ - public static function provider_error_handling(): array + public static function provider_error_handling(): Iterator { - return [ - 'exception in handler factory' => [ - <<<'PHP' + yield 'exception in handler factory' => [ + <<<'PHP' fn () => throw new RuntimeException('No handler') PHP, - ], - 'error in handler factory' => [ - <<<'PHP' + ]; + yield 'error in handler factory' => [ + <<<'PHP' fn () => ['no'=> 'thing']['undefined'] PHP, - ], - 'factory does not return a handler instance' => [ - <<<'PHP' + ]; + yield 'factory does not return a handler instance' => [ + <<<'PHP' fn (LoggerInterface $logger) => new class extends stdClass { public function handle(ServerRequestInterface $request): ResponseInterface { @@ -33,14 +33,14 @@ public function handle(ServerRequestInterface $request): ResponseInterface } } PHP, - ], - 'factory references undefined class' => [ - <<<'PHP' + ]; + yield 'factory references undefined class' => [ + <<<'PHP' fn (LoggerInterface $logger) => new \My\Undefined\RequestHandlerClass() PHP, - ], - 'uncaught exception during handler' => [ - <<<'PHP' + ]; + yield 'uncaught exception during handler' => [ + <<<'PHP' fn (LoggerInterface $logger) => new class implements RequestHandler { public function handle(ServerRequestInterface $request): ResponseInterface { @@ -48,9 +48,9 @@ public function handle(ServerRequestInterface $request): ResponseInterface } } PHP, - ], - 'php error in handler' => [ - <<<'PHP' + ]; + yield 'php error in handler' => [ + <<<'PHP' fn (LoggerInterface $logger) => new class implements RequestHandler { public function handle(ServerRequestInterface $request): ResponseInterface { @@ -59,9 +59,9 @@ public function handle(ServerRequestInterface $request): ResponseInterface } } PHP, - ], - 'TypeError in handler' => [ - <<<'PHP' + ]; + yield 'TypeError in handler' => [ + <<<'PHP' fn (LoggerInterface $logger) => new class implements RequestHandler { public function handle(ServerRequestInterface $request): ResponseInterface { @@ -73,7 +73,6 @@ private function doThings(string $uhoh): ResponseInterface { } } PHP, - ], ]; } diff --git a/test/blackbox/HttpHeadersTest.php b/test/blackbox/HttpHeadersTest.php index 540b925..1949521 100644 --- a/test/blackbox/HttpHeadersTest.php +++ b/test/blackbox/HttpHeadersTest.php @@ -5,9 +5,10 @@ namespace test\blackbox; use GuzzleHttp\RequestOptions; +use Iterator; use PHPUnit\Framework\Attributes\DataProvider; -class HttpHeadersTest extends BaseBlackboxTestCase +final class HttpHeadersTest extends BaseBlackboxTestCase { private static string $handler_url; @@ -28,42 +29,40 @@ public function handle(ServerRequestInterface $request): ResponseInterface } /** - * @return array, array>}> + * @return Iterator, array>}> */ - public static function provider_headers(): array + public static function provider_headers(): Iterator { - return [ - 'no headers specified, just the defaults' => [ - [], - [ - 'Content-Length' => ['17'], - 'Content-Type' => ['text/html; charset=UTF-8'], - ], + yield 'no headers specified, just the defaults' => [ + [], + [ + 'Content-Length' => ['17'], + 'Content-Type' => ['text/html; charset=UTF-8'], ], - 'custom content type' => [ - ['Content-Type' => 'application/json'], - [ - 'Content-Length' => ['17'], - 'Content-Type' => ['application/json'], - ], + ]; + yield 'custom content type' => [ + ['Content-Type' => 'application/json'], + [ + 'Content-Length' => ['17'], + 'Content-Type' => ['application/json'], ], - 'custom content type in different case' => [ - ['content-type' => 'application/json'], - [ - 'Content-Length' => ['17'], - 'Content-Type' => ['application/json'], - ], + ]; + yield 'custom content type in different case' => [ + ['content-type' => 'application/json'], + [ + 'Content-Length' => ['17'], + 'Content-Type' => ['application/json'], ], - 'custom headers' => [ - [ - 'Content-Type' => 'application/json', - 'X-My-Custom' => 'Value', - ], - [ - 'Content-Length' => ['17'], - 'Content-Type' => ['application/json'], - 'X-My-Custom' => ['Value'], - ], + ]; + yield 'custom headers' => [ + [ + 'Content-Type' => 'application/json', + 'X-My-Custom' => 'Value', + ], + [ + 'Content-Length' => ['17'], + 'Content-Type' => ['application/json'], + 'X-My-Custom' => ['Value'], ], ]; } diff --git a/test/blackbox/HttpResponseCodesTest.php b/test/blackbox/HttpResponseCodesTest.php index 338b1bd..6f1b24b 100644 --- a/test/blackbox/HttpResponseCodesTest.php +++ b/test/blackbox/HttpResponseCodesTest.php @@ -6,7 +6,7 @@ use PHPUnit\Framework\Attributes\TestWith; -class HttpResponseCodesTest extends BaseBlackboxTestCase +final class HttpResponseCodesTest extends BaseBlackboxTestCase { private static string $handler_url; diff --git a/test/blackbox/OutputBufferingTest.php b/test/blackbox/OutputBufferingTest.php index 1699d0a..ed10957 100644 --- a/test/blackbox/OutputBufferingTest.php +++ b/test/blackbox/OutputBufferingTest.php @@ -6,7 +6,7 @@ use test\blackbox\BaseBlackboxTestCase; -class OutputBufferingTest extends BaseBlackboxTestCase +final class OutputBufferingTest extends BaseBlackboxTestCase { public function testItHandlesErrorEvenIfOutputBeganBeforeFramework(): void { diff --git a/test/blackbox/RequestParsingTest.php b/test/blackbox/RequestParsingTest.php index 096c952..b409132 100644 --- a/test/blackbox/RequestParsingTest.php +++ b/test/blackbox/RequestParsingTest.php @@ -10,7 +10,7 @@ /** * @phpstan-type TGuzzleRequestOptions array{form_params?: array|scalar|null>, json?: mixed} */ -class RequestParsingTest extends BaseBlackboxTestCase +final class RequestParsingTest extends BaseBlackboxTestCase { private static string $handler_url; diff --git a/test/blackbox/StackdriverLoggingTest.php b/test/blackbox/StackdriverLoggingTest.php index 8dc1c23..f207143 100644 --- a/test/blackbox/StackdriverLoggingTest.php +++ b/test/blackbox/StackdriverLoggingTest.php @@ -1,10 +1,12 @@ assertIsNumeric($request_log_msg['context']['mem_mb']); - $this->assertSame(2.0, round((float) $request_log_msg['context']['mem_mb'])); + $this->assertEqualsWithDelta(2.0, $request_log_msg['context']['mem_mb'], 0.5); unset($request_log_msg['context']['mem_mb']); $this->assertSame( @@ -177,8 +178,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface unset($request_log_msg['httpRequest']['latency']); // Currently the test handler is taking around 2.1Mb, just assert it's close to that - $this->assertIsNumeric($request_log_msg['context']['mem_mb']); - $this->assertSame(2.0, round((float) $request_log_msg['context']['mem_mb'])); + $this->assertEqualsWithDelta(2.0, $request_log_msg['context']['mem_mb'], 0.5); unset($request_log_msg['context']['mem_mb']); $this->assertSame( @@ -199,7 +199,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface ], 'reportLocation' => [ 'filePath' => '/var/www/vendor/ingenerator/microframework/src/MicroFramework.php', - 'lineNumber' => 153, + 'lineNumber' => 152, 'functionName' => 'Ingenerator\MicroFramework\MicroFramework->logUncaughtException', ], ], @@ -211,21 +211,21 @@ public function handle(ServerRequestInterface $request): ResponseInterface 'file' => '/var/www/htdocs/dynamic/StackdriverLoggingTest_54a57cd69e4889f9c6cfdde12f4607f1461dec3e/index.php', 'line' => 17, 'trace' => <<<'TEXT' - #0 /var/www/vendor/ingenerator/microframework/src/MicroFramework.php(82): MyHandler->handle() + #0 /var/www/vendor/ingenerator/microframework/src/MicroFramework.php(81): MyHandler->handle() #1 /var/www/htdocs/dynamic/StackdriverLoggingTest_54a57cd69e4889f9c6cfdde12f4607f1461dec3e/index.php(21): Ingenerator\MicroFramework\MicroFramework->execute() #2 {main} TEXT, ], 'logging.googleapis.com/sourceLocation' => [ 'file' => '/var/www/vendor/ingenerator/microframework/src/MicroFramework.php', - 'line' => 153, + 'line' => 152, 'function' => 'Ingenerator\MicroFramework\MicroFramework->logUncaughtException', ], '@type' => 'type.googleapis.com/google.devtools.clouderrorreporting.v1beta1.ReportedErrorEvent', 'stack_trace' => <<<'TEXT' PHP Warning: RuntimeException: I broke in /var/www/htdocs/dynamic/StackdriverLoggingTest_54a57cd69e4889f9c6cfdde12f4607f1461dec3e/index.php:17 Stack trace: - #0 /var/www/vendor/ingenerator/microframework/src/MicroFramework.php(82): MyHandler->handle() + #0 /var/www/vendor/ingenerator/microframework/src/MicroFramework.php(81): MyHandler->handle() #1 /var/www/htdocs/dynamic/StackdriverLoggingTest_54a57cd69e4889f9c6cfdde12f4607f1461dec3e/index.php(21): Ingenerator\MicroFramework\MicroFramework->execute() #2 {main} TEXT, @@ -338,8 +338,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface unset($request_log_msg['httpRequest']['latency']); // Currently the test handler is taking around 2.1Mb, just assert it's close to that - $this->assertIsNumeric($request_log_msg['context']['mem_mb']); - $this->assertSame(2.0, round((float) $request_log_msg['context']['mem_mb'])); + $this->assertEqualsWithDelta(2.0, $request_log_msg['context']['mem_mb'], 0.5); unset($request_log_msg['context']['mem_mb']); $this->assertSame( diff --git a/test/integration/DefaultStackdriverLoggerProviderTest.php b/test/integration/DefaultStackdriverLoggerProviderTest.php index 50cbc54..3794260 100644 --- a/test/integration/DefaultStackdriverLoggerProviderTest.php +++ b/test/integration/DefaultStackdriverLoggerProviderTest.php @@ -10,7 +10,7 @@ use Ingenerator\PHPUtils\Logging\StackdriverApplicationLogger; use PHPUnit\Framework\TestCase; -class DefaultStackdriverLoggerProviderTest extends TestCase +final class DefaultStackdriverLoggerProviderTest extends TestCase { public function testItIsInitialisableLoggerProvider(): void { diff --git a/test/unit/FactoryFunctionTest.php b/test/unit/FactoryFunctionTest.php index f94499d..5158d88 100644 --- a/test/unit/FactoryFunctionTest.php +++ b/test/unit/FactoryFunctionTest.php @@ -1,5 +1,7 @@ > + * @return Iterator> */ - public static function provider_calculate_request_start(): array + public static function provider_calculate_request_start(): Iterator { - return [ - '64-bit with int hrtime' => [ - 'hrtime_start' => 2993548241208, - 'hrtime_now' => 2993548244583, - 'microtime_now' => 1709456239.781800000, - 'expect_start_time' => 1709456239.781796625, - ], - '32-bit with float hrtime (and longer latency)' => [ - 'hrtime_start' => 2992348244583.0, - 'hrtime_now' => 2993548244583.0, - 'microtime_now' => 1709456239.7818, - 'expect_start_time' => 1709456238.5818, - ], + yield '64-bit with int hrtime' => [ + 'hrtime_start' => 2993548241208, + 'hrtime_now' => 2993548244583, + 'microtime_now' => 1709456239.781800000, + 'expect_start_time' => 1709456239.781796625, + ]; + yield '32-bit with float hrtime (and longer latency)' => [ + 'hrtime_start' => 2992348244583.0, + 'hrtime_now' => 2993548244583.0, + 'microtime_now' => 1709456239.7818, + 'expect_start_time' => 1709456238.5818, ]; }