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
2 changes: 2 additions & 0 deletions config/set/type-declaration-docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddParamArrayDocblockFromDimFetchAccessRector;
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForArrayDimAssignedObjectRector;
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForCommonObjectDenominatorRector;
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector;
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector;
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector;

Expand All @@ -38,6 +39,7 @@
AddReturnDocblockForScalarArrayFromAssignsRector::class,
DocblockReturnArrayFromDirectArrayInstanceRector::class,
AddReturnDocblockForArrayDimAssignedObjectRector::class,
AddReturnDocblockForJsonArrayRector::class,

// tests
AddParamArrayDocblockFromDataProviderRector::class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddReturnDocblockForJsonArrayRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;

use Nette\Utils\Json;

final class JsonUtils
{
public function provide(string $contents): array
{
return Json::decode($contents, true);
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;

use Nette\Utils\Json;

final class JsonUtils
{
/**
* @return array<string, mixed>
*/
public function provide(string $contents): array
{
return Json::decode($contents, true);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;

final class SkipAlreadyFilled
{
/**
* @return array<string, array<string, mixed>>
*/
public function provide(string $contents): array
{
return json_decode($contents, true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;

final class SkipMissingArg
{
public function provide(string $contents): array
{
return json_decode($contents, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;

final class SomeClass
{
public function provide(string $contents): array
{
return json_decode($contents, true);
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector\Fixture;

final class SomeClass
{
/**
* @return array<string, mixed>
*/
public function provide(string $contents): array
{
return json_decode($contents, true);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclarationDocblocks\Rector\ClassMethod\AddReturnDocblockForJsonArrayRector;

return RectorConfig::configure()
->withRules([AddReturnDocblockForJsonArrayRector::class]);
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use Rector\Rector\AbstractRector;
use Rector\TypeDeclarationDocblocks\Enum\NetteClassName;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -49,7 +50,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isName($node->class, 'Nette\Utils\Json')) {
if (! $this->isName($node->class, NetteClassName::JSON)) {
return null;
}

Expand Down
30 changes: 15 additions & 15 deletions rules/Php84/Rector/Foreach_/ForeachToArrayAllRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ public function provideMinPhpVersion(): int
return PhpVersionFeature::ARRAY_ALL;
}

private function refactorBooleanAssignmentPattern(StmtsAwareInterface $node): ?Node
private function refactorBooleanAssignmentPattern(StmtsAwareInterface $stmtsAware): ?Node
{
foreach ($node->stmts as $key => $stmt) {
foreach ($stmtsAware->stmts as $key => $stmt) {
if (! $stmt instanceof Foreach_) {
continue;
}

$prevStmt = $node->stmts[$key - 1] ?? null;
$prevStmt = $stmtsAware->stmts[$key - 1] ?? null;
if (! $prevStmt instanceof Expression) {
continue;
}
Expand All @@ -136,7 +136,7 @@ private function refactorBooleanAssignmentPattern(StmtsAwareInterface $node): ?N
}

if ($this->stmtsManipulator->isVariableUsedInNextStmt(
$node,
$stmtsAware,
$key + 1,
(string) $this->getName($foreach->valueVar)
)) {
Expand Down Expand Up @@ -174,26 +174,26 @@ private function refactorBooleanAssignmentPattern(StmtsAwareInterface $node): ?N
$newAssign = new Assign($assignedVariable, $funcCall);
$newExpression = new Expression($newAssign);

unset($node->stmts[$key - 1]);
$node->stmts[$key] = $newExpression;
unset($stmtsAware->stmts[$key - 1]);
$stmtsAware->stmts[$key] = $newExpression;

$node->stmts = array_values($node->stmts);
$stmtsAware->stmts = array_values($stmtsAware->stmts);

return $node;
return $stmtsAware;
}

return null;
}

private function refactorEarlyReturnPattern(StmtsAwareInterface $node): ?Node
private function refactorEarlyReturnPattern(StmtsAwareInterface $stmtsAware): ?Node
{
foreach ($node->stmts as $key => $stmt) {
foreach ($stmtsAware->stmts as $key => $stmt) {
if (! $stmt instanceof Foreach_) {
continue;
}

$foreach = $stmt;
$nextStmt = $node->stmts[$key + 1] ?? null;
$nextStmt = $stmtsAware->stmts[$key + 1] ?? null;

if (! $nextStmt instanceof Return_) {
continue;
Expand Down Expand Up @@ -236,11 +236,11 @@ private function refactorEarlyReturnPattern(StmtsAwareInterface $node): ?Node

$funcCall = $this->nodeFactory->createFuncCall('array_all', [$foreach->expr, $arrowFunction]);

$node->stmts[$key] = new Return_($funcCall);
unset($node->stmts[$key + 1]);
$node->stmts = array_values($node->stmts);
$stmtsAware->stmts[$key] = new Return_($funcCall);
unset($stmtsAware->stmts[$key + 1]);
$stmtsAware->stmts = array_values($stmtsAware->stmts);

return $node;
return $stmtsAware;
}

return null;
Expand Down
13 changes: 13 additions & 0 deletions rules/TypeDeclarationDocblocks/Enum/NetteClassName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Rector\TypeDeclarationDocblocks\Enum;

final class NetteClassName
{
/**
* @var string
*/
public const JSON = 'Nette\Utils\Json';
}
Loading
Loading