From 2baa7a2b9136d4d4dc1e56dfc043cc71da3ee1df Mon Sep 17 00:00:00 2001 From: Sean Molenaar Date: Fri, 20 Nov 2020 20:52:54 +0100 Subject: Make PHPstan stricter Signed-off-by: Sean Molenaar --- CHANGELOG.md | 1 + composer.json | 1 + composer.lock | 53 ++++++++++++++++++++++++++++- lib/Command/Config/FeedAdd.php | 2 ++ lib/Config/FetcherConfig.php | 6 ++-- lib/Config/LegacyConfig.php | 5 ++- lib/Controller/FeedApiController.php | 3 ++ lib/Controller/FeedController.php | 2 +- lib/Controller/FolderApiController.php | 4 +-- lib/Controller/FolderController.php | 4 +-- lib/Cron/UpdaterJob.php | 2 +- lib/Db/EntityJSONSerializer.php | 2 +- lib/Db/Item.php | 10 +++--- lib/Db/ItemMapperV2.php | 4 +-- lib/Fetcher/Client/FeedIoClient.php | 2 +- lib/Fetcher/FeedFetcher.php | 2 +- lib/Service/Exceptions/ServiceException.php | 8 ++--- lib/Service/FeedServiceV2.php | 5 ++- lib/Service/ImportService.php | 1 + lib/Service/ItemServiceV2.php | 5 +-- lib/Service/StatusService.php | 4 +-- 21 files changed, 92 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c895ac303..a215b3012 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ The format is almost based on [Keep a Changelog](https://keepachangelog.com/en/1 - Add BATS as integration tests - Update FeedFetcher to import categories from feeds (#1248) - Update serialization of item to include categories (#1248) +- Make PHPStan stricter ### Fixed - Do not show deleted feeds in item list diff --git a/composer.json b/composer.json index 8858e3c83..030bdd501 100644 --- a/composer.json +++ b/composer.json @@ -58,6 +58,7 @@ "squizlabs/php_codesniffer": "^3.5.6", "phpstan/phpstan": "^0.12.43", "phpstan/phpstan-doctrine": "^0.12.22", + "phpstan/phpstan-strict-rules": "^0.12.5", "phpstan/phpstan-phpunit": "^0.12.16", "phpstan/extension-installer": "^1.0", "guzzlehttp/guzzle": "^7.2", diff --git a/composer.lock b/composer.lock index fc969b038..96742de47 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": "2e3812e44e38a2c33ac501d96007c786", + "content-hash": "c1d2d17af8c630b365bbd580f297a7d5", "packages": [ { "name": "andreskrey/readability.php", @@ -1497,6 +1497,57 @@ }, "time": "2021-03-06T11:51:27+00:00" }, + { + "name": "phpstan/phpstan-strict-rules", + "version": "0.12.9", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan-strict-rules.git", + "reference": "0705fefc7c9168529fd130e341428f5f10f4f01d" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/0705fefc7c9168529fd130e341428f5f10f4f01d", + "reference": "0705fefc7c9168529fd130e341428f5f10f4f01d", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0", + "phpstan/phpstan": "^0.12.66" + }, + "require-dev": { + "phing/phing": "^2.16.3", + "php-parallel-lint/php-parallel-lint": "^1.2", + "phpstan/phpstan-phpunit": "^0.12.16", + "phpunit/phpunit": "^7.5.20" + }, + "type": "phpstan-extension", + "extra": { + "branch-alias": { + "dev-master": "0.12-dev" + }, + "phpstan": { + "includes": [ + "rules.neon" + ] + } + }, + "autoload": { + "psr-4": { + "PHPStan\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "Extra strict and opinionated rules for PHPStan", + "support": { + "issues": "https://github.com/phpstan/phpstan-strict-rules/issues", + "source": "https://github.com/phpstan/phpstan-strict-rules/tree/0.12.9" + }, + "time": "2021-01-13T08:50:28+00:00" + }, { "name": "phpunit/php-code-coverage", "version": "9.2.5", diff --git a/lib/Command/Config/FeedAdd.php b/lib/Command/Config/FeedAdd.php index 4b4279267..52485c55a 100644 --- a/lib/Command/Config/FeedAdd.php +++ b/lib/Command/Config/FeedAdd.php @@ -2,6 +2,7 @@ declare(strict_types=1); namespace OCA\News\Command\Config; +use OCA\News\Db\Feed; use OCA\News\Service\Exceptions\ServiceConflictException; use OCA\News\Service\Exceptions\ServiceNotFoundException; use OCA\News\Service\FeedServiceV2; @@ -71,6 +72,7 @@ class FeedAdd extends Command } try { + /** @var Feed $feed */ $feed = $this->feedService->create( $user, $url, diff --git a/lib/Config/FetcherConfig.php b/lib/Config/FetcherConfig.php index 7c3e5f3d1..ce2e7db8d 100644 --- a/lib/Config/FetcherConfig.php +++ b/lib/Config/FetcherConfig.php @@ -84,7 +84,7 @@ class FetcherConfig $url = new \Net_URL2($proxy); $creds = $config->getSystemValue('proxyuserpwd', null); - if ($creds) { + if ($creds !== null) { $auth = explode(':', $creds, 2); $url->setUserinfo($auth[0], $auth[1]); } @@ -106,10 +106,10 @@ class FetcherConfig 'headers' => ['User-Agent' => static::DEFAULT_USER_AGENT, 'Accept' => static::DEFAULT_ACCEPT], ]; - if (!empty($this->proxy)) { + if (!is_null($this->proxy)) { $config['proxy'] = $this->proxy; } - if (!empty($this->redirects)) { + if (!is_null($this->redirects)) { $config['redirect.max'] = $this->redirects; } diff --git a/lib/Config/LegacyConfig.php b/lib/Config/LegacyConfig.php index 3267e132e..b0d78f9f1 100644 --- a/lib/Config/LegacyConfig.php +++ b/lib/Config/LegacyConfig.php @@ -69,9 +69,8 @@ class LegacyConfig } else { foreach ($configValues as $key => $value) { if (property_exists($this, $key)) { - $type = gettype($this->$key); - settype($value, $type); - $this->$key = $value; + settype($value, gettype($this->$key)); //@phpstan-ignore-line + $this->$key = $value; //@phpstan-ignore-line } else { $this->logger->warning( 'Configuration value "' . $key . diff --git a/lib/Controller/FeedApiController.php b/lib/Controller/FeedApiController.php index 43d92b7ca..ffea84254 100644 --- a/lib/Controller/FeedApiController.php +++ b/lib/Controller/FeedApiController.php @@ -16,6 +16,7 @@ namespace OCA\News\Controller; use Exception; +use OCA\News\Db\Feed; use OCA\News\Service\Exceptions\ServiceConflictException; use OCA\News\Service\Exceptions\ServiceNotFoundException; use OCA\News\Service\FeedServiceV2; @@ -170,6 +171,7 @@ class FeedApiController extends ApiController $folderId = $folderId === 0 ? null : $folderId; try { + /** @var Feed $feed */ $feed = $this->feedService->find($this->getUserId(), $feedId); $feed->setFolderId($folderId); $this->feedService->update($this->getUserId(), $feed); @@ -194,6 +196,7 @@ class FeedApiController extends ApiController public function rename(int $feedId, string $feedTitle) { try { + /** @var Feed $feed */ $feed = $this->feedService->find($this->getUserId(), $feedId); $feed->setTitle($feedTitle); $this->feedService->update($this->getUserId(), $feed); diff --git a/lib/Controller/FeedController.php b/lib/Controller/FeedController.php index fd041ea08..9c97d34b1 100644 --- a/lib/Controller/FeedController.php +++ b/lib/Controller/FeedController.php @@ -266,7 +266,7 @@ class FeedController extends Controller 'starred' => count($this->itemService->starred($this->getUserId())) ]; - if ($feed) { + if (!is_null($feed)) { $params['feeds'] = [$feed]; } diff --git a/lib/Controller/FolderApiController.php b/lib/Controller/FolderApiController.php index 71fc503e2..ce5d69f2b 100644 --- a/lib/Controller/FolderApiController.php +++ b/lib/Controller/FolderApiController.php @@ -91,7 +91,7 @@ class FolderApiController extends ApiController */ public function delete(?int $folderId) { - if (empty($folderId)) { + if (is_null($folderId)) { return new JSONResponse([], Http::STATUS_BAD_REQUEST); } @@ -117,7 +117,7 @@ class FolderApiController extends ApiController */ public function update(?int $folderId, string $name) { - if (empty($folderId)) { + if (is_null($folderId)) { return new JSONResponse([], Http::STATUS_BAD_REQUEST); } diff --git a/lib/Controller/FolderController.php b/lib/Controller/FolderController.php index da03f9863..9b06b3495 100644 --- a/lib/Controller/FolderController.php +++ b/lib/Controller/FolderController.php @@ -104,7 +104,7 @@ class FolderController extends Controller */ public function delete(?int $folderId) { - if (empty($folderId)) { + if (is_null($folderId)) { return new JSONResponse([], Http::STATUS_BAD_REQUEST); } try { @@ -129,7 +129,7 @@ class FolderController extends Controller */ public function rename(?int $folderId, string $folderName) { - if (empty($folderId)) { + if (is_null($folderId)) { return new JSONResponse([], Http::STATUS_BAD_REQUEST); } try { diff --git a/lib/Cron/UpdaterJob.php b/lib/Cron/UpdaterJob.php index d5ecce593..4675d6de7 100644 --- a/lib/Cron/UpdaterJob.php +++ b/lib/Cron/UpdaterJob.php @@ -57,7 +57,7 @@ class UpdaterJob extends TimedJob */ protected function run($argument) { - $uses_cron = $this->config->getAppValue( + $uses_cron = (bool) $this->config->getAppValue( Application::NAME, 'useCronUpdates', Application::DEFAULT_SETTINGS['useCronUpdates'] diff --git a/lib/Db/EntityJSONSerializer.php b/lib/Db/EntityJSONSerializer.php index 2646ed97a..3612d27f1 100644 --- a/lib/Db/EntityJSONSerializer.php +++ b/lib/Db/EntityJSONSerializer.php @@ -27,7 +27,7 @@ trait EntityJSONSerializer { $result = []; foreach ($properties as $property) { - $result[$property] = $this->$property; + $result[$property] = $this->$property; //@phpstan-ignore-line } return $result; } diff --git a/lib/Db/Item.php b/lib/Db/Item.php index 97b3bc1ca..2d87babf7 100644 --- a/lib/Db/Item.php +++ b/lib/Db/Item.php @@ -126,7 +126,7 @@ class Item extends Entity implements IAPI, \JsonSerializable public function generateSearchIndex(): void { - $categoriesString = !empty($this->getCategories()) + $categoriesString = !is_null($this->getCategories()) ? implode('', $this->getCategories()) : ''; @@ -542,7 +542,7 @@ class Item extends Entity implements IAPI, \JsonSerializable public function setCategories(array $categories = null): self { - $categoriesJson = !empty($categories) ? json_encode($categories) : null; + $categoriesJson = !is_null($categories) ? json_encode($categories) : null; $this->setCategoriesJson($categoriesJson); return $this; @@ -577,11 +577,11 @@ class Item extends Entity implements IAPI, \JsonSerializable /** * Format for exporting. * - * @param $feeds + * @param array $feeds List of feeds * * @return array */ - public function toExport($feeds): array + public function toExport(array $feeds): array { return [ 'guid' => $this->getGuid(), @@ -622,7 +622,7 @@ class Item extends Entity implements IAPI, \JsonSerializable /** * Check if a given mimetype is supported * - * @param string $mime mimetype to check + * @param string|null $mime mimetype to check * * @return boolean */ diff --git a/lib/Db/ItemMapperV2.php b/lib/Db/ItemMapperV2.php index 93457d4f9..a8b101719 100644 --- a/lib/Db/ItemMapperV2.php +++ b/lib/Db/ItemMapperV2.php @@ -132,12 +132,12 @@ class ItemMapperV2 extends NewsMapperV2 * @param int $feedId ID of the feed * @param string $guidHash hash to find with * - * @return Item|Entity + * @return Item * * @throws DoesNotExistException * @throws MultipleObjectsReturnedException */ - public function findForUserByGuidHash(string $userId, int $feedId, string $guidHash): Item + public function findForUserByGuidHash(string $userId, int $feedId, string $guidHash): Entity { $builder = $this->db->getQueryBuilder(); $builder->select('items.*') diff --git a/lib/Fetcher/Client/FeedIoClient.php b/lib/Fetcher/Client/FeedIoClient.php index 3a1427e84..ac1f62a1f 100644 --- a/lib/Fetcher/Client/FeedIoClient.php +++ b/lib/Fetcher/Client/FeedIoClient.php @@ -61,7 +61,7 @@ class FeedIoClient implements ClientInterface return new Response($psrResponse, $duration); } catch (BadResponseException $e) { - switch ((int) $e->getResponse()->getStatusCode()) { + switch ($e->getResponse()->getStatusCode()) { case 404: throw new NotFoundException($e->getMessage()); default: diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php index 01cb20853..c25f4ddb9 100755 --- a/lib/Fetcher/FeedFetcher.php +++ b/lib/Fetcher/FeedFetcher.php @@ -112,7 +112,7 @@ class FeedFetcher implements IFeedFetcher ?string $password ): array { $url2 = new Net_URL2($url); - if (!empty($user) && !empty(trim($user))) { + if (!is_null($user) && trim($user) !== '') { $url2->setUserinfo(urlencode($user), urlencode($password)); } $url = $url2->getNormalizedURL(); diff --git a/lib/Service/Exceptions/ServiceException.php b/lib/Service/Exceptions/ServiceException.php index 69b963ab6..b4804f1de 100644 --- a/lib/Service/Exceptions/ServiceException.php +++ b/lib/Service/Exceptions/ServiceException.php @@ -27,9 +27,9 @@ abstract class ServiceException extends Exception /** * Constructor * - * @param string $msg the error message - * @param int $code - * @param Exception|null $previous + * @param string $msg the error message + * @param int $code + * @param Exception|null $previous */ final public function __construct(string $msg, int $code = 0, ?Exception $previous = null) { @@ -39,7 +39,7 @@ abstract class ServiceException extends Exception /** * Create exception from Mapper exception. * - * @param IMapperException|Exception $exception Existing exception + * @param IMapperException $exception Existing exception * * @return static */ diff --git a/lib/Service/FeedServiceV2.php b/lib/Service/FeedServiceV2.php index d49c540b1..9d577db97 100644 --- a/lib/Service/FeedServiceV2.php +++ b/lib/Service/FeedServiceV2.php @@ -18,7 +18,6 @@ use FeedIo\Reader\ReadErrorException; use HTMLPurifier; use OCA\News\Db\FeedMapperV2; -use OCA\News\Db\Folder; use OCA\News\Fetcher\FeedFetcher; use OCA\News\Service\Exceptions\ServiceConflictException; use OCA\News\Service\Exceptions\ServiceNotFoundException; @@ -206,7 +205,7 @@ class FeedServiceV2 extends Service * @var Feed $feed * @var Item[] $items */ - list($feed, $items) = $this->feedFetcher->fetch($feedUrl, true, '0', $full_text, $user, $password); + list($feed, $items) = $this->feedFetcher->fetch($feedUrl, $full_text, $user, $password); } catch (ReadErrorException $ex) { $this->logger->debug($ex->getMessage()); throw new ServiceNotFoundException($ex->getMessage()); @@ -307,7 +306,7 @@ class FeedServiceV2 extends Service $feed->setLastUpdateError(null); $unreadCount = 0; - array_map(function (Item $item) use (&$unreadCount) { + array_map(function (Item $item) use (&$unreadCount): void { if ($item->isUnread()) { $unreadCount++; } diff --git a/lib/Service/ImportService.php b/lib/Service/ImportService.php index e7b6ab7be..7b593356e 100644 --- a/lib/Service/ImportService.php +++ b/lib/Service/ImportService.php @@ -106,6 +106,7 @@ class ImportService ->setFolderId(null) ->setPreventUpdate(true); + /** @var Feed $feed */ $feed = $this->feedService->insert($feed); $feedsDict[$feed->getLink()] = $feed; } diff --git a/lib/Service/ItemServiceV2.php b/lib/Service/ItemServiceV2.php index da675450f..fafd5e558 100644 --- a/lib/Service/ItemServiceV2.php +++ b/lib/Service/ItemServiceV2.php @@ -89,6 +89,7 @@ class ItemServiceV2 extends Service public function insertOrUpdate(Item $item): Entity { try { + /** @var Item $db_item */ $db_item = $this->findByGuidHash($item->getFeedId(), $item->getGuidHash()); // Transfer user modifications @@ -243,7 +244,7 @@ class ItemServiceV2 extends Service * @param int $feedId * @param string $guidHash * - * @return Item|Entity + * @return Item * * @throws DoesNotExistException * @throws MultipleObjectsReturnedException @@ -307,7 +308,7 @@ class ItemServiceV2 extends Service */ public function findAllAfter(string $userId, int $feedType, int $updatedSince): array { - if (!in_array($feedType, [ListType::STARRED, ListType::UNREAD, ListType::ALL_ITEMS])) { + if (!in_array($feedType, [ListType::STARRED, ListType::UNREAD, ListType::ALL_ITEMS], true)) { throw new ServiceValidationException('Trying to find in unknown type'); } diff --git a/lib/Service/StatusService.php b/lib/Service/StatusService.php index d33d99bad..a15f30c76 100644 --- a/lib/Service/StatusService.php +++ b/lib/Service/StatusService.php @@ -45,11 +45,11 @@ class StatusService //Is NC cron enabled? $cronMode = $this->settings->getAppValue('core', 'backgroundjobs_mode'); //Expect nextcloud cron - $cronOff = !$this->settings->getAppValue( + $cronOff = !boolval($this->settings->getAppValue( Application::NAME, 'useCronUpdates', Application::DEFAULT_SETTINGS['useCronUpdates'] - ); + )); // check for cron modes which may lead to problems return $cronMode === 'cron' || $cronOff; -- cgit v1.2.3