Skip to content

Commit

Permalink
Fix issues discovered by PHPStan
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Feb 2, 2022
1 parent 9cb8eca commit ac61872
Show file tree
Hide file tree
Showing 14 changed files with 78 additions and 38 deletions.
5 changes: 2 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
parameters:
bootstrapFiles:
- src/constants.php

ignoreErrors:
- '(Call to an undefined static method F3::([sg]et|error|reroute)\(\))'
- vendor/simplepie/simplepie/library/SimplePie.php
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
6 changes: 1 addition & 5 deletions src/controllers/Items/Stats.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@ class Stats {
/** @var \daos\Sources sources */
private $sourcesDao;

/** @var \controllers\Tags tags controller */
private $tagsController;

/** @var \daos\Tags tags */
private $tagsDao;

/** @var View view helper */
private $view;

public function __construct(Authentication $authentication, \daos\Items $itemsDao, \daos\Sources $sourcesDao, \controllers\Tags $tagsController, \daos\Tags $tagsDao, View $view) {
public function __construct(Authentication $authentication, \daos\Items $itemsDao, \daos\Sources $sourcesDao, \daos\Tags $tagsDao, View $view) {
$this->authentication = $authentication;
$this->itemsDao = $itemsDao;
$this->sourcesDao = $sourcesDao;
$this->tagsController = $tagsController;
$this->tagsDao = $tagsDao;
$this->view = $view;
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/Opml/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private function processGroup(SimpleXMLElement $xml, array $tags = []) {
$xml->registerXPathNamespace('selfoss', 'https://selfoss.aditu.de/');

// tags are the words of the outline parent
$title = (string) $xml->attributes(null)->title;
$title = (string) $xml->attributes()->title;
if ($title !== '' && $title !== '/') {
$tags[] = $title;
// for new tags, try to import tag color, otherwise use random color
Expand Down Expand Up @@ -165,7 +165,7 @@ private function addSubscription(SimpleXMLElement $xml, array $tags) {
// Optional attributes: title, htmlUrl, language, title, version
// Selfoss namespaced attributes: spout, params

$attrs = $xml->attributes(null);
$attrs = $xml->attributes();
$nsattrs = $xml->attributes('selfoss', true);

// description
Expand Down
14 changes: 13 additions & 1 deletion src/daos/CommonSqlDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@

trait CommonSqlDatabase {
/**
* Execute SQL statement
* Execute SQL statement.
*
* @param string $cmd
* @param array|scalar $args
*
* @return \PDOStatement
*/
public function execute($cmd, $args = []) {
return $this->connection->execute($cmd, $args);
}

/**
* Execute SQL statement and fetch the result as an associative array (when applicable).
*
* @param string $cmd
* @param array|scalar $args
Expand Down
14 changes: 12 additions & 2 deletions src/daos/DatabaseInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,23 @@ interface DatabaseInterface {
const PARAM_DATETIME = 4;

/**
* Execute SQL statement(s)
* Execute SQL statement.
*
* @param string $cmd
* @param array|scalar $args
*
* @return array|null
* @return \PDOStatement
*/
public function execute($cmd, $args = []);

/**
* Execute SQL statement and fetch the result as an associative array (when applicable).
*
* @param string $cmd
* @param array|scalar $args
*
* @return ?array
**/
public function exec($cmd, $args = []);

/**
Expand Down
2 changes: 2 additions & 0 deletions src/daos/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* @license GPLv3 (https://www.gnu.org/licenses/gpl-3.0.html)
* @author Harald Lapp <[email protected]>
* @author Tobias Zeising <[email protected]>
*
* @mixin ItemsInterface
*/
class Items {
/** @var ItemsInterface Instance of backend specific items class */
Expand Down
8 changes: 5 additions & 3 deletions src/daos/Sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* @author Harald Lapp <[email protected]>
* @author Daniel Seither <[email protected]>
* @author Tobias Zeising <[email protected]>
*
* @mixin SourcesInterface
*/
class Sources {
/** @var SourcesInterface Instance of backend specific sources class */
Expand Down Expand Up @@ -138,15 +140,15 @@ public function validate($title, $spout, array $params) {
}

foreach ($validation as $validate) {
if ($validate === 'alpha' && !preg_match("[A-Za-Z._\b]+", $value)) {
if ($validate === 'alpha' && !preg_match("([A-Za-z._\b]+)", $value)) {
$result[$id] = 'only alphabetic characters allowed for ' . $spout->params[$id]['title'];
} elseif ($validate === 'email' && !preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $value)) {
} elseif ($validate === 'email' && !preg_match('(^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$)', $value)) {
$result[$id] = $spout->params[$id]['title'] . ' is not a valid email address';
} elseif ($validate === 'numeric' && !is_numeric($value)) {
$result[$id] = 'only numeric values allowed for ' . $spout->params[$id]['title'];
} elseif ($validate === 'int' && (int) $value != $value) {
$result[$id] = 'only integer values allowed for ' . $spout->params[$id]['title'];
} elseif ($validate === 'alnum' && !preg_match("[A-Za-Z0-9._\b]+", $value)) {
} elseif ($validate === 'alnum' && !preg_match("([A-Za-z0-9._\b]+)", $value)) {
$result[$id] = 'only alphanumeric values allowed for ' . $spout->params[$id]['title'];
} elseif ($validate === 'notempty' && strlen(trim($value)) === 0) {
$result[$id] = 'empty value for ' . $spout->params[$id]['title'] . ' not allowed';
Expand Down
2 changes: 2 additions & 0 deletions src/daos/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* @copyright Copyright (c) Tobias Zeising (http://www.aditu.de)
* @license GPLv3 (https://www.gnu.org/licenses/gpl-3.0.html)
* @author Tobias Zeising <[email protected]>
*
* @mixin TagsInterface
*/
class Tags {
/** @var TagsInterface Instance of backend specific sources class */
Expand Down
4 changes: 2 additions & 2 deletions src/daos/mysql/Items.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,11 +704,11 @@ public function bulkStatusUpdate(array $statuses) {
':id' => [$id, \PDO::PARAM_INT],
':statusUpdate' => [$q['datetime'], \PDO::PARAM_STR],
];
$updated = $this->database->exec(
$updated = $this->database->execute(
'UPDATE ' . $this->configuration->dbPrefix . 'items
SET ' . implode(', ', array_values($q['updates'])) . '
WHERE id = :id AND updatetime < :statusUpdate', $params);
if ($updated == 0) {
if ($updated->rowCount() === 0) {
// entry status was updated in between so updatetime must
// be updated to ensure client side consistency of
// statuses.
Expand Down
10 changes: 8 additions & 2 deletions src/helpers/ContentLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ public function fetch($source) {
}

try {
// Clear the value in case we need it in catch clause.
$iconUrl = null;
$iconUrl = $item->getIcon();
if (strlen(trim($iconUrl)) > 0) {
if (isset($iconCache[$iconUrl])) {
Expand Down Expand Up @@ -262,12 +264,16 @@ public function fetch($source) {
}
} catch (\Throwable $e) {
// cache failure
$iconCache[$iconUrl] = '';
if ($iconUrl !== null) {
$iconCache[$iconUrl] = '';
}
$this->logger->error('icon: error', ['exception' => $e]);
} catch (\Exception $e) {
// For PHP 5
// cache failure
$iconCache[$iconUrl] = '';
if ($iconUrl !== null) {
$iconCache[$iconUrl] = '';
}
$this->logger->error('icon: error', ['exception' => $e]);
}

Expand Down
31 changes: 22 additions & 9 deletions src/helpers/DatabaseConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,14 @@ private function type($val) {
}

/**
* Execute SQL statement
* Execute SQL statement.
*
* @param string $cmd
* @param array|scalar $args
*
* @return ?array
* @return \PDOStatement
**/
public function exec($cmd, $args = []) {
$result = null;

public function execute($cmd, $args = []) {
if (is_scalar($args)) {
$args = [1 => $args];
}
Expand Down Expand Up @@ -174,12 +172,27 @@ public function exec($cmd, $args = []) {
throw $e;
}

if ($query->columnCount() !== 0) {
$result = $query->fetchAll(PDO::FETCH_ASSOC);
return $query;
}

/**
* Execute SQL statement and fetch the result as an associative array (when applicable).
*
* @param string $cmd
* @param array|scalar $args
*
* @return ?array
**/
public function exec($cmd, $args = []) {
$statement = $this->execute($cmd, $args);

$result = null;
if ($statement->columnCount() !== 0) {
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
}

$query->closeCursor();
unset($query);
$statement->closeCursor();
unset($statement);

return $result;
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public function loadImage($data, $format = self::FORMAT_PNG, $width = null, $hei
} else {
$image->setImageFormat('png24');
$image->setImageCompression(\Imagick::COMPRESSION_UNDEFINED);
$image->setOption('png:compression-level', 9);
$image->setOption('png:compression-level', '9');
}

return new ImageHolder((string) $image, $format, $image->getImageWidth(), $image->getImageHeight());
Expand Down
10 changes: 4 additions & 6 deletions src/helpers/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use function Http\Response\send;
use Psr\Http\Message\ResponseInterface;
use Stringy\Stringy as S;

/**
* Helper class for rendering template
Expand Down Expand Up @@ -36,12 +37,9 @@ public function __construct(Configuration $configuration) {
*/
public function getBaseUrl() {
// base url in config.ini file
if (strlen($this->configuration->baseUrl) > 0) {
$base = $this->configuration->baseUrl;
$length = strlen($base);
if ($length > 0 && substr($base, $length - 1, 1) !== '/') {
$base .= '/';
}
$base = $this->configuration->baseUrl;
if ($base !== '') {
$base = (string) S::create($base)->ensureRight('/');
} else { // auto generate base url
$protocol = 'http';
if ((isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ||
Expand Down
4 changes: 2 additions & 2 deletions src/spouts/rss/fulltextrss.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ private static function removeTrackersFromUrl($url) {

// Next, rebuild URL
$real_url = $url['scheme'] . '://';
if (isset($url['user']) && isset($url['password'])) {
$real_url .= $url['user'] . ':' . $url['password'] . '@';
if (isset($url['user']) && isset($url['pass'])) {
$real_url .= $url['user'] . ':' . $url['pass'] . '@';
}
$real_url .= $url['host'] . $url['path'];

Expand Down

0 comments on commit ac61872

Please sign in to comment.