mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-19 01:22:22 +00:00
add support for single array item magic in tag value nodes
This commit is contained in:
parent
7e348b4de1
commit
234b3ea8f5
3
ecs.yaml
3
ecs.yaml
|
@ -56,6 +56,9 @@ parameters:
|
|||
# false positive
|
||||
- 'src/Testing/PHPUnit/AbstractRectorTestCase.php'
|
||||
|
||||
SlevomatCodingStandard\Sniffs\Variables\UnusedVariableSniff.UnusedVariable:
|
||||
- 'rules/php-office/src/Rector/MethodCall/IncreaseColumnIndexRector.php'
|
||||
|
||||
Symplify\CodingStandard\Sniffs\Debug\CommentedOutCodeSniff.Found:
|
||||
# notes
|
||||
- 'rules/php72/src/Rector/Each/ListEachRector.php'
|
||||
|
|
|
@ -157,6 +157,9 @@ abstract class AbstractTagValueNode implements AttributeAwareNodeInterface, PhpD
|
|||
}
|
||||
|
||||
$arrayItemAsString = $this->printArrayItem($value, $key);
|
||||
|
||||
$arrayItemAsString = $this->correctArraySingleItemPrint($value, $arrayItemAsString);
|
||||
|
||||
/** @var string $key */
|
||||
$items[$key] = $arrayItemAsString;
|
||||
}
|
||||
|
@ -328,6 +331,32 @@ abstract class AbstractTagValueNode implements AttributeAwareNodeInterface, PhpD
|
|||
|
||||
$json = Strings::replace($json, '#([^\"])' . $itemKey . '([^\"])#', '$1"' . $itemKey . '"$2');
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
private function correctArraySingleItemPrint($value, string $arrayItemAsString): string
|
||||
{
|
||||
if (count($value) !== 1) {
|
||||
return $arrayItemAsString;
|
||||
}
|
||||
|
||||
if ($this->originalContent === null) {
|
||||
return $arrayItemAsString;
|
||||
}
|
||||
|
||||
// item is in the original in same format → use it
|
||||
if (Strings::contains($this->originalContent, $arrayItemAsString)) {
|
||||
return $arrayItemAsString;
|
||||
}
|
||||
|
||||
// is original item used the same, just without {} brackets?
|
||||
$nakedItem = trim($arrayItemAsString, '{}');
|
||||
|
||||
if (! Strings::contains($this->originalContent, '(' . $nakedItem . ')')) {
|
||||
return $arrayItemAsString;
|
||||
}
|
||||
|
||||
return $nakedItem;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user