Skip to content

Commit 2dc6fb2

Browse files
authored
Merge pull request #1015 from WyriHaximus/add-rector
Add Rector
2 parents a23855e + e01ebd4 commit 2dc6fb2

12 files changed

+142
-96
lines changed

Makefile

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ SHELL=bash
33

44
.PHONY: *
55

6-
COMPOSER_SHOW_EXTENSION_LIST=$(shell composer show -t | grep -o "\-\-\(ext-\).\+" | sort | uniq | cut -d- -f4- | tr -d '\n' | grep . | sed '/^$$/d' | xargs | sed -e 's/ /, /g' | tr -cd '[:alnum:],' | sed 's/.$$//')
6+
COMPOSER_SHOW_EXTENSION_LIST_PROD=$(shell composer show -t | grep -o "\-\-\(ext-\).\+" | sort | uniq | cut -d- -f4- | tr -d '\n' | grep . | sed '/^$$/d' | xargs | sed -e 's/ /, /g' | tr -cd '[:alnum:],' | sed 's/.$$//')
7+
COMPOSER_SHOW_EXTENSION_LIST_DEV=$(shell composer show -s | grep -o "\(ext-\).\+" | sort | uniq | cut -d- -f2- | cut -d" " -f1 | xargs | sed -e 's/ /, /g' | tr -cd '[:alnum:],')
8+
COMPOSER_SHOW_EXTENSION_LIST=$(shell echo "${COMPOSER_SHOW_EXTENSION_LIST_PROD},${COMPOSER_SHOW_EXTENSION_LIST_DEV}")
79
SLIM_DOCKER_IMAGE=$(shell php -r 'echo count(array_intersect(["gd", "vips"], explode(",", "${COMPOSER_SHOW_EXTENSION_LIST}"))) > 0 ? "" : "-slim";')
10+
NTS_OR_ZTS_DOCKER_IMAGE=$(shell php -r 'echo count(array_intersect(["parallel"], explode(",", "${COMPOSER_SHOW_EXTENSION_LIST}"))) > 0 ? "zts" : "nts";')
811
PHP_VERSION:=$(shell docker run --rm -v "`pwd`:`pwd`" jess/jq jq -r -c '.config.platform.php' "`pwd`/composer.json" | php -r "echo str_replace('|', '.', explode('.', implode('|', explode('.', stream_get_contents(STDIN), 2)), 2)[0]);")
9-
CONTAINER_NAME=$(shell echo "ghcr.io/wyrihaximusnet/php:${PHP_VERSION}-nts-alpine${SLIM_DOCKER_IMAGE}-dev")
12+
CONTAINER_NAME=$(shell echo "ghcr.io/wyrihaximusnet/php:${PHP_VERSION}-${NTS_OR_ZTS_DOCKER_IMAGE}-alpine${SLIM_DOCKER_IMAGE}-dev")
1013
COMPOSER_CACHE_DIR=$(shell composer config --global cache-dir -q || echo ${HOME}/.composer-php/cache)
1114
COMPOSER_CONTAINER_CACHE_DIR=$(shell docker run --rm -it ${CONTAINER_NAME} composer config --global cache-dir -q || echo ${HOME}/.composer-php/cache)
1215

@@ -35,32 +38,34 @@ endif
3538
all: ## Runs everything ####
3639
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | grep -v "####" | awk 'BEGIN {FS = ":.*?## "}; {printf "%s\n", $$1}' | xargs --open-tty $(MAKE)
3740

38-
3941
syntax-php: ## Lint PHP syntax ##*LH*##
4042
$(DOCKER_RUN) vendor/bin/parallel-lint --exclude vendor .
4143

44+
rector-upgrade: ## Upgrade any automatically upgradable old code ###
45+
$(DOCKER_RUN) vendor/bin/rector -c ./etc/qa/rector.php
46+
4247
cs-fix: ## Fix any automatically fixable code style issues ###
4348
$(DOCKER_RUN) vendor/bin/phpcbf --parallel=$(THREADS) --cache=./var/.phpcs.cache.json --standard=./etc/qa/phpcs.xml || $(DOCKER_RUN) vendor/bin/phpcbf --parallel=$(THREADS) --cache=./var/.phpcs.cache.json --standard=./etc/qa/phpcs.xml || $(DOCKER_RUN) vendor/bin/phpcbf --parallel=$(THREADS) --cache=./var/.phpcs.cache.json --standard=./etc/qa/phpcs.xml -vvvv
4449

