Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump PHP to 8.1 #138

Merged
merged 7 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/bc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.0']
['8.1']
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['8.0', '8.1', '8.2', '8.3']
['8.1', '8.2', '8.3']
2 changes: 1 addition & 1 deletion .github/workflows/composer-require-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.0', '8.1', '8.2', '8.3']
['8.1', '8.2', '8.3']
8 changes: 0 additions & 8 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,3 @@ jobs:
['ubuntu-latest']
php: >-
['8.1', '8.2', '8.3']
psalm80:
uses: yiisoft/actions/.github/workflows/psalm.yml@master
with:
psalm-config: psalm80.xml
os: >-
['ubuntu-latest']
php: >-
['8.0']
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ Thumbs.db
composer.phar
composer.lock

# phpunit itself is not needed
phpunit.phar
# local phpunit config
# PHPUnit
/phpunit.phar
/phpunit.xml
# phpunit cache
.phpunit.result.cache
/.phpunit.cache
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class (@olegbaturin)
- Chg #137: Add separate parameters for each of `HtmlRenderer` settings in constructor. Mark `$settings` parameter as
deprecated (@vjik)
- Enh #138: Raise the minimum PHP version to 8.1 and minor refactoring (@vjik)

## 3.3.0 July 11, 2024

Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ The package provides advanced error handling. The features are:

## Requirements

- PHP 8.0 or higher.
- PHP 8.1 or higher.
- `DOM` PHP extension.
- `JSON` PHP extension.
- `mbstring` PHP extension.

