From d16b0f2d20c3c0d42ad67d7345371dcc2ef589a0 Mon Sep 17 00:00:00 2001 From: Michel Roux Date: Sat, 17 Jun 2023 00:07:40 +0200 Subject: [PATCH] fully psalm lvl2 compliant --- composer.json | 1 + composer.lock | 316 +++++++++++++++++++++++++- lib/AppInfo/Application.php | 23 +- lib/Controller/SettingsController.php | 40 +++- lib/Hooks.php | 31 +-- lib/Settings/Personal.php | 13 +- lib/Settings/PersonalSection.php | 5 +- lib/config.php | 63 ----- psalm.xml | 5 - 9 files changed, 382 insertions(+), 115 deletions(-) delete mode 100644 lib/config.php diff --git a/composer.json b/composer.json index f0f7091..03eaf88 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,7 @@ "type": "project", "license": "AGPL-3.0-or-later", "require-dev": { + "symfony/event-dispatcher": "^5.4.22", "nextcloud/ocp": "^27.0.0", "psalm/phar": "^5.12.0", "nextcloud/coding-standard": "^1.1.1" diff --git a/composer.lock b/composer.lock index eaf77a9..5fa5a64 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5c7be477b217d6074abc6c2f8d2c2256", + "content-hash": "72e9bc76d92d0e26e0b3af7ed7ce6a68", "packages": [], "packages-dev": [ { @@ -379,6 +379,320 @@ "source": "https://github.com/php-fig/log/tree/1.1.4" }, "time": "2021-05-03T11:20:27+00:00" + }, + { + "name": "symfony/deprecation-contracts", + "version": "v2.5.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/deprecation-contracts.git", + "reference": "e8b495ea28c1d97b5e0c121748d6f9b53d075c66" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/deprecation-contracts/zipball/e8b495ea28c1d97b5e0c121748d6f9b53d075c66", + "reference": "e8b495ea28c1d97b5e0c121748d6f9b53d075c66", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.5-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "files": [ + "function.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "A generic function and convention to trigger deprecation notices", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/deprecation-contracts/tree/v2.5.2" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-01-02T09:53:40+00:00" + }, + { + "name": "symfony/event-dispatcher", + "version": "v5.4.22", + "source": { + "type": "git", + "url": "https://github.com/symfony/event-dispatcher.git", + "reference": "1df20e45d56da29a4b1d8259dd6e950acbf1b13f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/event-dispatcher/zipball/1df20e45d56da29a4b1d8259dd6e950acbf1b13f", + "reference": "1df20e45d56da29a4b1d8259dd6e950acbf1b13f", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/deprecation-contracts": "^2.1|^3", + "symfony/event-dispatcher-contracts": "^2|^3", + "symfony/polyfill-php80": "^1.16" + }, + "conflict": { + "symfony/dependency-injection": "<4.4" + }, + "provide": { + "psr/event-dispatcher-implementation": "1.0", + "symfony/event-dispatcher-implementation": "2.0" + }, + "require-dev": { + "psr/log": "^1|^2|^3", + "symfony/config": "^4.4|^5.0|^6.0", + "symfony/dependency-injection": "^4.4|^5.0|^6.0", + "symfony/error-handler": "^4.4|^5.0|^6.0", + "symfony/expression-language": "^4.4|^5.0|^6.0", + "symfony/http-foundation": "^4.4|^5.0|^6.0", + "symfony/service-contracts": "^1.1|^2|^3", + "symfony/stopwatch": "^4.4|^5.0|^6.0" + }, + "suggest": { + "symfony/dependency-injection": "", + "symfony/http-kernel": "" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\EventDispatcher\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides tools that allow your application components to communicate with each other by dispatching events and listening to them", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/event-dispatcher/tree/v5.4.22" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2023-03-17T11:31:58+00:00" + }, + { + "name": "symfony/event-dispatcher-contracts", + "version": "v2.5.2", + "source": { + "type": "git", + "url": "https://github.com/symfony/event-dispatcher-contracts.git", + "reference": "f98b54df6ad059855739db6fcbc2d36995283fe1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/event-dispatcher-contracts/zipball/f98b54df6ad059855739db6fcbc2d36995283fe1", + "reference": "f98b54df6ad059855739db6fcbc2d36995283fe1", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "psr/event-dispatcher": "^1" + }, + "suggest": { + "symfony/event-dispatcher-implementation": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.5-dev" + }, + "thanks": { + "name": "symfony/contracts", + "url": "https://github.com/symfony/contracts" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Contracts\\EventDispatcher\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Generic abstractions related to dispatching event", + "homepage": "https://symfony.com", + "keywords": [ + "abstractions", + "contracts", + "decoupling", + "interfaces", + "interoperability", + "standards" + ], + "support": { + "source": "https://github.com/symfony/event-dispatcher-contracts/tree/v2.5.2" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-01-02T09:53:40+00:00" + }, + { + "name": "symfony/polyfill-php80", + "version": "v1.27.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php80.git", + "reference": "7a6ff3f1959bb01aefccb463a0f2cd3d3d2fd936" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php80/zipball/7a6ff3f1959bb01aefccb463a0f2cd3d3d2fd936", + "reference": "7a6ff3f1959bb01aefccb463a0f2cd3d3d2fd936", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.27-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Php80\\": "" + }, + "classmap": [ + "Resources/stubs" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ion Bazan", + "email": "ion.bazan@gmail.com" + }, + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-php80/tree/v1.27.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-11-03T14:55:06+00:00" } ], "aliases": [], diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 913555c..1c0d0c2 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -14,17 +14,32 @@ namespace OCA\Epubreader\AppInfo; use OCA\Epubreader\Hooks; use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Util; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; -class Application extends App { +class Application extends App implements IBootstrap { public const APP_ID = 'epubreader'; public function __construct() { parent::__construct(self::APP_ID); - $l = \OC::$server->getL10N('epubreader'); - Hooks::register(); - Util::addscript('epubreader', 'plugin'); + Util::addscript(self::APP_ID, 'plugin'); + } + + public function boot(IBootContext $context): void { + /** @psalm-suppress DeprecatedMethod */ + Util::connectHook('\OCP\Config', 'js', 'OCA\Epubreader\Hooks', 'announce_settings'); + + $context->injectFn(function (EventDispatcherInterface $dispatcher) { + $dispatcher->addListener('OC\Files::preDelete', [Hooks::class, 'deleteFile']); + $dispatcher->addListener('OC\User::preDelete', [Hooks::class, 'deleteUser']); + }); + } + + public function register(IRegistrationContext $context): void { } } diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index d0759e0..a3c8421 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -12,30 +12,48 @@ namespace OCA\Epubreader\Controller; -use OCA\Epubreader\Config; +use OCA\Epubreader\AppInfo\Application; use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IRequest; class SettingsController extends Controller { + private string $userId; + private IL10N $l10n; + private IConfig $configManager; + + public function __construct( + string $appName, + IRequest $request, + string $userId, + IL10N $l10n, + IConfig $configManager + ) { + parent::__construct($appName, $request); + $this->userId = $userId; + $this->l10n = $l10n; + $this->configManager = $configManager; + } + /** * @brief set preference for file type association * * @NoAdminRequired * - * @param int $EpubEnable - * @param int $PdfEnable - * @param int $CbxEnable + * @param string $EpubEnable + * @param string $PdfEnable + * @param string $CbxEnable */ - public function setPreference(int $EpubEnable, int $PdfEnable, int $CbxEnable): JSONResponse { - $l = \OC::$server->getL10N('epubreader'); - - Config::set('epub_enable', $EpubEnable); - Config::set('pdf_enable', $PdfEnable); - Config::set('cbx_enable', $CbxEnable); + public function setPreference(string $EpubEnable, string $PdfEnable, string $CbxEnable): JSONResponse { + $this->configManager->setUserValue($this->userId, Application::APP_ID, 'epub_enable', $EpubEnable); + $this->configManager->setUserValue($this->userId, Application::APP_ID, 'pdf_enable', $PdfEnable); + $this->configManager->setUserValue($this->userId, Application::APP_ID, 'cbx_enable', $CbxEnable); $response = [ - 'data' => ['message' => $l->t('Settings updated successfully.')], + 'data' => ['message' => $this->l10n->t('Settings updated successfully.')], 'status' => 'success' ]; diff --git a/lib/Hooks.php b/lib/Hooks.php index 98bce2f..71d6f64 100644 --- a/lib/Hooks.php +++ b/lib/Hooks.php @@ -10,32 +10,17 @@ namespace OCA\Epubreader; -use \OC\User\User as User; -use OCP\Files\Node; +use OCA\Epubreader\AppInfo\Application; +use OCP\IConfig; use OCP\IDBConnection; -use OCP\Util; +use OCP\IUser; +use OCP\Server; class Hooks { - public static function register(): void { - /** @psalm-suppress DeprecatedMethod */ - Util::connectHook('\OCP\Config', 'js', 'OCA\Epubreader\Hooks', 'announce_settings'); - - \OC::$server->getRootFolder()->listen('\OC\Files', 'preDelete', function (Node $node) { - $fileId = $node->getId(); - $connection = \OC::$server->getDatabaseConnection(); - self::deleteFile($connection, $fileId); - }); - \OC::$server->getUserManager()->listen('\OC\User', 'preDelete', function (User $user) { - $userId = $user->getUID(); - $connection = \OC::$server->getDatabaseConnection(); - self::deleteUser($connection, $userId); - }); - } - public static function announce_settings(array $settings): void { // Nextcloud encodes this as JSON, Owncloud does not (yet) (#75) - // TODO: rmeove this when Owncloud starts encoding oc_appconfig as JSON just like it already encodes most other properties + // TODO: remove this when Owncloud starts encoding oc_appconfig as JSON just like it already encodes most other properties if (array_key_exists('array', $settings) && is_array($settings['array']) && array_key_exists('oc_appconfig', $settings['array']) @@ -44,9 +29,9 @@ class Hooks { /** @var array $array */ $array = ($isJson) ? json_decode((string) $settings['array']['oc_appconfig'], true) : $settings['array']['oc_appconfig']; $array['filesReader'] = [ - 'enableEpub' => Config::get('epub_enable', 'true'), - 'enablePdf' => Config::get('pdf_enable', 'true'), - 'enableCbx' => Config::get('cbx_enable', 'true'), + 'enableEpub' => Server::get(IConfig::class)->getUserValue(Server::get(IUser::class)->getUID(), Application::APP_ID, 'epub_enable', 'true'), + 'enablePdf' => Server::get(IConfig::class)->getUserValue(Server::get(IUser::class)->getUID(), Application::APP_ID, 'pdf_enable', 'true'), + 'enableCbx' => Server::get(IConfig::class)->getUserValue(Server::get(IUser::class)->getUID(), Application::APP_ID, 'cbx_enable', 'true'), ]; $settings['array']['oc_appconfig'] = ($isJson) ? json_encode($array) : $array; } diff --git a/lib/Settings/Personal.php b/lib/Settings/Personal.php index 0a582b3..5220547 100644 --- a/lib/Settings/Personal.php +++ b/lib/Settings/Personal.php @@ -11,6 +11,7 @@ namespace OCA\Epubreader\Settings; +use OCA\Epubreader\AppInfo\Application; use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; use OCP\Settings\ISettings; @@ -34,12 +35,12 @@ class Personal implements ISettings { */ public function getForm(): TemplateResponse { $parameters = [ - 'EpubEnable' => $this->configManager->getUserValue($this->userId, 'epubreader', 'epub_enable'), - 'PdfEnable' => $this->configManager->getUserValue($this->userId, 'epubreader', 'pdf_enable'), - 'CbxEnable' => $this->configManager->getUserValue($this->userId, 'epubreader', 'cbx_enable'), + 'EpubEnable' => $this->configManager->getUserValue($this->userId, Application::APP_ID, 'epub_enable'), + 'PdfEnable' => $this->configManager->getUserValue($this->userId, Application::APP_ID, 'pdf_enable'), + 'CbxEnable' => $this->configManager->getUserValue($this->userId, Application::APP_ID, 'cbx_enable'), ]; - return new TemplateResponse('epubreader', 'settings-personal', $parameters, ''); + return new TemplateResponse(Application::APP_ID, 'settings-personal', $parameters, ''); } /** @@ -56,7 +57,7 @@ class Personal implements ISettings { * @since 9.1 */ public function getSection(): string { - return 'epubreader'; + return Application::APP_ID; } /** @@ -65,7 +66,7 @@ class Personal implements ISettings { * @return string */ public function getSectionID(): string { - return 'epubreader'; + return Application::APP_ID; } /** diff --git a/lib/Settings/PersonalSection.php b/lib/Settings/PersonalSection.php index 014c3b9..7cbc202 100644 --- a/lib/Settings/PersonalSection.php +++ b/lib/Settings/PersonalSection.php @@ -11,6 +11,7 @@ namespace OCA\Epubreader\Settings; +use OCA\Epubreader\AppInfo\Application; use OCP\IL10N; use OCP\IURLGenerator; use OCP\Settings\IIconSection; @@ -31,7 +32,7 @@ class PersonalSection implements IIconSection { * @return string */ public function getIcon(): string { - return $this->urlGenerator->imagePath('epubreader', 'app.svg'); + return $this->urlGenerator->imagePath(Application::APP_ID, 'app.svg'); } /** @@ -40,7 +41,7 @@ class PersonalSection implements IIconSection { * @return string */ public function getID(): string { - return 'epubreader'; + return Application::APP_ID; } /** diff --git a/lib/config.php b/lib/config.php deleted file mode 100644 index 5d88bce..0000000 --- a/lib/config.php +++ /dev/null @@ -1,63 +0,0 @@ -getConfig()->getUserValue(\OC_User::getUser(), 'epubreader', $key, $default); - } - - /** - * @brief set user config value - * - * @param string $key key for value to change - * @param string $value value to use - * @return bool success - */ - public static function set(string $key, string $value): bool { - return \OC::$server->getConfig()->setUserValue(\OC_User::getUser(), 'epubreader', $key, $value); - } - - /** - * @brief get app config value - * - * @param string $key value to retrieve - * @param string $default default value to use - * @return string retrieved value or default - */ - public static function getApp(string $key, string $default): string { - return \OC::$server->getConfig()->getAppValue('epubreader', $key, $default); - } - - /** - * @brief set app config value - * - * @param string $key key for value to change - * @param string $value value to use - * @return bool success - */ - public static function setApp(string $key, string $value): bool { - return \OC::$server->getConfig()->setAppValue('epubreader', $key, $value); - } -} diff --git a/psalm.xml b/psalm.xml index fef7a59..50fd490 100644 --- a/psalm.xml +++ b/psalm.xml @@ -29,11 +29,6 @@ - - - - -