add support for single array item magic in tag value nodes

This commit is contained in:
TomasVotruba 2020-06-06 13:34:32 +02:00
parent 7e348b4de1
commit 234b3ea8f5
2 changed files with 32 additions and 0 deletions

View File

@ -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'

View File

@ -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;
}
}