aboutsummaryrefslogtreecommitdiffstats
path: root/build/rector.php
diff options
context:
space:
mode:
authorCôme Chilliet <come.chilliet@nextcloud.com>2024-09-26 11:11:20 +0200
committerCôme Chilliet <come.chilliet@nextcloud.com>2024-10-15 10:38:05 +0200
commit28153443a55290877f4507a06b404f39c1cd6ec1 (patch)
tree5a32cf046b1f146348745111e069462c44772fb0 /build/rector.php
parent832695d5654bb3bfe81a29a3557d99b2b4a334f0 (diff)
downloadnextcloud-server-28153443a55290877f4507a06b404f39c1cd6ec1.tar.gz
nextcloud-server-28153443a55290877f4507a06b404f39c1cd6ec1.zip
chore(rector): Add rule to import Nextcloud classes in all files
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>
Diffstat (limited to 'build/rector.php')
-rw-r--r--build/rector.php25
1 files changed, 25 insertions, 0 deletions
diff --git a/build/rector.php b/build/rector.php
index ed8b748663e..9df995a0044 100644
--- a/build/rector.php
+++ b/build/rector.php
@@ -7,10 +7,32 @@ declare(strict_types=1);
* 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',
@@ -30,9 +52,12 @@ $config = RectorConfig::configure()
])
// 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, '!! '));