Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade the codebase up to PHP 8.1 syntax using Rector #1860

Merged
merged 1 commit into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ PHP_CS_FIXER_URL="https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download
PHP_CS_FIXER_CACHE=.php_cs.cache

PHPSTAN=./vendor/bin/phpstan
RECTOR=./vendor/bin/rector

PSALM=./.tools/psalm
PSALM_URL="https://github.com/vimeo/psalm/releases/download/v4.15.0/psalm.phar"
Expand Down Expand Up @@ -96,6 +97,14 @@ psalm-baseline: vendor
psalm: vendor $(PSALM)
$(PSALM) --threads=max

.PHONY: rector
rector: vendor $(RECTOR)
$(RECTOR) process

.PHONY: rector-check
rector-check: vendor $(RECTOR)
$(RECTOR) process --dry-run

.PHONY: validate
validate:
composer validate --strict
Expand All @@ -119,7 +128,7 @@ profile: vendor $(BENCHMARK_SOURCES)

.PHONY: autoreview
autoreview: ## Runs various checks (static analysis & AutoReview test suite)
autoreview: phpstan psalm validate test-autoreview
autoreview: phpstan psalm validate test-autoreview rector-check

.PHONY: test
test: ## Runs all the tests
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,15 @@
"brianium/paratest": "^6.3",
"fidry/makefile": "^0.2.0",
"helmich/phpunit-json-assert": "^3.0",
"phpspec/prophecy-phpunit": "^2.0",
"phpspec/prophecy": "^1.15",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/extension-installer": "^1.1.0",
"phpstan/phpstan": "^1.10.15",
"phpstan/phpstan-phpunit": "^1.0.0",
"phpstan/phpstan-strict-rules": "^1.1.0",
"phpstan/phpstan-webmozart-assert": "^1.0.2",
"phpunit/phpunit": "^9.5.5",
"rector/rector": "^0.16.0",
"symfony/phpunit-bridge": "^5.4 || ^6.0",
"symfony/yaml": "^5.4 || ^6.0",
"thecodingmachine/phpstan-safe-rule": "^1.2.0"
Expand Down
63 changes: 62 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 58 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Property\ReadOnlyPropertyRector;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src',
// TODO uncomment as a separate PR
// __DIR__ . '/tests/phpunit',
]);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
]);

$rectorConfig->skip([
ReadOnlyPropertyRector::class => [
// property can't be readonly as it's returned by reference and may be updated
__DIR__ . '/src/TestFramework/Coverage/TestLocations.php',
],
]);
};
8 changes: 4 additions & 4 deletions src/Command/ConfigureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
use function Safe\json_encode;
use function Safe\sprintf;
use stdClass;
use function strpos;
use function str_starts_with;
use Symfony\Component\Console\Input\InputOption;

