1
0

rector.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 PhpParser\Node;
  8. use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
  9. use Rector\Config\RectorConfig;
  10. use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
  11. use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
  12. use Rector\ValueObject\Application\File;
  13. $nextcloudDir = dirname(__DIR__);
  14. class NextcloudNamespaceSkipVoter implements ClassNameImportSkipVoterInterface {
  15. private array $namespacePrefixes = [
  16. 'OC',
  17. 'OCA',
  18. 'OCP',
  19. ];
  20. private array $skippedClassNames = [
  21. 'Backend',
  22. 'Connection',
  23. 'Exception',
  24. 'IManager',
  25. 'IProvider',
  26. 'Manager',
  27. 'Plugin',
  28. 'Provider',
  29. ];
  30. public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node) : bool {
  31. if (in_array($fullyQualifiedObjectType->getShortName(), $this->skippedClassNames)) {
  32. // Skip common class names to avoid confusion
  33. return true;
  34. }
  35. foreach ($this->namespacePrefixes as $prefix) {
  36. if (str_starts_with($fullyQualifiedObjectType->getClassName(), $prefix . '\\')) {
  37. // Import Nextcloud namespaces
  38. return false;
  39. }
  40. }
  41. // Skip everything else
  42. return true;
  43. }
  44. }
  45. $config = RectorConfig::configure()
  46. ->withPaths([
  47. $nextcloudDir . '/apps',
  48. // $nextcloudDir . '/config',
  49. // $nextcloudDir . '/core',
  50. // $nextcloudDir . '/lib',
  51. // $nextcloudDir . '/ocs',
  52. // $nextcloudDir . '/ocs-provider',
  53. // $nextcloudDir . '/tests',
  54. // $nextcloudDir . '/themes',
  55. ])
  56. ->withSkip([
  57. $nextcloudDir . '/apps/*/3rdparty/*',
  58. $nextcloudDir . '/apps/*/build/stubs/*',
  59. $nextcloudDir . '/apps/*/composer/*',
  60. $nextcloudDir . '/apps/*/config/*',
  61. ])
  62. // uncomment to reach your current PHP version
  63. // ->withPhpSets()
  64. ->withImportNames(importShortClasses:false)
  65. ->withTypeCoverageLevel(0)
  66. ->withConfiguredRule(ClassPropertyAssignToConstructorPromotionRector::class, [
  67. 'inline_public' => true,
  68. 'rename_property' => true,
  69. ]);
  70. $config->registerService(NextcloudNamespaceSkipVoter::class, tag:ClassNameImportSkipVoterInterface::class);
  71. /* Ignore all files ignored by git */
  72. $ignoredEntries = shell_exec('git status --porcelain --ignored ' . escapeshellarg($nextcloudDir));
  73. $ignoredEntries = explode("\n", $ignoredEntries);
  74. $ignoredEntries = array_filter($ignoredEntries, static fn (string $line) => str_starts_with($line, '!! '));
  75. $ignoredEntries = array_map(static fn (string $line) => substr($line, 3), $ignoredEntries);
  76. $ignoredEntries = array_values($ignoredEntries);
  77. foreach ($ignoredEntries as $ignoredEntry) {
  78. if (str_ends_with($ignoredEntry, '/')) {
  79. $config->withSkip([$ignoredEntry . '*']);
  80. } else {
  81. $config->withSkip([$ignoredEntry . '/*']);
  82. }
  83. }
  84. return $config;