## Installation
Expand Down
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"ext-dom": "*",
"ext-json": "*",
"ext-mbstring": "*",
"alexkart/curl-builder": "^1.0",
"cebe/markdown": "^1.2",
Expand All @@ -48,16 +47,16 @@
"yiisoft/injector": "^1.0"
},
"require-dev": {
"httpsoft/http-message": "^1.0.9",
"maglnet/composer-require-checker": "^4.4",
"phpunit/phpunit": "^9.5",
"httpsoft/http-message": "^1.1.6",
"maglnet/composer-require-checker": "^4.7.1",
"phpunit/phpunit": "^10.5.44",
"psr/event-dispatcher": "^1.0",
"rector/rector": "^2.0",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.25",
"yiisoft/di": "^1.1",
"yiisoft/test-support": "^3.0"
"rector/rector": "^2.0.7",
"roave/infection-static-analysis-plugin": "^1.35",
"spatie/phpunit-watcher": "^1.24",
"vimeo/psalm": "^5.26.1|^6",
"yiisoft/di": "^1.3",
"yiisoft/test-support": "^3.0.1"
},
"autoload": {
"psr-4": {
Expand All @@ -79,6 +78,7 @@
},
"config": {
"sort-packages": true,
"bump-after-update": "dev",
"allow-plugins": {
"infection/extension-installer": true,
"composer/package-versions-deprecated": true
Expand Down
27 changes: 14 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
executionOrder="random"
failOnRisky="true"
failOnWarning="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true"
colors="true"
displayDetailsOnPhpunitDeprecations="true"
>
<php>
<ini name="error_reporting" value="-1"/>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
Expand All @@ -22,9 +23,9 @@
</testsuite>
</testsuites>

<coverage>
<source>
<include>
<directory>./src</directory>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</source>
</phpunit>
19 changes: 0 additions & 19 deletions psalm80.xml

This file was deleted.

25 changes: 10 additions & 15 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,19 @@
use Rector\Config\RectorConfig;
use Rector\Php71\Rector\FuncCall\RemoveExtraParametersRector;
use Rector\Php74\Rector\Closure\ClosureToArrowFunctionRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Php81\Rector\FuncCall\NullToStrictStringFuncCallArgRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
return RectorConfig::configure()
->withPaths([
__DIR__ . '/src',
__DIR__ . '/tests',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80,
]);

$rectorConfig->skip([
])
->withPhpSets(php81: true)
->withRules([
InlineConstructorDefaultToPropertyRector::class,
])
->withSkip([
ClosureToArrowFunctionRector::class,
NullToStrictStringFuncCallArgRector::class,
RemoveExtraParametersRector::class,
]);
};
4 changes: 2 additions & 2 deletions src/CompositeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ final class CompositeException extends Exception
/**
* @var Throwable[]
*/
private array $rest;
private readonly array $rest;

public function __construct(
private Throwable $first,
private readonly Throwable $first,
Throwable ...$rest,
) {
$this->rest = $rest;
Expand Down
4 changes: 2 additions & 2 deletions src/ErrorData.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ final class ErrorData implements Stringable
* @param array<string, string|string[]> $headers The headers to add to the response.
*/
public function __construct(
private string $content,
private array $headers = [],
private readonly string $content,
private readonly array $headers = [],
) {
}

Expand Down
8 changes: 4 additions & 4 deletions src/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
* @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last.
*/
public function __construct(
private LoggerInterface $logger,
private ThrowableRendererInterface $defaultRenderer,
private ?EventDispatcherInterface $eventDispatcher = null,
private int $exitShutdownHandlerDepth = 2
private readonly LoggerInterface $logger,
private readonly ThrowableRendererInterface $defaultRenderer,
private readonly ?EventDispatcherInterface $eventDispatcher = null,
private readonly int $exitShutdownHandlerDepth = 2

Check warning on line 53 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ * @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for error events. * @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last. */ - public function __construct(private readonly LoggerInterface $logger, private readonly ThrowableRendererInterface $defaultRenderer, private readonly ?EventDispatcherInterface $eventDispatcher = null, private readonly int $exitShutdownHandlerDepth = 2) + public function __construct(private readonly LoggerInterface $logger, private readonly ThrowableRendererInterface $defaultRenderer, private readonly ?EventDispatcherInterface $eventDispatcher = null, private readonly int $exitShutdownHandlerDepth = 1) { } /**

Check warning on line 53 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ * @param EventDispatcherInterface|null $eventDispatcher Event dispatcher for error events. * @param int $exitShutdownHandlerDepth Depth of the exit() shutdown handler to ensure it's executed last. */ - public function __construct(private readonly LoggerInterface $logger, private readonly ThrowableRendererInterface $defaultRenderer, private readonly ?EventDispatcherInterface $eventDispatcher = null, private readonly int $exitShutdownHandlerDepth = 2) + public function __construct(private readonly LoggerInterface $logger, private readonly ThrowableRendererInterface $defaultRenderer, private readonly ?EventDispatcherInterface $eventDispatcher = null, private readonly int $exitShutdownHandlerDepth = 3) { } /**
) {
}

Expand All @@ -68,7 +68,7 @@
$renderer ??= $this->defaultRenderer;

try {
$this->logger->error($t->getMessage(), ['throwable' => $t]);

Check warning on line 71 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ { $renderer ??= $this->defaultRenderer; try { - $this->logger->error($t->getMessage(), ['throwable' => $t]); + $this->logger->error($t->getMessage(), []); return $this->debug ? $renderer->renderVerbose($t, $request) : $renderer->render($t, $request); } catch (Throwable $t) { return new ErrorData((string) $t);

Check warning on line 71 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { $renderer ??= $this->defaultRenderer; try { - $this->logger->error($t->getMessage(), ['throwable' => $t]); + return $this->debug ? $renderer->renderVerbose($t, $request) : $renderer->render($t, $request); } catch (Throwable $t) { return new ErrorData((string) $t);
return $this->debug ? $renderer->renderVerbose($t, $request) : $renderer->render($t, $request);
} catch (Throwable $t) {
return new ErrorData((string) $t);
Expand Down Expand Up @@ -108,14 +108,14 @@
return;
}

if ($this->memoryReserveSize > 0) {

Check warning on line 111 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "GreaterThan": --- Original +++ New @@ @@ if ($this->enabled) { return; } - if ($this->memoryReserveSize > 0) { + if ($this->memoryReserveSize >= 0) { $this->memoryReserve = str_repeat('x', $this->memoryReserveSize); } $this->initializeOnce();

Check warning on line 111 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "GreaterThanNegotiation": --- Original +++ New @@ @@ if ($this->enabled) { return; } - if ($this->memoryReserveSize > 0) { + if ($this->memoryReserveSize <= 0) { $this->memoryReserve = str_repeat('x', $this->memoryReserveSize); } $this->initializeOnce();
$this->memoryReserve = str_repeat('x', $this->memoryReserveSize);
}

$this->initializeOnce();

Check warning on line 115 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ if ($this->memoryReserveSize > 0) { $this->memoryReserve = str_repeat('x', $this->memoryReserveSize); } - $this->initializeOnce(); + // Handles throwable that isn't caught otherwise, echo output and exit. set_exception_handler(function (Throwable $t) : void { if (!$this->enabled) {

// Handles throwable that isn't caught otherwise, echo output and exit.
set_exception_handler(function (Throwable $t): void {

Check warning on line 118 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FunctionCallRemoval": --- Original +++ New @@ @@ $this->memoryReserve = str_repeat('x', $this->memoryReserveSize); } $this->initializeOnce(); - // Handles throwable that isn't caught otherwise, echo output and exit. - set_exception_handler(function (Throwable $t) : void { - if (!$this->enabled) { - return; - } - $this->renderThrowableAndTerminate($t); - }); + // Handles PHP execution errors such as warnings and notices. set_error_handler(function (int $severity, string $message, string $file, int $line) : bool { if (!$this->enabled) {
if (!$this->enabled) {
return;
}
Expand All @@ -126,10 +126,10 @@
// Handles PHP execution errors such as warnings and notices.
set_error_handler(function (int $severity, string $message, string $file, int $line): bool {
if (!$this->enabled) {
return false;

Check warning on line 129 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "FalseValue": --- Original +++ New @@ @@ // Handles PHP execution errors such as warnings and notices. set_error_handler(function (int $severity, string $message, string $file, int $line) : bool { if (!$this->enabled) { - return false; + return true; } if (!(error_reporting() & $severity)) { // This error code is not included in error_reporting.
}

if (!(error_reporting() & $severity)) {

Check warning on line 132 in src/ErrorHandler.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "BitwiseAnd": --- Original +++ New @@ @@ if (!$this->enabled) { return false; } - if (!(error_reporting() & $severity)) { + if (!(error_reporting() | $severity)) { // This error code is not included in error_reporting. return true; }
// This error code is not included in error_reporting.
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Event/ApplicationError.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
*/
final class ApplicationError
{
public function __construct(private Throwable $throwable)
{
public function __construct(
private readonly Throwable $throwable,
) {
}

public function getThrowable(): Throwable
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ErrorException.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ErrorException extends \ErrorException implements FriendlyExceptionInterfa
];

/** @psalm-param DebugBacktraceType $backtrace */
public function __construct(string $message = '', int $code = 0, int $severity = 1, string $filename = __FILE__, int $line = __LINE__, Exception $previous = null, private array $backtrace = [])
public function __construct(string $message = '', int $code = 0, int $severity = 1, string $filename = __FILE__, int $line = __LINE__, Exception $previous = null, private readonly array $backtrace = [])
{
parent::__construct($message, $code, $severity, $filename, $line, $previous);
$this->addXDebugTraceToFatalIfAvailable();
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/ErrorCatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
final class ErrorCatcher implements MiddlewareInterface
{
public function __construct(
private ThrowableResponseFactoryInterface $throwableResponseFactory,
private ?EventDispatcherInterface $eventDispatcher = null,
private readonly ThrowableResponseFactoryInterface $throwableResponseFactory,
private readonly ?EventDispatcherInterface $eventDispatcher = null,
) {
}

Expand Down
8 changes: 4 additions & 4 deletions src/Middleware/ExceptionResponder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ final class ExceptionResponder implements MiddlewareInterface
* catching exceptions that can be thrown in the process of body generation.
*/
public function __construct(
private array $exceptionMap,
private ResponseFactoryInterface $responseFactory,
private Injector $injector,
private bool $checkResponseBody = false,
private readonly array $exceptionMap,
private readonly ResponseFactoryInterface $responseFactory,
private readonly Injector $injector,
private readonly bool $checkResponseBody = false,
) {
}

Expand Down
14 changes: 7 additions & 7 deletions src/Renderer/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,36 +51,36 @@
*/
final class HtmlRenderer implements ThrowableRendererInterface
{
private GithubMarkdown $markdownParser;
private readonly GithubMarkdown $markdownParser;

/**
* @var string The full path to the default template directory.
*/
private string $defaultTemplatePath;
private readonly string $defaultTemplatePath;

/**
* @var string The full path of the template file for rendering exceptions without call stack information.
*
* This template should be used in production.
*/
private string $template;
private readonly string $template;

/**
* @var string The full path of the template file for rendering exceptions with call stack information.
*
* This template should be used in development.
*/
private string $verboseTemplate;
private readonly string $verboseTemplate;

/**
* @var int The maximum number of source code lines to be displayed. Defaults to 19.
*/
private int $maxSourceLines;
private readonly int $maxSourceLines;

/**
* @var int The maximum number of trace source code lines to be displayed. Defaults to 13.
*/
private int $maxTraceLines;
private readonly int $maxTraceLines;

/**
* @var string|null The trace header line with placeholders to be be substituted. Defaults to null.
Expand All @@ -94,7 +94,7 @@ final class HtmlRenderer implements ThrowableRendererInterface
* <a href="ide://open?file={file}&line={line}">{icon}</a>
* ```
*/
private ?string $traceHeaderLine;
private readonly ?string $traceHeaderLine;

/**
* @var string[]|null The list of vendor paths is determined automatically.
Expand Down
3 changes: 3 additions & 0 deletions tests/ErrorHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\ErrorHandler\Tests;

use PHPUnit\Framework\Attributes\WithoutErrorHandler;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use RuntimeException;
Expand Down Expand Up @@ -85,6 +86,7 @@ public function testHandleThrowableWithoutExposedDetailsCallsRender(): void
$this->errorHandler->handle($throwable);
}

#[WithoutErrorHandler]
public function testHandleError(): void
{
$array = [];
Expand All @@ -94,6 +96,7 @@ public function testHandleError(): void
$this->errorHandler->unregister();
}

#[WithoutErrorHandler]
public function testHandleErrorWithCatching(): void
{
$this->errorHandler->register();
Expand Down
Loading
Loading