4550
cs: ## Check the code for code style issues ##*LCH*##
4651
$(DOCKER_RUN) vendor/bin/phpcs --parallel=$(THREADS) --cache=./var/.phpcs.cache.json --standard=./etc/qa/phpcs.xml
4752

4853
stan: ## Run static analysis (PHPStan) ##*LCH*##
49-
$(DOCKER_RUN) vendor/bin/phpstan analyse src tests --level max --ansi -c ./etc/qa/phpstan.neon
54+
$(DOCKER_RUN) vendor/bin/phpstan analyse etc src tests --level max --ansi -c ./etc/qa/phpstan.neon
5055

5156
unit-testing: ## Run tests ##*A*##
5257
$(DOCKER_RUN) vendor/bin/phpunit --colors=always -c ./etc/qa/phpunit.xml --coverage-text --coverage-html ./var/tests-unit-coverage-html --coverage-clover ./var/tests-unit-clover-coverage.xml
5358
$(DOCKER_RUN) test -n "$(COVERALLS_REPO_TOKEN)" && test -n "$(COVERALLS_RUN_LOCALLY)" && test -f ./var/tests-unit-clover-coverage.xml && vendor/bin/php-coveralls -v --coverage_clover ./build/logs/clover.xml --json_path ./var/tests-unit-clover-coverage-upload.json || true
5459

55-
unit-testing-raw: ## Run tests ##*D*##
60+
unit-testing-raw: ## Run tests ##*D*## ####
5661
php vendor/phpunit/phpunit/phpunit --colors=always -c ./etc/qa/phpunit.xml --coverage-text --coverage-html ./var/tests-unit-coverage-html --coverage-clover ./var/tests-unit-clover-coverage.xml
5762
test -n "$(COVERALLS_REPO_TOKEN)" && test -n "$(COVERALLS_RUN_LOCALLY)" && test -f ./var/tests-unit-clover-coverage.xml && ./vendor/bin/php-coveralls -v --coverage_clover ./build/logs/clover.xml --json_path ./var/tests-unit-clover-coverage-upload.json || true
5863

5964
mutation-testing: ## Run mutation testing ##*LCH*##
60-
$(DOCKER_RUN) vendor/bin/infection --ansi --log-verbosity=all --threads=$(THREADS) || (cat ./var/infection.log && false)
65+
$(DOCKER_RUN) vendor/bin/infection --ansi --log-verbosity=all --ignore-msi-with-no-mutations --threads=$(THREADS) || (cat ./var/infection.log && false)
6166

6267
mutation-testing-raw: ## Run mutation testing ####
63-
vendor/bin/infection --ansi --log-verbosity=all --threads=$(THREADS) || (cat ./var/infection.log && false)
68+
vendor/bin/infection --ansi --log-verbosity=all --ignore-msi-with-no-mutations --threads=$(THREADS) || (cat ./var/infection.log && false)
6469

6570
composer-require-checker: ## Ensure we require every package used in this package directly ##*C*##
6671
$(DOCKER_RUN) vendor/bin/composer-require-checker --ignore-parse-errors --ansi -vvv --config-file=./etc/qa/composer-require-checker.json
@@ -89,6 +94,7 @@ outdated: ### Show outdated dependencies ####
8994
shell: ## Provides Shell access in the expected environment ####
9095
$(DOCKER_RUN) bash
9196

