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
2 changes: 2 additions & 0 deletions config/set/php83.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php83\Rector\Class_\ReadOnlyAnonymousClassRector;
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
use Rector\Php83\Rector\FuncCall\CombineHostPortLdapUriRector;
Expand All @@ -14,5 +15,6 @@
AddTypeToConstRector::class,
CombineHostPortLdapUriRector::class,
RemoveGetClassGetParentClassNoArgsRector::class,
ReadOnlyAnonymousClassRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace newRector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Fixture;

use Rector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Source\ParentAlreadyReadonly;

new class() extends ParentAlreadyReadonly
{
private readonly string $name = 'test';
};

?>
-----
<?php

namespace newRector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Fixture;

use Rector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Source\ParentAlreadyReadonly;

new readonly class() extends ParentAlreadyReadonly
{
private string $name = 'test';
};

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace newRector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Fixture;

(new class()
{
private readonly string $name = 'test';
});

?>
-----
<?php

namespace newRector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Fixture;

(new readonly class()
{
private string $name = 'test';
});

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace newRector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Fixture;

class SkipNamedClass
{
private readonly string $name = 'test';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace newRector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Fixture;

new class()
{
private readonly string $name = 'test';
};

?>
-----
<?php

namespace newRector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Fixture;

new readonly class()
{
private string $name = 'test';
};

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ReadOnlyAnonymousClassRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php83\Rector\Class_\ReadOnlyAnonymousClassRector\Source;

readonly abstract class ParentAlreadyReadonly
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php83\Rector\Class_\ReadOnlyAnonymousClassRector;
use Rector\ValueObject\PhpVersionFeature;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(ReadOnlyAnonymousClassRector::class);

$rectorConfig->phpVersion(PhpVersionFeature::READONLY_ANONYMOUS_CLASS);
};
251 changes: 251 additions & 0 deletions rules/Php82/NodeManipulator/ReadonlyClassManipulator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
<?php

declare(strict_types=1);

namespace Rector\Php82\NodeManipulator;

use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionProperty;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Rector\Php81\Enum\AttributeName;
use Rector\Php81\NodeManipulator\AttributeGroupNewLiner;
use Rector\PHPStan\ScopeFetcher;
use Rector\Privatization\NodeManipulator\VisibilityManipulator;
use Rector\ValueObject\Application\File;
use Rector\ValueObject\MethodName;
use Rector\ValueObject\Visibility;

final readonly class ReadonlyClassManipulator
{
public function __construct(
private VisibilityManipulator $visibilityManipulator,
private PhpAttributeAnalyzer $phpAttributeAnalyzer,
private ReflectionProvider $reflectionProvider,
private AttributeGroupNewLiner $attributeGroupNewLiner
) {
}

public function process(Class_ $class, File $file): Class_|null
{
$scope = ScopeFetcher::fetch($class);
if ($this->shouldSkip($class, $scope)) {
return null;
}

$this->visibilityManipulator->makeReadonly($class);

$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);

if ($constructClassMethod instanceof ClassMethod) {
foreach ($constructClassMethod->getParams() as $param) {
$this->visibilityManipulator->removeReadonly($param);

if ($param->attrGroups !== []) {
$this->attributeGroupNewLiner->newLine($file, $param);
}
}
}

foreach ($class->getProperties() as $property) {
$this->visibilityManipulator->removeReadonly($property);

if ($property->attrGroups !== []) {
$this->attributeGroupNewLiner->newLine($file, $property);
}
}

if ($class->attrGroups !== []) {
$this->attributeGroupNewLiner->newLine($file, $class);
}

return $class;
}

/**
* @return ClassReflection[]
*/
private function resolveParentClassReflections(Scope $scope): array
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return [];
}

return $classReflection->getParents();
}

/**
* @param Property[] $properties
*/
private function hasNonTypedProperty(array $properties): bool
{
foreach ($properties as $property) {
// properties of readonly class must always have type
if ($property->type === null) {
return true;
}
}

return false;
}

private function shouldSkip(Class_ $class, Scope $scope): bool
{
$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return true;
}

if ($this->shouldSkipClass($class)) {
return true;
}

$parents = $this->resolveParentClassReflections($scope);

if (! $class->isAnonymous() && ! $class->isFinal()) {
return ! $this->isExtendsReadonlyClass($parents);
}

foreach ($parents as $parent) {
if (! $parent->isReadOnly()) {
return true;
}
}

$properties = $class->getProperties();
if ($this->hasWritableProperty($properties)) {
return true;
}

if ($this->hasNonTypedProperty($properties)) {
return true;
}

if ($this->shouldSkipConsumeTraitProperty($class)) {
return true;
}

$constructClassMethod = $class->getMethod(MethodName::CONSTRUCT);
if (! $constructClassMethod instanceof ClassMethod) {
// no __construct means no property promotion, skip if class has no property defined
return $properties === [];
}

$params = $constructClassMethod->getParams();
if ($params === []) {
// no params means no property promotion, skip if class has no property defined
return $properties === [];
}

return $this->shouldSkipParams($params);
}

private function shouldSkipConsumeTraitProperty(Class_ $class): bool
{
$traitUses = $class->getTraitUses();
foreach ($traitUses as $traitUse) {
foreach ($traitUse->traits as $trait) {
$traitName = $trait->toString();

// trait not autoloaded
if (! $this->reflectionProvider->hasClass($traitName)) {
return true;
}

$traitClassReflection = $this->reflectionProvider->getClass($traitName);
$nativeReflection = $traitClassReflection->getNativeReflection();

if ($this->hasReadonlyProperty($nativeReflection->getProperties())) {
return true;
}
}
}

return false;
}

/**
* @param ReflectionProperty[] $properties
*/
private function hasReadonlyProperty(array $properties): bool
{
foreach ($properties as $property) {
if (! $property->isReadOnly()) {
return true;
}
}

return false;
}

/**
* @param ClassReflection[] $parents
*/
private function isExtendsReadonlyClass(array $parents): bool
{
foreach ($parents as $parent) {
if ($parent->isReadOnly()) {
return true;
}
}

return false;
}

/**
* @param Property[] $properties
*/
private function hasWritableProperty(array $properties): bool
{
foreach ($properties as $property) {
if (! $property->isReadonly()) {
return true;
}
}

return false;
}

private function shouldSkipClass(Class_ $class): bool
{
// need to have test fixture once feature added to nikic/PHP-Parser
if ($this->visibilityManipulator->hasVisibility($class, Visibility::READONLY)) {
return true;
}

if ($this->phpAttributeAnalyzer->hasPhpAttribute($class, AttributeName::ALLOW_DYNAMIC_PROPERTIES)) {
return true;
}

return $class->extends instanceof FullyQualified && ! $this->reflectionProvider->hasClass(
$class->extends->toString()
);
}

/**
* @param Param[] $params
*/
private function shouldSkipParams(array $params): bool
{
foreach ($params as $param) {
// has non-readonly property promotion
if (! $this->visibilityManipulator->hasVisibility($param, Visibility::READONLY) && $param->isPromoted()) {
return true;
}

// type is missing, invalid syntax
if ($param->type === null) {
return true;
}
}

return false;
}
}
Loading