Skip to content

Commit d827311

Browse files
authored
Add Rector for automated refactoring and code quality improvements (#6)
1 parent 2c6ea90 commit d827311

9 files changed

Lines changed: 25 additions & 23 deletions

File tree

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"phpunit/phpunit": "^9.5",
2727
"nyholm/psr7": "^1.8",
2828
"league/mime-type-detection": "^1.16",
29-
"php-cs-fixer/shim": "^3.75"
29+
"php-cs-fixer/shim": "^3.75",
30+
"rector/rector": "^2.0"
3031
},
3132
"minimum-stability": "stable",
3233
"prefer-stable": true,
@@ -50,10 +51,12 @@
5051
"vendor/bin/phpunit"
5152
],
5253
"lint-fix": [
53-
"PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix -v"
54+
"PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix -v",
55+
"vendor/bin/rector process"
5456
],
5557
"lint": [
56-
"PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix -v --dry-run"
58+
"PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix -v --dry-run",
59+
"vendor/bin/rector process --dry-run"
5760
]
5861
}
5962
}

rector.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Set\ValueObject\LevelSetList;
7+
8+
return RectorConfig::configure()
9+
->withPaths([__DIR__ . '/src',])
10+
->withSets([LevelSetList::UP_TO_PHP_80,])
11+
->withPreparedSets(deadCode: true, codeQuality: true, typeDeclarations: true, symfonyCodeQuality: true);

