Merge pull request #14966 from Art4/phpstan-strict-rules

Add PHPStan strict rules
This commit is contained in:
Philipp 2025-06-18 06:53:47 +02:00 committed by GitHub
commit ccd3ebbd11
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 127 additions and 60 deletions

View file

@ -2,6 +2,9 @@
#
# SPDX-License-Identifier: CC0-1.0
includes:
- vendor/phpstan/phpstan-strict-rules/rules.neon
parameters:
level: 3
@ -23,6 +26,10 @@ parameters:
dynamicConstantNames:
- DB_UPDATE_VERSION
# See all rules at https://github.com/phpstan/phpstan-strict-rules/blob/2.0.x/rules.neon
strictRules:
allRules: false
ignoreErrors:
-
# Ignore missing GdImage class in PHP <= 7.4
@ -38,3 +45,17 @@ parameters:
# Ignore missing IMAP\Connection class in PHP <= 8.0
message: '(^Parameter .+ has invalid type IMAP\\Connection\.$)'
path: src
-
# #Fixme: Ignore type mismatch of BaseRepository::$factory in child classes
message: '#^PHPDoc type Friendica\\.+ of property Friendica\\.+\:\:\$factory is not the same as PHPDoc type Friendica\\Capabilities\\ICanCreateFromTableRow of overridden property Friendica\\BaseRepository\:\:\$factory\.$#'
identifier: property.phpDocType
count: 13
path: src
-
# #Fixme: Ignore type mismatch of BaseModule::$response in BaseApi module
message: '#^PHPDoc type Friendica\\Module\\Api\\ApiResponse of property Friendica\\Module\\BaseApi\:\:\$response is not the same as PHPDoc type Friendica\\Capabilities\\ICanCreateResponses of overridden property Friendica\\BaseModule\:\:\$response\.$#'
identifier: property.phpDocType
count: 1
path: src/Module/BaseApi.php

View file

