Skip to content

Commit

Permalink
PHPStan: Remove “lying” @vars
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jtojnar committed Feb 22, 2023
1 parent ced4c07 commit 296f8c4
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions src/daos/mysql/Sources.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -69,11 +69,11 @@ public function add(string $title, array $tags, ?string $filter, string $spout,
* @param array<string, mixed> $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),
Expand Down Expand Up @@ -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', [
Expand Down
2 changes: 0 additions & 2 deletions src/helpers/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace helpers;

use Elphin\IcoFileLoader;
use Elphin\IcoFileLoader\IcoFileService;
use GuzzleHttp\Psr7\Uri;
use GuzzleHttp\Psr7\UriResolver;
Expand Down Expand Up @@ -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();
}

Expand Down
4 changes: 2 additions & 2 deletions src/helpers/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/spouts/twitter/TwitterV1ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions utils/PHPStan/Stubs/IcoFileLoader.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Elphin\IcoFileLoader;

class Icon {
/**
* @phpstan-return ?IconImage Can be null when an icon is empty.
*/
public function findBest();
}

class IconImage {
}

0 comments on commit 296f8c4

Please sign in to comment.