Skip to content

Commit 954cbed

Browse files
xepozzvjik
andauthored
Raise minimum PHP version to 8.0, add rector (#59)
Co-authored-by: Sergei Predvoditelev <[email protected]>
1 parent 721cc67 commit 954cbed

18 files changed

+121
-154
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
os: >-
2929
['ubuntu-latest', 'windows-latest']
3030
php: >-
31-
['7.4', '8.0', '8.1']
31+
['8.0', '8.1']

.github/workflows/rector.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
on:
2+
pull_request:
3+
paths-ignore:
4+
- 'docs/**'
5+
- 'README.md'
6+
- 'CHANGELOG.md'
7+
- '.gitignore'
8+
- '.gitattributes'
9+
- 'infection.json.dist'
10+
- 'psalm.xml'
11+
12+
name: rector
13+
14+
jobs:
15+
rector:
16+
uses: yiisoft/actions/.github/workflows/rector.yml@master
17+
with:
18+
os: >-
19+
['ubuntu-latest']
20+
php: >-
21+
['8.0']

.github/workflows/static.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
os: >-
2929
['ubuntu-latest']
3030
php: >-
31-
['7.4', '8.0', '8.1']
31+
['8.0', '8.1']

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 3.0.1 under development
44

5-
- no changes in this release.
5+
- Enh #59: Raise minimum PHP version to 8.0 (@xepozz, @vjik)
66

77
## 3.0.0 September 07, 2022
88

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ request instance, dispatcher executes it produces a response instance.
2020

2121
## Requirements
2222

23-
- PHP 7.4 or higher.
23+
- PHP 8.0 or higher.
2424

2525
## Installation
2626

2727
The package could be installed with composer:
2828

2929
```shell
30-
composer require yiisoft/middleware-dispatcher --prefer-dist
30+
composer require yiisoft/middleware-dispatcher
3131
```
3232

3333
## General usage

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"source": "https://github.com/yiisoft/middleware-dispatcher"
1818
},
1919
"require": {
20-
"php": "^7.4|^8.0",
20+
"php": "^8.0",
2121
"psr/container": "^1.0|^2.0",
2222
"psr/event-dispatcher": "^1.0",
2323
"psr/http-message": "^1.0",
@@ -30,6 +30,7 @@
3030
"require-dev": {
3131
"nyholm/psr7": "^1.4",
3232
"phpunit/phpunit": "^9.5",
33+
"rector/rector": "^0.14.3",
3334
"roave/infection-static-analysis-plugin": "^1.16",
3435
"spatie/phpunit-watcher": "^1.23",
3536
"vimeo/psalm": "^4.18",

rector.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
6+
use Rector\Config\RectorConfig;
7+
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
8+
use Rector\Set\ValueObject\LevelSetList;
9+
10+
return static function (RectorConfig $rectorConfig): void {
11+
$rectorConfig->paths([
12+
__DIR__ . '/src',
13+
__DIR__ . '/tests',
14+
]);
15+
16+
// register a single rule
17+
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
18+
19+
// define sets of rules
20+
$rectorConfig->sets([
21+
LevelSetList::UP_TO_PHP_80,
22+
]);
23+
24+
$rectorConfig->skip([
25+
ClassPropertyAssignToConstructorPromotionRector::class,
26+
]);
27+
};

src/Event/AfterMiddleware.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@
1212
*/
1313
final class AfterMiddleware
1414
{
15-
private MiddlewareInterface $middleware;
16-
private ?ResponseInterface $response;
17-
1815
/**
1916
* @param MiddlewareInterface $middleware Middleware that was executed.
2017
* @param ResponseInterface|null $response Response generated by middleware or null in case there was an error.
2118
*/
22-
public function __construct(MiddlewareInterface $middleware, ?ResponseInterface $response)
23-
{
24-
$this->middleware = $middleware;
25-
$this->response = $response;
19+
public function __construct(
20+
private MiddlewareInterface $middleware,
21+
private ?ResponseInterface $response
22+
) {
2623
}
2724

2825
/**

src/Event/BeforeMiddleware.php

+4-7
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,14 @@
1212
*/
1313
final class BeforeMiddleware
1414
{
15-
private MiddlewareInterface $middleware;
16-
private ServerRequestInterface $request;
17-
1815
/**
1916
* @param MiddlewareInterface $middleware Middleware to be executed.
2017
* @param ServerRequestInterface $request Request to be passed to the middleware.
2118
*/
22-
public function __construct(MiddlewareInterface $middleware, ServerRequestInterface $request)
23-
{
24-
$this->middleware = $middleware;
25-
$this->request = $request;
19+
public function __construct(
20+
private MiddlewareInterface $middleware,
21+
private ServerRequestInterface $request
22+
) {
2623
}
2724

2825
/**

src/InvalidMiddlewareDefinitionException.php

+8-22
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
use function array_slice;
1414
use function count;
15-
use function get_class;
1615
use function gettype;
1716
use function is_array;
1817
use function is_bool;
@@ -23,19 +22,12 @@
2322

2423
final class InvalidMiddlewareDefinitionException extends InvalidArgumentException implements FriendlyExceptionInterface
2524
{
26-
/**
27-
* @var mixed
28-
*/
29-
private $definition;
3025
private string $definitionString;
3126

32-
/**
33-
* @param mixed $middlewareDefinition
34-
*/
35-
public function __construct($middlewareDefinition)
36-
{
37-
$this->definition = $middlewareDefinition;
38-
$this->definitionString = $this->convertDefinitionToString($middlewareDefinition);
27+
public function __construct(
28+
private mixed $definition
29+
) {
30+
$this->definitionString = $this->convertDefinitionToString($definition);
3931

4032
parent::__construct(
4133
'Parameter should be either PSR middleware class name or a callable. Got ' . $this->definitionString . '.'
@@ -199,13 +191,10 @@ private function isControllerWithNonExistAction(): bool
199191
&& class_exists($this->definition[0]);
200192
}
201193

202-
/**
203-
* @param mixed $middlewareDefinition
204-
*/
205-
private function convertDefinitionToString($middlewareDefinition): string
194+
private function convertDefinitionToString(mixed $middlewareDefinition): string
206195
{
207196
if (is_object($middlewareDefinition)) {
208-
return 'an instance of "' . get_class($middlewareDefinition) . '"';
197+
return 'an instance of "' . $middlewareDefinition::class . '"';
209198
}
210199

211200
if (is_string($middlewareDefinition)) {
@@ -224,10 +213,7 @@ private function convertDefinitionToString($middlewareDefinition): string
224213
return $this->convertToString($middlewareDefinition);
225214
}
226215

227-
/**
228-
* @param mixed $value
229-
*/
230-
private function convertToString($value): string
216+
private function convertToString(mixed $value): string
231217
{
232218
if (is_string($value)) {
233219
return '"' . $value . '"';
@@ -246,7 +232,7 @@ private function convertToString($value): string
246232
}
247233

248234
if (is_object($value)) {
249-
return get_class($value);
235+
return $value::class;
250236
}
251237

252238
return gettype($value);

src/MiddlewareDispatcher.php

+4-8
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,16 @@ final class MiddlewareDispatcher
1919
* @var MiddlewareStack|null The middleware stack.
2020
*/
2121
private ?MiddlewareStack $stack = null;
22-
private MiddlewareFactory $middlewareFactory;
23-
private ?EventDispatcherInterface $eventDispatcher;
2422

2523
/**
2624
* @var array[]|callable[]|string[]
2725
*/
2826
private array $middlewareDefinitions = [];
2927

30-
public function __construct(MiddlewareFactory $middlewareFactory, ?EventDispatcherInterface $eventDispatcher = null)
31-
{
32-
$this->middlewareFactory = $middlewareFactory;
33-
$this->eventDispatcher = $eventDispatcher;
28+
public function __construct(
29+
private MiddlewareFactory $middlewareFactory,
30+
private ?EventDispatcherInterface $eventDispatcher = null
31+
) {
3432
}
3533

3634
/**
@@ -68,8 +66,6 @@ public function dispatch(
6866
* typed parameters are automatically injected using dependency injection container.
6967
* Current request and handler could be obtained by type-hinting for {@see ServerRequestInterface}
7068
* and {@see RequestHandlerInterface}.
71-
*
72-
* @return self
7369
*/
7470
public function withMiddlewares(array $middlewareDefinitions): self
7571
{

src/MiddlewareFactory.php

+9-18
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,13 @@
2525
*/
2626
final class MiddlewareFactory
2727
{
28-
private ContainerInterface $container;
29-
private WrapperFactoryInterface $wrapperFactory;
30-
3128
/**
3229
* @param ContainerInterface $container Container to use for resolving definitions.
3330
*/
34-
public function __construct(ContainerInterface $container, WrapperFactoryInterface $wrapperFactory)
35-
{
36-
$this->container = $container;
37-
$this->wrapperFactory = $wrapperFactory;
31+
public function __construct(
32+
private ContainerInterface $container,
33+
private WrapperFactoryInterface $wrapperFactory
34+
) {
3835
}
3936

4037
/**
@@ -55,7 +52,7 @@ public function __construct(ContainerInterface $container, WrapperFactoryInterfa
5552
*
5653
* @throws InvalidMiddlewareDefinitionException
5754
*/
58-
public function create($middlewareDefinition): MiddlewareInterface
55+
public function create(array|callable|string $middlewareDefinition): MiddlewareInterface
5956
{
6057
if ($this->isMiddlewareClassDefinition($middlewareDefinition)) {
6158
/** @var MiddlewareInterface */
@@ -80,22 +77,18 @@ public function create($middlewareDefinition): MiddlewareInterface
8077
}
8178

8279
/**
83-
* @param mixed $definition
84-
*
8580
* @psalm-assert-if-true class-string<MiddlewareInterface> $definition
8681
*/
87-
private function isMiddlewareClassDefinition($definition): bool
82+
private function isMiddlewareClassDefinition(mixed $definition): bool
8883
{
8984
return is_string($definition)
9085
&& is_subclass_of($definition, MiddlewareInterface::class);
9186
}
9287

9388
/**
94-
* @param mixed $definition
95-
*
9689
* @psalm-assert-if-true array|Closure $definition
9790
*/
98-
private function isCallableDefinition($definition): bool
91+
private function isCallableDefinition(mixed $definition): bool
9992
{
10093
if ($definition instanceof Closure) {
10194
return true;
@@ -113,19 +106,17 @@ class_exists($definition[0]) ? get_class_methods($definition[0]) : [],
113106
}
114107

115108
/**
116-
* @param mixed $definition
117-
*
118109
* @psalm-assert-if-true ArrayDefinitionConfig $definition
119110
*/
120-
private function isArrayDefinition($definition): bool
111+
private function isArrayDefinition(mixed $definition): bool
121112
{
122113
if (!is_array($definition)) {
123114
return false;
124115
}
125116

126117
try {
127118
DefinitionValidator::validateArrayDefinition($definition);
128-
} catch (InvalidConfigException $e) {
119+
} catch (InvalidConfigException) {
129120
return false;
130121
}
131122

0 commit comments

Comments
 (0)