Skip to content

Commit

Permalink
PHPStan prepare exceptions (#6037)
Browse files Browse the repository at this point in the history
Take advantage of
https://phpstan.org/blog/bring-your-exceptions-under-control

Minimum changes to pass `tooWideThrowType` and `implicitThrows`.

Revert some mistakes from:
#5504
Preparation needed before new PRs of the same type:
#5962

Fix several wrong PHPDocs and catches:

> Method ... has ...Exception in PHPDoc @throws tag but it's not thrown.

> Dead catch - ...Exception is never thrown in the try block.
  • Loading branch information
Alkarex committed Jan 15, 2024
1 parent 52f6c83 commit 314077a
Show file tree
Hide file tree
Showing 41 changed files with 204 additions and 164 deletions.
1 change: 0 additions & 1 deletion app/Controllers/authController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function indexAction(): void {
*
* It forwards to the correct login page (form) or main page if
* the user is already connected.
* @throws Minz_ConfigurationParamException
*/
public function loginAction(): void {
if (FreshRSS_Auth::hasAccess() && Minz_Request::paramString('u') === '') {
Expand Down
4 changes: 2 additions & 2 deletions app/Controllers/feedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public function firstAction(): void {
/**
* @param array<string,mixed> $attributes
* @throws FreshRSS_AlreadySubscribed_Exception
* @throws FreshRSS_FeedNotAdded_Exception
* @throws FreshRSS_BadUrl_Exception
* @throws FreshRSS_Feed_Exception
* @throws FreshRSS_FeedNotAdded_Exception
* @throws Minz_FileNotExistException
*/
public static function addFeed(string $url, string $title = '', int $cat_id = 0, string $new_cat_name = '',
Expand Down Expand Up @@ -874,7 +875,6 @@ public function actualizeAction(): int {

/**
* @throws Minz_ConfigurationNamespaceException
* @throws JsonException
* @throws Minz_PDOConnectionException
*/
public static function renameFeed(int $feed_id, string $feed_name): bool {
Expand Down
1 change: 1 addition & 0 deletions app/Controllers/indexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ public function opmlAction(): void {
/**
* This method returns a list of entries based on the Context object.
* @return Traversable<FreshRSS_Entry>
* @throws FreshRSS_EntriesGetter_Exception
*/
public static function listEntriesByContext(): Traversable {
$entryDAO = FreshRSS_Factory::createEntryDao();
Expand Down
2 changes: 1 addition & 1 deletion app/Controllers/tagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function addAction(): void {

/**
* @throws Minz_ConfigurationNamespaceException
* @throws Minz_PDOConnectionException|JsonException
* @throws Minz_PDOConnectionException
*/
public function renameAction(): void {
if (!FreshRSS_Auth::hasAccess()) {
Expand Down
18 changes: 9 additions & 9 deletions app/Controllers/updateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ public static function isGit(): bool {

/**
* Automatic change to the new name of edge branch since FreshRSS 1.18.0.
* @throws Minz_Exception
*/
public static function migrateToGitEdge(): bool {
$errorMessage = 'Error during git checkout to edge branch. Please change branch manually!';

if (!is_writable(FRESHRSS_PATH . '/.git/config')) {
throw new Exception($errorMessage);
throw new Minz_Exception($errorMessage);
}

//Note `git branch --show-current` requires git 2.22+
exec('git symbolic-ref --short HEAD', $output, $return);
if ($return != 0) {
throw new Exception($errorMessage);
throw new Minz_Exception($errorMessage);
}
$line = implode('', $output);
if ($line !== 'master' && $line !== 'dev') {
Expand All @@ -33,13 +34,13 @@ public static function migrateToGitEdge(): bool {
unset($output);
exec('git checkout edge --guess -f', $output, $return);
if ($return != 0) {
throw new Exception($errorMessage);
throw new Minz_Exception($errorMessage);
}

unset($output);
exec('git reset --hard FETCH_HEAD', $output, $return);
if ($return != 0) {
throw new Exception($errorMessage);
throw new Minz_Exception($errorMessage);
}

return true;
Expand All @@ -64,6 +65,7 @@ public static function hasGitUpdate(): bool {
chdir(FRESHRSS_PATH);
$output = [];
try {
/** @throws ValueError */
exec('git fetch --prune', $output, $return);
if ($return == 0) {
$output = [];
Expand All @@ -72,7 +74,7 @@ public static function hasGitUpdate(): bool {
$line = implode('; ', $output);
Minz_Log::warning('git fetch warning: ' . $line);
}
} catch (Exception $e) {
} catch (Throwable $e) {
Minz_Log::warning('git fetch error: ' . $e->getMessage());
}
chdir($cwd);
Expand Down Expand Up @@ -101,11 +103,9 @@ public static function gitPull() {

$output = [];
self::migrateToGitEdge();
} catch (Exception $e) {
} catch (Throwable $e) {
Minz_Log::warning('Git error: ' . $e->getMessage());
if (empty($output)) {
$output = $e->getMessage();
}
$output = $e->getMessage();
$return = 1;
}
chdir($cwd);
Expand Down
6 changes: 5 additions & 1 deletion app/Controllers/userController.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ public function manageAction(): void {
}
}

/** @param array<string,mixed> $userConfigOverride */
/**
* @param array<string,mixed> $userConfigOverride
* @throws Minz_ConfigurationNamespaceException
* @throws Minz_PDOConnectionException
*/
public static function createUser(string $new_user_name, ?string $email, string $passwordPlain,
array $userConfigOverride = [], bool $insertDefaultFeeds = true): bool {
$userConfig = [];
Expand Down
2 changes: 1 addition & 1 deletion app/Exceptions/AlreadySubscribedException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

class FreshRSS_AlreadySubscribed_Exception extends Exception {
class FreshRSS_AlreadySubscribed_Exception extends Minz_Exception {

private string $feedName = '';

Expand Down
2 changes: 1 addition & 1 deletion app/Exceptions/ContextException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
/**
* An exception raised when a context is invalid
*/
class FreshRSS_Context_Exception extends Exception {
class FreshRSS_Context_Exception extends Minz_Exception {

}
6 changes: 0 additions & 6 deletions app/Exceptions/DAOException.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Exceptions/EntriesGetterException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);

class FreshRSS_EntriesGetter_Exception extends Exception {
class FreshRSS_EntriesGetter_Exception extends Minz_Exception {

}
2 changes: 1 addition & 1 deletion app/Exceptions/FeedException.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
declare(strict_types=1);

class FreshRSS_Feed_Exception extends Exception {
class FreshRSS_Feed_Exception extends Minz_Exception {

}
2 changes: 1 addition & 1 deletion app/Exceptions/FeedNotAddedException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

class FreshRSS_FeedNotAdded_Exception extends Exception {
class FreshRSS_FeedNotAdded_Exception extends Minz_Exception {

private string $url = '';

Expand Down
2 changes: 1 addition & 1 deletion app/Exceptions/ZipException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

class FreshRSS_Zip_Exception extends Exception {
class FreshRSS_Zip_Exception extends Minz_Exception {

private int $zipErrorCode = 0;

Expand Down
2 changes: 1 addition & 1 deletion app/Exceptions/ZipMissingException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
declare(strict_types=1);

class FreshRSS_ZipMissing_Exception extends Exception {
class FreshRSS_ZipMissing_Exception extends Minz_Exception {
}
2 changes: 0 additions & 2 deletions app/Models/CategoryDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ protected function autoUpdateDb(array $errorInfo): bool {
/**
* @param array{'name':string,'id'?:int,'kind'?:int,'lastUpdate'?:int,'error'?:int|bool,'attributes'?:string|array<string,mixed>} $valuesTmp
* @return int|false
* @throws JsonException
*/
public function addCategory(array $valuesTmp) {
// TRIM() to provide a type hint as text
Expand Down Expand Up @@ -155,7 +154,6 @@ public function addCategoryObject(FreshRSS_Category $category) {
/**
* @param array{'name':string,'kind':int,'attributes'?:array<string,mixed>|mixed|null} $valuesTmp
* @return int|false
* @throws JsonException
*/
public function updateCategory(int $id, array $valuesTmp) {
// No tag of the same name
Expand Down
7 changes: 6 additions & 1 deletion app/Models/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public static function initSystem(bool $reload = false): void {
}
}

/**
* @throws FreshRSS_Context_Exception
*/
public static function &systemConf(): FreshRSS_SystemConfiguration {
if (FreshRSS_Context::$system_conf === null) {
throw new FreshRSS_Context_Exception('System configuration not initialised!');
Expand All @@ -88,7 +91,6 @@ public static function hasSystemConf(): bool {

/**
* Initialize the context for the current user.
* @throws Minz_ConfigurationParamException
*/
public static function initUser(string $username = '', bool $userMustExist = true): void {
FreshRSS_Context::$user_conf = null;
Expand Down Expand Up @@ -153,6 +155,9 @@ public static function initUser(string $username = '', bool $userMustExist = tru
}
}

/**
* @throws FreshRSS_Context_Exception
*/
public static function &userConf(): FreshRSS_UserConfiguration {
if (FreshRSS_Context::$user_conf === null) {
throw new FreshRSS_Context_Exception('User configuration not initialised!');
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ public function isDay(int $day, int $today): bool {

/**
* @param array<string,mixed> $attributes
* @throws Minz_Exception
*/
public static function getContentByParsing(string $url, string $path, array $attributes = [], int $maxRedirs = 3): string {
$cachePath = FreshRSS_Feed::cacheFilename($url, $attributes, FreshRSS_Feed::KIND_HTML_XPATH);
Expand Down Expand Up @@ -741,7 +742,7 @@ public static function getContentByParsing(string $url, string $path, array $att
$html = trim(sanitizeHTML($content, $base));
return $html;
} else {
throw new Exception();
throw new Minz_Exception();
}
}

Expand Down
5 changes: 5 additions & 0 deletions app/Models/EntryDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,7 @@ public static function sqlBooleanSearch(string $alias, FreshRSS_BooleanSearch $f
/**
* @param 'ASC'|'DESC' $order
* @return array{0:array<int|string>,1:string}
* @throws FreshRSS_EntriesGetter_Exception
*/
protected function sqlListEntriesWhere(string $alias = '', ?FreshRSS_BooleanSearch $filters = null,
int $state = FreshRSS_Entry::STATE_ALL,
Expand Down Expand Up @@ -1059,6 +1060,7 @@ protected function sqlListEntriesWhere(string $alias = '', ?FreshRSS_BooleanSear
* @param int $id category/feed/tag ID
* @param 'ASC'|'DESC' $order
* @return array{0:array<int|string>,1:string}
* @throws FreshRSS_EntriesGetter_Exception
*/
private function sqlListWhere(string $type = 'a', int $id = 0, int $state = FreshRSS_Entry::STATE_ALL,
string $order = 'DESC', int $limit = 1, string $firstId = '', ?FreshRSS_BooleanSearch $filters = null,
Expand Down Expand Up @@ -1126,6 +1128,7 @@ private function sqlListWhere(string $type = 'a', int $id = 0, int $state = Fres
* @param 'ASC'|'DESC' $order
* @param int $id category/feed/tag ID
* @return PDOStatement|false
* @throws FreshRSS_EntriesGetter_Exception
*/
private function listWhereRaw(string $type = 'a', int $id = 0, int $state = FreshRSS_Entry::STATE_ALL,
string $order = 'DESC', int $limit = 1, string $firstId = '', ?FreshRSS_BooleanSearch $filters = null,
Expand Down Expand Up @@ -1161,6 +1164,7 @@ private function listWhereRaw(string $type = 'a', int $id = 0, int $state = Fres
* @param int $id category/feed/tag ID
* @param 'ASC'|'DESC' $order
* @return Traversable<FreshRSS_Entry>
* @throws FreshRSS_EntriesGetter_Exception
*/
public function listWhere(string $type = 'a', int $id = 0, int $state = FreshRSS_Entry::STATE_ALL,
string $order = 'DESC', int $limit = 1, string $firstId = '',
Expand Down Expand Up @@ -1226,6 +1230,7 @@ public function listByIds(array $ids, string $order = 'DESC'): Traversable {
* @param int $id category/feed/tag ID
* @param 'ASC'|'DESC' $order
* @return array<numeric-string>|null
* @throws FreshRSS_EntriesGetter_Exception
*/
public function listIdsWhere(string $type = 'a', int $id = 0, int $state = FreshRSS_Entry::STATE_ALL,
string $order = 'DESC', int $limit = 1, string $firstId = '', ?FreshRSS_BooleanSearch $filters = null): ?array {
Expand Down
27 changes: 18 additions & 9 deletions app/Models/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class FreshRSS_Feed extends Minz_Model {
private string $hubUrl = '';
private string $selfUrl = '';

/**
* @throws FreshRSS_BadUrl_Exception
*/
public function __construct(string $url, bool $validate = true) {
if ($validate) {
$this->_url($url);
Expand Down Expand Up @@ -248,6 +251,9 @@ public function _id(int $value): void {
$this->id = $value;
}

/**
* @throws FreshRSS_BadUrl_Exception
*/
public function _url(string $value, bool $validate = true): void {
$this->hash = '';
$url = $value;
Expand Down Expand Up @@ -323,9 +329,16 @@ public function _nbEntries(int $value): void {
$this->nbEntries = $value;
}

/**
* @throws Minz_FileNotExistException
* @throws FreshRSS_Feed_Exception
*/
public function load(bool $loadDetails = false, bool $noCache = false): ?SimplePie {
if ($this->url != '') {
// @phpstan-ignore-next-line
/**
* @phpstan-ignore-next-line
* @throws Minz_FileNotExistException
*/
if (CACHE_PATH == '') {
throw new Minz_FileNotExistException(
'CACHE_PATH',
Expand Down Expand Up @@ -615,9 +628,6 @@ private function dotPathsForStandardJsonFeed(): array {
];
}

/**
* @throws FreshRSS_Context_Exception
*/
public function loadJson(): ?SimplePie {
if ($this->url == '') {
return null;
Expand Down Expand Up @@ -654,9 +664,6 @@ public function loadJson(): ?SimplePie {
return $this->simplePieFromContent($feedContent);
}

/**
* @throws FreshRSS_Context_Exception
*/
public function loadHtmlXpath(): ?SimplePie {
if ($this->url == '') {
return null;
Expand Down Expand Up @@ -799,7 +806,6 @@ public function loadHtmlXpath(): ?SimplePie {

/**
* @return int|null The max number of unread articles to keep, or null if disabled.
* @throws JsonException
*/
public function keepMaxUnread() {
$keepMaxUnread = $this->attributeInt('keep_max_n_unread');
Expand Down Expand Up @@ -881,7 +887,10 @@ public function cleanOldEntries() {
return false;
}

/** @param array<string,mixed> $attributes */
/**
* @param array<string,mixed> $attributes
* @throws FreshRSS_Context_Exception
*/
public static function cacheFilename(string $url, array $attributes, int $kind = FreshRSS_Feed::KIND_RSS): string {
$simplePie = customSimplePie($attributes);
$filename = $simplePie->get_cache_filename($url);
Expand Down
1 change: 0 additions & 1 deletion app/Models/FeedDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ protected function autoUpdateDb(array $errorInfo): bool {
* @param array{'url':string,'kind':int,'category':int,'name':string,'website':string,'description':string,'lastUpdate':int,'priority'?:int,
* 'pathEntries'?:string,'httpAuth':string,'error':int|bool,'ttl'?:int,'attributes'?:string|array<string|mixed>} $valuesTmp
* @return int|false
* @throws JsonException
*/
public function addFeed(array $valuesTmp) {
$sql = 'INSERT INTO `_feed` (url, kind, category, name, website, description, `lastUpdate`, priority, `pathEntries`, `httpAuth`, error, ttl, attributes)
Expand Down

0 comments on commit 314077a

Please sign in to comment.