diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index a70b2c1..a66018c 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: true matrix: - php: [7.2, 7.3, 7.4] + php: [8.0, 8.1] name: PHP ${{ matrix.php }} @@ -16,10 +16,10 @@ jobs: - 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 diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 0000000..4dad68e --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,28 @@ +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); diff --git a/CHANGELOG.md b/CHANGELOG.md index ce2639b..7273c2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 \ No newline at end of file +- Laravel middleware diff --git a/composer.json b/composer.json index 30141f7..63b75a2 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -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 diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 66bc256..0f3add2 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,22 +1,13 @@ - - - - tests - - - - - src/ - - + + + + src/ + + + + + tests + + diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..fea70e3 --- /dev/null +++ b/rector.php @@ -0,0 +1,27 @@ +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); +}; diff --git a/src/Laravel/ServerTimingMiddleware.php b/src/Laravel/ServerTimingMiddleware.php index cb37c6d..18167bd 100644 --- a/src/Laravel/ServerTimingMiddleware.php +++ b/src/Laravel/ServerTimingMiddleware.php @@ -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) diff --git a/src/ServerTiming.php b/src/ServerTiming.php index f18192a..b9f33c8 100644 --- a/src/ServerTiming.php +++ b/src/ServerTiming.php @@ -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 */ @@ -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; diff --git a/src/ServerTimingMiddleware.php b/src/ServerTimingMiddleware.php index f26e570..976816c 100644 --- a/src/ServerTimingMiddleware.php +++ b/src/ServerTimingMiddleware.php @@ -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; @@ -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 { diff --git a/src/ServerTimings.php b/src/ServerTimings.php index 7463e77..1f06e2f 100644 --- a/src/ServerTimings.php +++ b/src/ServerTimings.php @@ -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 */ @@ -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 { diff --git a/tests/ServerTimingsTest.php b/tests/ServerTimingsTest.php index 84373cc..2d0a955 100644 --- a/tests/ServerTimingsTest.php +++ b/tests/ServerTimingsTest.php @@ -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(); @@ -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);