Skip to content

Commit

Permalink
fixup! [PHP 8.2] Add rule for AllowDynamicProperties attribute (#1225)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 26, 2021
1 parent 1255b91 commit 5555104
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 14 deletions.
4 changes: 4 additions & 0 deletions packages/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,8 @@ final class AttributeKey
* @var string
*/
public const STATEMENT_DEPTH = 'statementDepth';
/**
* @var string
*/
public const HAS_NEW_INHERITED_TYPE = 'has_new_inherited_type';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

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

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddPropertyTypeDeclarationRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(\Symplify\SmartFileSystem\SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return \Iterator<\Symplify\SmartFileSystem\SmartFileInfo>
*/
public function provideData(): \Iterator
{
return $this->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,21 @@
<?php

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

class SomeClass extends ParentClass
{
public $name;
}

?>
-----
<?php

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

class SomeClass extends ParentClass
{
public string $name;
}

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

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(\Rector\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,6 @@ private function shouldSkipParam(Param $param, ClassMethod | Function_ $function
}

// already set → skip
return ! $param->type->getAttribute(NewType::HAS_NEW_INHERITED_TYPE, false);
return ! $param->type->getAttribute(AttributeKey::HAS_NEW_INHERITED_TYPE, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\Rector\Property;

use PhpParser\Node;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\AddPropertyTypeDeclarationRectorTest
*/
final class AddPropertyTypeDeclarationRector extends AbstractRector implements ConfigurableRectorInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add type to property by added rules, mostly public/property by parent type', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass extends ParentClass
{
public $name;
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
class SomeClass extends ParentClass
{
public string $name;
}
CODE_SAMPLE

)
]);
}

/**
* @return array<class-string<\PhpParser\Node>>
*/
public function getNodeTypes(): array
{
return array(\PhpParser\Node\Stmt\Property::class);
}

/**
* @param \PhpParser\Node\Stmt\Property $node
*/
public function refactor(Node $node): ?Node
{
// change the node

return $node;
}

/**
* @param array<string $configuration
* @return void
*/
public function configure(array $configuration): void
{
// TODO: Implement configure() method.
}
}
32 changes: 32 additions & 0 deletions rules/TypeDeclaration/ValueObject/AddPropertyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclaration\ValueObject;

use PHPStan\Type\Type;

final class AddPropertyType
{
public function __construct(
private string $class,
private string $propertyName,
private Type $type
) {
}

public function getClass(): string
{
return $this->class;
}

public function getPropertyName(): string
{
return $this->propertyName;
}

public function getType(): Type
{
return $this->type;
}
}
13 changes: 0 additions & 13 deletions rules/TypeDeclaration/ValueObject/NewType.php

This file was deleted.

0 comments on commit 5555104

Please sign in to comment.