Skip to content

Commit

Permalink
chore(rector): Add rule to import Nextcloud classes in all files
Browse files Browse the repository at this point in the history
We skip all files outside of OC/OCA/OCP from "use" auto-import because
 it’s not always desirable to import classes when they have generic
 names, so it should be the developer choice.

Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
  • Loading branch information
come-nc committed Sep 26, 2024
1 parent 4febe10 commit ec5b140
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions build/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,32 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/

use PhpParser\Node;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\Config\RectorConfig;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\ValueObject\Application\File;

$nextcloudDir = dirname(__DIR__);

class NextcloudNamespaceSkipVoter implements ClassNameImportSkipVoterInterface {
private array $namespacePrefixes = [
'OC',
'OCA',
'OCP',
];
public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node) : bool {
foreach ($this->namespacePrefixes as $prefix) {
if (str_starts_with($fullyQualifiedObjectType->getClassName(), $prefix . '\\')) {
// Import Nextcloud namespaces
return false;
}
}
// Skip everything else
return true;
}
}

$config = RectorConfig::configure()
->withPaths([
$nextcloudDir . '/apps',
Expand All @@ -30,9 +52,12 @@
])
// uncomment to reach your current PHP version
// ->withPhpSets()
->withImportNames(importShortClasses:false)
->withTypeCoverageLevel(0);

$config->registerService(NextcloudNamespaceSkipVoter::class, tag:ClassNameImportSkipVoterInterface::class);

/* Ignore all files ignored by git */
$ignoredEntries = shell_exec('git status --porcelain --ignored ' . escapeshellarg($nextcloudDir));
$ignoredEntries = explode("\n", $ignoredEntries);
$ignoredEntries = array_filter($ignoredEntries, static fn (string $line) => str_starts_with($line, '!! '));
Expand Down

0 comments on commit ec5b140

Please sign in to comment.