Skip to content
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
20 changes: 20 additions & 0 deletions config/set/strict-booleans.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector;
use Rector\Strict\Rector\Empty_\DisallowedEmptyRuleFixerRector;
use Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector;
use Rector\Strict\Rector\Ternary\BooleanInTernaryOperatorRuleFixerRector;
use Rector\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
BooleanInBooleanNotRuleFixerRector::class,
DisallowedEmptyRuleFixerRector::class,
BooleanInIfConditionRuleFixerRector::class,
BooleanInTernaryOperatorRuleFixerRector::class,
DisallowedShortTernaryRuleFixerRector::class,
]);
};
12 changes: 6 additions & 6 deletions packages/Caching/ValueObject/Storage/FileCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
final class FileCacheStorage implements CacheStorageInterface
{
public function __construct(
private string $directory,
private \Symfony\Component\Filesystem\Filesystem $filesystem
private readonly string $directory,
private readonly \Symfony\Component\Filesystem\Filesystem $filesystem
) {
}

Expand Down Expand Up @@ -53,7 +53,7 @@ public function save(string $key, string $variableKey, mixed $data): void
$this->filesystem-> mkdir($cacheFilePaths->getFirstDirectory());
$this->filesystem->mkdir($cacheFilePaths->getSecondDirectory());

$path = $cacheFilePaths->getFilePath();
$filePath = $cacheFilePaths->getFilePath();

$tmpPath = \sprintf('%s/%s.tmp', $this->directory, Random::generate());
$errorBefore = \error_get_last();
Expand All @@ -70,14 +70,14 @@ public function save(string $key, string $variableKey, mixed $data): void

// for performance reasons we don't use SmartFileSystem
FileSystem::write($tmpPath, \sprintf("<?php declare(strict_types = 1);\n\nreturn %s;", $exported));
$renameSuccess = @\rename($tmpPath, $path);
$renameSuccess = @\rename($tmpPath, $filePath);
if ($renameSuccess) {
return;
}

@\unlink($tmpPath);
if (\DIRECTORY_SEPARATOR === '/' || ! \file_exists($path)) {
throw new CachingException(\sprintf('Could not write data to cache file %s.', $path));
if (\DIRECTORY_SEPARATOR === '/' || ! \file_exists($filePath)) {
throw new CachingException(\sprintf('Could not write data to cache file %s.', $filePath));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ final class CommentRemovingNodeTraverser extends NodeTraverser
public function __construct(CommentRemovingNodeVisitor $commentRemovingNodeVisitor)
{
$this->addVisitor($commentRemovingNodeVisitor);

parent::__construct();
}
}
5 changes: 5 additions & 0 deletions packages/Set/ValueObject/SetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ final class SetList implements SetListInterface
*/
public const DEAD_CODE = __DIR__ . '/../../../config/set/dead-code.php';

/**
* @var string
*/
public const STRICT_BOOLEANS = __DIR__ . '/../../../config/set/strict-booleans.php';

/**
* @var string
*/
Expand Down
12 changes: 6 additions & 6 deletions packages/Testing/PHPUnit/Behavior/MovingFilesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ protected function assertFileWasAdded(string $expectedFilePath, string $expected

$wasFound = false;

foreach ($addedFilePathsWithContents as $addedFilePathsWithContent) {
if ($addedFilePathsWithContent->getFilePath() !== $expectedFilePath) {
foreach ($addedFilePathsWithContents as $addedFilePathWithContent) {
if ($addedFilePathWithContent->getFilePath() !== $expectedFilePath) {
continue;
}

$this->assertSame($expectedFileContents, $addedFilePathsWithContent->getFileContent());
$this->assertSame($expectedFileContents, $addedFilePathWithContent->getFileContent());
$wasFound = true;
}

Expand All @@ -46,9 +46,9 @@ private function resolveAddedFilePathsWithContents(): array
return $addedFilePathsWithContents;
}

foreach ($addedFilesWithNodes as $addedFileWithNodes) {
$fileContent = $nodesWithFileDestinationPrinter->printNodesWithFileDestination($addedFileWithNodes);
$addedFilePathsWithContents[] = new AddedFileWithContent($addedFileWithNodes->getFilePath(), $fileContent);
foreach ($addedFilesWithNodes as $addedFileWithNode) {
$fileContent = $nodesWithFileDestinationPrinter->printNodesWithFileDestination($addedFileWithNode);
$addedFilePathsWithContents[] = new AddedFileWithContent($addedFileWithNode->getFilePath(), $fileContent);
}

return $addedFilePathsWithContents;
Expand Down
3 changes: 2 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ parameters:
paths:
- packages/Caching/ValueObject/Storage/FileCacheStorage.php
-
message: '#"@\\rename\(\$tmpPath, \$path\)" is forbidden to use#'
message: '#"@\\rename(.*?)" is forbidden to use#'
paths:
- packages/Caching/ValueObject/Storage/FileCacheStorage.php
-
Expand Down Expand Up @@ -731,3 +731,4 @@ parameters:
# resolve before Rector 1.0
- '#Call to deprecated method findParentType#'
- '#Call to deprecated method findParentByTypes#'
- '#Instantiation of deprecated class Rector\\Core\\DependencyInjection\\CompilerPass\\AutowireArrayParameterCompilerPass#'
17 changes: 8 additions & 9 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\CodingStyle\ValueObject\ReturnArrayClassMethodToYield;
use Rector\Config\RectorConfig;
use Rector\Naming\Rector\Foreach_\RenameForeachValueVariableToMatchExprVariableRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\ValueObject\LevelSetList;
Expand All @@ -28,6 +27,7 @@
SetList::EARLY_RETURN,
PHPUnitSetList::PHPUNIT_CODE_QUALITY,
SetList::CODING_STYLE,
SetList::STRICT_BOOLEANS,
]);

$rectorConfig->rules([DeclareStrictTypesRector::class]);
Expand Down Expand Up @@ -71,17 +71,9 @@
'**/Source/*',
'**/Expected/*',
'**/Expected*',
__DIR__ . '/tests/PhpUnit/MultipleFilesChangedTrait/MultipleFilesChangedTraitTest.php',

// to keep original API from PHPStan untouched
__DIR__ . '/packages/Caching/ValueObject/Storage/FileCacheStorage.php',

// keep configs untouched, as the classes are just strings
UseClassKeywordForClassNameResolutionRector::class => [__DIR__ . '/config', '*/config/*'],
RenameForeachValueVariableToMatchExprVariableRector::class => [
// false positive on plurals
__DIR__ . '/packages/Testing/PHPUnit/Behavior/MovingFilesTrait.php',
],

// avoid simplifying itself
\Rector\CodeQuality\Rector\FuncCall\SimplifyRegexPatternRector::class => [
Expand All @@ -92,6 +84,13 @@
\Rector\TypeDeclaration\Rector\ClassMethod\AddMethodCallBasedStrictParamTypeRector::class => [
__DIR__ . '/rules/DeadCode/Rector/If_/RemoveUnusedNonEmptyArrayBeforeForeachRector.php',
],

\Rector\Strict\Rector\If_\BooleanInIfConditionRuleFixerRector::class => [
__DIR__ . '/src/DependencyInjection',
],
\Rector\Strict\Rector\Ternary\DisallowedShortTernaryRuleFixerRector::class => [
__DIR__ . '/src/DependencyInjection',
],
]);

$rectorConfig->phpstanConfig(__DIR__ . '/phpstan-for-rector.neon');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Strict\Rector\BooleanNot\BooleanInBooleanNotRuleFixerRector\Fixture;

use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Symplify\RuleDocGenerator\ValueObject\RuleClassWithFilePath;

final class SkipNegatedBoolean
{
public function run(RuleClassWithFilePath $ruleClassWithFilePath)
{
return ! is_a($ruleClassWithFilePath->getClass(), PostRectorInterface::class, true);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

6 changes: 4 additions & 2 deletions rules/Naming/Naming/PropertyNaming.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,14 @@ private function fqnToShortName(string $fqn): string
private function removeInterfaceSuffixPrefix(string $className, string $category): string
{
// suffix
if (Strings::match($className, '#' . $category . '$#i')) {
$iSuffixMatch = Strings::match($className, '#' . $category . '$#i');
if ($iSuffixMatch !== null) {
return Strings::substring($className, 0, -strlen($category));
}

// prefix
if (Strings::match($className, '#^' . $category . '#i')) {
$iPrefixMatch = Strings::match($className, '#^' . $category . '#i');
if ($iPrefixMatch !== null) {
return Strings::substring($className, strlen($category));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ public function getNodeTypes(): array
public function refactorWithScope(Node $node, Scope $scope): ?Expr
{
$exprType = $scope->getType($node->expr);
if ($exprType->isBoolean()->yes()) {
return null;
}

return $this->exactCompareFactory->createIdenticalFalsyCompare($exprType, $node->expr, $this->treatAsNonEmpty);
}
Expand Down
Loading