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
79 changes: 79 additions & 0 deletions config/nextcloud-35/nextcloud-35-deprecations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

use Nextcloud\Rector\Rector\OrderBySortDirectionRector;
use Nextcloud\Rector\Rector\TypeLookupNameToGetNameRector;
use Nextcloud\Rector\Set\NextcloudSets;
use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Renaming\ValueObject\RenameClassAndConstFetch;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([NextcloudSets::NEXTCLOUD_34]);
$rectorConfig->rule(OrderBySortDirectionRector::class);
$rectorConfig->rule(TypeLookupNameToGetNameRector::class);
$rectorConfig->skip([
// These are the server-internal classes implementing the OCP\DB\Schema wrapper itself,
// they still work with the raw Doctrine\DBAL\Types\Type and must not be rewritten.
TypeLookupNameToGetNameRector::class => [
'lib/private/DB/MigrationService.php',
'lib/private/DB/Schema/Column.php',
],
]);
$rectorConfig->ruleWithConfiguration(
RenameClassRector::class,
[
'Doctrine\DBAL\Schema\Table' => 'OCP\DB\Schema\ITable',
'Doctrine\DBAL\Schema\Column' => 'OCP\DB\Schema\IColumn',
'Doctrine\DBAL\Schema\Index' => 'OCP\DB\Schema\IIndex',
'Doctrine\DBAL\Schema\ForeignKeyConstraint' => 'OCP\DB\Schema\IForeignKeyConstraint',
'Doctrine\DBAL\Schema\SchemaException' => 'OCP\DB\Schema\SchemaException',
],
);
$rectorConfig->ruleWithConfiguration(
RenameClassConstFetchRector::class,
[
new RenameClassAndConstFetch('OCP\DB\Types', 'BIGINT', 'OCP\DB\Schema\ColumnType', 'Bigint'),
new RenameClassAndConstFetch('OCP\DB\Types', 'BINARY', 'OCP\DB\Schema\ColumnType', 'Binary'),
new RenameClassAndConstFetch('OCP\DB\Types', 'BLOB', 'OCP\DB\Schema\ColumnType', 'Blob'),
new RenameClassAndConstFetch('OCP\DB\Types', 'BOOLEAN', 'OCP\DB\Schema\ColumnType', 'Boolean'),
new RenameClassAndConstFetch('OCP\DB\Types', 'DATE', 'OCP\DB\Schema\ColumnType', 'Date'),
new RenameClassAndConstFetch(
'OCP\DB\Types',
'DATE_IMMUTABLE',
'OCP\DB\Schema\ColumnType',
'DateImmutable',
),
new RenameClassAndConstFetch('OCP\DB\Types', 'DATETIME', 'OCP\DB\Schema\ColumnType', 'Datetime'),
new RenameClassAndConstFetch(
'OCP\DB\Types',
'DATETIME_IMMUTABLE',
'OCP\DB\Schema\ColumnType',
'DatetimeImmutable',
),
new RenameClassAndConstFetch('OCP\DB\Types', 'DATETIME_TZ', 'OCP\DB\Schema\ColumnType', 'DatetimeTz'),
new RenameClassAndConstFetch(
'OCP\DB\Types',
'DATETIME_TZ_IMMUTABLE',
'OCP\DB\Schema\ColumnType',
'DatetimeTzImmutable',
),
new RenameClassAndConstFetch('OCP\DB\Types', 'DECIMAL', 'OCP\DB\Schema\ColumnType', 'Decimal'),
new RenameClassAndConstFetch('OCP\DB\Types', 'FLOAT', 'OCP\DB\Schema\ColumnType', 'Float'),
new RenameClassAndConstFetch('OCP\DB\Types', 'INTEGER', 'OCP\DB\Schema\ColumnType', 'Integer'),
new RenameClassAndConstFetch('OCP\DB\Types', 'SMALLINT', 'OCP\DB\Schema\ColumnType', 'Smallint'),
new RenameClassAndConstFetch('OCP\DB\Types', 'STRING', 'OCP\DB\Schema\ColumnType', 'String'),
new RenameClassAndConstFetch('OCP\DB\Types', 'TEXT', 'OCP\DB\Schema\ColumnType', 'Text'),
new RenameClassAndConstFetch('OCP\DB\Types', 'TIME', 'OCP\DB\Schema\ColumnType', 'Time'),
new RenameClassAndConstFetch(
'OCP\DB\Types',
'TIME_IMMUTABLE',
'OCP\DB\Schema\ColumnType',
'TimeImmutable',
),
new RenameClassAndConstFetch('OCP\DB\Types', 'JSON', 'OCP\DB\Schema\ColumnType', 'Json'),
],
);
};
1 change: 0 additions & 1 deletion src/Rector/LegacyGetterToOcpServerGetRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public function configure(array $configuration): void
}
/**
* @psalm-suppress MixedPropertyTypeCoercion
* @phpstan-ignore assign.propertyType
*/
$this->legacyGetterToOcpServerGet = $configuration;
}
Expand Down
120 changes: 120 additions & 0 deletions src/Rector/OrderBySortDirectionRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

declare(strict_types=1);

/*
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Nextcloud\Rector\Rector;

use Override;
use PHPStan\Type\ObjectType;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

use function strtolower;

final class OrderBySortDirectionRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace the \'ASC\'/\'DESC\' string argument of IQueryBuilder::orderBy() and ::addOrderBy() '
. 'with the \SortDirection enum',
[
new CodeSample(
<<<'CODE_SAMPLE'
$query->orderBy('name', 'ASC');
$query->addOrderBy('name', 'DESC');
CODE_SAMPLE,
<<<'CODE_SAMPLE'
$query->orderBy('name', \SortDirection::Ascending);
$query->addOrderBy('name', \SortDirection::Descending);
CODE_SAMPLE,
),
],
);
}

/**
* @return array<class-string<Node>>
*/
#[Override]
public function getNodeTypes(): array
{
return [MethodCall::class];
}

#[Override]
public function refactor(Node $node): ?Node
{
if (!($node instanceof MethodCall)) {
return null;
}

if (!$this->isNames($node->name, ['orderBy', 'addOrderBy'])) {
return null;
}

if (!$this->isObjectType($node->var, new ObjectType('OCP\DB\QueryBuilder\IQueryBuilder'))) {
return null;
}

$orderArg = $this->resolveOrderArg($node->getArgs());
if (!$orderArg instanceof Arg) {
return null;
}

if (!$orderArg->value instanceof String_) {
return null;
}

$enumCase = match (strtolower($orderArg->value->value)) {
'asc' => 'Ascending',
'desc' => 'Descending',
default => null,
};

if ($enumCase === null) {
return null;
}

$orderArg->value = new ClassConstFetch(new FullyQualified('SortDirection'), $enumCase);

return $node;
}

/**
* @param Arg[] $args
*/
private function resolveOrderArg(array $args): ?Arg
{
$position = 0;
foreach ($args as $arg) {
if ($arg->name !== null) {
if ($this->isName($arg->name, 'order')) {
return $arg;
}

continue;
}

if ($position === 1) {
return $arg;
}

++$position;
}

return null;
}
}
Loading