-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Replace fetch with fetchAssociative #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Nextcloud\Rector\Rector\ReplaceFetchAllMethodCallRector; | ||
| use Nextcloud\Rector\Set\NextcloudSets; | ||
| use Rector\Config\RectorConfig; | ||
|
|
||
| return static function (RectorConfig $rectorConfig): void { | ||
| $rectorConfig->sets([NextcloudSets::NEXTCLOUD_27]); | ||
| $rectorConfig->rule(ReplaceFetchAllMethodCallRector::class); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH | ||
| * SPDX-FileContributor: Carl Schwan | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace Nextcloud\Rector\Rector; | ||
|
|
||
| use PHPStan\Type\ObjectType; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Arg; | ||
| use PhpParser\Node\Expr\ClassConstFetch; | ||
| use PhpParser\Node\Expr\MethodCall; | ||
| use PhpParser\Node\Identifier; | ||
| use Rector\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| use function is_string; | ||
|
|
||
| final class ReplaceFetchAllMethodCallRector extends AbstractRector | ||
| { | ||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Change \OCP\DB\IResult->fetchAll() to ->fetchAllAssociative() and other replacements', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| use OCP\DB\IResult; | ||
| use OCP\DB\IConnection; | ||
| use OCP\DB\IQueryBuilder; | ||
|
|
||
| class SomeClass | ||
| { | ||
| public function run(IConnection $connection) | ||
| { | ||
| $qb = $connection->getQueryBuilder(); | ||
| $result = $qb->exec(); | ||
| return $result->fetchAll(); | ||
| } | ||
| } | ||
| CODE_SAMPLE, | ||
| <<<'CODE_SAMPLE' | ||
| use OCP\DB\IResult; | ||
| use OCP\DB\IConnection; | ||
| use OCP\DB\IQueryBuilder; | ||
|
|
||
| class SomeClass | ||
| { | ||
| public function run(IConnection $connection) | ||
| { | ||
| $qb = $connection->getQueryBuilder(); | ||
| $result = $qb->exec(); | ||
| return $result->fetchAllAssociative(); | ||
| } | ||
| } | ||
| CODE_SAMPLE, | ||
| ), | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [MethodCall::class]; | ||
| } | ||
|
|
||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if (!($node instanceof MethodCall)) { | ||
| return null; | ||
| } | ||
| if ($this->isObjectType($node->var, new ObjectType('OCP\DB\IResult'))) { | ||
| return $this->refactorResultStatement($node); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private function refactorResultStatement(MethodCall $methodCall): ?MethodCall | ||
| { | ||
| if ($this->isName($methodCall->name, 'fetchColumn')) { | ||
| $methodCall->name = new Identifier('fetchOne'); | ||
|
|
||
| return $methodCall; | ||
| } | ||
|
|
||
| if ($this->isName($methodCall->name, 'fetch')) { | ||
| $args = $methodCall->getArgs(); | ||
| if ($args === []) { | ||
| $methodCall->name = new Identifier('fetchAssociative'); | ||
|
|
||
| return $methodCall; | ||
| } | ||
|
|
||
| $firstArg = $args[0]; | ||
|
|
||
| $newMethodName = $this->resolveFirstMethodName($firstArg, false); | ||
| if (is_string($newMethodName)) { | ||
| $methodCall->args = []; | ||
|
|
||
| $methodCall->name = new Identifier($newMethodName); | ||
|
|
||
| return $methodCall; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| if ($this->isName($methodCall->name, 'fetchAll')) { | ||
| $args = $methodCall->getArgs(); | ||
| if ($args === []) { | ||
| $methodCall->name = new Identifier('fetchAllAssociative'); | ||
|
|
||
| return $methodCall; | ||
| } | ||
|
|
||
| $firstArg = $args[0]; | ||
|
|
||
| $newMethodName = $this->resolveFirstMethodName($firstArg, true); | ||
| if (is_string($newMethodName)) { | ||
| $methodCall->args = []; | ||
|
|
||
| $methodCall->name = new Identifier($newMethodName); | ||
|
|
||
| return $methodCall; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private function resolveFirstMethodName(Arg $firstArg, bool $all = false): ?string | ||
| { | ||
| if (!$firstArg->value instanceof ClassConstFetch) { | ||
| return null; | ||
| } | ||
|
|
||
| $classConstFetch = $firstArg->value; | ||
| if (!$this->isName($classConstFetch->class, 'PDO')) { | ||
| return null; | ||
| } | ||
|
|
||
| if ($this->isName($classConstFetch->name, 'FETCH_COLUMN')) { | ||
| return $all ? 'fetchFirstColumn' : 'fetchOne'; | ||
| } | ||
|
|
||
| if ($this->isName($classConstFetch->name, 'FETCH_ASSOC')) { | ||
| return $all ? 'fetchAllAssociative' : 'fetchAssociative'; | ||
| } | ||
|
|
||
| if ($this->isName($classConstFetch->name, 'FETCH_NUM')) { | ||
| return $all ? 'fetchAllNumeric' : 'fetchNumeric'; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
tests/Rector/ReplaceFetchAllMethodCallRector/Fixture/test_fixture_fetch.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCP\AppFramework; | ||
CarlSchwan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| use OCP\IDBConnection; | ||
| use PDO; | ||
|
|
||
| class SomeClass | ||
| { | ||
| public function __construct(IDBConnection $connection) { | ||
| $qb = $connection->getQueryBuilder(); | ||
| $result = $qb->select('*') | ||
| ->from('pages') | ||
| ->executeQuery(); | ||
|
|
||
| $result->fetchAll(); | ||
| $result->fetchAll(PDO::FETCH_COLUMN); | ||
| $result->fetchAll(PDO::FETCH_ASSOC); | ||
| $result->fetchAll(PDO::FETCH_NUM); | ||
|
|
||
| $result->fetch(PDO::FETCH_COLUMN); | ||
| $result->fetch(PDO::FETCH_ASSOC); | ||
| $result->fetch(); | ||
| $result->fetch(PDO::FETCH_NUM); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCP\AppFramework; | ||
|
|
||
| use OCP\IDBConnection; | ||
| use PDO; | ||
|
|
||
| class SomeClass | ||
| { | ||
| public function __construct(IDBConnection $connection) { | ||
| $qb = $connection->getQueryBuilder(); | ||
| $result = $qb->select('*') | ||
| ->from('pages') | ||
| ->executeQuery(); | ||
|
|
||
| $result->fetchAllAssociative(); | ||
| $result->fetchFirstColumn(); | ||
| $result->fetchAllAssociative(); | ||
| $result->fetchAllNumeric(); | ||
|
|
||
| $result->fetchOne(); | ||
| $result->fetchAssociative(); | ||
| $result->fetchAssociative(); | ||
| $result->fetchNumeric(); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
33 changes: 33 additions & 0 deletions
33
tests/Rector/ReplaceFetchAllMethodCallRector/ReplaceFetchAllMethodCallRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace Nextcloud\Rector\Test\Rector\ReplaceFetchAllMethodCallRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class ReplaceFetchAllMethodCallRectorTest 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/config.php'; | ||
| } | ||
| } |
16 changes: 16 additions & 0 deletions
16
tests/Rector/ReplaceFetchAllMethodCallRector/config/config.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
CarlSchwan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /* | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| use Nextcloud\Rector\Rector\ReplaceFetchAllMethodCallRector; | ||
| use Rector\Config\RectorConfig; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([ | ||
| ReplaceFetchAllMethodCallRector::class, | ||
| ]); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.