Skip to content

Commit

Permalink
Add readonly by default on new promoted property (#6650)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Jan 5, 2025
1 parent 2e82ab4 commit 8bc80b4
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

declare(strict_types=1);

use Rector\ValueObject\PhpVersion;
use Rector\Config\RectorConfig;
use Rector\Tests\Transform\Rector\FuncCall\FuncCallToMethodCallRector\Source\SomeTranslator;
use Rector\Transform\Rector\FuncCall\FuncCallToMethodCallRector;
use Rector\Transform\ValueObject\FuncCallToMethodCall;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->phpVersion(PhpVersion::PHP_80);

$rectorConfig
->ruleWithConfiguration(FuncCallToMethodCallRector::class, [
new FuncCallToMethodCall('view', 'Namespaced\SomeRenderer', 'render'),
Expand Down
10 changes: 8 additions & 2 deletions src/FileSystem/FilesFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function findInDirectoriesAndFiles(
// filter files by specific suffix
if ($hasOnlySuffix) {
/** @var string $onlySuffix */
$fileWithSuffixFilter = (static fn(string $filePath): bool => str_ends_with($filePath, $onlySuffix));
$fileWithSuffixFilter = (static fn (string $filePath): bool => str_ends_with($filePath, $onlySuffix));
} elseif ($suffixes !== []) {
$fileWithSuffixFilter = static function (string $filePath) use ($suffixes): bool {
$filePathExtension = pathinfo($filePath, PATHINFO_EXTENSION);
Expand Down Expand Up @@ -88,7 +88,13 @@ function (string $file): bool {

// filtering files in directories collection
$directories = $this->fileAndDirectoryFilter->filterDirectories($filesAndDirectories);
$filteredFilePathsInDirectories = $this->findInDirectories($directories, $suffixes, $hasOnlySuffix, $onlySuffix, $sortByName);
$filteredFilePathsInDirectories = $this->findInDirectories(
$directories,
$suffixes,
$hasOnlySuffix,
$onlySuffix,
$sortByName
);

$filePaths = [...$filteredFilePaths, ...$filteredFilePathsInDirectories];
return $this->unchangedFilesFilter->filterFilePaths($filePaths);
Expand Down
10 changes: 9 additions & 1 deletion src/PhpParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeDecorator\PropertyTypeDecorator;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Php\PhpVersionProvider;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\ValueObject\PhpVersionFeature;

/**
* @see \Rector\Tests\PhpParser\Node\NodeFactoryTest
Expand All @@ -65,7 +67,8 @@ public function __construct(
private PhpDocInfoFactory $phpDocInfoFactory,
private StaticTypeMapper $staticTypeMapper,
private PropertyTypeDecorator $propertyTypeDecorator,
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private PhpVersionProvider $phpVersionProvider,
) {
}

Expand Down Expand Up @@ -291,6 +294,11 @@ public function createPromotedPropertyParam(PropertyMetadata $propertyMetadata):
$propertyFlags = $propertyMetadata->getFlags();
$param->flags = $propertyFlags !== 0 ? $propertyFlags : Modifiers::PRIVATE;

// make readonly by default
if ($this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::READONLY_PROPERTY)) {
$param->flags |= Modifiers::READONLY;
}

return $param;
}

Expand Down
5 changes: 1 addition & 4 deletions tests/FileSystem/FilesFinder/FilesFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,7 @@ public function testFilterBySuffix(): void
$this->assertCount(3, $foundNoFilterFiles);

$foundFiles = $this->filesFinder->findInDirectoriesAndFiles(
[
__DIR__ . '/SourceWithSuffix',
__DIR__ . '/SourceWithSuffix/other_unrelated_file.php',
],
[__DIR__ . '/SourceWithSuffix', __DIR__ . '/SourceWithSuffix/other_unrelated_file.php'],
['php'],
true,
'Controller'
Expand Down
8 changes: 7 additions & 1 deletion tests/NodeManipulator/ClassDependencyManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
use PhpParser\NodeVisitor\NameResolver;
use PhpParser\PrettyPrinter\Standard;
use PHPStan\Type\ObjectType;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\NodeManipulator\ClassDependencyManipulator;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\ValueObject\PhpVersionFeature;

final class ClassDependencyManipulatorTest extends AbstractLazyTestCase
{
Expand All @@ -30,7 +33,11 @@ final class ClassDependencyManipulatorTest extends AbstractLazyTestCase
protected function setUp(): void
{
$this->classDependencyManipulator = $this->make(ClassDependencyManipulator::class);

$this->printerStandard = new Standard();

// use at least readonly property
SimpleParameterProvider::setParameter(Option::PHP_VERSION_FEATURES, PhpVersionFeature::READONLY_PROPERTY);
}

public function testEmptyClass(): void
Expand All @@ -46,7 +53,6 @@ public function testEmptyClass(): void
public function testSingleMethod(): void
{
$someClass = new Class_(new Identifier('SingleMethodClass'));

$this->setNamespacedName($someClass);

$someClass->stmts[] = new ClassMethod('firstMethod');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ConstantProperties
const SOME_CONST = 'value';
public $someProperty;
public $anotherProperty;
public function __construct(private \EventDispatcherInterface $eventDispatcher)
public function __construct(private readonly \EventDispatcherInterface $eventDispatcher)
{
}
}
2 changes: 1 addition & 1 deletion tests/NodeManipulator/Fixture/expected_empty_class.php.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class EmptyClass
{
public function __construct(private \EventDispatcherInterface $eventDispatcher)
public function __construct(private readonly \EventDispatcherInterface $eventDispatcher)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class ClassWithMethodAndProperty
{
private $someProperty;
public function __construct(private \EventDispatcherInterface $eventDispatcher)
public function __construct(private readonly \EventDispatcherInterface $eventDispatcher)
{
}
function someMethod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class SingleMethodClass
{
public function __construct(private \EventDispatcherInterface $eventDispatcher)
public function __construct(private readonly \EventDispatcherInterface $eventDispatcher)
{
}
function firstMethod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class ClassWithSingleProperty
{
private $someProperty;
public function __construct(private \EventDispatcherInterface $eventDispatcher)
public function __construct(private readonly \EventDispatcherInterface $eventDispatcher)
{
}
}

0 comments on commit 8bc80b4

Please sign in to comment.