@ -157,6 +157,7 @@
"php-mock/php-mock-phpunit": "^2.10",
"phpmd/phpmd": "^2.15",
"phpstan/phpstan": "^2.0",
"phpstan/phpstan-strict-rules": "^2.0",
"phpunit/phpunit": "^9"
},
"scripts": {

50
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "897b878d6db24b9a6437bd9f971478be",
"content-hash": "e93a8ac7e31cf3e5e0ca76134e5ffa0b",
"packages": [
{
"name": "asika/simple-console",
@ -5849,6 +5849,54 @@
],
"time": "2024-11-11T15:43:04+00:00"
},
{
"name": "phpstan/phpstan-strict-rules",
"version": "2.0.0",
"source": {
"type": "git",
"url": "https://github.com/phpstan/phpstan-strict-rules.git",
"reference": "a4a6a08bd4a461e516b9c3b8fdbf0f1883b34158"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/phpstan/phpstan-strict-rules/zipball/a4a6a08bd4a461e516b9c3b8fdbf0f1883b34158",
"reference": "a4a6a08bd4a461e516b9c3b8fdbf0f1883b34158",
"shasum": ""
},
"require": {
"php": "^7.4 || ^8.0",
"phpstan/phpstan": "^2.0"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.2",
"phpstan/phpstan-deprecation-rules": "^2.0",
"phpstan/phpstan-phpunit": "^2.0",
"phpunit/phpunit": "^9.6"
},
"type": "phpstan-extension",
"extra": {
"phpstan": {
"includes": [
"rules.neon"
]
}
},
"autoload": {
"psr-4": {
"PHPStan\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"description": "Extra strict and opinionated rules for PHPStan",
"support": {
"issues": "https://github.com/phpstan/phpstan-strict-rules/issues",
"source": "https://github.com/phpstan/phpstan-strict-rules/tree/2.0.0"
},
"time": "2024-10-26T16:04:33+00:00"
},
{
"name": "phpunit/php-code-coverage",
"version": "9.2.31",

View file

@ -92,5 +92,7 @@ HELP;
CoreWorker::unclaimProcess($process);
$this->processRepo->delete($process);
return;
}
}

View file

@ -157,10 +157,10 @@ class Pager
'text' => $this->l10n->t('newer'),
'class' => 'previous' . ($this->getPage() == 1 ? ' disabled' : '')
],
'next' => [
'next' => [
'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),
'text' => $this->l10n->t('older'),
'class' => 'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
'class' => 'next' . ($displayedItemCount < $this->getItemsPerPage() ? ' disabled' : '')
]
];
@ -208,15 +208,15 @@ class Pager
'class' => $this->getPage() == 1 ? 'disabled' : ''
];
$numpages = $totalItemCount / $this->getItemsPerPage();
$numpages = (int) ceil($totalItemCount / $this->getItemsPerPage());
$numstart = 1;
$numstop = $numpages;
$numstop = $numpages;
// Limit the number of displayed page number buttons.
if ($numpages > 8) {
$numstart = (($this->getPage() > 4) ? ($this->getPage() - 4) : 1);
$numstop = (($this->getPage() > ($numpages - 7)) ? $numpages : ($numstart + 8));
$numstart = ($this->getPage() > 4) ? ($this->getPage() - 4) : 1;
$numstop = ($this->getPage() > ($numpages - 7)) ? $numpages : ($numstart + 8);
}
$pages = [];
@ -237,25 +237,9 @@ class Pager
}
}
if (($totalItemCount % $this->getItemsPerPage()) != 0) {
if ($i == $this->getPage()) {
$pages[$i] = [
'url' => '#',
'text' => $i,
'class' => 'current active'
];
} else {
$pages[$i] = [
'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . $i),
'text' => $i,
'class' => 'n'
];
}
}
$data['pages'] = $pages;
$lastpage = (($numpages > intval($numpages)) ? intval($numpages)+1 : $numpages);
$lastpage = (($numpages > intval($numpages)) ? intval($numpages) + 1 : $numpages);
$data['next'] = [
'url' => Strings::ensureQueryParameter($this->baseQueryString . '&page=' . ($this->getPage() + 1)),

View file

@ -55,7 +55,7 @@ class HTML
$xpath = new DOMXPath($doc);
/** @var \DOMNode[] $list */
/** @var \DOMNodeList<\DOMNode>|false $list */
$list = $xpath->query("//" . $tag);
foreach ($list as $node) {
$attr = [];
@ -1018,7 +1018,7 @@ class HTML
*/
public static function checkRelMeLink(DOMDocument $doc, UriInterface $meUrl): bool
{
$xpath = new \DOMXpath($doc);
$xpath = new \DOMXPath($doc);
// This expression checks that "me" is among the space-delimited values of the "rel" attribute.
// And that the href attribute contains exactly the provided URL

View file

@ -28,7 +28,7 @@ abstract class AbstractLock implements ICanLock
*/
protected function hasAcquiredLock(string $key): bool
{
return isset($this->acquireLock[$key]) && $this->acquiredLocks[$key] === true;
return isset($this->acquiredLocks[$key]) && $this->acquiredLocks[$key] === true;
}
/**

View file

@ -99,8 +99,11 @@ class Cache extends AbstractSessionHandler
}
#[\ReturnTypeWillChange]
public function gc($max_lifetime): bool
/**
* @return int|false
*/
public function gc($max_lifetime)
{
return true;
return 0; // Cache does not support garbage collection, so we return 0 to indicate no action taken
}
}

View file

@ -124,13 +124,23 @@ class Database extends AbstractSessionHandler
}
#[\ReturnTypeWillChange]
public function gc($max_lifetime): bool
/**
* @return int|false
*/
public function gc($max_lifetime)
{
try {
return $this->dba->delete('session', ["`expire` < ?", time()]);
$result = $this->dba->delete('session', ["`expire` < ?", time()]);
} catch (\Exception $exception) {
$this->logger->warning('Cannot use garbage collector.', ['exception' => $exception]);
return false;
}
if ($result !== false) {
// TODO: DBA::delete() returns true, but we need to return the number of deleted rows as interger
$result = 0;
}
return $result;
}
}

View file

@ -521,7 +521,7 @@ class Worker
}
if ($sleeping) {
DI::logger()->info('Cooldown ended.', ['max-load' => $load_cooldown, 'max-processes' => $processes_cooldown, 'load' => $load, 'called-by' => System::callstack(1)]);
DI::logger()->info('Cooldown ended.', ['max-load' => $load_cooldown, 'max-processes' => $processes_cooldown, 'load' => $load ?? [], 'called-by' => System::callstack(1)]);
}
}

View file

@ -193,7 +193,7 @@ class PostUpdate
Contact::removeDuplicates($contact['nurl'], $contact['uid']);
}
DBA::close($contact);
DBA::close($contacts);
DI::keyValue()->set('post_update_version', 1322);
DI::logger()->info('Done');

View file

