diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index f7bbdd8..99972ee 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -6,7 +6,21 @@ require_once './vendor/autoload.php'; use Nextcloud\CodingStandard\Config; -$config = new Config(); +class MyConfig extends Config +{ + public function getRules(): array + { + $rules = parent::getRules(); + $rules['@PhpCsFixer'] = true; + $rules['curly_braces_position']['classes_opening_brace'] = 'next_line_unless_newline_at_signature_end'; + $rules['phpdoc_separation'] = false; + $rules['phpdoc_to_comment'] = false; + $rules['single_line_comment_style'] = false; + return $rules; + } +} + +$config = new MyConfig(); $config ->getFinder() ->ignoreVCSIgnored(true) diff --git a/appinfo/routes.php b/appinfo/routes.php index 27cdf81..97580b5 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -1,7 +1,7 @@ getContainer(); + /** @var IRootFolder $rootFolder */ $rootFolder = $container->get(IRootFolder::class); + /** @var IDBConnection $dbConnection */ $dbConnection = $container->get(IDBConnection::class); $hooks = new Hooks($rootFolder, $dbConnection); diff --git a/lib/Controller/BookmarkController.php b/lib/Controller/BookmarkController.php index c608f10..64151bd 100644 --- a/lib/Controller/BookmarkController.php +++ b/lib/Controller/BookmarkController.php @@ -15,15 +15,10 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; -class BookmarkController extends Controller { - +class BookmarkController extends Controller +{ private BookmarkService $bookmarkService; - /** - * @param string $AppName - * @param IRequest $request - * @param BookmarkService $bookmarkService - */ public function __construct( string $AppName, IRequest $request, @@ -38,12 +33,9 @@ class BookmarkController extends Controller { * * @NoAdminRequired * @NoCSRFRequired - * - * @param int $fileId - * @param ?string $name - * @param ?string $type */ - public function get(int $fileId, ?string $name = null, ?string $type = null): JSONResponse { + public function get(int $fileId, ?string $name = null, ?string $type = null): JSONResponse + { return new JSONResponse($this->bookmarkService->get($fileId, $name, $type)); } @@ -52,14 +44,9 @@ class BookmarkController extends Controller { * * @NoAdminRequired * @NoCSRFRequired - * - * @param int $fileId - * @param string $name - * @param string $value - * @param ?string $type - * @param ?string $content */ - public function set(int $fileId, string $name, string $value, ?string $type = null, ?string $content = null): JSONResponse { + public function set(int $fileId, string $name, string $value, ?string $type = null, ?string $content = null): JSONResponse + { return new JSONResponse($this->bookmarkService->set($fileId, $name, $value, $type, $content)); } @@ -68,10 +55,9 @@ class BookmarkController extends Controller { * * @NoAdminRequired * @NoCSRFRequired - * - * @param int $fileId */ - public function getCursor(int $fileId): JSONResponse { + public function getCursor(int $fileId): JSONResponse + { return new JSONResponse($this->bookmarkService->getCursor($fileId)); } @@ -80,11 +66,9 @@ class BookmarkController extends Controller { * * @NoAdminRequired * @NoCSRFRequired - * - * @param int $fileId - * @param string $value */ - public function setCursor(int $fileId, string $value): JSONResponse { + public function setCursor(int $fileId, string $value): JSONResponse + { return new JSONResponse($this->bookmarkService->setCursor($fileId, $value)); } @@ -93,11 +77,9 @@ class BookmarkController extends Controller { * * @NoAdminRequired * @NoCSRFRequired - * - * @param int $fileId - * @param string name */ - public function delete(int $fileId, string $name): void { + public function delete(int $fileId, string $name): void + { $this->bookmarkService->delete($fileId, $name); } @@ -106,10 +88,9 @@ class BookmarkController extends Controller { * * @NoAdminRequired * @NoCSRFRequired - * - * @param int $fileId */ - public function deleteCursor(int $fileId): void { + public function deleteCursor(int $fileId): void + { $this->bookmarkService->deleteCursor($fileId); } } diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php index 266a1a4..fe41468 100644 --- a/lib/Controller/PageController.php +++ b/lib/Controller/PageController.php @@ -24,8 +24,8 @@ use OCP\IRequest; use OCP\IURLGenerator; use OCP\Share\IManager; -class PageController extends Controller { - +class PageController extends Controller +{ private IURLGenerator $urlGenerator; private IRootFolder $rootFolder; private IManager $shareManager; @@ -33,16 +33,6 @@ class PageController extends Controller { private BookmarkService $bookmarkService; private PreferenceService $preferenceService; - /** - * @param string $AppName - * @param IRequest $request - * @param IURLGenerator $urlGenerator - * @param IRootFolder $rootFolder - * @param IManager $shareManager - * @param string $UserId - * @param BookmarkService $bookmarkService - * @param PreferenceService $preferenceService - */ public function __construct( string $AppName, IRequest $request, @@ -66,21 +56,17 @@ class PageController extends Controller { * @PublicPage * @NoCSRFRequired */ - public function showReader(): TemplateResponse { + public function showReader(): TemplateResponse + { $templates = [ 'application/epub+zip' => 'epubreader', 'application/x-cbr' => 'cbreader', - 'application/pdf' => 'pdfreader' + 'application/pdf' => 'pdfreader', ]; - /** - * @var array{ - * fileId: int, - * fileName: string, - * fileType: string - * } $fileInfo - */ $fileInfo = $this->getFileInfo((string) $this->request->getParam('file')); + + /** @var int $fileId */ $fileId = $fileInfo['fileId']; $type = (string) $this->request->getParam('type'); $scope = $template = $templates[$type]; @@ -96,7 +82,7 @@ class PageController extends Controller { 'defaults' => $this->toJson($this->preferenceService->getDefault($scope)), 'preferences' => $this->toJson($this->preferenceService->get($scope, $fileId)), 'metadata' => $this->toJson([]), - 'annotations' => $this->toJson($this->bookmarkService->get($fileId)) + 'annotations' => $this->toJson($this->bookmarkService->get($fileId)), ]; $policy = new ContentSecurityPolicy(); @@ -123,19 +109,20 @@ class PageController extends Controller { * * @param string $path path-fragment from url * - * @throws NotFoundException|InvalidPathException + * @throws InvalidPathException|NotFoundException */ - private function getFileInfo(string $path): array { + private function getFileInfo(string $path): array + { $count = 0; - $shareToken = preg_replace("/(?:\/index\.php)?\/s\/([A-Za-z0-9]{15,32})\/download.*/", "$1", $path, 1, $count); + $shareToken = preg_replace('/(?:\\/index\\.php)?\\/s\\/([A-Za-z0-9]{15,32})\\/download.*/', '$1', $path, 1, $count); - if ($count === 1) { + if (1 === $count) { /* shared file or directory */ $node = $this->shareManager->getShareByToken($shareToken)->getNode(); $type = $node->getType(); /* shared directory, need file path to continue, */ - if ($type == FileInfo::TYPE_FOLDER && $node instanceof Folder) { + if (FileInfo::TYPE_FOLDER == $type && $node instanceof Folder) { $query = []; parse_str(parse_url($path, PHP_URL_QUERY), $query); if (isset($query['path']) && is_string($query['path'])) { @@ -151,23 +138,22 @@ class PageController extends Controller { } else { $filePath = $path; $fileId = $this->rootFolder->getUserFolder($this->userId) - ->get(preg_replace("/.*\/remote.php\/webdav(.*)/", "$1", rawurldecode((string) $this->request->getParam('file')))) - ->getId(); + ->get(preg_replace('/.*\\/remote.php\\/webdav(.*)/', '$1', rawurldecode((string) $this->request->getParam('file')))) + ->getId() + ; } - $pathInfo = pathInfo($filePath); - if (!is_array($pathInfo)) { - throw new InvalidPathException("Can not get info for $filePath"); - } + $pathInfo = pathinfo($filePath); return [ 'fileName' => $pathInfo['filename'], 'fileType' => strtolower($pathInfo['extension'] ?? ''), - 'fileId' => $fileId + 'fileId' => $fileId, ]; } - private function toJson(array $value): string { + private function toJson(array $value): string + { return htmlspecialchars(json_encode($value), ENT_QUOTES, 'UTF-8'); } } diff --git a/lib/Controller/PreferenceController.php b/lib/Controller/PreferenceController.php index 2da664d..e48024c 100644 --- a/lib/Controller/PreferenceController.php +++ b/lib/Controller/PreferenceController.php @@ -15,15 +15,10 @@ use OCP\AppFramework\Controller; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; -class PreferenceController extends Controller { - +class PreferenceController extends Controller +{ private PreferenceService $preferenceService; - /** - * @param string $AppName - * @param IRequest $request - * @param PreferenceService $preferenceService - */ public function __construct( string $AppName, IRequest $request, @@ -39,11 +34,10 @@ class PreferenceController extends Controller { * @NoAdminRequired * @NoCSRFRequired * - * @param string $scope - * @param int $fileId * @param ?string $name if null, return all preferences for $scope + $fileId */ - public function get(string $scope, int $fileId, ?string $name = null): JSONResponse { + public function get(string $scope, int $fileId, ?string $name = null): JSONResponse + { return new JSONResponse($this->preferenceService->get($scope, $fileId, $name)); } @@ -52,27 +46,22 @@ class PreferenceController extends Controller { * * @NoAdminRequired * @NoCSRFRequired - * - * @param string $scope - * @param int $fileId - * @param string $name - * @param string $value */ - public function set(string $scope, int $fileId, string $name, string $value): JSONResponse { + public function set(string $scope, int $fileId, string $name, string $value): JSONResponse + { return new JSONResponse($this->preferenceService->set($scope, $fileId, $name, $value)); } - /** * @brief return default preference * * @NoAdminRequired * @NoCSRFRequired * - * @param string $scope * @param string $name if null, return all default preferences for scope */ - public function getDefault(string $scope, string $name): JSONResponse { + public function getDefault(string $scope, string $name): JSONResponse + { return new JSONResponse($this->preferenceService->getDefault($scope, $name)); } @@ -81,33 +70,25 @@ class PreferenceController extends Controller { * * @NoAdminRequired * @NoCSRFRequired - * - * @param string $scope - * @param string $name - * @param string $value */ - public function setDefault(string $scope, string $name, string $value): JSONResponse { + public function setDefault(string $scope, string $name, string $value): JSONResponse + { return new JSONResponse($this->preferenceService->setDefault($scope, $name, $value)); } /** * @brief delete preference - * - * @param string $scope - * @param int $fileId - * @param string $name */ - public function delete(string $scope, int $fileId, string $name): void { + public function delete(string $scope, int $fileId, string $name): void + { $this->preferenceService->delete($scope, $fileId, $name); } /** * @brief delete default preference - * - * @param string $scope - * @param string $name */ - public function deleteDefault(string $scope, string $name): void { + public function deleteDefault(string $scope, string $name): void + { $this->preferenceService->deleteDefault($scope, $name); } } diff --git a/lib/Controller/SettingsController.php b/lib/Controller/SettingsController.php index a3c8421..81afe36 100644 --- a/lib/Controller/SettingsController.php +++ b/lib/Controller/SettingsController.php @@ -1,7 +1,7 @@ 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' => $this->l10n->t('Settings updated successfully.')], - 'status' => 'success' + 'status' => 'success', ]; return new JSONResponse($response); diff --git a/lib/Db/Bookmark.php b/lib/Db/Bookmark.php index 76ac1d0..1ed4031 100644 --- a/lib/Db/Bookmark.php +++ b/lib/Db/Bookmark.php @@ -10,8 +10,8 @@ namespace OCA\Epubreader\Db; -class Bookmark extends ReaderEntity implements \JsonSerializable { - +class Bookmark extends ReaderEntity implements \JsonSerializable +{ protected string $userId; // user protected int $fileId; // book (identified by fileId) for which this mark is valid protected string $type; // type, defaults to "bookmark" @@ -19,7 +19,8 @@ class Bookmark extends ReaderEntity implements \JsonSerializable { protected string $value; // bookmark value (format-specific, eg. page number for PDF, CFI for epub, etc) protected string $content; // bookmark content (annotations etc), can be empty - public function jsonSerialize(): array { + public function jsonSerialize(): array + { return [ 'id' => $this->getId(), 'userId' => $this->getUserId(), @@ -28,11 +29,12 @@ class Bookmark extends ReaderEntity implements \JsonSerializable { 'name' => $this->getName(), 'value' => $this->conditional_json_decode($this->getValue()), 'content' => $this->conditional_json_decode($this->getContent()), - 'lastModified' => $this->getLastModified() + 'lastModified' => $this->getLastModified(), ]; } - public function toService(): array { + public function toService(): array + { return [ 'name' => $this->getName(), 'type' => $this->getType(), @@ -42,56 +44,68 @@ class Bookmark extends ReaderEntity implements \JsonSerializable { ]; } - public function getUserId(): string { + public function getUserId(): string + { return $this->userId; } - public function setUserId(string $userId): void { + public function setUserId(string $userId): void + { $this->userId = $userId; $this->markFieldUpdated('userId'); } - public function getFileId(): int { + public function getFileId(): int + { return $this->fileId; } - public function setFileId(int $fileId): void { + public function setFileId(int $fileId): void + { $this->fileId = $fileId; $this->markFieldUpdated('fileId'); } - public function getType(): string { + public function getType(): string + { return $this->type; } - public function setType(string $type): void { + public function setType(string $type): void + { $this->type = $type; $this->markFieldUpdated('type'); } - public function getName(): string { + public function getName(): string + { return $this->name; } - public function setName(string $name): void { + public function setName(string $name): void + { $this->name = $name; $this->markFieldUpdated('name'); } - public function getValue(): string { + public function getValue(): string + { return $this->value; } - public function setValue(string $value): void { + public function setValue(string $value): void + { $this->value = $value; $this->markFieldUpdated('value'); } - public function getContent(): string { + public function getContent(): string + { return $this->content; } - public function setContent(string $content): void { + public function setContent(string $content): void + { $this->content = $content; $this->markFieldUpdated('content'); } diff --git a/lib/Db/BookmarkMapper.php b/lib/Db/BookmarkMapper.php index 4f76308..2359cf4 100644 --- a/lib/Db/BookmarkMapper.php +++ b/lib/Db/BookmarkMapper.php @@ -13,40 +13,35 @@ namespace OCA\Epubreader\Db; use OCA\Epubreader\Utility\Time; use OCP\IDBConnection; -class BookmarkMapper extends ReaderMapper { - +class BookmarkMapper extends ReaderMapper +{ private string $userId; - /** - * @param IDbConnection $db - * @param string $UserId - * @param Time $time - */ - public function __construct(IDBConnection $db, string $UserId, Time $time) { + public function __construct(IDBConnection $db, string $UserId, Time $time) + { parent::__construct($db, 'reader_bookmarks', Bookmark::class, $time); $this->userId = $UserId; } /** * @brief get bookmarks for $fileId+$userId(+$name) - * @param int $fileId - * @param ?string $name - * @param ?string $type * * @return ReaderEntity[] */ - public function get(int $fileId, ?string $name = null, ?string $type = null): array { + public function get(int $fileId, ?string $name = null, ?string $type = null): array + { $query = $this->db->getQueryBuilder(); $query->select('*') ->from($this->getTableName()) ->where($query->expr()->eq('file_id', $query->createNamedParameter($fileId))) - ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($this->userId))); + ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($this->userId))) + ; - if ($type !== null) { + if (null !== $type) { $query->andWhere($query->expr()->eq('type', $query->createNamedParameter($type))); } - if ($name !== null) { + if (null !== $name) { $query->andWhere($query->expr()->eq('name', $query->createNamedParameter($name))); } @@ -56,18 +51,13 @@ class BookmarkMapper extends ReaderMapper { /** * @brief write bookmark to database * - * @param int $fileId - * @param string $name - * @param string $value - * @param ?string $type - * @param ?string $content - * * @return ReaderEntity the newly created or updated bookmark */ - public function set(int $fileId, string $name, string $value, ?string $type = null, ?string $content = null): ReaderEntity { + public function set(int $fileId, string $name, string $value, ?string $type = null, ?string $content = null): ReaderEntity + { $result = $this->get($fileId, $name); - if(empty($result)) { + if (empty($result)) { // anonymous bookmarks are named after their contents if (empty($name)) { $name = $value; @@ -75,7 +65,7 @@ class BookmarkMapper extends ReaderMapper { // default type is "bookmark" if (null === $type) { - $type = "bookmark"; + $type = 'bookmark'; } $bookmark = new Bookmark(); diff --git a/lib/Db/Preference.php b/lib/Db/Preference.php index 0f11de3..65309bc 100644 --- a/lib/Db/Preference.php +++ b/lib/Db/Preference.php @@ -10,15 +10,16 @@ namespace OCA\Epubreader\Db; -class Preference extends ReaderEntity implements \JsonSerializable { - +class Preference extends ReaderEntity implements \JsonSerializable +{ protected string $userId; // user for whom this preference is valid protected string $scope; // scope (default or specific renderer) protected int $fileId; // file for which this preference is set protected string $name; // preference name protected string $value; // preference value - public function jsonSerialize(): array { + public function jsonSerialize(): array + { return [ 'id' => $this->getId(), 'scope' => $this->getScope(), @@ -29,54 +30,65 @@ class Preference extends ReaderEntity implements \JsonSerializable { ]; } - public function toService(): array { + public function toService(): array + { return [ 'name' => $this->getName(), 'value' => $this->conditional_json_decode($this->getValue()), ]; } - public function getUserId(): string { + public function getUserId(): string + { return $this->userId; } - public function setUserId(string $userId): void { + public function setUserId(string $userId): void + { $this->userId = $userId; $this->markFieldUpdated('userId'); } - public function getScope(): string { + public function getScope(): string + { return $this->scope; } - public function setScope(string $scope): void { + public function setScope(string $scope): void + { $this->scope = $scope; $this->markFieldUpdated('scope'); } - public function getFileId(): int { + public function getFileId(): int + { return $this->fileId; } - public function setFileId(int $fileId): void { + public function setFileId(int $fileId): void + { $this->fileId = $fileId; $this->markFieldUpdated('fileId'); } - public function getName(): string { + public function getName(): string + { return $this->name; } - public function setName(string $name): void { + public function setName(string $name): void + { $this->name = $name; $this->markFieldUpdated('name'); } - public function getValue(): string { + public function getValue(): string + { return $this->value; } - public function setValue(string $value): void { + public function setValue(string $value): void + { $this->value = $value; $this->markFieldUpdated('value'); } diff --git a/lib/Db/PreferenceMapper.php b/lib/Db/PreferenceMapper.php index 07e8abe..dc5e0b0 100644 --- a/lib/Db/PreferenceMapper.php +++ b/lib/Db/PreferenceMapper.php @@ -13,11 +13,12 @@ namespace OCA\Epubreader\Db; use OCA\Epubreader\Utility\Time; use OCP\IDBConnection; -class PreferenceMapper extends ReaderMapper { - +class PreferenceMapper extends ReaderMapper +{ private string $userId; - public function __construct(IDBConnection $db, string $UserId, Time $time) { + public function __construct(IDBConnection $db, string $UserId, Time $time) + { parent::__construct($db, 'reader_prefs', Preference::class, $time); $this->userId = $UserId; } @@ -25,19 +26,17 @@ class PreferenceMapper extends ReaderMapper { /** * @brief get preferences for $scope+$fileId+$userId(+$name) * - * @param string $scope - * @param int $fileId - * @param ?string $name - * * @return ReaderEntity[] */ - public function get(string $scope, int $fileId, ?string $name = null): array { + public function get(string $scope, int $fileId, ?string $name = null): array + { $query = $this->db->getQueryBuilder(); $query->select('*') ->from($this->getTableName()) ->where($query->expr()->eq('scope', $query->createNamedParameter($scope))) ->andWhere($query->expr()->eq('file_id', $query->createNamedParameter($fileId))) - ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($this->userId))); + ->andWhere($query->expr()->eq('user_id', $query->createNamedParameter($this->userId))) + ; if (!empty($name)) { $query->andWhere($query->expr()->eq('name', $query->createNamedParameter($name))); @@ -49,17 +48,13 @@ class PreferenceMapper extends ReaderMapper { /** * @brief write preference to database * - * @param string $scope - * @param int $fileId - * @param string $name - * @param string $value - * * @return ReaderEntity the newly created or updated preference */ - public function set(string $scope, int $fileId, string $name, string $value): ReaderEntity { + public function set(string $scope, int $fileId, string $name, string $value): ReaderEntity + { $result = $this->get($scope, $fileId, $name); - if(empty($result)) { + if (empty($result)) { $preference = new Preference(); $preference->setScope($scope); $preference->setFileId($fileId); diff --git a/lib/Db/ReaderEntity.php b/lib/Db/ReaderEntity.php index 66eb48a..0b530fe 100644 --- a/lib/Db/ReaderEntity.php +++ b/lib/Db/ReaderEntity.php @@ -15,41 +15,44 @@ use OCP\AppFramework\Db\Entity; /** * @psalm-type SerializedEntity = array */ -abstract class ReaderEntity extends Entity { - +abstract class ReaderEntity extends Entity +{ protected int $lastModified; // modification timestamp /** - * returns decoded json if input is json, otherwise returns input + * returns decoded json if input is json, otherwise returns input. * - * @return string|array + * @return array|string */ - public function conditional_json_decode(string $el): mixed { + public function conditional_json_decode(string $el): mixed + { /** @var array $result */ $result = json_decode($el); - if (json_last_error() === JSON_ERROR_NONE) { + if (JSON_ERROR_NONE === json_last_error()) { return $result; - } else { - return $el; } + + return $el; } - public function getLastModified(): int { + public function getLastModified(): int + { return $this->lastModified; } - public function setLastModified(int $lastModified): void { + public function setLastModified(int $lastModified): void + { $this->lastModified = $lastModified; $this->markFieldUpdated('lastModified'); } /** - * @psalm-return SerializedEntity + * @return SerializedEntity */ abstract public function toService(): array; /** - * @psalm-return SerializedEntity + * @return SerializedEntity */ abstract public function jsonSerialize(): array; } diff --git a/lib/Db/ReaderMapper.php b/lib/Db/ReaderMapper.php index ce6b9be..263e29b 100644 --- a/lib/Db/ReaderMapper.php +++ b/lib/Db/ReaderMapper.php @@ -18,28 +18,32 @@ use OCP\IDBConnection; /** * @template-extends QBMapper */ -abstract class ReaderMapper extends QBMapper { - +abstract class ReaderMapper extends QBMapper +{ private Time $time; /** - * @param IDBConnection $db Instance of the Db abstraction layer - * @param string $table the name of the table. set this to allow entity + * @param IDBConnection $db Instance of the Db abstraction layer + * @param string $table the name of the table. set this to allow entity * @param class-string $entity the name of the entity that the sql should be mapped to queries without using sql - * @param Time $time */ - public function __construct(IDBConnection $db, string $table, string $entity, Time $time) { + public function __construct(IDBConnection $db, string $table, string $entity, Time $time) + { parent::__construct($db, $table, $entity); $this->time = $time; } - public function update(Entity $entity): Entity { + public function update(Entity $entity): Entity + { $entity->setLastModified($this->time->getMicroTime()); + return parent::update($entity); } - public function insert(Entity $entity): Entity { + public function insert(Entity $entity): Entity + { $entity->setLastModified($this->time->getMicroTime()); + return parent::insert($entity); } } diff --git a/lib/Hooks.php b/lib/Hooks.php index 3c99361..8ed29b0 100644 --- a/lib/Hooks.php +++ b/lib/Hooks.php @@ -19,17 +19,19 @@ use OCP\IDBConnection; use OCP\IUser; use OCP\IUserSession; -class Hooks { - +class Hooks +{ private IRootFolder $rootFolder; private IDBConnection $dbConnection; - public function __construct(IRootFolder $rootFolder, IDBConnection $dbConnection) { + public function __construct(IRootFolder $rootFolder, IDBConnection $dbConnection) + { $this->rootFolder = $rootFolder; $this->dbConnection = $dbConnection; } - public function register(): void { + public function register(): void + { $this->rootFolder->listen('\OC\Files', 'preDelete', function (Node $node) { $this->deleteFile($node->getId()); }); @@ -39,15 +41,17 @@ class Hooks { }); } - public static function announce_settings(array $settings): void { + public static function announce_settings(array $settings): void + { // Nextcloud encodes this as JSON, Owncloud does not (yet) (#75) // TODO: remove this when Owncloud starts encoding oc_appconfig as JSON just like it already encodes most other properties $user = Server::get(IUserSession::class)->getUser(); - if ($user && - is_array($settings['array']) && - array_key_exists('oc_appconfig', $settings['array']) + if ($user + && is_array($settings['array']) + && array_key_exists('oc_appconfig', $settings['array']) ) { $isJson = self::isJson($settings['array']['oc_appconfig']); + /** @var array $array */ $array = ($isJson) ? json_decode((string) $settings['array']['oc_appconfig'], true) : $settings['array']['oc_appconfig']; $array['filesReader'] = [ @@ -59,7 +63,8 @@ class Hooks { } } - protected function deleteFile(int $fileId): void { + protected function deleteFile(int $fileId): void + { $queryBuilder = $this->dbConnection->getQueryBuilder(); $queryBuilder->delete('reader_bookmarks')->where('file_id = file_id')->setParameter('file_id', $fileId); $queryBuilder->executeStatement(); @@ -69,7 +74,8 @@ class Hooks { $queryBuilder->executeStatement(); } - protected function deleteUser(string $userId): void { + protected function deleteUser(string $userId): void + { $queryBuilder = $this->dbConnection->getQueryBuilder(); $queryBuilder->delete('reader_bookmarks')->where('user_id = user_id')->setParameter('user_id', $userId); $queryBuilder->executeStatement(); @@ -79,7 +85,8 @@ class Hooks { $queryBuilder->executeStatement(); } - private static function isJson(mixed $string): bool { - return is_string($string) && is_array(json_decode($string, true)) && json_last_error() == JSON_ERROR_NONE; + private static function isJson(mixed $string): bool + { + return is_string($string) && is_array(json_decode($string, true)) && JSON_ERROR_NONE == json_last_error(); } } diff --git a/lib/Migration/Version010404Date20201030180941.php b/lib/Migration/Version010404Date20201030180941.php index 8b56db3..cf98645 100644 --- a/lib/Migration/Version010404Date20201030180941.php +++ b/lib/Migration/Version010404Date20201030180941.php @@ -4,30 +4,27 @@ declare(strict_types=1); namespace OCA\Epubreader\Migration; -use Closure; use OCP\DB\ISchemaWrapper; use OCP\Migration\IOutput; use OCP\Migration\SimpleMigrationStep; /** - * Auto-generated migration step + * Auto-generated migration step. */ -class Version010404Date20201030180941 extends SimpleMigrationStep { - +class Version010404Date20201030180941 extends SimpleMigrationStep +{ /** - * @param IOutput $output - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` - * @param array $options + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` */ - public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void + { } /** - * @param IOutput $output - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` - * @param array $options + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` */ - public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options): ?ISchemaWrapper + { /** @var ISchemaWrapper $schema */ $schema = $schemaClosure(); @@ -129,14 +126,14 @@ class Version010404Date20201030180941 extends SimpleMigrationStep { $table->addIndex(['user_id'], 'reader_prefs_user_id_index'); $table->addIndex(['scope'], 'reader_prefs_scope_index'); } + return $schema; } /** - * @param IOutput $output - * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` - * @param array $options + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` */ - public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options): void + { } } diff --git a/lib/Service/BookmarkService.php b/lib/Service/BookmarkService.php index 860e8eb..aa940bc 100644 --- a/lib/Service/BookmarkService.php +++ b/lib/Service/BookmarkService.php @@ -16,15 +16,16 @@ use OCA\Epubreader\Db\ReaderEntity; /** * @psalm-import-type SerializedEntity from ReaderEntity */ -class BookmarkService extends Service { - +class BookmarkService extends Service +{ // "bookmark" name to use for the cursor (current reading position) private const CURSOR = '__CURSOR__'; private const BOOKMARK_TYPE = 'bookmark'; private BookmarkMapper $bookmarkMapper; - public function __construct(BookmarkMapper $bookmarkMapper) { + public function __construct(BookmarkMapper $bookmarkMapper) + { parent::__construct($bookmarkMapper); $this->bookmarkMapper = $bookmarkMapper; } @@ -34,68 +35,58 @@ class BookmarkService extends Service { * * bookmark type is format-dependent, eg CFI for epub, page number for CBR/CBZ, etc * - * @param int $fileId - * @param ?string $name - * @param ?string $type - * - * @psalm-return SerializedEntity[] + * @return SerializedEntity[] */ - public function get($fileId, ?string $name = null, ?string $type = null): array { + public function get(int $fileId, ?string $name = null, ?string $type = null): array + { $result = $this->bookmarkMapper->get($fileId, $name, $type); + return array_map( function (ReaderEntity $entity): array { return $entity->toService(); - }, $result); + }, + $result + ); } /** * @brief write bookmark * * position type is format-dependent, eg CFI for epub, page number for CBR/CBZ, etc - * - * @param int $fileId - * @param string $name - * @param string $value - * @param ?string $type - * @param ?string $content */ - public function set(int $fileId, string $name, string $value, ?string $type = null, ?string $content = null): ReaderEntity { + public function set(int $fileId, string $name, string $value, ?string $type = null, ?string $content = null): ReaderEntity + { return $this->bookmarkMapper->set($fileId, $name, $value, $type, $content); } /** * @brief get cursor (current position in book) * - * @param int $fileId - * - * @psalm-return SerializedEntity + * @return SerializedEntity */ - public function getCursor(int $fileId): array { + public function getCursor(int $fileId): array + { $result = $this->get($fileId, self::CURSOR); - if (count($result) === 1) { + if (1 === count($result)) { return $result[0]; } + return []; } /** * @brief set cursor (current position in book) - * - * @param int $fileId - * @param string $value */ - public function setCursor(int $fileId, string $value): ReaderEntity { + public function setCursor(int $fileId, string $value): ReaderEntity + { return $this->bookmarkMapper->set($fileId, self::CURSOR, $value, self::BOOKMARK_TYPE); } /** * @brief delete bookmark - * - * @param int $fileId - * @param ?string $name - * @param ?string $type */ - public function delete($fileId, ?string $name = null, ?string $type = null): void { + public function delete(int $fileId, ?string $name = null, ?string $type = null): void + { foreach ($this->bookmarkMapper->get($fileId, $name, $type) as $bookmark) { $this->bookmarkMapper->delete($bookmark); } @@ -103,10 +94,9 @@ class BookmarkService extends Service { /** * @brief delete cursor - * - * @param int $fileId */ - public function deleteCursor(int $fileId): void { + public function deleteCursor(int $fileId): void + { $this->delete($fileId, self::CURSOR, self::BOOKMARK_TYPE); } } diff --git a/lib/Service/PreferenceService.php b/lib/Service/PreferenceService.php index 7ceb5e8..915927f 100644 --- a/lib/Service/PreferenceService.php +++ b/lib/Service/PreferenceService.php @@ -16,18 +16,16 @@ use OCA\Epubreader\Db\ReaderEntity; /** * @psalm-import-type SerializedEntity from ReaderEntity */ -class PreferenceService extends Service { - +class PreferenceService extends Service +{ // (ab)use the fact that $fileId never goes below 1 by using the // value 0 to indicate a default preference private const DEFAULTS = 0; private PreferenceMapper $preferenceMapper; - /** - * @param PreferenceMapper $preferenceMapper - */ - public function __construct(PreferenceMapper $preferenceMapper) { + public function __construct(PreferenceMapper $preferenceMapper) + { parent::__construct($preferenceMapper); $this->preferenceMapper = $preferenceMapper; } @@ -38,18 +36,18 @@ class PreferenceService extends Service { * scope identifies preference source, i.e. which renderer the preference applies to * preference type is format-dependent, eg CFI for epub, page number for CBR/CBZ, etc * - * @param string $scope - * @param int $fileId - * @param ?string $name - * - * @psalm-return SerializedEntity + * @return SerializedEntity */ - public function get(string $scope, int $fileId, ?string $name = null): array { + public function get(string $scope, int $fileId, ?string $name = null): array + { $result = $this->preferenceMapper->get($scope, $fileId, $name); + return array_map( function (ReaderEntity $entity): array { return $entity->toService(); - }, $result); + }, + $result + ); } /** @@ -57,59 +55,43 @@ class PreferenceService extends Service { * * scope identifies preference source, i.e. which renderer the preference applies to * position type is format-dependent, eg CFI for epub, page number for CBR/CBZ, etc - * - * @param string $scope - * @param int $fileId - * @param string $name - * @param string $value */ - public function set(string $scope, int $fileId, string $name, string $value): ReaderEntity { + public function set(string $scope, int $fileId, string $name, string $value): ReaderEntity + { return $this->preferenceMapper->set($scope, $fileId, $name, $value); } /** * @brief get default preference - * - * @param string $scope - * @param ?string $name */ - public function getDefault(string $scope, ?string $name = null): array { + public function getDefault(string $scope, ?string $name = null): array + { return $this->get($scope, self::DEFAULTS, $name); } /** * @brief set default preference - * - * @param string $scope - * @param string $name - * @param string $value */ - public function setDefault($scope, $name, $value): ReaderEntity { + public function setDefault(string $scope, string $name, string $value): ReaderEntity + { return $this->preferenceMapper->set($scope, self::DEFAULTS, $name, $value); } /** * @brief delete preference - * - * @param string $scope - * @param int $fileId - * @param ?string $name - * */ - public function delete(string $scope, int $fileId, ?string $name = null): void { - foreach($this->preferenceMapper->get($scope, $fileId, $name) as $preference) { + public function delete(string $scope, int $fileId, ?string $name = null): void + { + foreach ($this->preferenceMapper->get($scope, $fileId, $name) as $preference) { $this->preferenceMapper->delete($preference); } } /** * @brief delete default - * - * @param string $scope - * @param ?string $name - * */ - public function deleteDefault(string $scope, ?string $name = null): void { + public function deleteDefault(string $scope, ?string $name = null): void + { $this->delete($scope, self::DEFAULTS, $name); } } diff --git a/lib/Service/Service.php b/lib/Service/Service.php index 2b861e1..2cd1c65 100644 --- a/lib/Service/Service.php +++ b/lib/Service/Service.php @@ -12,10 +12,12 @@ namespace OCA\Epubreader\Service; use OCA\Epubreader\Db\ReaderMapper; -abstract class Service { +abstract class Service +{ protected ReaderMapper $mapper; - public function __construct(ReaderMapper $mapper) { + public function __construct(ReaderMapper $mapper) + { $this->mapper = $mapper; } } diff --git a/lib/Settings/Personal.php b/lib/Settings/Personal.php index 906ced5..5851c43 100644 --- a/lib/Settings/Personal.php +++ b/lib/Settings/Personal.php @@ -1,6 +1,6 @@ userId = $userId; $this->configManager = $configManager; } @@ -30,7 +31,8 @@ class Personal implements ISettings { * @return TemplateResponse returns the instance with all parameters set, ready to be rendered * @since 9.1 */ - public function getForm(): TemplateResponse { + public function getForm(): TemplateResponse + { $parameters = [ 'EpubEnable' => $this->configManager->getUserValue($this->userId, Application::APP_ID, 'epub_enable', 'true'), 'PdfEnable' => $this->configManager->getUserValue($this->userId, Application::APP_ID, 'pdf_enable', 'true'), @@ -41,11 +43,10 @@ class Personal implements ISettings { } /** - * Print config section (ownCloud 10) - * - * @return TemplateResponse + * Print config section (ownCloud 10). */ - public function getPanel(): TemplateResponse { + public function getPanel(): TemplateResponse + { return $this->getForm(); } @@ -53,28 +54,29 @@ class Personal implements ISettings { * @return string the section ID, e.g. 'sharing' * @since 9.1 */ - public function getSection(): string { + public function getSection(): string + { return Application::APP_ID; } /** - * Get section ID (ownCloud 10) - * - * @return string + * Get section ID (ownCloud 10). */ - public function getSectionID(): string { + public function getSectionID(): string + { return Application::APP_ID; } /** * @return int whether the form should be rather on the top or bottom of - * the admin section. The forms are arranged in ascending order of the - * priority values. It is required to return a value between 0 and 100. + * the admin section. The forms are arranged in ascending order of the + * priority values. It is required to return a value between 0 and 100. * * E.g.: 70 * @since 9.1 */ - public function getPriority(): int { + public function getPriority(): int + { return 10; } } diff --git a/lib/Settings/PersonalSection.php b/lib/Settings/PersonalSection.php index 7cbc202..6ee4ca0 100644 --- a/lib/Settings/PersonalSection.php +++ b/lib/Settings/PersonalSection.php @@ -1,6 +1,6 @@ urlGenerator = $urlGenerator; $this->l = $l; } /** * returns the relative path to an 16*16 icon describing the section. - * - * @return string */ - public function getIcon(): string { + public function getIcon(): string + { return $this->urlGenerator->imagePath(Application::APP_ID, 'app.svg'); } /** - * returns the ID of the section. It is supposed to be a lower case string, - * - * @return string + * returns the ID of the section. It is supposed to be a lower case string,. */ - public function getID(): string { + public function getID(): string + { return Application::APP_ID; } /** - * returns the translated name as it should be displayed - * - * @return string + * returns the translated name as it should be displayed. */ - public function getName(): string { + public function getName(): string + { return $this->l->t('EPUB/CBZ/PDF ebook reader'); } /** - * returns priority for positioning - * - * @return int + * returns priority for positioning. */ - public function getPriority(): int { + public function getPriority(): int + { return 20; } } diff --git a/lib/Utility/Server.php b/lib/Utility/Server.php index c62c320..7cc4d26 100644 --- a/lib/Utility/Server.php +++ b/lib/Utility/Server.php @@ -18,7 +18,6 @@ declare(strict_types=1); * * You should have received a copy of the GNU Affero General Public License, version 3, * along with this program. If not, see - * */ namespace OCA\Epubreader\Utility; @@ -38,19 +37,20 @@ use Psr\Container\NotFoundExceptionInterface; * * @since 25.0.0 */ -final class Server { +final class Server +{ /** * @template T * @param class-string|string $serviceName - * @return T|mixed - * @psalm-template S as class-string|string + * @template S as class-string|string * @psalm-param S $serviceName - * @psalm-return (S is class-string ? T : mixed) + * @return (S is class-string ? T : mixed) * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface * @since 25.0.0 */ - public static function get(string $serviceName) { + public static function get(string $serviceName) + { /** @psalm-suppress UndefinedClass */ return \OC::$server->get($serviceName); } diff --git a/lib/Utility/Time.php b/lib/Utility/Time.php index fecbaf7..26a72fd 100644 --- a/lib/Utility/Time.php +++ b/lib/Utility/Time.php @@ -10,17 +10,20 @@ namespace OCA\Epubreader\Utility; -class Time { - - public function getTime(): int { +class Time +{ + public function getTime(): int + { return time(); } /** * @return int the current unix time in miliseconds */ - public function getMicroTime(): int { + public function getMicroTime(): int + { list($millisecs, $secs) = explode(' ', microtime()); - return (int) ($secs . substr($millisecs, 2, 6)); + + return (int) ($secs.substr($millisecs, 2, 6)); } } diff --git a/stubs/OC/Hooks/Emitter.php b/stubs/OC/Hooks/Emitter.php index dd9cf31..b14a8f1 100644 --- a/stubs/OC/Hooks/Emitter.php +++ b/stubs/OC/Hooks/Emitter.php @@ -20,32 +20,20 @@ * * You should have received a copy of the GNU Affero General Public License, version 3, * along with this program. If not, see - * */ + namespace OC\Hooks; /** - * Class Emitter + * Class Emitter. * * interface for all classes that are able to emit events * - * @package OC\Hooks * @deprecated 18.0.0 use events and the \OCP\EventDispatcher\IEventDispatcher service */ -interface Emitter { - /** - * @param string $scope - * @param string $method - * @param callable $callback - * @return void - */ - public function listen($scope, $method, callable $callback); +interface Emitter +{ + public function listen(string $scope, string $method, callable $callback): void; - /** - * @param string $scope optional - * @param string $method optional - * @param callable $callback optional - * @return void - */ - public function removeListener($scope = null, $method = null, callable $callback = null); + public function removeListener(string $scope = null, string $method = null, callable $callback = null): void; } diff --git a/templates/cbreader.php b/templates/cbreader.php index 1f23288..0bb0d5d 100644 --- a/templates/cbreader.php +++ b/templates/cbreader.php @@ -14,7 +14,7 @@ $metadata = $_['metadata']; $annotations = $_['annotations']; $title = htmlentities(basename($downloadLink)); $revision = '0048'; -$version = \OC::$server->getAppManager()->getAppVersion('epubreader') . '.' . $revision; +$version = \OC::$server->getAppManager()->getAppVersion('epubreader').'.'.$revision; /* Mobile safari, the new IE6 */ $idevice = (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone') @@ -28,28 +28,28 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager') ?> -' data-staticpath='linkTo('epubreader', ''));?>' data-scope='' data-cursor='' data-defaults='' data-preferences='' data-metadata='' data-annotations=''> +' data-staticpath='linkTo('epubreader', '')); ?>' data-scope='' data-cursor='' data-defaults='' data-preferences='' data-metadata='' data-annotations=''> - + - <?php p($title);?> + <?php p($title); ?> - - - - - - - - + + + + + + + + - - - + + + - + @@ -109,7 +109,7 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager') diff --git a/templates/epubreader.php b/templates/epubreader.php index 68e5397..3d6a33f 100644 --- a/templates/epubreader.php +++ b/templates/epubreader.php @@ -14,7 +14,7 @@ $metadata = $_['metadata']; $annotations = $_['annotations']; $title = htmlentities(basename($downloadLink)); $revision = '0072'; -$version = \OC::$server->getAppManager()->getAppVersion('epubreader') . '.' . $revision; +$version = \OC::$server->getAppManager()->getAppVersion('epubreader').'.'.$revision; /* Mobile safari, the new IE6 */ $idevice = (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone') @@ -28,38 +28,38 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager') ?> -' data-staticpath='linkTo('epubreader', ''));?>' data-scope='' data-cursor='' data-defaults='' data-preferences='' data-metadata='' data-annotations=''> +' data-staticpath='linkTo('epubreader', '')); ?>' data-scope='' data-cursor='' data-defaults='' data-preferences='' data-metadata='' data-annotations=''> - + - <?php p($title);?> + <?php p($title); ?> - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + - - - - - + + + + + - +
@@ -113,21 +113,21 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager')
- +
- + %
- +
@@ -172,7 +172,7 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager') night
- t('night mode can be toggled by clicking the book title')); ?>; + t('night mode can be toggled by clicking the book title'); ?>;
@@ -202,19 +202,19 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager')
@@ -234,7 +234,7 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager')
@@ -282,7 +282,7 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager')
- +
diff --git a/templates/pdfreader.php b/templates/pdfreader.php index 32cfcb9..124b91c 100644 --- a/templates/pdfreader.php +++ b/templates/pdfreader.php @@ -14,7 +14,7 @@ $metadata = $_['metadata']; $annotations = $_['annotations']; $title = htmlentities(basename($downloadLink)); $revision = '0130'; -$version = \OC::$server->getAppManager()->getAppVersion('epubreader') . '.' . $revision; +$version = \OC::$server->getAppManager()->getAppVersion('epubreader').'.'.$revision; /* Mobile safari, the new IE6 */ $idevice = (strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone') @@ -28,50 +28,50 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager') ?> -' data-staticpath='linkTo('epubreader', ''));?>' data-scope='' data-cursor='' data-defaults='' data-preferences='' data-metadata='' data-annotations=''> +' data-staticpath='linkTo('epubreader', '')); ?>' data-scope='' data-cursor='' data-defaults='' data-preferences='' data-metadata='' data-annotations=''> - + - <?php p($title);?> + <?php p($title); ?> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - +
@@ -216,7 +216,7 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager')
- t("menu")); ?> + t('menu')); ?>
@@ -311,7 +311,7 @@ $nonce = class_exists('\OC\Security\CSP\ContentSecurityPolicyNonceManager')
- +
diff --git a/templates/settings-personal.php b/templates/settings-personal.php index b17cf25..64033c0 100644 --- a/templates/settings-personal.php +++ b/templates/settings-personal.php @@ -7,42 +7,41 @@ * later. * See the COPYING-README file. */ - script('epubreader', 'settings'); style('epubreader', 'settings'); ?>
-

t('Reader'));?>

 
+

t('Reader')); ?>

 

t('Select file types for which Reader should be the default viewer.')); ?>

/>

/>

/>