|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Testing\Command; |
| 11 | + |
| 12 | +use OCP\App\IAppManager; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | + |
| 17 | +class StaticHunt extends Command { |
| 18 | + private const SKIP_REGEX = [ |
| 19 | + '@/templates/.+.php$@', |
| 20 | + '@/ajax/.+.php$@', |
| 21 | + '@/register_command.php$@', |
| 22 | + ]; |
| 23 | + |
| 24 | + public function __construct( |
| 25 | + private IAppManager $appManager, |
| 26 | + ) { |
| 27 | + parent::__construct(); |
| 28 | + } |
| 29 | + |
| 30 | + #[\Override] |
| 31 | + protected function configure(): void { |
| 32 | + $this |
| 33 | + ->setName('testing:static-hunt') |
| 34 | + ->setDescription('Hunt for static properties in classes'); |
| 35 | + } |
| 36 | + |
| 37 | + #[\Override] |
| 38 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 39 | + $folders = [ |
| 40 | + '' => __DIR__ . '/../../../../lib/private/legacy', |
| 41 | + '\\OC' => __DIR__ . '/../../../../lib/private', |
| 42 | + '\\OC\\Core' => __DIR__ . '/../../../../core', |
| 43 | + ]; |
| 44 | + $apps = $this->appManager->getAllAppsInAppsFolders(); |
| 45 | + foreach ($apps as $app) { |
| 46 | + $info = $this->appManager->getAppInfo($app); |
| 47 | + if (!isset($info['namespace'])) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + $folders['\\OCA\\' . $info['namespace']] = $this->appManager->getAppPath($app) . '/lib'; |
| 51 | + } |
| 52 | + $stats = [ |
| 53 | + 'classes' => 0, |
| 54 | + 'properties' => 0, |
| 55 | + ]; |
| 56 | + foreach ($folders as $namespace => $folder) { |
| 57 | + $this->scanFolder($folder, $namespace, $output, $stats); |
| 58 | + } |
| 59 | + |
| 60 | + $output->writeln('<info>Found ' . $stats['properties'] . ' static properties spread among ' . $stats['classes'] . ' classes</info>'); |
| 61 | + |
| 62 | + return 0; |
| 63 | + } |
| 64 | + |
| 65 | + private function scanFolder(string $folder, string $namespace, OutputInterface $output, array &$stats): void { |
| 66 | + $folder = realpath($folder); |
| 67 | + $output->writeln('Folder ' . $folder, OutputInterface::VERBOSITY_VERBOSE); |
| 68 | + foreach ($this->recursiveGlob($folder) as $filename) { |
| 69 | + try { |
| 70 | + $filename = realpath($filename); |
| 71 | + if (($namespace === '\\OC') && str_contains($filename, 'lib/private/legacy')) { |
| 72 | + // Skip legacy in OC as it’s scanned with an empty namespace separately |
| 73 | + continue; |
| 74 | + } |
| 75 | + foreach (self::SKIP_REGEX as $skipRegex) { |
| 76 | + if (preg_match($skipRegex, $filename)) { |
| 77 | + continue 2; |
| 78 | + } |
| 79 | + } |
| 80 | + $classname = $namespace . substr(str_replace('/', '\\', substr($filename, strlen($folder))), 0, -4); |
| 81 | + $output->writeln('Class ' . $classname, OutputInterface::VERBOSITY_VERBOSE); |
| 82 | + if (!class_exists($classname)) { |
| 83 | + continue; |
| 84 | + } |
| 85 | + $rClass = new \ReflectionClass($classname); |
| 86 | + $staticProperties = $rClass->getStaticProperties(); |
| 87 | + if (empty($staticProperties)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + $stats['classes']++; |
| 91 | + $output->writeln('<info># ' . str_replace(\OC::$SERVERROOT, '', $filename) . " $classname</info>"); |
| 92 | + foreach ($staticProperties as $property => $value) { |
| 93 | + $propertyObject = $rClass->getProperty($property); |
| 94 | + $stats['properties']++; |
| 95 | + $output->write("$propertyObject"); |
| 96 | + } |
| 97 | + $output->writeln(''); |
| 98 | + } catch (\Throwable $t) { |
| 99 | + $output->writeln("$t"); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private function recursiveGlob(string $path, int $depth = 1): \Generator { |
| 105 | + $pattern = $path . str_repeat('/*', $depth); |
| 106 | + yield from glob($pattern . '.php'); |
| 107 | + if (!empty(glob($pattern, GLOB_ONLYDIR))) { |
| 108 | + yield from $this->recursiveGlob($path, $depth + 1); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments