From 296f8c403f89bd4a69f1589cddb5a782a4586706 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Wed, 22 Feb 2023 01:08:04 +0100 Subject: [PATCH] =?UTF-8?q?PHPStan:=20Remove=20=E2=80=9Clying=E2=80=9D=20`?= =?UTF-8?q?@var`s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PHPStan 1.10 contains a new bleeding edge feature that complains when the `@var` does not match the inferred type: PHPDoc tag @var with type string is not subtype of native type non-empty-string|false. Let’s use more precise type. Or an `assert`, when the type is forced by another function. https://phpstan.org/blog/phpstan-1-10-comes-with-lie-detector --- phpstan.neon | 1 + src/daos/mysql/Sources.php | 6 +++--- src/helpers/Image.php | 2 -- src/helpers/View.php | 4 ++-- src/spouts/twitter/TwitterV1ApiClient.php | 4 ++-- utils/PHPStan/Stubs/IcoFileLoader.stub | 13 +++++++++++++ 6 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 utils/PHPStan/Stubs/IcoFileLoader.stub diff --git a/phpstan.neon b/phpstan.neon index e6080215fa..480af97e6a 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -14,6 +14,7 @@ parameters: stubFiles: - utils/PHPStan/Stubs/Dice.stub + - utils/PHPStan/Stubs/IcoFileLoader.stub ignoreErrors: # The arguments are provided by SimplePie and we want the value of a later one. diff --git a/src/daos/mysql/Sources.php b/src/daos/mysql/Sources.php index 646227321f..07badb4793 100644 --- a/src/daos/mysql/Sources.php +++ b/src/daos/mysql/Sources.php @@ -44,11 +44,11 @@ public function __construct(Configuration $configuration, DatabaseInterface $dat * @return int new id */ public function add(string $title, array $tags, ?string $filter, string $spout, array $params): int { - /** @var string $params */ // For PHPStan: Or an exception will be thrown. $params = @json_encode($params); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception(json_last_error_msg(), json_last_error()); } + assert($params !== false); // For PHPStan: Exception would be thrown when the function returns false. return $this->database->insert('INSERT INTO ' . $this->configuration->dbPrefix . 'sources (title, tags, filter, spout, params) VALUES (:title, :tags, :filter, :spout, :params)', [ ':title' => trim($title), @@ -69,11 +69,11 @@ public function add(string $title, array $tags, ?string $filter, string $spout, * @param array $params the new params */ public function edit(int $id, string $title, array $tags, ?string $filter, string $spout, array $params): void { - /** @var string $params */ // For PHPStan: Or an exception will be thrown. $params = @json_encode($params); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception(json_last_error_msg(), json_last_error()); } + assert($params !== false); // For PHPStan: Exception would be thrown when the function returns false. $this->database->exec('UPDATE ' . $this->configuration->dbPrefix . 'sources SET title=:title, tags=:tags, filter=:filter, spout=:spout, params=:params WHERE id=:id', [ ':title' => trim($title), @@ -283,11 +283,11 @@ public function getTags(int $id): array { * @return int id if any record is found */ public function checkIfExists(string $title, string $spout, array $params): int { - /** @var string $params */ // For PHPStan: Or an exception will be thrown. $params = @json_encode($params); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception(json_last_error_msg(), json_last_error()); } + assert($params !== false); // For PHPStan: Exception would be thrown when the function returns false. // Check if a entry exists with same title, spout and params $result = $this->database->exec('SELECT id FROM ' . $this->configuration->dbPrefix . 'sources WHERE title=:title AND spout=:spout AND params=:params', [ diff --git a/src/helpers/Image.php b/src/helpers/Image.php index 7c4039b375..63eaa97545 100644 --- a/src/helpers/Image.php +++ b/src/helpers/Image.php @@ -4,7 +4,6 @@ namespace helpers; -use Elphin\IcoFileLoader; use Elphin\IcoFileLoader\IcoFileService; use GuzzleHttp\Psr7\Uri; use GuzzleHttp\Psr7\UriResolver; @@ -223,7 +222,6 @@ public function loadImage( } if ($image === null) { - /** @var ?IcoFileLoader\IconImage */ // Type annotation says it cannot return null but that is not the case when the ico file does not contain images. $image = $icon->findBest(); } diff --git a/src/helpers/View.php b/src/helpers/View.php index 1535530409..a598b3a048 100644 --- a/src/helpers/View.php +++ b/src/helpers/View.php @@ -111,11 +111,11 @@ public function error(string $message) { public function jsonError($data) { header('Content-type: application/json'); - /** @var string $error */ // For PHPStan: Or an exception will be thrown. $error = @json_encode($data); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception(json_last_error_msg(), json_last_error()); } + assert($error !== false); // For PHPStan: Exception would be thrown when the function returns false. $this->error($error); } @@ -130,11 +130,11 @@ public function jsonError($data) { public function jsonSuccess($data) { header('Content-type: application/json'); - /** @var string $message */ // For PHPStan: Or an exception will be thrown. $message = @json_encode($data); if (json_last_error() !== JSON_ERROR_NONE) { throw new Exception(json_last_error_msg(), json_last_error()); } + assert($message !== false); // For PHPStan: Exception would be thrown when the function returns false. exit($message); } diff --git a/src/spouts/twitter/TwitterV1ApiClient.php b/src/spouts/twitter/TwitterV1ApiClient.php index aea45f3217..15e39b9b3d 100644 --- a/src/spouts/twitter/TwitterV1ApiClient.php +++ b/src/spouts/twitter/TwitterV1ApiClient.php @@ -178,9 +178,9 @@ private function getThumbnail(stdClass $item): ?string { private static function replaceEntities(string $text, array $entities): HtmlString { /** @var string built text */ $result = ''; - /** @var int number of bytes in text */ + /** @var int<0, max> number of bytes in text */ $length = strlen($text); - /** @var int index of the currently processed byte in the text */ + /** @var int<0, max> index of the currently processed byte in the text */ $i = 0; /** @var int index of the currently processed Unicode code point in the text */ $cpi = -1; diff --git a/utils/PHPStan/Stubs/IcoFileLoader.stub b/utils/PHPStan/Stubs/IcoFileLoader.stub new file mode 100644 index 0000000000..169cbe421c --- /dev/null +++ b/utils/PHPStan/Stubs/IcoFileLoader.stub @@ -0,0 +1,13 @@ +