Skip to content
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
8 changes: 4 additions & 4 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

$rectorConfig->sets([
LevelSetList::UP_TO_PHP_72,
// SetList::CODE_QUALITY,
// SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::DEAD_CODE,
// SetList::PRIVATIZATION,
// SetList::NAMING,
// SetList::TYPE_DECLARATION,
SetList::NAMING,
SetList::TYPE_DECLARATION,
// SetList::EARLY_RETURN,
// SetList::TYPE_DECLARATION_STRICT,
// PHPUnitSetList::PHPUNIT_CODE_QUALITY,
Expand Down
22 changes: 11 additions & 11 deletions src/ServerTiming/Stopwatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class Stopwatch implements StopwatchInterface
/**
* @var SymfonyStopWatch
*/
private $stopwatch;
private $symfonyStopWatch;

/**
* @var int
* @var int|null
*/
private $memory = null;
private $memory;

/**
* @var string[]
Expand All @@ -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);
array_push($this->keys, $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;
Expand Down Expand Up @@ -120,7 +120,7 @@ public function get(string $key): ?int

public function stopwatch(): SymfonyStopWatch
{
return $this->stopwatch;
return $this->symfonyStopWatch;
}

public function memory(): ?int
Expand Down
6 changes: 3 additions & 3 deletions src/ServerTimingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ final class ServerTimingMiddleware implements MiddlewareInterface
private $stopwatch;

/**
* @var float
* @var mixed|float
*/
private $start;

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/QueryTimerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@

class QueryTimerTest extends TestCase
{
public function testShouldBeTrue()
public function testShouldBeTrue(): void
{
$this->assertTrue(true);
}

public function testShouldStartAndStopQueryTimer()
public function testShouldStartAndStopQueryTimer(): void
{
$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());
}
}
32 changes: 16 additions & 16 deletions tests/ServerTimingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@

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 $request, ResponseInterface $response) {
$next = function (ServerRequestInterface $serverRequest, ResponseInterface $response): \Psr\Http\Message\ResponseInterface {
$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\.]+/";
Expand All @@ -66,22 +66,22 @@ 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 $request) {
$default = function (ServerRequestInterface $serverRequest): \Psr\Http\Message\ResponseInterface {
$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\.]+/";
Expand All @@ -93,23 +93,23 @@ 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 $request, ResponseInterface $response) {
$next = function (ServerRequestInterface $serverRequest, ResponseInterface $response): \Psr\Http\Message\ResponseInterface {
$response->getBody()->write("Success");
return $response;
};

$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]+/';
Expand All @@ -121,27 +121,27 @@ 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 $request, ResponseInterface $response) {
$next = function (ServerRequestInterface $serverRequest, ResponseInterface $response): \Psr\Http\Message\ResponseInterface {
$response->getBody()->write("Success");
return $response;
};

$timing = new ServerTimingMiddleware(
$serverTimingMiddleware = new ServerTimingMiddleware(
new Stopwatch(),
[
"bootstrap" => "Startup",
"process" => null,
"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]+/';
Expand Down
18 changes: 9 additions & 9 deletions tests/StopwatchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,48 +37,48 @@

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"));
$stopwatch->set("water", 100);
$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(
"Symfony\Component\Stopwatch\Stopwatch",
\Symfony\Component\Stopwatch\Stopwatch::class,
$stopwatch->stopwatch()
);
}

public function testShouldGetMemory()
public function testShouldGetMemory(): void
{
$stopwatch = new Stopwatch();
$this->assertNull($stopwatch->memory());
Expand Down