PHPStan Level 7 complete (#5406)

* PHPStan Level 7 complete

* Start PHPStan Level 8

* Forgot exclude .phtml
This commit is contained in:
Alexandre Alapetite
2023-05-22 20:55:59 +02:00
committed by GitHub
parent d554d0f673
commit 445cc23abd
10 changed files with 92 additions and 30 deletions

View File

@@ -61,7 +61,7 @@
"phpcs": "phpcs . -s",
"phpcbf": "phpcbf . -p -s",
"phpstan": "phpstan analyse --memory-limit 512M .",
"phpstan-next": "phpstan analyse --level 7 --memory-limit 512M $(find . -type d -name 'vendor' -prune -o -name '*.php' -o -name '*.phtml' | grep -Fxvf ./tests/phpstan-next.txt | sort | paste -s -)",
"phpstan-next": "phpstan analyse --level 8 --memory-limit 512M $(find . -type d -name 'vendor' -prune -o -name '*.php' -o -name '*.phtml' | grep -Fxvf ./tests/phpstan-next.txt | sort | paste -s -)",
"phpunit": "phpunit --bootstrap ./tests/bootstrap.php --verbose ./tests",
"translations": "cli/manipulate.translation.php -a format",
"test": [

View File

@@ -9,7 +9,7 @@
* @property-read string $environment
* @property array<string,bool> $extensions_enabled
* @property-read string $mailer
* @property-read array<string|int|bool> $smtp
* @property-read array{'hostname':string,'host':string,'auth':bool,'username':string,'password':string,'secure':string,'port':int,'from':string} $smtp
* @property string $title
*/
class Minz_Configuration {

View File

@@ -34,7 +34,7 @@ class Minz_Mailer {
/** @var string */
private $mailer;
/** @var array<string|int|bool> */
/** @var array{'hostname':string,'host':string,'auth':bool,'username':string,'password':string,'secure':string,'port':int,'from':string} */
private $smtp_config;
/** @var int */
private $debug_level;
@@ -71,7 +71,7 @@ class Minz_Mailer {
public function mail(string $to, string $subject): bool {
ob_start();
$this->view->render();
$body = ob_get_contents();
$body = ob_get_contents() ?: '';
ob_end_clean();
PHPMailer::$validator = 'html5';

View File

@@ -12,7 +12,7 @@ class Minz_Migrator
/** @var string[] */
private $applied_versions;
/** @var array<string> */
/** @var array<callable> */
private $migrations = [];
/**
@@ -37,7 +37,7 @@ class Minz_Migrator
}
$applied_migrations = array_filter(explode("\n", $applied_migrations));
$migration_files = scandir($migrations_path);
$migration_files = scandir($migrations_path) ?: [];
$migration_files = array_filter($migration_files, static function (string $filename) {
$file_extension = pathinfo($filename, PATHINFO_EXTENSION);
return $file_extension === 'php';
@@ -131,7 +131,7 @@ class Minz_Migrator
return;
}
foreach (scandir($directory) as $filename) {
foreach (scandir($directory) ?: [] as $filename) {
$file_extension = pathinfo($filename, PATHINFO_EXTENSION);
if ($file_extension !== 'php') {
continue;
@@ -149,6 +149,10 @@ class Minz_Migrator
ADMIN_LOG
);
}
if (!is_callable($migration_callback)) {
throw new BadFunctionCallException("{$migration_version} migration cannot be called.");
}
$this->addMigration($migration_version, $migration_callback);
}
}
@@ -158,17 +162,11 @@ class Minz_Migrator
*
* @param string $version The version of the migration (be careful, migrations
* are sorted with the `strnatcmp` function)
* @param ?callable $callback The migration function to execute, it should
* @param callable $callback The migration function to execute, it should
* return true on success and must return false
* on error
*
* @throws BadFunctionCallException if the callback isnt callable.
*/
public function addMigration(string $version, ?callable $callback): void {
if (!is_callable($callback)) {
throw new BadFunctionCallException("{$version} migration cannot be called.");
}
public function addMigration(string $version, callable $callback): void {
$this->migrations[$version] = $callback;
}

View File

@@ -50,7 +50,7 @@ abstract class Minz_Pdo extends PDO {
// PHP8+: PDO::prepare(string $query, array $options = []): PDOStatement|false
/**
* @param string $query
* @param array<int,string>|null $options
* @param array<int,string> $options
* @return PDOStatement|false
* @phpstan-ignore-next-line
*/

View File

@@ -6,6 +6,6 @@ class GoogleGroupsExtension extends Minz_Extension {
}
public static function findFeed(string $url): string {
return preg_replace('%^(https?://groups.google.com/forum)/#!forum/(.+)$%i', '$1/feed/$2/msgs/rss.xml', $url);
return preg_replace('%^(https?://groups.google.com/forum)/#!forum/(.+)$%i', '$1/feed/$2/msgs/rss.xml', $url) ?? '';
}
}

View File

@@ -63,6 +63,7 @@ function _dateCeiling(string $isoDate): string {
return $x[0] . 'T' . $t;
}
/** @phpstan-return ($isoDate is null ? null : ($isoDate is '' ? null : string)) */
function _noDelimit(?string $isoDate): ?string {
return $isoDate === null || $isoDate === '' ? null : str_replace(array('-', ':'), '', $isoDate); //FIXME: Bug with negative time zone
}
@@ -70,7 +71,8 @@ function _noDelimit(?string $isoDate): ?string {
function _dateRelative(?string $d1, ?string $d2): ?string {
if ($d2 === null) {
return $d1 !== null && $d1[0] !== 'P' ? $d1 : null;
} elseif ($d2 !== '' && $d2[0] != 'P' && $d1 !== null && $d1[0] !== 'P') {
}
if ($d2 !== '' && $d2[0] != 'P' && $d1 !== null && $d1[0] !== 'P') {
$y2 = substr($d2, 0, 4);
if (strlen($y2) < 4 || !ctype_digit($y2)) { //Does not start by a year
$d2 = _noDelimit($d2);

View File

@@ -1,6 +1,6 @@
parameters:
# TODO: Increase rule-level https://phpstan.org/user-guide/rule-levels
level: 6
level: 7
treatPhpDocTypesAsCertain: false
fileExtensions:
- php

View File

@@ -17,14 +17,6 @@ class MigratorTest extends TestCase
self::assertTrue($result);
}
public function testAddMigrationFailsIfUncallableMigration(): void {
$this->expectException(BadFunctionCallException::class);
$this->expectExceptionMessage('foo migration cannot be called.');
$migrator = new Minz_Migrator();
$migrator->addMigration('foo', null);
}
public function testMigrationsIsSorted(): void {
$migrator = new Minz_Migrator();
$migrator->addMigration('2_foo', function () {

View File

@@ -1,7 +1,77 @@
# List of files, which are not yet passing PHPStan level 7 https://phpstan.org/user-guide/rule-levels
# Used for automated tests to avoid regressions in files already passing that level.
# Can be regenerated with something like:
# find . -type d -name 'vendor' -prune -o -name '*.php' -exec sh -c 'vendor/bin/phpstan analyse --level 7 --memory-limit 512M {} >/dev/null 2>/dev/null || echo {}' \;
# find . -type d -name 'vendor' -prune -o -name '*.php' -exec sh -c 'vendor/bin/phpstan analyse --level 8 --memory-limit 512M {} >/dev/null 2>/dev/null || echo {}' \;
./lib/Minz/Mailer.php
./lib/Minz/Migrator.php
./app/Controllers/apiController.php
./app/Controllers/authController.php
./app/Controllers/categoryController.php
./app/Controllers/configureController.php
./app/Controllers/feedController.php
./app/Controllers/importExportController.php
./app/Controllers/indexController.php
./app/Controllers/javascriptController.php
./app/Controllers/tagController.php
./app/Controllers/updateController.php
./app/Controllers/userController.php
./app/install.php
./app/layout/aside_configure.phtml
./app/layout/aside_feed.phtml
./app/layout/header.phtml
./app/layout/layout.phtml
./app/layout/nav_menu.phtml
./app/layout/simple.phtml
./app/Mailers/UserMailer.php
./app/Models/Auth.php
./app/Models/BooleanSearch.php
./app/Models/Category.php
./app/Models/Context.php
./app/Models/DatabaseDAO.php
./app/Models/DatabaseDAOPGSQL.php
./app/Models/Entry.php
./app/Models/Feed.php
./app/Models/FeedDAO.php
./app/Models/FormAuth.php
./app/Models/Search.php
./app/Models/Share.php
./app/Models/StatsDAO.php
./app/Services/ImportService.php
./app/views/auth/index.phtml
./app/views/configure/archiving.phtml
./app/views/configure/display.phtml
./app/views/configure/integration.phtml
./app/views/configure/reading.phtml
./app/views/configure/shortcut.phtml
./app/views/configure/system.phtml
./app/views/feed/contentSelectorPreview.phtml
./app/views/helpers/category/update.phtml
./app/views/helpers/configure/query.phtml
./app/views/helpers/export/opml.phtml
./app/views/helpers/extension/configure.phtml
./app/views/helpers/extension/details.phtml
./app/views/helpers/feed/update.phtml
./app/views/helpers/index/normal/entry_bottom.phtml
./app/views/helpers/index/normal/entry_header.phtml
./app/views/helpers/javascript_vars.phtml
./app/views/helpers/stream-footer.phtml
./app/views/index/about.phtml
./app/views/index/global.phtml
./app/views/index/normal.phtml
./app/views/index/reader.phtml
./app/views/stats/idle.phtml
./app/views/subscription/index.phtml
./app/views/user/manage.phtml
./app/views/user/profile.phtml
./app/views/user/validateEmail.phtml
./cli/_cli.php
./cli/create-user.php
./cli/delete-user.php
./cli/do-install.php
./cli/i18n/I18nData.php
./cli/i18n/I18nValue.php
./cli/list-users.php
./cli/reconfigure.php
./cli/user-info.php
./lib/lib_install.php
./lib/Minz/ModelPdo.php
./lib/Minz/Translate.php