apply properties to constants rule from SOLID

This commit is contained in:
TomasVotruba 2020-02-15 00:21:14 +01:00
parent fcec6e301e
commit d1b4779889
11 changed files with 38 additions and 31 deletions

View File

@ -29,9 +29,9 @@ use Rector\NodeTypeResolver\Node\AttributeKey;
final class ParsedNodeCollector
{
/**
* @var string[]
* @var class-string[]
*/
private $collectableNodeTypes = [
private const COLLECTABLE_NODE_TYPES = [
Class_::class,
Interface_::class,
ClassConst::class,
@ -163,7 +163,7 @@ final class ParsedNodeCollector
public function isCollectableNode(Node $node): bool
{
foreach ($this->collectableNodeTypes as $collectableNodeType) {
foreach (self::COLLECTABLE_NODE_TYPES as $collectableNodeType) {
if (is_a($node, $collectableNodeType, true)) {
return true;
}

View File

@ -239,3 +239,4 @@ parameters:
- '#Parameter \#1 \$possibleSubtype of method Rector\\TypeDeclaration\\PhpParserTypeAnalyzer\:\:isSubtypeOf\(\) expects PhpParser\\Node\\Identifier\|PhpParser\\Node\\Name\|PhpParser\\Node\\NullableType\|PhpParser\\Node\\UnionType, PhpParser\\Node given#'
- '#Parameter \#2 \$inferredReturnNode of method Rector\\TypeDeclaration\\Rector\\FunctionLike\\ReturnTypeDeclarationRector\:\:addReturnType\(\) expects PhpParser\\Node, PhpParser\\Node\\Identifier\|PhpParser\\Node\\Name\|PhpParser\\Node\\NullableType\|PhpParser\\Node\\UnionType\|null given#'
- '#Method Rector\\FileSystemRector\\Rector\\AbstractFileSystemRector\:\:wrapToArg\(\) should return array<PhpParser\\Node\\Arg\> but returns array<PhpParser\\Node\\Arg\|PhpParser\\Node\\Expr\>#'
- '#Strict comparison using \=\=\= between mixed and null will always evaluate to false#'

View File

@ -1,3 +1,6 @@
services:
Rector\SOLID\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector: null
parameters:
sets:
- 'coding-style'

View File

@ -118,6 +118,7 @@ PHP
return null;
}
$haveNodeChanged = false;
foreach ((array) $node->stmts as $key => $stmt) {
if ($stmt instanceof Expression) {
$stmt = $stmt->expr;
@ -133,11 +134,16 @@ PHP
continue;
}
$haveNodeChanged = true;
// move all nodes one level up
array_splice($node->stmts, $key, count($stmt->stmts) - 1, $stmt->stmts);
}
return $node;
if ($haveNodeChanged) {
return $node;
}
return null;
}
private function isAlwaysTruableNode(Node $node): bool

View File

@ -120,10 +120,12 @@ PHP
}
$this->refactorArgument($node->args[$position]);
return $node;
}
}
return $node;
return null;
}
private function refactorFuncCall(FuncCall $funcCall): FuncCall

View File

