Skip to content

Commit

Permalink
fixup! fixup! [PHP 8.2] Add rule for AllowDynamicProperties attribute (
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 26, 2021
1 parent 5555104 commit af80bf7
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\Fixture;

class SomeClass extends ParentClass
use Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\Source\ParentClassWithProperty;

final class SomeClass extends ParentClassWithProperty
{
public $name;
}
Expand All @@ -13,7 +15,9 @@ class SomeClass extends ParentClass

namespace Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\Fixture;

class SomeClass extends ParentClass
use Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\Source\ParentClassWithProperty;

final class SomeClass extends ParentClassWithProperty
{
public string $name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\Source;

abstract class ParentClassWithProperty
{

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

declare(strict_types=1);

use PHPStan\Type\StringType;
use Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\Source\ParentClassWithProperty;
use Rector\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector;
use Rector\TypeDeclaration\ValueObject\AddPropertyTypeDeclaration;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(\Rector\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector::class);

$services->set(AddPropertyTypeDeclarationRector::class)
->configure([
new AddPropertyTypeDeclaration(ParentClassWithProperty::class, 'name', new StringType())
]);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,35 @@
namespace Rector\TypeDeclaration\Rector\Property;

use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\TypeDeclaration\ValueObject\AddPropertyTypeDeclaration;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\AddPropertyTypeDeclarationRectorTest
*/
final class AddPropertyTypeDeclarationRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var AddPropertyTypeDeclaration[]
*/
private array $addPropertyTypeDeclarations = [];

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add type to property by added rules, mostly public/property by parent type', [
new CodeSample(
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClass extends ParentClass
{
Expand All @@ -34,7 +48,10 @@ class SomeClass extends ParentClass
public string $name;
}
CODE_SAMPLE
,
[

]
)
]);
}
Expand All @@ -44,25 +61,61 @@ class SomeClass extends ParentClass
*/
public function getNodeTypes(): array
{
return array(\PhpParser\Node\Stmt\Property::class);
return array(Property::class);
}

/**
* @param \PhpParser\Node\Stmt\Property $node
* @param Property $node
*/
public function refactor(Node $node): ?Node
// this would be nice
// public function refactorWithScope(Node $node, Scope $scope): ?Node
{
// change the node
// type is already known
if ($node->type !== null) {
return null;
}

$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
throw new ShouldNotHappenException();
}

$classReflection = $scope->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return null;
}


foreach ($this->addPropertyTypeDeclarations as $addPropertyTypeDeclaration) {
if (! $classReflection->isSubclassOf($addPropertyTypeDeclaration->getClass())) {
continue;
}

if (! $this->isName($node, $addPropertyTypeDeclaration->getPropertyName())) {
continue;
}

$typeNode = $this->staticTypeMapper->mapPHPStanTypeToPhpParserNode($addPropertyTypeDeclaration->getType(), TypeKind::PROPERTY());
if ($typeNode === null) {
// invalid configuration
throw new ShouldNotHappenException();
}

$node->type = $typeNode;
return $node;
}

return $node;
return null;
}

/**
* @param array<string $configuration
* @param AddPropertyTypeDeclaration[] $configuration
* @return void
*/
public function configure(array $configuration): void
{
// TODO: Implement configure() method.
Assert::allIsAOf($configuration, AddPropertyTypeDeclaration::class);
$this->addPropertyTypeDeclarations = $configuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PHPStan\Type\Type;

final class AddPropertyType
final class AddPropertyTypeDeclaration
{
public function __construct(
private string $class,
Expand Down

0 comments on commit af80bf7

Please sign in to comment.