/**
Expand Down Expand Up @@ -228,7 +228,7 @@ private function saveConfig(
);
}

private function abort(): void
private function abort(): never
{
throw new RuntimeException('Configuration generation aborted');
}
Expand All @@ -244,10 +244,10 @@ private function getJsonSchemaPathOrUrl(): string
try {
$version = InstalledVersions::getPrettyVersion(Application::PACKAGE_NAME);

if ($version === null || strpos($version, 'dev-') === 0) {
if ($version === null || str_starts_with($version, 'dev-')) {
$version = 'master';
}
} catch (OutOfBoundsException $e) {
} catch (OutOfBoundsException) {
$version = 'master';
}

Expand Down
2 changes: 1 addition & 1 deletion src/Command/RunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ private function runConfigurationCommand(Locator $locator, IO $io): void
{
try {
$locator->locateOneOf(SchemaConfigurationLoader::POSSIBLE_DEFAULT_CONFIG_FILES);
} catch (FileNotFound|FileOrDirectoryNotFound $exception) {
} catch (FileNotFound|FileOrDirectoryNotFound) {
$configureCommand = $this->getApplication()->find('configure');

$args = [
Expand Down
5 changes: 1 addition & 4 deletions src/Config/ConsoleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@
*/
class ConsoleHelper
{
private FormatterHelper $formatterHelper;

public function __construct(FormatterHelper $formatterHelper)
public function __construct(private readonly FormatterHelper $formatterHelper)
{
$this->formatterHelper = $formatterHelper;
}

public function writeSection(OutputInterface $output, string $text, string $style = 'bg=blue;fg=white'): void
Expand Down
9 changes: 3 additions & 6 deletions src/Config/Guesser/PhpUnitPathGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
namespace Infection\Config\Guesser;

use stdClass;
use function strpos;
use function str_contains;
use function trim;

/**
Expand All @@ -46,11 +46,8 @@ final class PhpUnitPathGuesser
{
private const CURRENT_DIR_PATH = '.';

private stdClass $composerJsonContent;

public function __construct(stdClass $composerJsonContent)
public function __construct(private readonly stdClass $composerJsonContent)
{
$this->composerJsonContent = $composerJsonContent;
}

public function guess(): string
Expand Down Expand Up @@ -79,7 +76,7 @@ private function getPhpUnitDir(array $parsedPaths): string
{
foreach ($parsedPaths as $namespace => $parsedPath) {
// for old Symfony projects (<=2.7) phpunit.xml is located in ./app folder
if (strpos($namespace, 'SymfonyStandard') !== false && trim($parsedPath, '/') === 'app') {
if (str_contains($namespace, 'SymfonyStandard') && trim($parsedPath, '/') === 'app') {
return 'app';
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/Config/Guesser/SourceDirGuesser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@
*/
class SourceDirGuesser
{
private stdClass $composerJsonContent;

public function __construct(stdClass $composerJsonContent)
public function __construct(private readonly stdClass $composerJsonContent)
{
$this->composerJsonContent = $composerJsonContent;
}

/**
Expand Down Expand Up @@ -117,7 +114,7 @@ private function parsePsrSection(array $autoloadDirs): array
* @param string[]|string $path
* @param string[] $dirs
*/
private function parsePath($path, array &$dirs): void
private function parsePath(array|string $path, array &$dirs): void
{
if (is_array($path)) {
array_walk_recursive(
Expand Down
22 changes: 8 additions & 14 deletions src/Config/ValueProvider/ExcludeDirsProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
use Infection\FileSystem\Locator\Locator;
use Infection\FileSystem\Locator\RootsFileOrDirectoryLocator;
use function Safe\glob;
use function str_contains;
use function str_replace;
use function strpos;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Filesystem\Filesystem;
Expand All @@ -60,15 +60,11 @@
{
public const EXCLUDED_ROOT_DIRS = ['vendor', 'tests', 'test'];

private ConsoleHelper $consoleHelper;
private QuestionHelper $questionHelper;
private Filesystem $filesystem;

public function __construct(ConsoleHelper $consoleHelper, QuestionHelper $questionHelper, Filesystem $filesystem)
{
$this->consoleHelper = $consoleHelper;
$this->questionHelper = $questionHelper;
$this->filesystem = $filesystem;
public function __construct(
private readonly ConsoleHelper $consoleHelper,
private readonly QuestionHelper $questionHelper,
private readonly Filesystem $filesystem
) {
}

/**
Expand Down Expand Up @@ -108,9 +104,7 @@
$globDirs = glob($sourceDirs[0] . '/*', GLOB_ONLYDIR);

$autocompleteValues = array_map(
static function (string $dir) use ($sourceDirs): string {
return str_replace($sourceDirs[0] . '/', '', $dir);
},
static fn (string $dir): string => str_replace($sourceDirs[0] . '/', '', $dir),
$globDirs
);
}
Expand All @@ -132,7 +126,7 @@
private function getValidator(Locator $locator): Closure
{
return static function ($answer) use ($locator) {
if (!$answer || strpos($answer, '*') !== false) {
if (!$answer || str_contains($answer, '*')) {

Check warning on line 129 in src/Config/ValueProvider/ExcludeDirsProvider.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "LogicalNot": --- Original +++ New @@ @@ private function getValidator(Locator $locator) : Closure { return static function ($answer) use($locator) { - if (!$answer || str_contains($answer, '*')) { + if ($answer || str_contains($answer, '*')) { return $answer; } $locator->locate($answer);

Check warning on line 129 in src/Config/ValueProvider/ExcludeDirsProvider.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "LogicalOr": --- Original +++ New @@ @@ private function getValidator(Locator $locator) : Closure { return static function ($answer) use($locator) { - if (!$answer || str_contains($answer, '*')) { + if (!$answer && str_contains($answer, '*')) { return $answer; } $locator->locate($answer);

Check warning on line 129 in src/Config/ValueProvider/ExcludeDirsProvider.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "LogicalOrAllSubExprNegation": --- Original +++ New @@ @@ private function getValidator(Locator $locator) : Closure { return static function ($answer) use($locator) { - if (!$answer || str_contains($answer, '*')) { + if ($answer || !str_contains($answer, '*')) { return $answer; } $locator->locate($answer);

Check warning on line 129 in src/Config/ValueProvider/ExcludeDirsProvider.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "LogicalOrNegation": --- Original +++ New @@ @@ private function getValidator(Locator $locator) : Closure { return static function ($answer) use($locator) { - if (!$answer || str_contains($answer, '*')) { + if (!(!$answer || str_contains($answer, '*'))) { return $answer; } $locator->locate($answer);

Check warning on line 129 in src/Config/ValueProvider/ExcludeDirsProvider.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "LogicalOrSingleSubExprNegation": --- Original +++ New @@ @@ private function getValidator(Locator $locator) : Closure { return static function ($answer) use($locator) { - if (!$answer || str_contains($answer, '*')) { + if (!$answer || !str_contains($answer, '*')) { return $answer; } $locator->locate($answer);
return $answer;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Config/ValueProvider/PCOVDirectoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
*/
class PCOVDirectoryProvider
{
private ?string $phpConfiguredPcovDirectory;
private ?string $phpConfiguredPcovDirectory = null;

public function __construct(?string $iniValue = null)
{
try {
$this->phpConfiguredPcovDirectory = $iniValue ?? ini_get('pcov.directory');
} catch (InfoException $e) {
} catch (InfoException) {
// Probably not using PCOV
$this->phpConfiguredPcovDirectory = null;
}
Expand Down
18 changes: 7 additions & 11 deletions src/Config/ValueProvider/PhpUnitCustomExecutablePathProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,18 @@
*/
final class PhpUnitCustomExecutablePathProvider
{
private TestFrameworkFinder $phpUnitExecutableFinder;
private ConsoleHelper $consoleHelper;
private QuestionHelper $questionHelper;

public function __construct(TestFrameworkFinder $phpUnitExecutableFinder, ConsoleHelper $consoleHelper, QuestionHelper $questionHelper)
{
$this->phpUnitExecutableFinder = $phpUnitExecutableFinder;
$this->consoleHelper = $consoleHelper;
$this->questionHelper = $questionHelper;
public function __construct(
private readonly TestFrameworkFinder $phpUnitExecutableFinder,
private readonly ConsoleHelper $consoleHelper,
private readonly QuestionHelper $questionHelper
) {
}

public function get(IO $io): ?string
{
try {
$this->phpUnitExecutableFinder->find(TestFrameworkTypes::PHPUNIT);
} catch (FinderException $e) {
} catch (FinderException) {
$io->writeln(['']);

$questionText = $this->consoleHelper->getQuestion(
Expand All @@ -80,10 +76,10 @@
$question = new Question($questionText);
$question->setValidator($this->getValidator());

return str_replace(

Check warning on line 79 in src/Config/ValueProvider/PhpUnitCustomExecutablePathProvider.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "UnwrapStrReplace": --- Original +++ New @@ @@ $questionText = $this->consoleHelper->getQuestion('We did not find phpunit executable. Please provide custom absolute path'); $question = new Question($questionText); $question->setValidator($this->getValidator()); - return str_replace(DIRECTORY_SEPARATOR, '/', (string) $this->questionHelper->ask($io->getInput(), $io->getOutput(), $question)); + return (string) $this->questionHelper->ask($io->getInput(), $io->getOutput(), $question); } return null; }
DIRECTORY_SEPARATOR,
'/',
$this->questionHelper->ask($io->getInput(), $io->getOutput(), $question)
(string) $this->questionHelper->ask($io->getInput(), $io->getOutput(), $question)

Check warning on line 82 in src/Config/ValueProvider/PhpUnitCustomExecutablePathProvider.php

View workflow job for this annotation

GitHub Actions / Mutation Testing Code Review Annotations 8.1

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ $questionText = $this->consoleHelper->getQuestion('We did not find phpunit executable. Please provide custom absolute path'); $question = new Question($questionText); $question->setValidator($this->getValidator()); - return str_replace(DIRECTORY_SEPARATOR, '/', (string) $this->questionHelper->ask($io->getInput(), $io->getOutput(), $question)); + return str_replace(DIRECTORY_SEPARATOR, '/', $this->questionHelper->ask($io->getInput(), $io->getOutput(), $question)); } return null; }
);
}

Expand Down
Loading