Skip to content

Commit 1a4e2af

Browse files
authored
refactor: Introduce Rector and apply it (#218)
1 parent 81e521d commit 1a4e2af

39 files changed

+814
-1501
lines changed

Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ INFECTION_BIN = vendor/bin/infection
3535
INFECTION = $(INFECTION_BIN) --skip-initial-tests --coverage=$(COVERAGE_DIR) --only-covered --show-mutations --min-msi=$(TARGET_MSI) --min-covered-msi=$(TARGET_MSI) --ansi --threads=max
3636
INFECTION_WITH_INITIAL_TESTS = $(INFECTION_BIN) --only-covered --show-mutations --min-msi=$(TARGET_MSI) --min-covered-msi=$(TARGET_MSI) --ansi --threads=max
3737

38+
RECTOR_BIN = vendor-bin/rector/vendor/bin/rector
39+
RECTOR = $(RECTOR_BIN)
40+
3841

3942
#
4043
# Commands
@@ -141,6 +144,14 @@ clear_coverage: ## Clears the coverage reports
141144
clear_coverage:
142145
rm -rf $(COVERAGE_DIR) || true
143146

147+
.PHONY: rector
148+
rector: $(RECTOR_BIN)
149+
$(RECTOR)
150+
151+
.PHONY: rector_lint
152+
rector_lint: $(RECTOR_BIN) dist
153+
$(RECTOR) --dry-run
154+
144155

145156
#
146157
# Rules from files
@@ -176,3 +187,8 @@ $(COVERAGE_JUNIT): $(PHPUNIT_BIN) $(SRC_TESTS_FILES) phpunit.xml.dist
176187

177188
$(INFECTION_BIN): vendor
178189
touch -c $@
190+
191+
192+
$(RECTOR_BIN): vendor
193+
composer bin rector install
194+
touch -c $@

phpstan-tests.neon.dist

+1-10
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,8 @@ parameters:
3232
- path: tests/Input/ParallelizationInputTest.php
3333
message: '#assertTrue\(\) with false will always evaluate to false\.#'
3434

35-
- path: tests/Process/DummyProcessFactory.php
36-
message: '#DummyProcess#'
37-
38-
- path: tests/Process/SymfonyProcessLauncherTest.php
39-
message: '#DummyProcess#'
40-
41-
- path: tests/Input/RawInputTest.php
42-
message: '#FakeInput#'
43-
4435
- path: tests/ParallelExecutorFactoryTest.php
45-
message: '#ParallelExecutorFactory::create\(\) expects callable#'
36+
message: '#ParallelExecutorFactory::create\(\) expects Closure#'
4637

4738
- path: tests/Fixtures/Command/LegacyCommand.php
4839
message: '#Static method .* is unused#'

rector.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <office@webmozarts.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
use Rector\Config\RectorConfig;
15+
use Rector\Php71\Rector\FuncCall\CountOnNullRector;
16+
use Rector\Set\ValueObject\LevelSetList;
17+
18+
return static function (RectorConfig $rectorConfig): void {
19+
$rectorConfig->paths([
20+
__DIR__.'/src',
21+
__DIR__.'/tests',
22+
]);
23+
24+
$rectorConfig->sets([
25+
LevelSetList::UP_TO_PHP_81,
26+
]);
27+
28+
$rectorConfig->skip([
29+
CountOnNullRector::class,
30+
]);
31+
};

src/ChunkedItemsIterator.php

+12-23
Original file line numberDiff line numberDiff line change
@@ -20,44 +20,39 @@
2020
use function array_map;
2121
use function count;
2222
use function explode;
23-
use function gettype;
2423
use function is_array;
2524
use function is_numeric;
26-
use function is_object;
2725
use function iter\chunk;
2826
use function iter\mapWithKeys;
2927
use function iter\values;
3028
use function Safe\stream_get_contents;
3129
use function sprintf;
30+
use function str_contains;
3231
use function str_replace;
3332
use const PHP_EOL;
3433

3534
final class ChunkedItemsIterator
3635
{
37-
/**
38-
* @var list<string>|Iterator<string>
39-
*/
40-
private iterable $items;
41-
4236
/**
4337
* @var Iterator<list<string>>
4438
*/
45-
private Iterator $itemsChunks;
39+
private readonly Iterator $itemsChunks;
4640

4741
/**
4842
* @var 0|positive-int|null
4943
*/
50-
private ?int $numberOfItems;
44+
private readonly ?int $numberOfItems;
5145

5246
/**
5347
* @internal Use the static factory methods instead.
5448
*
5549
* @param list<string>|Iterator<string> $items
5650
* @param positive-int $batchSize
5751
*/
58-
public function __construct(iterable $items, int $batchSize)
59-
{
60-
$this->items = $items;
52+
public function __construct(
53+
private readonly iterable $items,
54+
int $batchSize,
55+
) {
6156
$this->itemsChunks = chunk($items, $batchSize);
6257
$this->numberOfItems = is_array($items) ? count($items) : null;
6358
}
@@ -138,18 +133,16 @@ private static function normalizeItems(array $items): array
138133
}
139134

140135
/**
141-
* @param mixed $items
142-
*
143136
* @return Iterator<string>
144137
*/
145-
private static function normalizeItemStream($items): Iterator
138+
private static function normalizeItemStream(mixed $items): Iterator
146139
{
147140
Assert::isIterable(
148141
$items,
149142
sprintf(
150143
'Expected the fetched items to be a list or an iterable of strings. Got "%s".',
151144
// TODO: use get_debug_type when dropping PHP 7.4 support
152-
is_object($items) ? $items::class : gettype($items),
145+
get_debug_type($items),
153146
),
154147
);
155148

@@ -161,11 +154,7 @@ private static function normalizeItemStream($items): Iterator
161154
);
162155
}
163156

164-
/**
165-
* @param mixed $item
166-
* @param array-key $index
167-
*/
168-
private static function normalizeItem($item, $index): string
157+
private static function normalizeItem(mixed $item, int|string $index): string
169158
{
170159
if (is_numeric($item)) {
171160
return (string) $item;
@@ -176,12 +165,12 @@ private static function normalizeItem($item, $index): string
176165
sprintf(
177166
'The items are potentially passed to the child processes via the STDIN. For this reason they are expected to be string values. Got "%s" for the item "%s".',
178167
// TODO: use get_debug_type when dropping PHP 7.4 support
179-
is_object($item) ? $item::class : gettype($item),
168+
get_debug_type($item),
180169
$index,
181170
),
182171
);
183172
Assert::false(
184-
'' !== PHP_EOL && false !== mb_strpos($item, PHP_EOL),
173+
'' !== PHP_EOL && str_contains($item, PHP_EOL),
185174
sprintf(
186175
'An item cannot contain a line return. Got one for "%s" for the item "%s".',
187176
str_replace(PHP_EOL, '<lineReturn>', $item),

src/Configuration.php

+5-29
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,6 @@
2121

2222
final class Configuration
2323
{
24-
/**
25-
* @var positive-int
26-
*/
27-
private int $numberOfProcesses;
28-
29-
/**
30-
* @var positive-int
31-
*/
32-
private int $segmentSize;
33-
34-
/**
35-
* @var positive-int|null
36-
*/
37-
private ?int $numberOfSegments;
38-
39-
/**
40-
* @var positive-int|0|null
41-
*/
42-
private ?int $totalNumberOfBatches;
43-
4424
/**
4525
* @internal Use the static factory methods instead.
4626
*
@@ -50,15 +30,11 @@ final class Configuration
5030
* @param positive-int|0|null $totalNumberOfBatches
5131
*/
5232
public function __construct(
53-
int $numberOfProcesses,
54-
int $segmentSize,
55-
?int $numberOfSegments,
56-
?int $totalNumberOfBatches
33+
private readonly int $numberOfProcesses,
34+
private readonly int $segmentSize,
35+
private readonly ?int $numberOfSegments,
36+
private readonly ?int $totalNumberOfBatches,
5737
) {
58-
$this->numberOfProcesses = $numberOfProcesses;
59-
$this->segmentSize = $segmentSize;
60-
$this->numberOfSegments = $numberOfSegments;
61-
$this->totalNumberOfBatches = $totalNumberOfBatches;
6238
}
6339

6440
/**
@@ -72,7 +48,7 @@ public static function create(
7248
?int $numberOfItems,
7349
int $numberOfProcesses,
7450
int $segmentSize,
75-
int $batchSize
51+
int $batchSize,
7652
): self {
7753
// We always check those (and not the calculated ones) since they come from the command
7854
// configuration so an issue there hints on a misconfiguration which should be fixed.

src/Deprecation.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class Deprecation
2525
* @param string $message The message of the deprecation
2626
* @param mixed ...$args Values to insert in the message using printf() formatting
2727
*/
28-
public static function trigger(string $message, ...$args): void
28+
public static function trigger(string $message, mixed ...$args): void
2929
{
3030
trigger_deprecation(
3131
'webmozarts/console-parallelization',

src/ErrorHandler/LoggingErrorHandler.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818

1919
final class LoggingErrorHandler implements ErrorHandler
2020
{
21-
private ErrorHandler $decoratedErrorHandler;
22-
23-
public function __construct(?ErrorHandler $decoratedErrorHandler = null)
24-
{
25-
$this->decoratedErrorHandler = $decoratedErrorHandler ?? new NullErrorHandler();
21+
public function __construct(
22+
private readonly ErrorHandler $decoratedErrorHandler = new NullErrorHandler(),
23+
) {
2624
}
2725

2826
public function handleError(string $item, Throwable $throwable, Logger $logger): int

src/ErrorHandler/ResetServiceErrorHandler.php

+2-7
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,10 @@
2020

2121
final class ResetServiceErrorHandler implements ErrorHandler
2222
{
23-
private ResetInterface $resettable;
24-
private ErrorHandler $decoratedErrorHandler;
25-
2623
public function __construct(
27-
ResetInterface $resettable,
28-
?ErrorHandler $decoratedErrorHandler = null
24+
private readonly ResetInterface $resettable,
25+
private readonly ErrorHandler $decoratedErrorHandler = new NullErrorHandler(),
2926
) {
30-
$this->resettable = $resettable;
31-
$this->decoratedErrorHandler = $decoratedErrorHandler ?? new NullErrorHandler();
3227
}
3328

3429
public static function forContainer(?ContainerInterface $container): ErrorHandler

src/ErrorHandler/ThrowableCodeErrorHandler.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616
use Throwable;
1717
use Webmozart\Assert\Assert;
1818
use Webmozarts\Console\Parallelization\Logger\Logger;
19+
use function max;
1920

2021
final class ThrowableCodeErrorHandler implements ErrorHandler
2122
{
22-
private ErrorHandler $decoratedErrorHandler;
23-
24-
public function __construct(?ErrorHandler $decoratedErrorHandler = null)
25-
{
26-
$this->decoratedErrorHandler = $decoratedErrorHandler ?? new NullErrorHandler();
23+
public function __construct(
24+
private readonly ErrorHandler $decoratedErrorHandler = new NullErrorHandler(),
25+
) {
2726
}
2827

2928
public function handleError(string $item, Throwable $throwable, Logger $logger): int

src/Input/ChildCommandFactory.php

+5-20
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,30 @@
1818
use Webmozart\Assert\Assert;
1919
use function array_filter;
2020
use function array_map;
21-
use function array_merge;
2221
use function explode;
2322
use function Safe\getcwd;
2423
use function sprintf;
25-
use function strval;
2624

2725
/**
2826
* @internal
2927
*/
3028
final class ChildCommandFactory
3129
{
32-
private string $phpExecutable;
33-
private string $scriptPath;
34-
private string $commandName;
35-
private InputDefinition $commandDefinition;
36-
3730
public function __construct(
38-
string $phpExecutable,
39-
string $scriptPath,
40-
string $commandName,
41-
InputDefinition $commandDefinition
31+
private readonly string $phpExecutable,
32+
private readonly string $scriptPath,
33+
private readonly string $commandName,
34+
private readonly InputDefinition $commandDefinition,
4235
) {
4336
self::validateScriptPath($scriptPath);
44-
45-
$this->phpExecutable = $phpExecutable;
46-
$this->scriptPath = $scriptPath;
47-
$this->commandName = $commandName;
48-
$this->commandDefinition = $commandDefinition;
4937
}
5038

5139
/**
5240
* @return list<string>
5341
*/
5442
public function createChildCommand(InputInterface $input): array
5543
{
56-
return array_merge(
57-
$this->createBaseCommand($input),
58-
$this->getForwardedOptions($input),
59-
);
44+
return [...$this->createBaseCommand($input), ...$this->getForwardedOptions($input)];
6045
}
6146

6247
/**

0 commit comments

Comments
 (0)