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
6 changes: 3 additions & 3 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ jobs:
strategy:
fail-fast: true
matrix:
php: [7.2, 7.3, 7.4]
php: [8.0, 8.1]

name: PHP ${{ matrix.php }}

steps:
- uses: actions/checkout@v1

- name: Setup PHP version
uses: shivammathur/setup-php@v1
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extension-csv: mbstring
extensions: mbstring
coverage: none

- name: Check PHP version
Expand Down
28 changes: 28 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . DIRECTORY_SEPARATOR . 'tests')
->in(__DIR__ . DIRECTORY_SEPARATOR . 'src')
->append(['.php_cs']);

$rules = [
'@Symfony' => true,
'phpdoc_no_empty_return' => false,
'array_syntax' => ['syntax' => 'short'],
'yoda_style' => false,
'binary_operator_spaces' => [
'operators' => [
'=>' => 'align',
'=' => 'align',
],
],
'concat_space' => ['spacing' => 'one'],
'not_operator_with_space' => false,
];

$rules['increment_style'] = ['style' => 'post'];

return (new PhpCsFixer\Config())
->setUsingCache(true)
->setRules($rules)
->setFinder($finder);
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.0] - 2021-12-22

### Changed
- Minimum PHP version to 8.0

## [1.0.0] (2019-12-06)
### Added
- classes for Server-Timing measurement
- PSR-15 middleware to gather default metrics
- Laravel middleware
- Laravel middleware
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"license": "MIT",
"homepage": "https://github.com/fetzi/server-timing",
"require": {
"php": "^7.2",
"php": "^8.0",
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5",
"squizlabs/php_codesniffer": "^3.5"
"friendsofphp/php-cs-fixer": "^3.4",
"phpunit/phpunit": "^9.0",
"rector/rector": "^0.12.8"
},
"autoload": {
"psr-4": {
Expand All @@ -31,7 +32,8 @@
},
"scripts": {
"test": "vendor/bin/phpunit",
"lint": "vendor/bin/phpcs --standard=PSR2,PSR12 src/"
"lint": "php-cs-fixer fix -v --dry-run",
"fix": "php-cs-fixer fix -v"
},
"config": {
"sort-packages": true
Expand Down
31 changes: 11 additions & 20 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Server-Timing Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" backupStaticAttributes="false" colors="true" verbose="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Server-Timing Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
27 changes: 27 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

use Rector\Core\Configuration\Option;
use Rector\Php74\Rector\Property\TypedPropertyRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
// get parameters
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [
__DIR__ . '/src'
]);

// Define what rule sets will be applied
$containerConfigurator->import(SetList::CODE_QUALITY);
$containerConfigurator->import(LevelSetList::UP_TO_PHP_80);

// get services (needed for register a single rule)
// $services = $containerConfigurator->services();

// register a single rule
// $services->set(TypedPropertyRector::class);
};
8 changes: 1 addition & 7 deletions src/Laravel/ServerTimingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,8 @@ class ServerTimingMiddleware
{
private const REQUEST_TIME = 'REQUEST_TIME_FLOAT';

/**
* @var ServerTimings
*/
private $serverTimings;

public function __construct(ServerTimings $serverTimings)
public function __construct(private ServerTimings $serverTimings)
{
$this->serverTimings = $serverTimings;
}

public function handle($request, Closure $next)
Expand Down
32 changes: 7 additions & 25 deletions src/ServerTiming.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,18 @@

namespace Fetzi\ServerTiming;

class ServerTiming
class ServerTiming implements \Stringable
{
/**
* @var string
*/
private $name;

/**
* @var string
*/
private $description;
private ?float $start = null;

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

/**
* @var float
*/
private $end;
private ?float $end = null;

public function __construct(string $name, ?string $description = null)
public function __construct(private string $name, private ?string $description = null)
{
$this->name = $name;
$this->description = $description;
}

/**
* captures the starting microtime value for the server timing
* captures the starting microtime value for the server timing.
*
* @param float $fixedValue allows to set start to a predefined value
*/
Expand All @@ -41,14 +23,14 @@ public function start(float $fixedValue = null): void
}

/**
* captures the end microtime value for the server timing
* captures the end microtime value for the server timing.
*/
public function stop(): void
{
$this->end = microtime(true);
}

public function __toString()
public function __toString(): string
{
$timing = $this->name;

Expand Down
11 changes: 2 additions & 9 deletions src/ServerTimingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Fetzi\ServerTiming;

use Fetzi\ServerTiming\ServerTimings;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
Expand All @@ -12,18 +11,12 @@ class ServerTimingMiddleware implements MiddlewareInterface
{
private const REQUEST_TIME = 'REQUEST_TIME_FLOAT';

/**
* @var ServerTimings
*/
private $serverTimings;

public function __construct(ServerTimings $serverTimings)
public function __construct(private ServerTimings $serverTimings)
{
$this->serverTimings = $serverTimings;
}

/**
* @inheritdoc
* {@inheritdoc}
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
Expand Down
19 changes: 7 additions & 12 deletions src/ServerTimings.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,26 @@

class ServerTimings
{
/**
* @var array
*/
private $timings = [];
private array $timings = [];

/**
* creates a new ServerTiming instance and registers it
* creates a new ServerTiming instance and registers it.
*
* @param string $name the name of the server timing
* @param string $description the description for the server timing
* @param string $name the name of the server timing
* @param string $description the description for the server timing
*
* @return ServerTiming
*/
public function create(string $name, ?string $description = null)
{
$serverTiming = new ServerTiming($name, $description);
$serverTiming = new ServerTiming($name, $description);
$this->timings[] = $serverTiming;

return $serverTiming;
}

/**
* returns the formatted Server-Timing header value
* returns the formatted Server-Timing header value.
*
* @return string
*/
Expand All @@ -42,11 +39,9 @@ public function getTimings(): ?string
}

/**
* adds the stored server timings to the given response instance
* adds the stored server timings to the given response instance.
*
* @param ResponseInterface $response the response to add to
*
* @return ResponseInterface
*/
public function addToResponse(ResponseInterface $response): ResponseInterface
{
Expand Down
6 changes: 3 additions & 3 deletions tests/ServerTimingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class ServerTimingsTest extends TestCase
{
public function testWithEmptyTimings()
{
$serverTimings = new ServerTimings;
$response = $this->prophesize(ResponseInterface::class);
$serverTimings = new ServerTimings();
$response = $this->prophesize(ResponseInterface::class);

$response->withAddedHeader()->shouldNotBeCalled();

Expand All @@ -20,7 +20,7 @@ public function testWithEmptyTimings()

public function testWithAddedTimings()
{
$serverTimings = new ServerTimings;
$serverTimings = new ServerTimings();
$serverTimings->create('foo');
$serverTimings->create('bar');
$response = $this->prophesize(ResponseInterface::class);
Expand Down