@ -8,7 +8,7 @@
namespace Friendica\Network;
use DOMDocument;
use DomXPath;
use DOMXPath;
use Exception;
use Friendica\Content\Text\HTML;
use Friendica\Core\Hook;
@ -1273,7 +1273,7 @@ class Probe
return [];
}
$xpath = new DomXPath($doc);
$xpath = new DOMXPath($doc);
$vcards = $xpath->query("//div[contains(concat(' ', @class, ' '), ' vcard ')]");
if (!is_object($vcards)) {

View file

@ -48,8 +48,8 @@ class Image
public function __construct(string $data, string $type = '', string $filename = '', bool $imagick = true)
{
$this->filename = $filename;
$type = Images::addMimeTypeByDataIfInvalid($type, $data);
$type = Images::addMimeTypeByExtensionIfInvalid($type, $filename);
$type = Images::addMimeTypeByDataIfInvalid($type, $data);
$type = Images::addMimeTypeByExtensionIfInvalid($type, $filename);
if (Images::isSupportedMimeType($type)) {
$this->originType = $this->outputType = Images::getImageTypeByMimeType($type);
@ -108,7 +108,7 @@ class Image
private function isAnimatedWebP(string $data)
{
$header_format = 'A4Riff/I1Filesize/A4Webp/A4Vp/A74Chunk';
$header = @unpack($header_format, $data);
$header = @unpack($header_format, $data);
if (!isset($header['Riff']) || strtoupper($header['Riff']) !== 'RIFF') {
return false;
@ -348,7 +348,7 @@ class Image
return false;
}
$width = $this->getWidth();
$width = $this->getWidth();
$height = $this->getHeight();
$scale = Images::getScalingDimensions($width, $height, $max);
@ -363,12 +363,11 @@ class Image
* Rotates image
*
* @param integer $degrees degrees to rotate image
* @return mixed
*/
public function rotate(int $degrees)
public function rotate(int $degrees): void
{
if (!$this->isValid()) {
return false;
return;
}
if ($this->isImagick()) {
@ -393,12 +392,11 @@ class Image
*
* @param boolean $horiz optional, default true
* @param boolean $vert optional, default false
* @return mixed
*/
public function flip(bool $horiz = true, bool $vert = false)
public function flip(bool $horiz = true, bool $vert = false): void
{
if (!$this->isValid()) {
return false;
return;
}
if ($this->isImagick()) {
@ -414,8 +412,8 @@ class Image
return;
}
$w = imagesx($this->image);
$h = imagesy($this->image);
$w = imagesx($this->image);
$h = imagesy($this->image);
$flipped = imagecreate($w, $h);
if ($horiz) {
for ($x = 0; $x < $w; $x++) {
@ -523,7 +521,7 @@ class Image
return false;
}
$width = $this->getWidth();
$width = $this->getWidth();
$height = $this->getHeight();
if ((!$width) || (!$height)) {
@ -532,22 +530,22 @@ class Image
if ($width < $min && $height < $min) {
if ($width > $height) {
$dest_width = $min;
$dest_width = $min;
$dest_height = intval(($height * $min) / $width);
} else {
$dest_width = intval(($width * $min) / $height);
$dest_width = intval(($width * $min) / $height);
$dest_height = $min;
}
} else {
if ($width < $min) {
$dest_width = $min;
$dest_width = $min;
$dest_height = intval(($height * $min) / $width);
} else {
if ($height < $min) {
$dest_width = intval(($width * $min) / $height);
$dest_width = intval(($width * $min) / $height);
$dest_height = $min;
} else {
$dest_width = $width;
$dest_width = $width;
$dest_height = $height;
}
}
@ -622,7 +620,7 @@ class Image
imagedestroy($this->image);
}
$this->image = $dest;
$this->image = $dest;
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
}
@ -799,9 +797,9 @@ class Image
}
$row[] = [$colors['r'], $colors['g'], $colors['b']];
} else {
$index = imagecolorat($image->image, $x, $y);
$index = imagecolorat($image->image, $x, $y);
$colors = @imagecolorsforindex($image->image, $index);
$row[] = [$colors['red'], $colors['green'], $colors['blue']];
$row[] = [$colors['red'], $colors['green'], $colors['blue']];
}
}
$pixels[] = $row;
@ -830,7 +828,7 @@ class Image
if ($this->isImagick()) {
$this->image = new Imagick();
$draw = new ImagickDraw();
$draw = new ImagickDraw();
$this->image->newImage($scaled['width'], $scaled['height'], '', 'png');
} else {
$this->image = imagecreatetruecolor($scaled['width'], $scaled['height']);
@ -838,7 +836,7 @@ class Image
for ($y = 0; $y < $scaled['height']; ++$y) {
for ($x = 0; $x < $scaled['width']; ++$x) {
[$r, $g, $b] = $pixels[$y][$x];
list($r, $g, $b) = $pixels[$y][$x];
if ($draw !== null) {
$draw->setFillColor("rgb($r, $g, $b)");
$draw->point($x, $y);

View file

@ -592,7 +592,7 @@ class HTTPSignature
return [];
}
$sig_block = self::parseSigHeader($http_headers['HTTP_SIGNATURE']);
$sig_block = self::parseSigheader($http_headers['HTTP_SIGNATURE']);
if (empty($sig_block['keyId'])) {
DI::logger()->debug('No keyId', ['sig_block' => $sig_block]);
@ -652,7 +652,7 @@ class HTTPSignature
}
}
$sig_block = self::parseSigHeader($http_headers['HTTP_SIGNATURE']);
$sig_block = self::parseSigheader($http_headers['HTTP_SIGNATURE']);
// Add fields from the signature block to the header. See issue 8845
if (!empty($sig_block['created']) && empty($headers['(created)'])) {