@ -25,7 +25,7 @@ final class RegexPatternArgumentManipulator
/**
* @var int[]
*/
private $functionsWithPatternsToArgumentPosition = [
private const FUNCTIONS_WITH_PATTERNS_TO_ARGUMENT_POSITION = [
'preg_match' => 0,
'preg_replace_callback_array' => 0,
'preg_replace_callback' => 0,
@ -38,7 +38,7 @@ final class RegexPatternArgumentManipulator
/**
* @var int[][]
*/
private $staticMethodsWithPatternsToArgumentPosition = [
private const STATIC_METHODS_WITH_PATTERNS_TO_ARGUMENT_POSITION = [
Strings::class => [
'match' => 1,
'matchAll' => 1,
@ -107,7 +107,7 @@ final class RegexPatternArgumentManipulator
*/
private function processFuncCall(FuncCall $funcCall): array
{
foreach ($this->functionsWithPatternsToArgumentPosition as $functionName => $argumentPosition) {
foreach (self::FUNCTIONS_WITH_PATTERNS_TO_ARGUMENT_POSITION as $functionName => $argumentPosition) {
if (! $this->nodeNameResolver->isName($funcCall, $functionName)) {
continue;
}
@ -127,7 +127,7 @@ final class RegexPatternArgumentManipulator
*/
private function processStaticCall(StaticCall $staticCall): array
{
foreach ($this->staticMethodsWithPatternsToArgumentPosition as $type => $methodNamesToArgumentPosition) {
foreach (self::STATIC_METHODS_WITH_PATTERNS_TO_ARGUMENT_POSITION as $type => $methodNamesToArgumentPosition) {
if (! $this->nodeTypeResolver->isObjectType($staticCall->class, $type)) {
continue;
}

View File

@ -35,11 +35,6 @@ final class BetterNodeDumper extends NodeDumper
*/
private $nodeIds = 0;
/**
* @var bool
*/
private $skipEmpty = true;
/**
* @var string[]
*/
@ -127,7 +122,7 @@ final class BetterNodeDumper extends NodeDumper
foreach ($node->getSubNodeNames() as $key) {
$value = $node->{$key};
if ($this->skipEmpty && ($value === null || $value === [])) {
if (($value === null || $value === [])) {
continue;
}

View File

@ -45,7 +45,7 @@ final class AssignAndBinaryMap
/**
* @var string[]
*/
private $binaryOpToInverseClasses = [
private const BINARY_OP_TO_INVERSE_CLASSES = [
Identical::class => NotIdentical::class,
NotIdentical::class => Identical::class,
Equal::class => NotEqual::class,
@ -57,9 +57,9 @@ final class AssignAndBinaryMap
];
/**
* @var string[]
* @var class-string[]
*/
private $assignOpToBinaryOpClasses = [
private const ASSIGN_OP_TO_BINARY_OP_CLASSES = [
AssignBitwiseOr::class => BitwiseOr::class,
AssignBitwiseAnd::class => BitwiseAnd::class,
AssignBitwiseXor::class => BitwiseXor::class,
@ -81,7 +81,7 @@ final class AssignAndBinaryMap
public function __construct()
{
$this->binaryOpToAssignClasses = array_flip($this->assignOpToBinaryOpClasses);
$this->binaryOpToAssignClasses = array_flip(self::ASSIGN_OP_TO_BINARY_OP_CLASSES);
}
public function getAlternative(Node $node): ?string
@ -89,7 +89,7 @@ final class AssignAndBinaryMap
$nodeClass = get_class($node);
if ($node instanceof AssignOp) {
return $this->assignOpToBinaryOpClasses[$nodeClass] ?? null;
return self::ASSIGN_OP_TO_BINARY_OP_CLASSES[$nodeClass] ?? null;
}
if ($node instanceof BinaryOp) {
@ -103,6 +103,6 @@ final class AssignAndBinaryMap
{
$nodeClass = get_class($binaryOp);
return $this->binaryOpToInverseClasses[$nodeClass] ?? null;
return self::BINARY_OP_TO_INVERSE_CLASSES[$nodeClass] ?? null;
}
}

View File

@ -26,7 +26,7 @@ final class IdentifierManipulator
/**
* @var string[]
*/
private $nodeClassesWithIdentifier = [
private const NODE_CLASSES_WITH_IDENTIFIER = [
ClassConstFetch::class, MethodCall::class, PropertyFetch::class, StaticCall::class, ClassMethod::class,
];
@ -74,7 +74,7 @@ final class IdentifierManipulator
private function ensureNodeHasIdentifier(Node $node): void
{
if (in_array(get_class($node), $this->nodeClassesWithIdentifier, true)) {
if (in_array(get_class($node), self::NODE_CLASSES_WITH_IDENTIFIER, true)) {
return;
}
@ -82,7 +82,7 @@ final class IdentifierManipulator
'Node "%s" does not contain a "$name" property with "%s". Pass only one of "%s".',
get_class($node),
Identifier::class,
implode('", "', $this->nodeClassesWithIdentifier)
implode('", "', self::NODE_CLASSES_WITH_IDENTIFIER)
));
}
}

View File

@ -16,7 +16,7 @@ final class VisibilityManipulator
/**
* @var string[]
*/
private $allowedNodeTypes = [ClassMethod::class, Property::class, ClassConst::class, Class_::class];
private const ALLOWED_NODE_TYPES = [ClassMethod::class, Property::class, ClassConst::class, Class_::class];
/**
* @param ClassMethod|Property|ClassConst $node
@ -117,7 +117,7 @@ final class VisibilityManipulator
private function ensureIsClassMethodOrProperty(Node $node, string $location): void
{
foreach ($this->allowedNodeTypes as $allowedNodeType) {
foreach (self::ALLOWED_NODE_TYPES as $allowedNodeType) {
if (is_a($node, $allowedNodeType, true)) {
return;
}
@ -126,7 +126,7 @@ final class VisibilityManipulator
throw new InvalidNodeTypeException(sprintf(
'"%s" only accepts "%s" types. "%s" given.',
$location,
implode('", "', $this->allowedNodeTypes),
implode('", "', self::ALLOWED_NODE_TYPES),
get_class($node)
));
}

View File

@ -37,7 +37,7 @@ final class InjectAnnotationClassRector extends AbstractRector
/**
* @var string[]
*/
private $annotationToTagClass = [
private const ANNOTATION_TO_TAG_CLASS = [
PHPDIInject::class => PHPDIInjectTagValueNode::class,
JMSInject::class => JMSInjectTagValueNode::class,
];
@ -134,7 +134,7 @@ PHP
foreach ($this->annotationClasses as $annotationClass) {
$this->ensureAnnotationClassIsSupported($annotationClass);
$tagClass = $this->annotationToTagClass[$annotationClass];
$tagClass = self::ANNOTATION_TO_TAG_CLASS[$annotationClass];
$injectTagValueNode = $phpDocInfo->getByType($tagClass);
if ($injectTagValueNode === null) {
@ -155,14 +155,14 @@ PHP
private function ensureAnnotationClassIsSupported(string $annotationClass): void
{
if (isset($this->annotationToTagClass[$annotationClass])) {
if (isset(self::ANNOTATION_TO_TAG_CLASS[$annotationClass])) {
return;
}
throw new NotImplementedException(sprintf(
'Annotation class "%s" is not implemented yet. Use one of "%s" or add custom tag for it to Rector.',
$annotationClass,
implode('", "', array_keys($this->annotationToTagClass))
implode('", "', array_keys(self::ANNOTATION_TO_TAG_CLASS))
));
}