You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.5 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. use Nextcloud\Rector\Set\NextcloudSets;
  8. use PhpParser\Node;
  9. use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
  10. use Rector\Config\RectorConfig;
  11. use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
  12. use Rector\PHPUnit\Set\PHPUnitSetList;
  13. use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
  14. use Rector\ValueObject\Application\File;
  15. $nextcloudDir = dirname(__DIR__);
  16. class NextcloudNamespaceSkipVoter implements ClassNameImportSkipVoterInterface {
  17. private array $namespacePrefixes = [
  18. 'OC',
  19. 'OCA',
  20. 'OCP',
  21. ];
  22. private array $skippedClassNames = [
  23. 'Backend',
  24. 'Connection',
  25. 'Exception',
  26. 'IManager',
  27. 'IProvider',
  28. 'Manager',
  29. 'Plugin',
  30. 'Provider',
  31. ];
  32. public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node) : bool {
  33. if (in_array($fullyQualifiedObjectType->getShortName(), $this->skippedClassNames)) {
  34. // Skip common class names to avoid confusion
  35. return true;
  36. }
  37. foreach ($this->namespacePrefixes as $prefix) {
  38. if (str_starts_with($fullyQualifiedObjectType->getClassName(), $prefix . '\\')) {
  39. // Import Nextcloud namespaces
  40. return false;
  41. }
  42. }
  43. // Skip everything else
  44. return true;
  45. }
  46. }
  47. $config = RectorConfig::configure()
  48. ->withPaths([
  49. $nextcloudDir . '/apps',
  50. $nextcloudDir . '/core',
  51. $nextcloudDir . '/ocs',
  52. $nextcloudDir . '/ocs-provider',
  53. $nextcloudDir . '/console.php',
  54. $nextcloudDir . '/cron.php',
  55. $nextcloudDir . '/index.php',
  56. $nextcloudDir . '/occ',
  57. $nextcloudDir . '/public.php',
  58. $nextcloudDir . '/remote.php',
  59. $nextcloudDir . '/status.php',
  60. $nextcloudDir . '/version.php',
  61. $nextcloudDir . '/lib/private/Share20/ProviderFactory.php',
  62. $nextcloudDir . '/lib/private/Template',
  63. $nextcloudDir . '/tests',
  64. // $nextcloudDir . '/config',
  65. // $nextcloudDir . '/lib',
  66. // $nextcloudDir . '/themes',
  67. ])
  68. ->withSkip([
  69. $nextcloudDir . '/apps/*/3rdparty/*',
  70. $nextcloudDir . '/apps/*/build/stubs/*',
  71. $nextcloudDir . '/apps/*/composer/*',
  72. $nextcloudDir . '/apps/*/config/*',
  73. // The mock classes are excluded, as the tests explicitly test the annotations which should not be migrated to attributes
  74. $nextcloudDir . '/tests/lib/AppFramework/Middleware/Mock/*',
  75. $nextcloudDir . '/tests/lib/AppFramework/Middleware/Security/Mock/*',
  76. ])
  77. // uncomment to reach your current PHP version
  78. // ->withPhpSets()
  79. ->withImportNames(importShortClasses:false)
  80. ->withTypeCoverageLevel(0)
  81. ->withConfiguredRule(ClassPropertyAssignToConstructorPromotionRector::class, [
  82. 'inline_public' => true,
  83. 'rename_property' => true,
  84. ])
  85. ->withSets([
  86. NextcloudSets::NEXTCLOUD_27,
  87. PHPUnitSetList::PHPUNIT_100,
  88. ]);
  89. $config->registerService(NextcloudNamespaceSkipVoter::class, tag:ClassNameImportSkipVoterInterface::class);
  90. /* Ignore all files ignored by git */
  91. $ignoredEntries = shell_exec('git status --porcelain --ignored ' . escapeshellarg($nextcloudDir));
  92. $ignoredEntries = explode("\n", $ignoredEntries);
  93. $ignoredEntries = array_filter($ignoredEntries, static fn (string $line) => str_starts_with($line, '!! '));
  94. $ignoredEntries = array_map(static fn (string $line) => substr($line, 3), $ignoredEntries);
  95. $ignoredEntries = array_values($ignoredEntries);
  96. foreach ($ignoredEntries as $ignoredEntry) {
  97. if (str_ends_with($ignoredEntry, '/')) {
  98. $config->withSkip([$ignoredEntry . '*']);
  99. } else {
  100. $config->withSkip([$ignoredEntry . '/*']);
  101. }
  102. }
  103. return $config;