Skip to content

Commit

Permalink
Upgrade phpstan, phpunit
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Nov 11, 2024
1 parent 7af6f41 commit 9ab0a2c
Show file tree
Hide file tree
Showing 39 changed files with 145 additions and 143 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"guzzlehttp/psr7": "^2.2",
"mongodb/mongodb": "^1.8",
"php-amqplib/php-amqplib": "~2.4 || ^3",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-strict-rules": "^1.4",
"phpunit/phpunit": "^10.5.17",
"phpstan/phpstan": "^2",
"phpstan/phpstan-deprecation-rules": "^2",
"phpstan/phpstan-strict-rules": "^2",
"phpunit/phpunit": "^10.5.17 || ^11.0.7",
"predis/predis": "^1.1 || ^2",
"rollbar/rollbar": "^4.0",
"ruflin/elastica": "^7 || ^8",
Expand Down
96 changes: 74 additions & 22 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,111 +1,163 @@
parameters:
ignoreErrors:
-
message: "#^Property Monolog\\\\ErrorHandler\\:\\:\\$reservedMemory is never read, only written\\.$#"
message: '#^Property Monolog\\ErrorHandler\:\:\$reservedMemory is never read, only written\.$#'
identifier: property.onlyWritten
count: 1
path: src/Monolog/ErrorHandler.php

-
message: "#^Return type \\(array\\<array\\|bool\\|float\\|int\\|object\\|string\\|null\\>\\|bool\\|float\\|int\\|object\\|string\\|null\\) of method Monolog\\\\Formatter\\\\JsonFormatter\\:\\:normalize\\(\\) should be covariant with return type \\(array\\<array\\|bool\\|float\\|int\\|string\\|null\\>\\|bool\\|float\\|int\\|string\\|null\\) of method Monolog\\\\Formatter\\\\NormalizerFormatter\\:\\:normalize\\(\\)$#"
message: '#^Method Monolog\\Formatter\\JsonFormatter\:\:normalizeException\(\) return type has no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: src/Monolog/Formatter/JsonFormatter.php

-
message: "#^Cannot access offset 'table' on array\\<array\\|bool\\|float\\|int\\|string\\|null\\>\\|bool\\|float\\|int\\|object\\|string\\.$#"
message: '#^PHPDoc tag @return with type array\<string, array\<array\<string\>\|int\|string\>\|int\|string\>\|string is not subtype of native type array\.$#'
identifier: return.phpDocType
count: 1
path: src/Monolog/Formatter/JsonFormatter.php

-
message: '#^Return type \(array\) of method Monolog\\Formatter\\JsonFormatter\:\:normalizeException\(\) should be covariant with return type \(array\<string, array\<array\<string\>\|int\|string\>\|int\|string\>\|string\) of method Monolog\\Formatter\\NormalizerFormatter\:\:normalizeException\(\)$#'
identifier: method.childReturnType
count: 1
path: src/Monolog/Formatter/JsonFormatter.php

-
message: '#^Return type \(array\<array\<mixed\>\|bool\|float\|int\|object\|string\|null\>\|bool\|float\|int\|object\|string\|null\) of method Monolog\\Formatter\\JsonFormatter\:\:normalize\(\) should be covariant with return type \(array\<array\<mixed\>\|bool\|float\|int\|string\|null\>\|bool\|float\|int\|string\|null\) of method Monolog\\Formatter\\NormalizerFormatter\:\:normalize\(\)$#'
identifier: method.childReturnType
count: 1
path: src/Monolog/Formatter/JsonFormatter.php

-
message: '#^Method Monolog\\Formatter\\NormalizerFormatter\:\:normalizeException\(\) should return array\<string, array\<array\<string\>\|int\|string\>\|int\|string\>\|string but returns array\<int, string\>\.$#'
identifier: return.type
count: 1
path: src/Monolog/Formatter/NormalizerFormatter.php

