diff --git a/src/PhpParser/Node/NodeFactory.php b/src/PhpParser/Node/NodeFactory.php index b049723948e..2172c2e67e7 100644 --- a/src/PhpParser/Node/NodeFactory.php +++ b/src/PhpParser/Node/NodeFactory.php @@ -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 @@ -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, ) { } @@ -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; } diff --git a/tests/Issues/EmptyLongArraySyntax/Fixture/empty_long_array.php b/tests/Issues/EmptyLongArraySyntax/Fixture/empty_long_array.php new file mode 100644 index 00000000000..c18caae774d --- /dev/null +++ b/tests/Issues/EmptyLongArraySyntax/Fixture/empty_long_array.php @@ -0,0 +1,16 @@ + diff --git a/tests/NodeManipulator/ClassDependencyManipulatorTest.php b/tests/NodeManipulator/ClassDependencyManipulatorTest.php index d93d2857fc2..23bd8685da5 100644 --- a/tests/NodeManipulator/ClassDependencyManipulatorTest.php +++ b/tests/NodeManipulator/ClassDependencyManipulatorTest.php @@ -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'); diff --git a/tests/NodeManipulator/Fixture/expected_class_const_property.php.inc b/tests/NodeManipulator/Fixture/expected_class_const_property.php.inc index 4d9bd55e8cd..92cae65cfb6 100644 --- a/tests/NodeManipulator/Fixture/expected_class_const_property.php.inc +++ b/tests/NodeManipulator/Fixture/expected_class_const_property.php.inc @@ -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) { } } diff --git a/tests/NodeManipulator/Fixture/expected_empty_class.php.inc b/tests/NodeManipulator/Fixture/expected_empty_class.php.inc index cfd440f5efe..c6e3fb6f60e 100644 --- a/tests/NodeManipulator/Fixture/expected_empty_class.php.inc +++ b/tests/NodeManipulator/Fixture/expected_empty_class.php.inc @@ -2,7 +2,7 @@ class EmptyClass { - public function __construct(private \EventDispatcherInterface $eventDispatcher) + public function __construct(private readonly \EventDispatcherInterface $eventDispatcher) { } } diff --git a/tests/NodeManipulator/Fixture/expected_method_and_property.php.inc b/tests/NodeManipulator/Fixture/expected_method_and_property.php.inc index 3893fd1d8bb..dad864579af 100644 --- a/tests/NodeManipulator/Fixture/expected_method_and_property.php.inc +++ b/tests/NodeManipulator/Fixture/expected_method_and_property.php.inc @@ -3,7 +3,7 @@ class ClassWithMethodAndProperty { private $someProperty; - public function __construct(private \EventDispatcherInterface $eventDispatcher) + public function __construct(private readonly \EventDispatcherInterface $eventDispatcher) { } function someMethod() diff --git a/tests/NodeManipulator/Fixture/expected_single_method.php.inc b/tests/NodeManipulator/Fixture/expected_single_method.php.inc index 6027ac82af9..380a81130fe 100644 --- a/tests/NodeManipulator/Fixture/expected_single_method.php.inc +++ b/tests/NodeManipulator/Fixture/expected_single_method.php.inc @@ -2,7 +2,7 @@ class SingleMethodClass { - public function __construct(private \EventDispatcherInterface $eventDispatcher) + public function __construct(private readonly \EventDispatcherInterface $eventDispatcher) { } function firstMethod() diff --git a/tests/NodeManipulator/Fixture/expected_single_property.php.inc b/tests/NodeManipulator/Fixture/expected_single_property.php.inc index d2374712fb1..90d546ad8a6 100644 --- a/tests/NodeManipulator/Fixture/expected_single_property.php.inc +++ b/tests/NodeManipulator/Fixture/expected_single_property.php.inc @@ -3,7 +3,7 @@ class ClassWithSingleProperty { private $someProperty; - public function __construct(private \EventDispatcherInterface $eventDispatcher) + public function __construct(private readonly \EventDispatcherInterface $eventDispatcher) { } }