From 4b325ad6be892ac19789ac3f06c3744da42bd6e4 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Thu, 1 Sep 2022 18:56:17 +0300 Subject: [PATCH 1/5] Apply Rector UP_TO_PHP_72 set --- tests/StopwatchTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/StopwatchTest.php b/tests/StopwatchTest.php index 7980e03..b41866b 100644 --- a/tests/StopwatchTest.php +++ b/tests/StopwatchTest.php @@ -73,7 +73,7 @@ public function testShouldGetSymfonyStopWatch() { $stopwatch = new Stopwatch(); $this->assertInstanceOf( - "Symfony\Component\Stopwatch\Stopwatch", + \Symfony\Component\Stopwatch\Stopwatch::class, $stopwatch->stopwatch() ); } From 2405f9fb0df8ab1341c7b593baa756e248952155 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Thu, 1 Sep 2022 18:57:26 +0300 Subject: [PATCH 2/5] Apply Rector CODE_QUALITY set --- rector.php | 2 +- src/ServerTiming/Stopwatch.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rector.php b/rector.php index 4f8b9ee..5dacdda 100644 --- a/rector.php +++ b/rector.php @@ -16,7 +16,7 @@ $rectorConfig->sets([ LevelSetList::UP_TO_PHP_72, - // SetList::CODE_QUALITY, + SetList::CODE_QUALITY, // SetList::DEAD_CODE, // SetList::PRIVATIZATION, // SetList::NAMING, diff --git a/src/ServerTiming/Stopwatch.php b/src/ServerTiming/Stopwatch.php index 53f6493..846ae3c 100644 --- a/src/ServerTiming/Stopwatch.php +++ b/src/ServerTiming/Stopwatch.php @@ -65,7 +65,7 @@ public function __construct() public function start(string $key): StopwatchInterface { $this->stopwatch->start($key); - array_push($this->keys, $key); + $this->keys[] = $key; return $this; } From e9fdefc4d409050fd4221b67709480c6f140007c Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Thu, 1 Sep 2022 18:58:44 +0300 Subject: [PATCH 3/5] Apply Rector DEAD_CODE set --- rector.php | 2 +- src/ServerTiming/Stopwatch.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rector.php b/rector.php index 5dacdda..0a14681 100644 --- a/rector.php +++ b/rector.php @@ -17,7 +17,7 @@ $rectorConfig->sets([ LevelSetList::UP_TO_PHP_72, SetList::CODE_QUALITY, - // SetList::DEAD_CODE, + SetList::DEAD_CODE, // SetList::PRIVATIZATION, // SetList::NAMING, // SetList::TYPE_DECLARATION, diff --git a/src/ServerTiming/Stopwatch.php b/src/ServerTiming/Stopwatch.php index 846ae3c..8070a30 100644 --- a/src/ServerTiming/Stopwatch.php +++ b/src/ServerTiming/Stopwatch.php @@ -45,7 +45,7 @@ class Stopwatch implements StopwatchInterface /** * @var int */ - private $memory = null; + private $memory; /** * @var string[] From 882b037cfa1708f016912574360d3772a0c2d6aa Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Thu, 1 Sep 2022 19:08:32 +0300 Subject: [PATCH 4/5] Apply Rector NAMING set --- rector.php | 2 +- src/ServerTiming/Stopwatch.php | 16 ++++++++-------- src/ServerTimingMiddleware.php | 4 ++-- tests/QueryTimerTest.php | 6 +++--- tests/ServerTimingTest.php | 24 ++++++++++++------------ 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/rector.php b/rector.php index 0a14681..aaa397b 100644 --- a/rector.php +++ b/rector.php @@ -19,7 +19,7 @@ SetList::CODE_QUALITY, SetList::DEAD_CODE, // SetList::PRIVATIZATION, - // SetList::NAMING, + SetList::NAMING, // SetList::TYPE_DECLARATION, // SetList::EARLY_RETURN, // SetList::TYPE_DECLARATION_STRICT, diff --git a/src/ServerTiming/Stopwatch.php b/src/ServerTiming/Stopwatch.php index 8070a30..77f0751 100644 --- a/src/ServerTiming/Stopwatch.php +++ b/src/ServerTiming/Stopwatch.php @@ -40,7 +40,7 @@ class Stopwatch implements StopwatchInterface /** * @var SymfonyStopWatch */ - private $stopwatch; + private $symfonyStopWatch; /** * @var int @@ -59,22 +59,22 @@ class Stopwatch implements StopwatchInterface public function __construct() { - $this->stopwatch = new SymfonyStopWatch(); + $this->symfonyStopWatch = new SymfonyStopWatch(); } public function start(string $key): StopwatchInterface { - $this->stopwatch->start($key); + $this->symfonyStopWatch->start($key); $this->keys[] = $key; return $this; } public function stop(string $key): StopwatchInterface { - if ($this->stopwatch->isStarted($key)) { - $event = $this->stopwatch->stop($key); - $duration = $event->getDuration(); - $this->memory = $event->getMemory(); + if ($this->symfonyStopWatch->isStarted($key)) { + $stopwatchEvent = $this->symfonyStopWatch->stop($key); + $duration = $stopwatchEvent->getDuration(); + $this->memory = $stopwatchEvent->getMemory(); $this->set($key, (int) $duration); } return $this; @@ -120,7 +120,7 @@ public function get(string $key): ?int public function stopwatch(): SymfonyStopWatch { - return $this->stopwatch; + return $this->symfonyStopWatch; } public function memory(): ?int diff --git a/src/ServerTimingMiddleware.php b/src/ServerTimingMiddleware.php index 678e7d1..348711d 100644 --- a/src/ServerTimingMiddleware.php +++ b/src/ServerTimingMiddleware.php @@ -87,7 +87,7 @@ public function __construct(StopwatchInterface $stopwatch = null, array $options $this->hydrate($options); } - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + public function process(ServerRequestInterface $serverRequest, RequestHandlerInterface $requestHandler): ResponseInterface { /* Time spent from starting the request to entering this middleware. */ if ($this->bootstrap) { @@ -99,7 +99,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface if ($this->process) { $this->stopwatch->start($this->process); } - $response = $handler->handle($request); + $response = $requestHandler->handle($serverRequest); if ($this->process) { $this->stopwatch->stop($this->process); } diff --git a/tests/QueryTimerTest.php b/tests/QueryTimerTest.php index 577a5e9..b7aa7f4 100644 --- a/tests/QueryTimerTest.php +++ b/tests/QueryTimerTest.php @@ -46,10 +46,10 @@ public function testShouldBeTrue() public function testShouldStartAndStopQueryTimer() { $stopwatch = new Stopwatch(); - $timer = new QueryTimer($stopwatch); - $timer->startQuery("SELECT * FROM brawndos"); + $queryTimer = new QueryTimer($stopwatch); + $queryTimer->startQuery("SELECT * FROM brawndos"); usleep(10000); - $timer->stopQuery(); + $queryTimer->stopQuery(); $this->assertArrayHasKey("SQL", $stopwatch->values()); } } diff --git a/tests/ServerTimingTest.php b/tests/ServerTimingTest.php index 5179084..5dea770 100644 --- a/tests/ServerTimingTest.php +++ b/tests/ServerTimingTest.php @@ -49,13 +49,13 @@ public function testShouldHandlePsr7() $response = (new ResponseFactory())->createResponse(); - $next = function (ServerRequestInterface $request, ResponseInterface $response) { + $next = function (ServerRequestInterface $serverRequest, ResponseInterface $response) { $response->getBody()->write("Success"); return $response; }; - $timing = new ServerTimingMiddleware(); - $response = $timing($request, $response, $next); + $serverTimingMiddleware = new ServerTimingMiddleware(); + $response = $serverTimingMiddleware($request, $response, $next); $header = $response->getHeader("Server-Timing")[0]; $regexp = "/Bootstrap;dur=[0-9\.]+, Process;dur=[0-9\.]+, Total;dur=[0-9\.]+/"; @@ -71,17 +71,17 @@ public function testShouldHandlePsr15() $request = (new ServerRequestFactory()) ->createServerRequest("GET", "https://example.com/"); - $default = function (ServerRequestInterface $request) { + $default = function (ServerRequestInterface $serverRequest) { $response = (new ResponseFactory())->createResponse(); $response->getBody()->write("Success"); return $response; }; - $collection = new MiddlewareCollection([ + $middlewareCollection = new MiddlewareCollection([ new ServerTimingMiddleware(), ]); - $response = $collection->dispatch($request, $default); + $response = $middlewareCollection->dispatch($request, $default); $header = $response->getHeader("Server-Timing")[0]; $regexp = "/Bootstrap;dur=[0-9\.]+, Process;dur=[0-9\.]+, Total;dur=[0-9\.]+/"; @@ -100,7 +100,7 @@ public function testShouldGenerateValidToken() $response = (new ResponseFactory())->createResponse(); - $next = function (ServerRequestInterface $request, ResponseInterface $response) { + $next = function (ServerRequestInterface $serverRequest, ResponseInterface $response) { $response->getBody()->write("Success"); return $response; }; @@ -108,8 +108,8 @@ public function testShouldGenerateValidToken() $stopwatch = new Stopwatch(); $stopwatch->set("DB Server", 100); - $timing = new ServerTimingMiddleware($stopwatch); - $response = $timing($request, $response, $next); + $serverTimingMiddleware = new ServerTimingMiddleware($stopwatch); + $response = $serverTimingMiddleware($request, $response, $next); $header = $response->getHeader("Server-Timing")[0]; $regexp = '/^dbserver;dur=100;desc="DB Server", Bootstrap;dur=[0-9]+, Process;dur=[0-9]+, Total;dur=[0-9]+/'; @@ -128,12 +128,12 @@ public function testShouldAlterDefaults() $response = (new ResponseFactory())->createResponse(); - $next = function (ServerRequestInterface $request, ResponseInterface $response) { + $next = function (ServerRequestInterface $serverRequest, ResponseInterface $response) { $response->getBody()->write("Success"); return $response; }; - $timing = new ServerTimingMiddleware( + $serverTimingMiddleware = new ServerTimingMiddleware( new Stopwatch(), [ "bootstrap" => "Startup", @@ -141,7 +141,7 @@ public function testShouldAlterDefaults() "total" => "Sum", ] ); - $response = $timing($request, $response, $next); + $response = $serverTimingMiddleware($request, $response, $next); $header = $response->getHeader("Server-Timing")[0]; $regexp = '/^Startup;dur=[0-9]+, Sum;dur=[0-9]+/'; From a2cbe8b860059e12eaee2ff4d7522e0f6121dce0 Mon Sep 17 00:00:00 2001 From: Mika Tuupola Date: Thu, 1 Sep 2022 19:21:48 +0300 Subject: [PATCH 5/5] Apply Rector TYPE_DECLARATION set --- rector.php | 2 +- src/ServerTiming/Stopwatch.php | 2 +- src/ServerTimingMiddleware.php | 2 +- tests/QueryTimerTest.php | 4 ++-- tests/ServerTimingTest.php | 16 ++++++++-------- tests/StopwatchTest.php | 16 ++++++++-------- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/rector.php b/rector.php index aaa397b..e2a9c53 100644 --- a/rector.php +++ b/rector.php @@ -20,7 +20,7 @@ SetList::DEAD_CODE, // SetList::PRIVATIZATION, SetList::NAMING, - // SetList::TYPE_DECLARATION, + SetList::TYPE_DECLARATION, // SetList::EARLY_RETURN, // SetList::TYPE_DECLARATION_STRICT, // PHPUnitSetList::PHPUNIT_CODE_QUALITY, diff --git a/src/ServerTiming/Stopwatch.php b/src/ServerTiming/Stopwatch.php index 77f0751..9643691 100644 --- a/src/ServerTiming/Stopwatch.php +++ b/src/ServerTiming/Stopwatch.php @@ -43,7 +43,7 @@ class Stopwatch implements StopwatchInterface private $symfonyStopWatch; /** - * @var int + * @var int|null */ private $memory; diff --git a/src/ServerTimingMiddleware.php b/src/ServerTimingMiddleware.php index 348711d..5f61739 100644 --- a/src/ServerTimingMiddleware.php +++ b/src/ServerTimingMiddleware.php @@ -51,7 +51,7 @@ final class ServerTimingMiddleware implements MiddlewareInterface private $stopwatch; /** - * @var float + * @var mixed|float */ private $start; diff --git a/tests/QueryTimerTest.php b/tests/QueryTimerTest.php index b7aa7f4..1ffa38c 100644 --- a/tests/QueryTimerTest.php +++ b/tests/QueryTimerTest.php @@ -38,12 +38,12 @@ class QueryTimerTest extends TestCase { - public function testShouldBeTrue() + public function testShouldBeTrue(): void { $this->assertTrue(true); } - public function testShouldStartAndStopQueryTimer() + public function testShouldStartAndStopQueryTimer(): void { $stopwatch = new Stopwatch(); $queryTimer = new QueryTimer($stopwatch); diff --git a/tests/ServerTimingTest.php b/tests/ServerTimingTest.php index 5dea770..9ec9498 100644 --- a/tests/ServerTimingTest.php +++ b/tests/ServerTimingTest.php @@ -42,14 +42,14 @@ class ServerTimingTest extends TestCase { - public function testShouldHandlePsr7() + public function testShouldHandlePsr7(): void { $request = (new ServerRequestFactory()) ->createServerRequest("GET", "https://example.com/"); $response = (new ResponseFactory())->createResponse(); - $next = function (ServerRequestInterface $serverRequest, ResponseInterface $response) { + $next = function (ServerRequestInterface $serverRequest, ResponseInterface $response): \Psr\Http\Message\ResponseInterface { $response->getBody()->write("Success"); return $response; }; @@ -66,12 +66,12 @@ public function testShouldHandlePsr7() $this->assertRegexp($regexp, $header); } - public function testShouldHandlePsr15() + public function testShouldHandlePsr15(): void { $request = (new ServerRequestFactory()) ->createServerRequest("GET", "https://example.com/"); - $default = function (ServerRequestInterface $serverRequest) { + $default = function (ServerRequestInterface $serverRequest): \Psr\Http\Message\ResponseInterface { $response = (new ResponseFactory())->createResponse(); $response->getBody()->write("Success"); return $response; @@ -93,14 +93,14 @@ public function testShouldHandlePsr15() } /* https://tools.ietf.org/html/rfc7230#section-3.2.6 */ - public function testShouldGenerateValidToken() + public function testShouldGenerateValidToken(): void { $request = (new ServerRequestFactory()) ->createServerRequest("GET", "https://example.com/"); $response = (new ResponseFactory())->createResponse(); - $next = function (ServerRequestInterface $serverRequest, ResponseInterface $response) { + $next = function (ServerRequestInterface $serverRequest, ResponseInterface $response): \Psr\Http\Message\ResponseInterface { $response->getBody()->write("Success"); return $response; }; @@ -121,14 +121,14 @@ public function testShouldGenerateValidToken() //$this->assertTrue((boolean) preg_match($regex, $header)); } - public function testShouldAlterDefaults() + public function testShouldAlterDefaults(): void { $request = (new ServerRequestFactory()) ->createServerRequest("GET", "https://example.com/"); $response = (new ResponseFactory())->createResponse(); - $next = function (ServerRequestInterface $serverRequest, ResponseInterface $response) { + $next = function (ServerRequestInterface $serverRequest, ResponseInterface $response): \Psr\Http\Message\ResponseInterface { $response->getBody()->write("Success"); return $response; }; diff --git a/tests/StopwatchTest.php b/tests/StopwatchTest.php index b41866b..45f3eea 100644 --- a/tests/StopwatchTest.php +++ b/tests/StopwatchTest.php @@ -37,12 +37,12 @@ class StopwatchTest extends TestCase { - public function testShouldBeTrue() + public function testShouldBeTrue(): void { $this->assertTrue(true); } - public function testShouldGetAndSetValues() + public function testShouldGetAndSetValues(): void { $stopwatch = new Stopwatch(); $this->assertNull($stopwatch->get("water")); @@ -50,26 +50,26 @@ public function testShouldGetAndSetValues() $this->assertEquals(100, $stopwatch->get("water")); } - public function testShouldReturnFromClosure() + public function testShouldReturnFromClosure(): void { $stopwatch = new Stopwatch(); - $value = $stopwatch->closure("name", function () { + $value = $stopwatch->closure("name", function (): string { return "Not sure?"; }); $this->assertEquals("Not sure?", $value); } - public function testShouldSetClosure() + public function testShouldSetClosure(): void { $stopwatch = new Stopwatch(); $this->assertNull($stopwatch->get("juice")); - $stopwatch->set("juice", function () { + $stopwatch->set("juice", function (): void { usleep(50000); }); $this->assertTrue($stopwatch->get("juice") > 0); } - public function testShouldGetSymfonyStopWatch() + public function testShouldGetSymfonyStopWatch(): void { $stopwatch = new Stopwatch(); $this->assertInstanceOf( @@ -78,7 +78,7 @@ public function testShouldGetSymfonyStopWatch() ); } - public function testShouldGetMemory() + public function testShouldGetMemory(): void { $stopwatch = new Stopwatch(); $this->assertNull($stopwatch->memory());