From 73447618e740119616db2f2e4745ec96c95b3169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Mon, 29 Sep 2025 12:23:37 +0200 Subject: [PATCH] chore: Run rector on lib/private/Template folder and cleanup more typing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- build/rector.php | 1 + lib/private/Template/Base.php | 29 +++----- lib/private/Template/JSCombiner.php | 74 +++++-------------- lib/private/Template/ResourceLocator.php | 3 +- .../Template/ResourceNotFoundException.php | 22 ++---- lib/private/Template/TemplateManager.php | 7 +- tests/lib/Template/ResourceLocatorTest.php | 7 +- 7 files changed, 47 insertions(+), 96 deletions(-) diff --git a/build/rector.php b/build/rector.php index 90b33bad6ff..fe6952d2b2b 100644 --- a/build/rector.php +++ b/build/rector.php @@ -67,6 +67,7 @@ $config = RectorConfig::configure() $nextcloudDir . '/status.php', $nextcloudDir . '/version.php', $nextcloudDir . '/lib/private/Share20/ProviderFactory.php', + $nextcloudDir . '/lib/private/Template', $nextcloudDir . '/tests', // $nextcloudDir . '/config', // $nextcloudDir . '/lib', diff --git a/lib/private/Template/Base.php b/lib/private/Template/Base.php index a13e6703960..d683cd6520a 100644 --- a/lib/private/Template/Base.php +++ b/lib/private/Template/Base.php @@ -1,39 +1,32 @@ vars = [ 'cspNonce' => $cspNonce, 'requesttoken' => $requestToken, ]; - $this->l10n = $l10n; - $this->template = $template; - $this->theme = $theme; } /** diff --git a/lib/private/Template/JSCombiner.php b/lib/private/Template/JSCombiner.php index a94f822a448..7ee76990d90 100644 --- a/lib/private/Template/JSCombiner.php +++ b/lib/private/Template/JSCombiner.php @@ -1,9 +1,12 @@ appData = $appData; - $this->urlGenerator = $urlGenerator; - $this->cacheFactory = $cacheFactory; + public function __construct( + protected IAppData $appData, + protected IURLGenerator $urlGenerator, + protected ICacheFactory $cacheFactory, + protected SystemConfig $config, + protected LoggerInterface $logger, + ) { $this->depsCache = $this->cacheFactory->createDistributed('JS-' . md5($this->urlGenerator->getBaseUrl())); - $this->config = $config; - $this->logger = $logger; } - /** - * @param string $root - * @param string $file - * @param string $app - * @return bool - */ - public function process($root, $file, $app) { + public function process(string $root, string $file, string $app): bool { if ($this->config->getValue('debug') || !$this->config->getValue('installed')) { return false; } @@ -76,12 +55,7 @@ class JSCombiner { return $this->cache($path, $fileName, $folder); } - /** - * @param string $fileName - * @param ISimpleFolder $folder - * @return bool - */ - protected function isCached($fileName, ISimpleFolder $folder) { + protected function isCached(string $fileName, ISimpleFolder $folder): bool { $fileName = str_replace('.json', '.js', $fileName); if (!$folder->fileExists($fileName)) { @@ -126,13 +100,7 @@ class JSCombiner { } } - /** - * @param string $path - * @param string $fileName - * @param ISimpleFolder $folder - * @return bool - */ - protected function cache($path, $fileName, ISimpleFolder $folder) { + protected function cache(string $path, string $fileName, ISimpleFolder $folder): bool { $deps = []; $fullPath = $path . '/' . $fileName; $data = json_decode(file_get_contents($fullPath)); @@ -183,12 +151,7 @@ class JSCombiner { } } - /** - * @param string $appName - * @param string $fileName - * @return string - */ - public function getCachedJS($appName, $fileName) { + public function getCachedJS(string $appName, string $fileName): string { $tmpfileLoc = explode('/', $fileName); $fileName = array_pop($tmpfileLoc); $fileName = str_replace('.json', '.js', $fileName); @@ -197,12 +160,9 @@ class JSCombiner { } /** - * @param string $root - * @param string $file * @return string[] */ - public function getContent($root, $file) { - /** @var array $data */ + public function getContent(string $root, string $file): array { $data = json_decode(file_get_contents($root . '/' . $file)); if (!is_array($data)) { return []; @@ -226,7 +186,7 @@ class JSCombiner { * * @throws NotFoundException */ - public function resetCache() { + public function resetCache(): void { $this->cacheFactory->createDistributed('JS-')->clear(); $appDirectory = $this->appData->getDirectoryListing(); foreach ($appDirectory as $folder) { diff --git a/lib/private/Template/ResourceLocator.php b/lib/private/Template/ResourceLocator.php index 59e7b2ccbfb..d954c2cff2e 100644 --- a/lib/private/Template/ResourceLocator.php +++ b/lib/private/Template/ResourceLocator.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace OC\Template; +use OCP\IConfig; use Psr\Log\LoggerInterface; abstract class ResourceLocator { @@ -23,7 +24,7 @@ abstract class ResourceLocator { public function __construct( protected LoggerInterface $logger, - \OCP\IConfig $config, + IConfig $config, ) { $this->mapping = [ \OC::$SERVERROOT => \OC::$WEBROOT diff --git a/lib/private/Template/ResourceNotFoundException.php b/lib/private/Template/ResourceNotFoundException.php index e51dfb5cb89..ca6756c1e7d 100644 --- a/lib/private/Template/ResourceNotFoundException.php +++ b/lib/private/Template/ResourceNotFoundException.php @@ -1,30 +1,24 @@ resource = $resource; - $this->webPath = $webPath; } - /** - * @return string - */ - public function getResourcePath() { + public function getResourcePath(): string { return $this->webPath . '/' . $this->resource; } } diff --git a/lib/private/Template/TemplateManager.php b/lib/private/Template/TemplateManager.php index 34da4deac72..0f0af51798a 100644 --- a/lib/private/Template/TemplateManager.php +++ b/lib/private/Template/TemplateManager.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace OC\Template; +use OC\SystemConfig; use OCP\App\IAppManager; use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; use OCP\AppFramework\Http\TemplateResponse; @@ -79,7 +80,7 @@ class TemplateManager implements ITemplateManager { $this->eventDispatcher->dispatchTyped($event); print($response->render()); } catch (\Throwable $e1) { - $logger = \OCP\Server::get(LoggerInterface::class); + $logger = Server::get(LoggerInterface::class); $logger->error('Rendering themed error page failed. Falling back to un-themed error page.', [ 'app' => 'core', 'exception' => $e1, @@ -112,8 +113,8 @@ class TemplateManager implements ITemplateManager { $debug = false; http_response_code($statusCode); try { - $debug = (bool)Server::get(\OC\SystemConfig::class)->getValue('debug', false); - $serverLogsDocumentation = Server::get(\OC\SystemConfig::class)->getValue('documentation_url.server_logs', ''); + $debug = (bool)Server::get(SystemConfig::class)->getValue('debug', false); + $serverLogsDocumentation = Server::get(SystemConfig::class)->getValue('documentation_url.server_logs', ''); $request = Server::get(IRequest::class); $content = $this->getTemplate('', 'exception', 'error', false); $content->assign('errorClass', get_class($exception)); diff --git a/tests/lib/Template/ResourceLocatorTest.php b/tests/lib/Template/ResourceLocatorTest.php index 599c8391ade..e1e783a38bd 100644 --- a/tests/lib/Template/ResourceLocatorTest.php +++ b/tests/lib/Template/ResourceLocatorTest.php @@ -9,6 +9,7 @@ namespace Test\Template; use OC\SystemConfig; +use OC\Template\ResourceLocator; use OC\Template\ResourceNotFoundException; use Psr\Log\LoggerInterface; @@ -46,7 +47,7 @@ class ResourceLocatorTest extends \Test\TestCase { $locator->expects($this->once()) ->method('doFindTheme') ->with('foo'); - /** @var \OC\Template\ResourceLocator $locator */ + /** @var ResourceLocator $locator */ $locator->find(['foo']); } @@ -69,13 +70,13 @@ class ResourceLocatorTest extends \Test\TestCase { $this->logger->expects($this->exactly(2)) ->method('debug') ->with($this->stringContains('map/foo')); - /** @var \OC\Template\ResourceLocator $locator */ + /** @var ResourceLocator $locator */ $locator->find(['foo']); } public function testAppendIfExist(): void { $locator = $this->getResourceLocator('theme'); - /** @var \OC\Template\ResourceLocator $locator */ + /** @var ResourceLocator $locator */ $method = new \ReflectionMethod($locator, 'appendIfExist'); $method->setAccessible(true);