mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-28 23:10:51 +00:00
b2b3a0f5cc
33fa626297
[DX] Make PhpDocInfoFactory explicitly required in Rector rule constructor, if needed (#5051)
72 lines
1.9 KiB
PHP
72 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\TypeDeclaration\Rector\Property;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Stmt\Class_;
|
|
use PHPStan\Analyser\Scope;
|
|
use Rector\Core\Rector\AbstractScopeAwareRector;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @deprecated This rule is deprecated, as based on docblock and public elements,
|
|
* use private properties and property promotion instead to use PHP language features.
|
|
*/
|
|
final class TypedPropertyFromStrictConstructorReadonlyClassRector extends AbstractScopeAwareRector implements MinPhpVersionInterface
|
|
{
|
|
public function getRuleDefinition() : RuleDefinition
|
|
{
|
|
return new RuleDefinition('Add typed public properties based only on strict constructor types in readonly classes', [new CodeSample(<<<'CODE_SAMPLE'
|
|
/**
|
|
* @immutable
|
|
*/
|
|
class SomeObject
|
|
{
|
|
public $name;
|
|
|
|
public function __construct(string $name)
|
|
{
|
|
$this->name = $name;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
/**
|
|
* @immutable
|
|
*/
|
|
class SomeObject
|
|
{
|
|
public string $name;
|
|
|
|
public function __construct(string $name)
|
|
{
|
|
$this->name = $name;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [Class_::class];
|
|
}
|
|
/**
|
|
* @param Class_ $node
|
|
*/
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
|
{
|
|
\trigger_error(\sprintf('The "%s" rule is deprecated as based on docblock and public elements. Use private properties and property promotion rules instead.', self::class), \E_USER_ERROR);
|
|
return null;
|
|
}
|
|
public function provideMinPhpVersion() : int
|
|
{
|
|
return PhpVersionFeature::TYPED_PROPERTIES;
|
|
}
|
|
}
|