src/Metrics/MetricsCollection.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ class MetricsCollection
88
{
99
private array $metrics = [];
1010

11-
public function __construct()
12-
{
13-
}
14-
1511
public function addGauge(string $prefix, string $label, float|int $value): void
1612
{
1713
$this->metrics[$prefix][$label] = $value;

src/Middleware/MetricsMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(private string $metricsUrl = '', private ?MetricsInt
2222

2323
public function __invoke(ServerRequestInterface $request, $next)
2424
{
25-
if ($this->metrics === null || $this->metricsUrl === '') {
25+
if (!$this->metrics instanceof MetricsInterface || $this->metricsUrl === '') {
2626
return $next($request);
2727
}
2828

src/Middleware/StaticFileMiddleware.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private function getMimeType(string $filename): string
6969
}
7070
}
7171

72-
if (class_exists('\League\MimeTypeDetection\FinfoMimeTypeDetector')) {
72+
if (class_exists(FinfoMimeTypeDetector::class)) {
7373
$detector = new FinfoMimeTypeDetector();
7474

7575
$type = $detector->detectMimeTypeFromPath($filename);

src/RequestHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515

1616
class RequestHandler implements RequestHandlerInterface
1717
{
18-
private HttpKernelInterface $kernel;
1918
private HttpFoundationFactory $httpFoundationFactory;
2019
private PsrHttpFactory $httpMessageFactory;
2120

22-
public function __construct(HttpKernelInterface $kernel)
21+
public function __construct(private HttpKernelInterface $kernel)
2322
{
24-
$this->kernel = $kernel;
2523
$this->httpFoundationFactory = new HttpFoundationFactory();
2624
$this->httpMessageFactory = new PsrHttpFactory();
2725
}

src/Runner.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@
99

1010
class Runner implements RunnerInterface
1111
{
12-
private KernelInterface $kernel;
13-
private ServerFactory $serverFactory;
14-
15-
public function __construct(ServerFactory $serverFactory, KernelInterface $kernel)
12+
public function __construct(private ServerFactory $serverFactory, private KernelInterface $kernel)
1613
{
17-
$this->serverFactory = $serverFactory;
18-
$this->kernel = $kernel;
1914
}
2015

2116
public function run(): int

src/ServerFactory.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use CrazyGoat\ReactPHPRuntime\Middleware\ErrorMiddleware;
1010
use CrazyGoat\ReactPHPRuntime\Middleware\MetricsMiddleware;
1111
use CrazyGoat\ReactPHPRuntime\Middleware\StaticFileMiddleware;
12+
use Psr\Http\Message\ResponseInterface;
1213
use Psr\Http\Message\ServerRequestInterface;
1314
use Psr\Http\Server\RequestHandlerInterface;
1415
use React\EventLoop\Loop;
@@ -53,7 +54,7 @@ private function getOption(string $name, string $envName, array $options, mixed
5354
public function createServer(RequestHandlerInterface $requestHandler): LoopInterface
5455
{
5556
$loop = Loop::get();
56-
$loop->addSignal(SIGTERM, function (int $signal) {
57+
$loop->addSignal(SIGTERM, function (int $signal): void {
5758
exit(128 + $signal);
5859
});
5960

@@ -62,9 +63,7 @@ public function createServer(RequestHandlerInterface $requestHandler): LoopInter
6263
new ErrorMiddleware($this->kernel->isDebug()),
6364
new MetricsMiddleware('/metrics', new BasicMetric()),
6465
new StaticFileMiddleware($this->options['root_dir']),
65-
function (ServerRequestInterface $request) use ($requestHandler) {
66-
return $requestHandler->handle($request);
67-
},
66+
fn(ServerRequestInterface $request): ResponseInterface => $requestHandler->handle($request),
6867
);
6968

7069
$listen = sprintf('%s:%s', $this->options['host'], $this->options['port']);

var/.php-cs-fixer.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"php":"8.4.5","version":"3.75.0","indent":" ","lineEnding":"\n","rules":{"array_indentation":true,"array_syntax":{"syntax":"short"},"cast_spaces":true,"concat_space":{"spacing":"one"},"function_declaration":{"closure_fn_spacing":"none"},"method_argument_space":true,"new_with_parentheses":{"anonymous_class":false},"single_space_around_construct":{"constructs_followed_by_a_single_space":["abstract","as","case","catch","class","const","const_import","do","else","elseif","enum","final","finally","for","foreach","function","function_import","if","insteadof","interface","match","named_argument","namespace","new","private","protected","public","readonly","static","switch","trait","try","type_colon","use","use_lambda","while"],"constructs_preceded_by_a_single_space":["as","else","elseif","use_lambda"]},"trailing_comma_in_multiline":{"after_heredoc":true,"elements":["arrays","match","arguments","parameters"]},"binary_operator_spaces":{"default":"at_least_single_space"},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"braces_position":{"allow_single_line_empty_anonymous_classes":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_type_declaration":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"no_blank_lines_after_class_opening":true,"no_extra_blank_lines":{"tokens":["use"]},"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":true,"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"unary_operator_spaces":{"only_dec_inc":true},"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"no_trailing_whitespace_in_string":true,"no_unreachable_default_argument_value":true,"align_multiline_comment":true,"no_empty_comment":true,"no_unused_imports":true,"phpdoc_scalar":true,"phpdoc_single_line_var_spacing":true,"phpdoc_trim":true,"phpdoc_var_annotation_correct_order":true,"no_empty_statement":true,"no_spaces_around_offset":true,"declare_strict_types":true,"strict_comparison":true,"get_class_to_class_keyword":true,"no_superfluous_phpdoc_tags":true},"hashes":{"\/home\/piotr\/test\/reactphp-runtime\/src\/ServerFactory.php":"6a11ace0d2dc9916ea8831fc06ef4953","\/home\/piotr\/test\/reactphp-runtime\/src\/Runner.php":"caff1c06f3bf192c2badbe4a9b662dca","\/home\/piotr\/test\/reactphp-runtime\/src\/Runtime.php":"778850f18c39b354fe6201a1a0a51dca","\/home\/piotr\/test\/reactphp-runtime\/src\/RequestHandler.php":"8dd2be0d037f7d402826a2a645f4babd","\/home\/piotr\/test\/reactphp-runtime\/src\/Middleware\/ErrorMiddleware.php":"f6db3ed9f4532c991a1e2ceb439e9084","\/home\/piotr\/test\/reactphp-runtime\/src\/Middleware\/StaticFileMiddleware.php":"a239226ac873965f797c861a52f73224","\/home\/piotr\/test\/reactphp-runtime\/src\/Middleware\/MetricsMiddleware.php":"4ce946d3e5b829d9893cceaacd2b0498","\/home\/piotr\/test\/reactphp-runtime\/src\/Error\/DefaultErrorHandler.php":"65fdbcdf8afc5ca34ebfe5cd07a59cc2","\/home\/piotr\/test\/reactphp-runtime\/src\/Error\/ErrorHandlerInterface.php":"1c780cb54eac9639723858c5afa3c8d0","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/MetricsCollection.php":"6f8cdef7b8f6729de3eee552ad3a9be4","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/Formatter\/JsonMetricsFormatter.php":"48ee0bc12b6fe1cba3a3f7d2b500f64d","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/Formatter\/MetricsFormatterInterface.php":"417ea6e7f02ec131b331fed58aa3dd21","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/Formatter\/TextMetricsFormatter.php":"1811eaa51d8bf2be4b1177e9ea54657c","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/BasicMetric.php":"7c291f240f23118830e29350297debbd","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/MetricsInterface.php":"8333672126a4e715fa7b9434fc9494d0","\/home\/piotr\/test\/reactphp-runtime\/tests\/RuntimeTest.php":"fd69dcc386bbaf2dd3537d8e239e7b6f","\/home\/piotr\/test\/reactphp-runtime\/tests\/RunnerTest.php":"46e43746a1e8428f3c85f0133c9c4fc4","\/home\/piotr\/test\/reactphp-runtime\/tests\/ServerFactoryTest.php":"0d13bd892365292a51854a41a7cbd9db"}}
1+
{"php":"8.4.5","version":"3.75.0","indent":" ","lineEnding":"\n","rules":{"array_indentation":true,"array_syntax":{"syntax":"short"},"cast_spaces":true,"concat_space":{"spacing":"one"},"function_declaration":{"closure_fn_spacing":"none"},"method_argument_space":true,"new_with_parentheses":{"anonymous_class":false},"single_space_around_construct":{"constructs_followed_by_a_single_space":["abstract","as","case","catch","class","const","const_import","do","else","elseif","enum","final","finally","for","foreach","function","function_import","if","insteadof","interface","match","named_argument","namespace","new","private","protected","public","readonly","static","switch","trait","try","type_colon","use","use_lambda","while"],"constructs_preceded_by_a_single_space":["as","else","elseif","use_lambda"]},"trailing_comma_in_multiline":{"after_heredoc":true,"elements":["arrays","match","arguments","parameters"]},"binary_operator_spaces":{"default":"at_least_single_space"},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"braces_position":{"allow_single_line_empty_anonymous_classes":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_type_declaration":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"no_blank_lines_after_class_opening":true,"no_extra_blank_lines":{"tokens":["use"]},"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":true,"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"unary_operator_spaces":{"only_dec_inc":true},"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true,"no_trailing_whitespace_in_string":true,"no_unreachable_default_argument_value":true,"align_multiline_comment":true,"no_empty_comment":true,"no_unused_imports":true,"phpdoc_scalar":true,"phpdoc_single_line_var_spacing":true,"phpdoc_trim":true,"phpdoc_var_annotation_correct_order":true,"no_empty_statement":true,"no_spaces_around_offset":true,"declare_strict_types":true,"strict_comparison":true,"get_class_to_class_keyword":true,"no_superfluous_phpdoc_tags":true},"hashes":{"\/home\/piotr\/test\/reactphp-runtime\/src\/ServerFactory.php":"6a11ace0d2dc9916ea8831fc06ef4953","\/home\/piotr\/test\/reactphp-runtime\/src\/Runner.php":"caff1c06f3bf192c2badbe4a9b662dca","\/home\/piotr\/test\/reactphp-runtime\/src\/Runtime.php":"778850f18c39b354fe6201a1a0a51dca","\/home\/piotr\/test\/reactphp-runtime\/src\/RequestHandler.php":"8dd2be0d037f7d402826a2a645f4babd","\/home\/piotr\/test\/reactphp-runtime\/src\/Middleware\/ErrorMiddleware.php":"f6db3ed9f4532c991a1e2ceb439e9084","\/home\/piotr\/test\/reactphp-runtime\/src\/Middleware\/StaticFileMiddleware.php":"a239226ac873965f797c861a52f73224","\/home\/piotr\/test\/reactphp-runtime\/src\/Middleware\/MetricsMiddleware.php":"4ce946d3e5b829d9893cceaacd2b0498","\/home\/piotr\/test\/reactphp-runtime\/src\/Error\/DefaultErrorHandler.php":"65fdbcdf8afc5ca34ebfe5cd07a59cc2","\/home\/piotr\/test\/reactphp-runtime\/src\/Error\/ErrorHandlerInterface.php":"1c780cb54eac9639723858c5afa3c8d0","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/MetricsCollection.php":"6f8cdef7b8f6729de3eee552ad3a9be4","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/Formatter\/JsonMetricsFormatter.php":"48ee0bc12b6fe1cba3a3f7d2b500f64d","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/Formatter\/MetricsFormatterInterface.php":"417ea6e7f02ec131b331fed58aa3dd21","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/Formatter\/TextMetricsFormatter.php":"1811eaa51d8bf2be4b1177e9ea54657c","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/MetricsInterface.php":"8333672126a4e715fa7b9434fc9494d0","\/home\/piotr\/test\/reactphp-runtime\/tests\/RuntimeTest.php":"fd69dcc386bbaf2dd3537d8e239e7b6f","\/home\/piotr\/test\/reactphp-runtime\/tests\/RunnerTest.php":"46e43746a1e8428f3c85f0133c9c4fc4","\/home\/piotr\/test\/reactphp-runtime\/tests\/ServerFactoryTest.php":"0d13bd892365292a51854a41a7cbd9db","\/home\/piotr\/test\/reactphp-runtime\/src\/Metrics\/BasicMetric.php":"3724889e1bbf5cba0084b611634c6154"}}

0 commit comments

Comments
 (0)