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
12 changes: 12 additions & 0 deletions config/nextcloud-33/nextcloud-33-deprecations.php
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);
};
2 changes: 1 addition & 1 deletion src/Rector/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ function (DocNode $docNode) use (&$attributeGroups) {
}
$tag = trim($docNode->name, '@');
// not a basic one
if (strpos($tag, '\\') !== \false) {
if (strpos($tag, '\\') !== false) {
return null;
}
foreach ($this->annotationsToAttributes as $annotationToAttribute) {
Expand Down
4 changes: 2 additions & 2 deletions src/Rector/RenameParameterRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ public function refactor(Node $node): ?Node
return null;
}

$hasChanged = \false;
$hasChanged = false;

foreach ($this->renameParameters as $renameParameter) {
foreach ($params as $param) {
if ($this->renameVariable($param->var, $renameParameter)) {
$hasChanged = \true;
$hasChanged = true;
}
}
}
Expand Down
166 changes: 166 additions & 0 deletions src/Rector/ReplaceFetchAllMethodCallRector.php
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;
}
}
3 changes: 3 additions & 0 deletions src/Set/NextcloudSets.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ final class NextcloudSets
public const NEXTCLOUD_28 = self::NEXTCLOUD_27;
public const NEXTCLOUD_29 = self::NEXTCLOUD_27;
public const NEXTCLOUD_30 = self::NEXTCLOUD_27;
public const NEXTCLOUD_31 = self::NEXTCLOUD_27;
public const NEXTCLOUD_32 = self::NEXTCLOUD_27;
public const NEXTCLOUD_33 = __DIR__ . '/../../config/nextcloud-33/nextcloud-33-deprecations.php';
}
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;

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();
}
}

?>
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 tests/Rector/ReplaceFetchAllMethodCallRector/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

/*
* 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,
]);