-
message: '#^Method Monolog\\Formatter\\NormalizerFormatter\:\:normalizeException\(\) should return array\<string, array\<array\<string\>\|int\|string\>\|int\|string\>\|string but returns array\<string, array\<int\<0, max\>\|string, array\<array\<string\>\|int\|string\>\|int\|string\>\|int\|string\>\.$#'
identifier: return.type
count: 1
path: src/Monolog/Formatter/NormalizerFormatter.php

-
message: '#^Cannot access offset ''table'' on array\<array\<mixed\>\|bool\|float\|int\|string\|null\>\|bool\|float\|int\|object\|string\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 1
path: src/Monolog/Formatter/WildfireFormatter.php

-
message: "#^Return type \\(array\\<array\\|bool\\|float\\|int\\|string\\|null\\>\\|bool\\|float\\|int\\|object\\|string\\|null\\) of method Monolog\\\\Formatter\\\\WildfireFormatter\\:\\:normalize\\(\\) should be covariant with return type \\(array\\<array\\|bool\\|float\\|int\\|string\\|null\\>\\|bool\\|float\\|int\\|string\\|null\\) of method Monolog\\\\Formatter\\\\NormalizerFormatter\\:\\:normalize\\(\\)$#"
message: '#^Return type \(array\<array\<mixed\>\|bool\|float\|int\|string\|null\>\|bool\|float\|int\|object\|string\|null\) of method Monolog\\Formatter\\WildfireFormatter\:\:normalize\(\) should be covariant with return type \(array\<array\<mixed\>\|bool\|float\|int\|string\|null\>\|bool\|float\|int\|string\|null\) of method Monolog\\Formatter\\NormalizerFormatter\:\:normalize\(\)$#'
identifier: method.childReturnType
count: 1
path: src/Monolog/Formatter/WildfireFormatter.php

-
message: "#^Access to constant VERSION on an unknown class Elasticsearch\\\\Client\\.$#"
message: '#^Access to constant VERSION on an unknown class Elasticsearch\\Client\.$#'
identifier: class.notFound
count: 1
path: src/Monolog/Handler/ElasticsearchHandler.php

-
message: "#^Call to method bulk\\(\\) on an unknown class Elasticsearch\\\\Client\\.$#"
message: '#^Call to method bulk\(\) on an unknown class Elasticsearch\\Client\.$#'
identifier: class.notFound
count: 1
path: src/Monolog/Handler/ElasticsearchHandler.php

-
message: "#^Method Monolog\\\\Handler\\\\ElasticsearchHandler\\:\\:createExceptionFromError\\(\\) should return Throwable but returns Elasticsearch\\\\Common\\\\Exceptions\\\\RuntimeException\\.$#"
message: '#^Method Monolog\\Handler\\ElasticsearchHandler\:\:createExceptionFromError\(\) should return Throwable but returns Elasticsearch\\Common\\Exceptions\\RuntimeException\.$#'
identifier: return.type
count: 1
path: src/Monolog/Handler/ElasticsearchHandler.php

-
message: "#^Method Monolog\\\\Handler\\\\ElasticsearchHandler\\:\\:createExceptionFromResponses\\(\\) should return Throwable but returns Elasticsearch\\\\Common\\\\Exceptions\\\\RuntimeException\\.$#"
message: '#^Method Monolog\\Handler\\ElasticsearchHandler\:\:createExceptionFromResponses\(\) should return Throwable but returns Elasticsearch\\Common\\Exceptions\\RuntimeException\.$#'
identifier: return.type
count: 1
path: src/Monolog/Handler/ElasticsearchHandler.php

-
message: "#^Parameter \\$client of method Monolog\\\\Handler\\\\ElasticsearchHandler\\:\\:__construct\\(\\) has invalid type Elasticsearch\\\\Client\\.$#"
message: '#^Parameter \$client of method Monolog\\Handler\\ElasticsearchHandler\:\:__construct\(\) has invalid type Elasticsearch\\Client\.$#'
identifier: class.notFound
count: 2
path: src/Monolog/Handler/ElasticsearchHandler.php

-
message: "#^Property Monolog\\\\Handler\\\\ElasticsearchHandler\\:\\:\\$client has unknown class Elasticsearch\\\\Client as its type\\.$#"
message: '#^Property Monolog\\Handler\\ElasticsearchHandler\:\:\$client has unknown class Elasticsearch\\Client as its type\.$#'
identifier: class.notFound
count: 1
path: src/Monolog/Handler/ElasticsearchHandler.php

