Skip to content

Commit

Permalink
Add readonly by default on new promoted property
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 5, 2025
1 parent 2e82ab4 commit 61355f0
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/PhpParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@
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\PhpVersion;
use Rector\ValueObject\PhpVersionFeature;

/**
* @see \Rector\Tests\PhpParser\Node\NodeFactoryTest
Expand All @@ -65,7 +68,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 +295,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
16 changes: 16 additions & 0 deletions tests/Issues/EmptyLongArraySyntax/Fixture/empty_long_array.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Issues\EmptyLongArraySyntax\Fixture;

use Rector\Tests\Issues\EmptyLongArraySyntax\Source\ParentWithEmptyLongArray;

final class EmptyLongArray extends ParentWithEmptyLongArray
{
public function run()
{
}
}

?>
1 change: 0 additions & 1 deletion tests/NodeManipulator/ClassDependencyManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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 61355f0

Please sign in to comment.