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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ public function __construct(array $items, ?string $originalContent = null)
public function __toString(): string
{
$items = $this->completeItemsQuotes($this->items);
$items = $this->filterOutMissingItems($items);
$items = $this->makeKeysExplicit($items);

return $this->printContentItems($items);
}

Expand Down Expand Up @@ -119,6 +121,10 @@ protected function printArrayItem(array $item, ?string $key = null): string
$json = Strings::replace($json, '#"#');
}

if ($this->originalContent !== null && $key !== null) {
$json = $this->quoteKeys($item, $key, $json, $this->originalContent);
}

return $keyPart . $json;
}

Expand Down Expand Up @@ -146,8 +152,9 @@ protected function printContentItems(array $items): string
continue;
}

$arrayItemAsString = $this->printArrayItem($value, $key);
/** @var string $key */
$items[$key] = $this->printArrayItem($value, $key);
$items[$key] = $arrayItemAsString;
}

return sprintf(
Expand Down Expand Up @@ -304,4 +311,19 @@ private function createQuotedKeyPattern(?string $silentKey, string $key, string
// @see https://regex101.com/r/VgvK8C/3/
return sprintf('#%s="#', $escapedKey);
}

private function quoteKeys(array $item, string $key, string $json, string $originalContent): string
{
foreach (array_keys($item) as $itemKey) {
// @see https://regex101.com/r/V7nq5D/1
$quotedKeyPattern = '#' . $key . '={(.*?)?\"' . $itemKey . '\"(.*?)?}#';
$isKeyQuoted = (bool) Strings::match($originalContent, $quotedKeyPattern);
if (! $isKeyQuoted) {
continue;
}

$json = Strings::replace($json, '#([^\"])' . $itemKey . '([^\"])#', '$1"' . $itemKey . '"$2');
}
return $json;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final class QuotesInNestedArray
{
/**
* @ORM\Column(options={"unsigned":true, "default":0})
* @ORM\Column(options={"unsigned"=true, "default"=0})
*/
private $loginCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint;

use Iterator;
use Nette\Utils\Strings;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\EntityTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\TableTagValueNode;
Expand All @@ -28,10 +27,6 @@ final class TagValueNodeReprintTest extends AbstractPhpDocInfoTest
*/
public function test(string $filePath, string $tagValueNodeClass): void
{
if (Strings::endsWith($filePath, 'QuotesInNestedArray.php')) {
$this->markTestSkipped('Quoting nested keys in annotations is in progress');
}

$this->doTestPrintedPhpDocInfo($filePath, $tagValueNodeClass);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Rector\PhpAttribute\PhpDocNode;

/**
* @see https://wiki.php.net/rfc/attributes_v2
*/
trait PhpAttributePhpDocNodePrintTrait
{
/**
Expand Down
32 changes: 27 additions & 5 deletions rules/php74/src/Rector/Function_/ReservedFnFunctionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\Rector\AbstractRector;
Expand All @@ -18,6 +19,19 @@
*/
final class ReservedFnFunctionRector extends AbstractRector
{
/**
* @var string[]
*/
private $reservedNamesToNewOnes = [];

public function __construct(array $reservedNamesToNewOnes = [
// PHP 7.4
'fn' => 'f',
])
{
$this->reservedNamesToNewOnes = $reservedNamesToNewOnes;
}

public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Change fn() function name, since it will be reserved keyword', [
Expand Down Expand Up @@ -68,12 +82,20 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node, 'fn')) {
return null;
}
foreach ($this->reservedNamesToNewOnes as $reservedName => $newName) {
if (! $this->isName($node->name, $reservedName)) {
continue;
}

$node->name = new Name('f');
if ($node instanceof FuncCall) {
$node->name = new Name($newName);
} else {
$node->name = new Identifier($newName);
}

return $node;
}

return $node;
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class SomeClass
{
public function run()
{
function fn($value)
function reservedFn($value)
{
return $value;
}

fn(5);
reservedFn(5);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Php74\Tests\Rector\Function_\ReservedFnFunctionRector;

use Iterator;
use PhpParser\Parser\Tokens;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Php74\Rector\Function_\ReservedFnFunctionRector;

Expand All @@ -16,10 +15,6 @@ final class ReservedFnFunctionRectorTest extends AbstractRectorTestCase
*/
public function test(string $file): void
{
if (defined(Tokens::class . '::T_FN')) {
$this->markTestSkipped('fn is reserved name in PHP 7.4');
}

$this->doTestFile($file);
}

Expand All @@ -28,8 +23,15 @@ public function provideData(): Iterator
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
protected function getRectorsWithConfiguration(): array
{
return ReservedFnFunctionRector::class;
return [
ReservedFnFunctionRector::class => [
'$reservedNamesToNewOnes' => [
// for testing purposes of "fn" even on PHP 7.3-
'reservedFn' => 'f',
],
],
];
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,6 @@ public function test(string $file): void
{
$this->doTestFile($file);

// test factory content
$this->assertFileExists($this->getTempPath() . '/AnotherClassFactory.php');
$this->assertFileEquals(
__DIR__ . '/Source/ExpectedAnotherClassFactory.php',
$this->getTempPath() . '/AnotherClassFactory.php'
);
}

/**
* @dataProvider provideDataMultipleArguments()
*/
public function testMultipleArguments(string $file): void
{
$this->markTestSkipped('Conflicting with previous test() for unknown reason. Works well separately');

$this->doTestFile($file);

// test factory content
$this->assertFileExists($this->getTempPath() . '/AnotherClassWithMoreArgumentsFactory.php');
$this->assertFileEquals(
Expand All @@ -45,11 +28,6 @@ public function testMultipleArguments(string $file): void
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideDataMultipleArguments(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureWithMultipleArguments');
}
Expand Down