-
message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#"
message: '#^Short ternary operator is not allowed\. Use null coalesce operator if applicable or consider using long ternary\.$#'
identifier: ternary.shortNotAllowed
count: 1
path: src/Monolog/Handler/FingersCrossedHandler.php

-
message: "#^Call to method setBody\\(\\) on an unknown class Swift_Message\\.$#"
message: '#^Call to method setBody\(\) on an unknown class Swift_Message\.$#'
identifier: class.notFound
count: 1
path: src/Monolog/Handler/MandrillHandler.php

-
message: "#^Call to method setDate\\(\\) on an unknown class Swift_Message\\.$#"
message: '#^Call to method setDate\(\) on an unknown class Swift_Message\.$#'
identifier: class.notFound
count: 1
path: src/Monolog/Handler/MandrillHandler.php

-
message: "#^Class Swift_Message not found\\.$#"
message: '#^Class Swift_Message not found\.$#'
identifier: class.notFound
count: 2
path: src/Monolog/Handler/MandrillHandler.php

-
message: "#^Cloning object of an unknown class Swift_Message\\.$#"
message: '#^Cloning object of an unknown class Swift_Message\.$#'
identifier: class.notFound
count: 1
path: src/Monolog/Handler/MandrillHandler.php

-
message: "#^Parameter \\$message of method Monolog\\\\Handler\\\\MandrillHandler\\:\\:__construct\\(\\) has invalid type Swift_Message\\.$#"
message: '#^Parameter \$message of method Monolog\\Handler\\MandrillHandler\:\:__construct\(\) has invalid type Swift_Message\.$#'
identifier: class.notFound
count: 3
path: src/Monolog/Handler/MandrillHandler.php

-
message: "#^Property Monolog\\\\Handler\\\\MandrillHandler\\:\\:\\$message has unknown class Swift_Message as its type\\.$#"
message: '#^Property Monolog\\Handler\\MandrillHandler\:\:\$message has unknown class Swift_Message as its type\.$#'
identifier: class.notFound
count: 1
path: src/Monolog/Handler/MandrillHandler.php

-
message: "#^Variable property access on \\$this\\(Monolog\\\\LogRecord\\)\\.$#"
message: '#^Variable property access on \$this\(Monolog\\LogRecord\)\.$#'
identifier: property.dynamicName
count: 4
path: src/Monolog/LogRecord.php

-
message: "#^Cannot assign offset Fiber to WeakMap\\<Fiber\\<mixed, mixed, mixed, mixed\\>, int\\>\\.$#"
message: '#^Cannot assign offset Fiber to WeakMap\<Fiber\<mixed, mixed, mixed, mixed\>, int\>\.$#'
identifier: offsetAssign.dimType
count: 1
path: src/Monolog/Logger.php

-
message: "#^Parameter \\#1 \\$level \\('alert'\\|'critical'\\|'debug'\\|'emergency'\\|'error'\\|'info'\\|'notice'\\|'warning'\\|Monolog\\\\Level\\) of method Monolog\\\\Logger\\:\\:log\\(\\) should be contravariant with parameter \\$level \\(mixed\\) of method Psr\\\\Log\\\\LoggerInterface\\:\\:log\\(\\)$#"
message: '#^Parameter \#1 \$level \(''alert''\|''critical''\|''debug''\|''emergency''\|''error''\|''info''\|''notice''\|''warning''\|Monolog\\Level\) of method Monolog\\Logger\:\:log\(\) should be contravariant with parameter \$level \(mixed\) of method Psr\\Log\\LoggerInterface\:\:log\(\)$#'
identifier: method.childParameterType
count: 1
path: src/Monolog/Logger.php

-
message: "#^Variable property access on \\$this\\(Monolog\\\\Logger\\)\\.$#"
message: '#^Variable property access on \$this\(Monolog\\Logger\)\.$#'
identifier: property.dynamicName
count: 1
path: src/Monolog/Logger.php

