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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,20 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Property;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\StringType;
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\PHPStan\ScopeFetcher;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\ConfiguredCodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Rector\TypeDeclaration\ValueObject\AddPropertyTypeDeclaration;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\TypeDeclaration\Rector\Property\AddPropertyTypeDeclarationRector\AddPropertyTypeDeclarationRectorTest
* @deprecated This rule is deprecated, as too niche and never used, not part of any set. Write a custom rule for the exact project case instead.
*/
final class AddPropertyTypeDeclarationRector extends AbstractRector implements ConfigurableRectorInterface
final class AddPropertyTypeDeclarationRector extends AbstractRector implements ConfigurableRectorInterface, DeprecatedInterface
{
/**
* @var array<string, AddPropertyTypeDeclaration[]>
*/
private array $addPropertyTypeDeclarationsByProperty = [];

public function __construct(
private readonly StaticTypeMapper $staticTypeMapper
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Add type to property by added rules, mostly public/property by parent type', [
Expand Down Expand Up @@ -71,62 +57,16 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
// type is already known
if ($node->type !== null) {
return null;
}

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

$propertyName = $this->getName($node);
if ($propertyName === null) {
return null;
}

foreach ($this->addPropertyTypeDeclarationsByProperty[strtolower($propertyName)] ?? [] as $addPropertyTypeDeclaration) {
if (! $this->isClassReflectionType($classReflection, $addPropertyTypeDeclaration->getClass())) {
continue;
}

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

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

return null;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as too niche and part of no set; write a custom rule for the exact project case instead',
self::class
));
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allIsAOf($configuration, AddPropertyTypeDeclaration::class);

$this->addPropertyTypeDeclarationsByProperty = [];
foreach ($configuration as $addPropertyTypeDeclaration) {
$this->addPropertyTypeDeclarationsByProperty[strtolower($addPropertyTypeDeclaration->getPropertyName())][] = $addPropertyTypeDeclaration;
}
}

private function isClassReflectionType(ClassReflection $classReflection, string $type): bool
{
if ($classReflection->hasTraitUse($type)) {
return true;
}

return $classReflection->is($type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PHPStan\Type\Type;
use Rector\Validation\RectorAssert;

/**
* @api used in deprecated AddPropertyTypeDeclarationRector configs
*/
final readonly class AddPropertyTypeDeclaration
{
public function __construct(
Expand Down
Loading