97+
9298
help: ## Show this help ####
9399
@printf "\033[33mUsage:\033[0m\n make [target]\n\n\033[33mTargets:\033[0m\n"
94100
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[32m%-32s\033[0m %s\n", $$1, $$2}' | tr -d '#'

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"php-parallel-lint/php-parallel-lint": "^1.4.0",
2323
"phpstan/phpstan": "^2.1.14",
2424
"phpunit/phpunit": "^12.1.4",
25+
"rector/rector": "^2.0.15",
2526
"roave/backward-compatibility-check": "^8.13.0",
2627
"squizlabs/php_codesniffer": "^3.12.2",
2728
"wyrihaximus/coding-standard": "^2.17.0",
@@ -31,7 +32,8 @@
3132
"wyrihaximus/makefiles": "^0.4.2"
3233
},
3334
"conflict": {
34-
"composer/pcre": "<3.3.2"
35+
"composer/pcre": "<3.3.2",
36+
"wyrihaximus/makefiles": "<0.4.0"
3537
},
3638
"suggest": {
3739
"wyrihaximus/async-test-utilities": "The recommended addition to this package when building ReactPHP packages and projects.",

composer.lock

Lines changed: 60 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

etc/qa/composer-require-checker.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"symbol-whitelist" : [
33
"null", "true", "false",
44
"static", "self", "parent",
5-
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "object"
5+
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "object",
6+
"Rector\\Configuration\\RectorConfigBuilder", "Rector\\Config\\RectorConfig"
67
],
78
"php-core-extensions" : [
89
"Core",

etc/qa/composer-unused.php

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,20 @@
44

55
use ComposerUnused\ComposerUnused\Configuration\Configuration;
66
use ComposerUnused\ComposerUnused\Configuration\NamedFilter;
7-
use ComposerUnused\ComposerUnused\Configuration\PatternFilter;
8-
use Webmozart\Glob\Glob;
97

10-
return static function (Configuration $config): Configuration {
11-
return $config
12-
->addNamedFilter(NamedFilter::fromString('ecoapm/libyear'))
13-
->addNamedFilter(NamedFilter::fromString('ergebnis/composer-normalize'))
14-
->addNamedFilter(NamedFilter::fromString('ergebnis/phpunit-slow-test-detector'))
15-
->addNamedFilter(NamedFilter::fromString('icanhazstring/composer-unused'))
16-
->addNamedFilter(NamedFilter::fromString('infection/infection'))
17-
->addNamedFilter(NamedFilter::fromString('maglnet/composer-require-checker'))
18-
->addNamedFilter(NamedFilter::fromString('php-coveralls/php-coveralls'))
19-
->addNamedFilter(NamedFilter::fromString('php-parallel-lint/php-console-highlighter'))
20-
->addNamedFilter(NamedFilter::fromString('php-parallel-lint/php-parallel-lint'))
21-
->addNamedFilter(NamedFilter::fromString('phpstan/phpstan'))
22-
->addNamedFilter(NamedFilter::fromString('roave/backward-compatibility-check'))
23-
->addNamedFilter(NamedFilter::fromString('squizlabs/php_codesniffer'))
24-
->addNamedFilter(NamedFilter::fromString('wyrihaximus/coding-standard'))
25-
->addNamedFilter(NamedFilter::fromString('wyrihaximus/phpstan-rules-wrapper'));
26-
};
8+
return static fn (Configuration $config): Configuration => $config
9+
->addNamedFilter(NamedFilter::fromString('ecoapm/libyear'))
10+
->addNamedFilter(NamedFilter::fromString('ergebnis/composer-normalize'))
11+
->addNamedFilter(NamedFilter::fromString('ergebnis/phpunit-slow-test-detector'))
12+
->addNamedFilter(NamedFilter::fromString('icanhazstring/composer-unused'))
13+
->addNamedFilter(NamedFilter::fromString('infection/infection'))
14+
->addNamedFilter(NamedFilter::fromString('maglnet/composer-require-checker'))
15+
->addNamedFilter(NamedFilter::fromString('php-coveralls/php-coveralls'))
16+
->addNamedFilter(NamedFilter::fromString('php-parallel-lint/php-console-highlighter'))
17+
->addNamedFilter(NamedFilter::fromString('php-parallel-lint/php-parallel-lint'))
18+
->addNamedFilter(NamedFilter::fromString('phpstan/phpstan'))
19+
->addNamedFilter(NamedFilter::fromString('rector/rector'))
20+
->addNamedFilter(NamedFilter::fromString('roave/backward-compatibility-check'))
21+
->addNamedFilter(NamedFilter::fromString('squizlabs/php_codesniffer'))
22+
->addNamedFilter(NamedFilter::fromString('wyrihaximus/coding-standard'))
23+
->addNamedFilter(NamedFilter::fromString('wyrihaximus/phpstan-rules-wrapper'));

etc/qa/phpcs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<arg name="cache" value="../../var/.phpcs.cache" /> <!-- cache the results and don't commit them -->
77
<arg value="np" /> <!-- n = ignore warnings, p = show progress -->
88

9+
<file>../../etc</file>
910
<file>../../src</file>
1011
<file>../../tests</file>
1112

etc/qa/psalm.xml

Lines changed: 0 additions & 59 deletions
This file was deleted.

etc/qa/rector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use WyriHaximus\TestUtilities\RectorConfig;
6+
7+
return RectorConfig::configure(dirname(__DIR__, 2));

infection.json.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"source": {
44
"directories": [
55
"src"
6+
],
7+
"excludes": [
8+
"RectorConfig.php"
69
]
710
},
811
"logs": {

src/RectorConfig.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WyriHaximus\TestUtilities;
6+
7+
use Rector\Config;
8+
use Rector\Configuration\RectorConfigBuilder;
9+
10+
final class RectorConfig
11+
{
12+
public static function configure(string $packageRootPath): RectorConfigBuilder
13+
{
14+
return Config\RectorConfig::configure()
15+
->withPaths([
16+
$packageRootPath . '/etc',
17+
$packageRootPath . '/src',
18+
$packageRootPath . '/tests',
19+
])
20+
->withAttributesSets(all: true)
21+
->withComposerBased(twig: true, doctrine: true, phpunit: true, symfony: true)
22+
->withPreparedSets(
23+
typeDeclarations: true,
24+
codeQuality: true,
25+
)
26+
->withPhpSets();
27+
}
28+
}

src/TestCase.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,22 +100,20 @@ final protected function rmdir(string $dir): void
100100
continue;
101101
}
102102

103-
if (unlink($node->getPathname()) !== true) {
103+
if (! unlink($node->getPathname())) {
104104
throw ErrorExceptionFactory::create('Error deleting file: ' . $node->getPathname());
105105
}
106106
}
107107

108-
if (@rmdir($dir) !== true) {
108+
if (! @rmdir($dir)) {
109109
throw ErrorExceptionFactory::create('Error deleting directory: ' . $dir);
110110
}
111111
}
112112

113113
final protected function getTmpDir(): string
114114
{
115-
if (! file_exists($this->tmpDir)) {
116-
if (@mkdir($this->tmpDir, self::DEFAULT_MODE, true) !== true) {
117-
throw ErrorExceptionFactory::create('Error creating directory: ' . $this->tmpDir);
118-
}
115+
if (! file_exists($this->tmpDir) && ! @mkdir($this->tmpDir, self::DEFAULT_MODE, true)) {
116+
throw ErrorExceptionFactory::create('Error creating directory: ' . $this->tmpDir);
119117
}
120118

121119
return $this->tmpDir;

tests/TestCaseTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace WyriHaximus\Tests\TestUtilities;
66

7+
use Override;
78
use PHPUnit\Framework\Attributes\DataProvider;
89
use PHPUnit\Framework\Attributes\Test;
910
use WyriHaximus\TestUtilities\TestCase;
@@ -13,7 +14,7 @@
1314
use function Safe\file_get_contents;
1415
use function Safe\file_put_contents;
1516
use function Safe\mkdir;
16-
use function strpos;
17+
use function str_starts_with;
1718
use function strtoupper;
1819
use function substr;
1920
use function sys_get_temp_dir;
@@ -30,11 +31,13 @@ final class TestCaseTest extends TestCase
3031

3132
private string $previousTemporaryDirectory = '';
3233

34+
#[Override]
3335
protected function setUp(): void
3436
{
3537
parent::setUp();
3638
}
3739

40+
#[Override]
3841
protected function tearDown(): void
3942
{
4043
parent::tearDown();
@@ -63,7 +66,7 @@ public function recursiveDirectoryCreation(): void
6366
#[DataProvider('provideTemporaryDirectory')]
6467
public function testTemporaryDirectoryAndGetFilesInDirectory(string $int): void
6568
{
66-
static::assertTrue(strtoupper(substr(PHP_OS, TestCase::WIN_START, TestCase::WIN_END)) === 'WIN' ? strpos(TestCase::WINDOWS_TEMP_DIR_PREFIX, sys_get_temp_dir()) === 0 : strpos($this->getTmpDir(), sys_get_temp_dir()) === 0);
69+
static::assertTrue(strtoupper(substr(PHP_OS, TestCase::WIN_START, TestCase::WIN_END)) === 'WIN' ? str_starts_with(TestCase::WINDOWS_TEMP_DIR_PREFIX, sys_get_temp_dir()) : str_starts_with($this->getTmpDir(), sys_get_temp_dir()));
6770
static::assertNotSame($this->getTmpDir(), $this->previousTemporaryDirectory);
6871

6972
$dir = $this->getTmpDir() . $this->getRandomNameSpace() . DIRECTORY_SEPARATOR;

0 commit comments

Comments
 (0)