-
message: "#^Parameter \\#1 \\$length of function random_bytes expects int\\<1, max\\>, int given\\.$#"
message: '#^Parameter \#1 \$length of function random_bytes expects int\<1, max\>, int given\.$#'
identifier: argument.type
count: 1
path: src/Monolog/Processor/UidProcessor.php
6 changes: 1 addition & 5 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ parameters:
- '#zend_monitor_|ZEND_MONITOR_#'
- '#MongoDB\\(Client|Collection)#'

# Cannot resolve this cleanly as different normalizers return different types but it is safe
- message: '#Return type \(string\) of method Monolog\\Formatter\\LineFormatter::normalizeException\(\) should be compatible with return type \(array\) of method Monolog\\Formatter\\NormalizerFormatter::normalizeException\(\)#'
paths:
- src/Monolog/Formatter/LineFormatter.php

includes:
- phpstan-baseline.neon
- phpstan-ignore-by-php-version.neon.php
- phar://phpstan.phar/conf/bleedingEdge.neon
- vendor/phpstan/phpstan-strict-rules/rules.neon
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnSkippedTests="true"
displayDetailsOnTestsThatTriggerNotices="true"
beStrictAboutTestsThatDoNotTestAnything="false">
<testsuites>
<testsuite name="Monolog Test Suite">
Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Formatter/GelfMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public function format(LogRecord $record): Message
{
$context = $extra = [];
if (isset($record->context)) {
/** @var mixed[] $context */
/** @var array<array<mixed>|bool|float|int|string|null> $context */
$context = parent::normalize($record->context);
}
if (isset($record->extra)) {
/** @var mixed[] $extra */
/** @var array<array<mixed>|bool|float|int|string|null> $extra */
$extra = parent::normalize($record->extra);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Monolog/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ protected function normalize(mixed $data, int $depth = 0): mixed
* Normalizes given exception with or without its own stack trace based on
* `includeStacktraces` property.
*
* @inheritDoc
* @return array<string, string|int|array<string|int|array<string>>>|string
*/
protected function normalizeException(Throwable $e, int $depth = 0): array
{
Expand Down
6 changes: 3 additions & 3 deletions src/Monolog/Formatter/LineFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function format(LogRecord $record): string
if (null === $output) {
$pcreErrorCode = preg_last_error();

throw new \RuntimeException('Failed to run preg_replace: ' . $pcreErrorCode . ' / ' . Utils::pcreLastErrorMessage($pcreErrorCode));
throw new \RuntimeException('Failed to run preg_replace: ' . $pcreErrorCode . ' / ' . preg_last_error_msg());
}
}

Expand Down Expand Up @@ -243,7 +243,7 @@ protected function replaceNewlines(string $str): string
if (null === $str) {
$pcreErrorCode = preg_last_error();

throw new \RuntimeException('Failed to run preg_replace: ' . $pcreErrorCode . ' / ' . Utils::pcreLastErrorMessage($pcreErrorCode));
throw new \RuntimeException('Failed to run preg_replace: ' . $pcreErrorCode . ' / ' . preg_last_error_msg());
}
}

Expand Down Expand Up @@ -309,6 +309,6 @@ private function stacktracesParser(\Throwable $e): string

private function stacktracesParserCustom(string $trace): string
{
return implode("\n", array_filter(array_map($this->stacktracesParser, explode("\n", $trace))));
return implode("\n", array_filter(array_map($this->stacktracesParser, explode("\n", $trace)), fn ($line) => $line !== false && $line !== ''));
}
}
2 changes: 1 addition & 1 deletion src/Monolog/Formatter/MongoDBFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private function legacyGetMongoDbDateTime(\DateTimeInterface $value): UTCDateTim
{
$milliseconds = floor(((float) $value->format('U.u')) * 1000);

$milliseconds = (PHP_INT_SIZE == 8) //64-bit OS?
$milliseconds = (PHP_INT_SIZE === 8) //64-bit OS?
? (int) $milliseconds
: (string) $milliseconds;

Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Formatter/NormalizerFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function setBasePath(string $path = ''): self
*/
protected function normalizeRecord(LogRecord $record): array
{
/** @var array<mixed> $normalized */
/** @var array<mixed[]|scalar|null> $normalized */
$normalized = $this->normalize($record->toArray());

return $normalized;
Expand Down Expand Up @@ -248,7 +248,7 @@ protected function normalize(mixed $data, int $depth = 0): mixed
}

/**
* @return mixed[]
* @return array<string, string|int|array<string|int|array<string>>>|string
*/
protected function normalizeException(Throwable $e, int $depth = 0)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Handler/BrowserConsoleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private static function handleCustomStyles(string $style, string $string): strin
if (null === $style) {
$pcreErrorCode = preg_last_error();

throw new \RuntimeException('Failed to run preg_replace_callback: ' . $pcreErrorCode . ' / ' . Utils::pcreLastErrorMessage($pcreErrorCode));
throw new \RuntimeException('Failed to run preg_replace_callback: ' . $pcreErrorCode . ' / ' . preg_last_error_msg());
}

return $style;
Expand All @@ -256,7 +256,7 @@ private static function handleCustomStyles(string $style, string $string): strin
private static function dump(string $title, array $dict): array
{
$script = [];
$dict = array_filter($dict);
$dict = array_filter($dict, fn ($value) => $value !== null);
if (\count($dict) === 0) {
return $script;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Monolog/Handler/ErrorLogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ class ErrorLogHandler extends AbstractProcessingHandler
public const OPERATING_SYSTEM = 0;
public const SAPI = 4;

/** @var 0|1|3|4 */
protected int $messageType;
protected bool $expandNewlines;

/**
* @param int $messageType Says where the error should go.
* @param 0|1|3|4 $messageType Says where the error should go.
* @param bool $expandNewlines If set to true, newlines in the message will be expanded to be take multiple log entries
*
* @throws \InvalidArgumentException If an unsupported message type is set
Expand Down Expand Up @@ -84,7 +85,7 @@ protected function write(LogRecord $record): void
if ($lines === false) {
$pcreErrorCode = preg_last_error();

throw new \RuntimeException('Failed to preg_split formatted string: ' . $pcreErrorCode . ' / '. Utils::pcreLastErrorMessage($pcreErrorCode));
throw new \RuntimeException('Failed to preg_split formatted string: ' . $pcreErrorCode . ' / '. preg_last_error_msg());
}
foreach ($lines as $line) {
error_log($line, $this->messageType);
Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Handler/MandrillHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ protected function send(string $content, array $records): void
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://mandrillapp.com/api/1.0/messages/send-raw.json');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
'key' => $this->apiKey,
'raw_message' => (string) $message,
Expand Down
2 changes: 1 addition & 1 deletion src/Monolog/Handler/OverflowHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function handle(LogRecord $record): bool
return false === $this->bubble;
}

if ($this->thresholdMap[$level] == 0) {
if ($this->thresholdMap[$level] === 0) {
// This current message is breaking the threshold. Flush the buffer and continue handling the current record
foreach ($this->buffer[$level] ?? [] as $buffered) {
$this->handler->handle($buffered);
Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Handler/SendGridHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ protected function send(string $content, array $records): void

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sendgrid.com/api/mail.send.json');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($message));
Curl\Util::execute($ch, 2);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Handler/Slack/SlackRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function getAttachmentColor(Level $level): string
*/
public function stringify(array $fields): string
{
/** @var array<mixed> $normalized */
/** @var array<array<mixed>|bool|float|int|string|null> $normalized */
$normalized = $this->normalizerFormatter->normalizeValue($fields);

$hasSecondDimension = \count(array_filter($normalized, 'is_array')) > 0;
Expand Down Expand Up @@ -341,7 +341,7 @@ private function generateAttachmentField(string $title, $value): array
*/
private function generateAttachmentFields(array $data): array
{
/** @var array<mixed> $normalized */
/** @var array<array<mixed>|string> $normalized */
$normalized = $this->normalizerFormatter->normalizeValue($data);

$fields = [];
Expand Down
Loading

0 comments on commit 9ab0a2c

Please sign in to comment.