Skip to content

Commit

Permalink
Merge pull request #9 from juliangut/3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Oct 19, 2023
2 parents 612b3c3 + 5b9930a commit c1c0dd6
Show file tree
Hide file tree
Showing 37 changed files with 532 additions and 543 deletions.
24 changes: 5 additions & 19 deletions .github/workflows/code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
coverage: none

- name: Composer install
uses: ramsey/composer-install@v2
with:
composer-options: "--prefer-dist"

- name: Install Symplify easy-ci
run: composer require --dev symplify/easy-ci:11.1.5 --no-interaction --no-progress --ansi --prefer-stable --prefer-dist

- id: output_php
run: echo "matrix=$(vendor/bin/easy-ci php-versions-json)" >> $GITHUB_OUTPUT
- id: composer-versions-matrix
uses: WyriHaximus/github-action-composer-php-versions-in-range@v1

outputs:
php: ${{ steps.output_php.outputs.matrix }}
php: ${{ steps.composer-versions-matrix.outputs.version }}
extensions: ${{ steps.composer-versions-matrix.outputs.extensions }}

code_analysis:
needs: prepare_env
Expand All @@ -53,6 +41,7 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: ${{ join(fromJson(needs.prepare_env.outputs.extensions), ', ') }}
coverage: none

- name: Composer install
Expand All @@ -66,9 +55,6 @@ jobs:
- name: Run check style
run: make lint-ecs

- name: Run copy/paste detection
run: make qa-phpcpd

- name: Run mess detection
run: make qa-phpmd

Expand Down
21 changes: 5 additions & 16 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,12 @@ jobs:
steps:
- uses: actions/checkout@v3

- uses: shivammathur/setup-php@v2
with:
php-version: 7.4
coverage: none

- name: Composer install
uses: ramsey/composer-install@v2
with:
composer-options: "--prefer-dist"

- name: Install Symplify easy-ci
run: composer require --dev symplify/easy-ci:11.1.5 --no-interaction --no-progress --ansi --prefer-stable --prefer-dist

- id: output_php
run: echo "matrix=$(vendor/bin/easy-ci php-versions-json)" >> $GITHUB_OUTPUT
- id: composer-versions-matrix
uses: WyriHaximus/github-action-composer-php-versions-in-range@v1

outputs:
php: ${{ steps.output_php.outputs.matrix }}
php: ${{ steps.composer-versions-matrix.outputs.version }}
extensions: ${{ steps.composer-versions-matrix.outputs.extensions }}

unit_tests:
needs: prepare_env
Expand All @@ -53,6 +41,7 @@ jobs:
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: ${{ join(fromJson(needs.prepare_env.outputs.extensions), ', ') }}
coverage: none

- name: Composer install
Expand Down
7 changes: 1 addition & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ fix:
make --no-print-directory fix-ecs


.PHONY: qa-phpcpd
qa-phpcpd:
vendor/bin/phpcpd src tests

.PHONY: qa-phpmd
qa-phpmd:
vendor/bin/phpmd src,tests ansi unusedcode,naming,design,controversial,codesize
Expand All @@ -38,15 +34,14 @@ qa-phpmnd:

.PHONY: qa-compatibility
qa-compatibility:
vendor/bin/phpcs --standard=PHPCompatibility --runtime-set testVersion 8.1- src tests
vendor/bin/phpcs --standard=PHPCompatibility --runtime-set testVersion 8.2- src tests

.PHONY: qa-phpstan
qa-phpstan:
vendor/bin/phpstan analyse --memory-limit=2G --no-progress

.PHONY: qa
qa:
make --no-print-directory qa-phpcpd && \
make --no-print-directory qa-phpmd && \
make --no-print-directory qa-phpmnd && \
make --no-print-directory qa-compatibility && \
Expand Down
12 changes: 4 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![PHP version](https://img.shields.io/badge/PHP-%3E%3D7.4-8892BF.svg?style=flat-square)](http://php.net)
[![PHP version](https://img.shields.io/badge/PHP-%3E%3D8.0-8892BF.svg?style=flat-square)](http://php.net)
[![Latest Version](https://img.shields.io/packagist/v/juliangut/slim-exception.svg?style=flat-square)](https://packagist.org/packages/juliangut/slim-exception)
[![License](https://img.shields.io/github/license/juliangut/slim-exception.svg?style=flat-square)](https://github.com/juliangut/slim-exception/blob/master/LICENSE)

Expand Down Expand Up @@ -112,14 +112,10 @@ $app->run($request);
trigger_error('This is embarrassing', \E_USER_ERROR);
```

## Upgrade from 1.x
## Upgrade from 2.x

Overall usage has been drastically simplified due to Slim 4 migration to exception based error handling, basically what slim-exception was doing in 1.x.

* Minimum Slim version is now 4.11
* ExceptionManager has been removed as its functionality is now integrated into Slim
* Exceptions no longer uses juliangut/http-exception, and thus they have no identifier
* Global error/exception handling has been moved from a trait (meant for App) to its own class ExceptionHandler
* Minimum PHP version is now 8.0
* Minimum Whoops version is now 2.15 as custom Inspector has been removed in favor of Whoop's frame filters

## Contributing

Expand Down
33 changes: 14 additions & 19 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.4|^8.0",
"php": "^8.0",
"ext-mbstring": "*",
"slim/slim": "^4.11",
"willdurand/negotiation": "^3.0"
Expand All @@ -32,27 +32,23 @@
"ext-dom": "*",
"ext-json": "*",
"ext-simplexml": "*",
"grifart/phpstan-oneline": "~0.4",
"filp/whoops": "^2.15",
"infection/infection": "~0.24",
"juliangut/easy-coding-standard-config": "^1.0",
"laminas/laminas-diactoros": "^2.2",
"overtrue/phplint": "^3.0|^4.0",
"infection/infection": "~0.25|~0.27",
"juliangut/easy-coding-standard-config": "^1.12",
"juliangut/phpstan-config": "^1.1",
"laminas/laminas-diactoros": "^2.20",
"overtrue/phplint": "^4.0|^5.0|^6.0",
"phpcompatibility/php-compatibility": "^9.3",
"phpmd/phpmd": "^2.10",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-strict-rules": "^1.0",
"phpunit/phpunit": "^9.5",
"povils/phpmnd": "^2.5",
"phpmd/phpmd": "^2.13",
"phpstan/phpstan": "^1.11",
"phpunit/phpunit": "^9.6.13|^10.3",
"povils/phpmnd": "^3.2",
"roave/security-advisories": "dev-master",
"sebastian/phpcpd": "^6.0",
"thecodingmachine/phpstan-strict-rules": "^1.0"
"symfony/var-dumper": "^6.0.4"
},
"suggest": {
"filp/whoops": "Enhance development error reporting and production stack-traces",
"symfony/var-dumper": "Help filp/whoops enhancing stack-traces"
"filp/whoops": "Enhance development error reporting (>=2.15)",
"symfony/var-dumper": "Help filp/whoops enhancing stack-traces (>=5.1)"
},
"autoload": {
"psr-4": {
Expand All @@ -69,8 +65,7 @@
"sort-packages": true,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"infection/extension-installer": true,
"phpstan/extension-installer": true
"infection/extension-installer": true
}
}
}
10 changes: 3 additions & 7 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

declare(strict_types=1);

use Jgut\ECS\Config\ConfigSet74;
use Jgut\ECS\Config\ConfigSet80;
use SlevomatCodingStandard\Sniffs\Exceptions\ReferenceThrowableOnlySniff;
use Symplify\EasyCodingStandard\Config\ECSConfig;

Expand All @@ -31,15 +31,11 @@
__DIR__ . '/tests',
]);

$configSet = new ConfigSet74();

$configSet
(new ConfigSet80())
->setHeader($header)
->enablePhpUnitRules()
->setAdditionalSkips([
ReferenceThrowableOnlySniff::class . '.ReferencedGeneralException' => [
__DIR__ . '/src/ExceptionHandler.php',
],
ReferenceThrowableOnlySniff::class . '.ReferencedGeneralException' => __DIR__ . '/src/ExceptionHandler.php',
])
->configure($ecsConfig);
};
5 changes: 2 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
includes:
- vendor/phpstan/phpstan/conf/bleedingEdge.neon
- %rootDir%/../../juliangut/phpstan-config/phpstan-phpunit.neon

parameters:
level: max
checkMissingCallableSignature: true
tipsOfTheDay: false
errorFormat: compact
parallel:
maximumNumberOfProcesses: 7
paths:
- src
typeAliases:
TraceLine: 'array{file: ?string, line: ?int, function: ?string, class: ?string, args: array<mixed>}'
ExceptionData: 'array{type: class-string<Throwable>, message: string, code: int, file: string, line: int, trace?: array<TraceLine>}'
ignoreErrors:
- message: '/^Call to function is_subclass_of\(\) with Whoops\\Handler\\HandlerInterface and .Jgut\\\\Slim\\\\Exception\\\\Whoops\\\\Renderer\\\\HtmlRenderer. will always evaluate to false\.$/'
path: src/Whoops/Handler/ErrorHandler.php
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd"
bootstrap="vendor/autoload.php"
beStrictAboutChangesToGlobalState="true"
beStrictAboutOutputDuringTests="true"
Expand Down
48 changes: 19 additions & 29 deletions src/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
namespace Jgut\Slim\Exception;

use ErrorException;
use Exception;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use ReflectionProperty;
use ReflectionClass;
use Slim\Exception\HttpException;
use Slim\Exception\HttpInternalServerErrorException;
use Slim\Interfaces\ErrorHandlerInterface;
Expand All @@ -26,29 +25,13 @@

class ExceptionHandler
{
protected ServerRequestInterface $request;

protected ErrorHandlerInterface $errorHandler;

protected bool $displayErrorDetails;

protected bool $logErrors;

protected bool $logErrorDetails;

public function __construct(
ServerRequestInterface $request,
ErrorHandlerInterface $errorHandler,
bool $displayErrorDetails,
bool $logErrors,
bool $logErrorDetails
) {
$this->request = $request;
$this->errorHandler = $errorHandler;
$this->displayErrorDetails = $displayErrorDetails;
$this->logErrors = $logErrors;
$this->logErrorDetails = $logErrorDetails;
}
protected ServerRequestInterface $request,
protected ErrorHandlerInterface $errorHandler,
protected bool $displayErrorDetails,
protected bool $logErrors,
protected bool $logErrorDetails,
) {}

/**
* Register exception handling.
Expand Down Expand Up @@ -110,7 +93,7 @@ public function handleShutdown(): void

// @codeCoverageIgnoreStart
if (!\defined('PHPUNIT_TEST')) {
exit;
exit; // @phpstan-ignore-line
}
// @codeCoverageIgnoreEnd
}
Expand Down Expand Up @@ -150,9 +133,16 @@ private function getFatalException(array $error): HttpException

$trace = $this->getBackTrace();
if (\count($trace) !== 0) {
$reflection = new ReflectionProperty(Exception::class, 'trace');
$reflection->setAccessible(true);
$reflection->setValue($exception, $trace);
$reflectionClass = new ReflectionClass($exception);
while ($reflectionClass->getParentClass() !== false) {
$reflectionClass = $reflectionClass->getParentClass();
}

if ($reflectionClass->hasProperty('trace')) {
$reflectionProperty = $reflectionClass->getProperty('trace');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($exception, $trace);
}
}

return $exception;
Expand All @@ -171,7 +161,7 @@ private function getBackTrace(): array
fn(array $frame): array => $this->normalizeFrame($frame),
xdebug_get_function_stack(),
);
} catch (ErrorException $exception) {
} catch (ErrorException) {
// @ignoreException
}

Expand Down
18 changes: 7 additions & 11 deletions src/Handler/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ class ErrorHandler extends SlimErrorHandler
\E_USER_DEPRECATED => LogLevel::WARNING,
];

protected Negotiator $negotiator;

/**
* @var ErrorRendererInterface|string|callable(Throwable, bool): string
*/
Expand All @@ -88,16 +86,14 @@ class ErrorHandler extends SlimErrorHandler
public function __construct(
CallableResolverInterface $callableResolver,
ResponseFactoryInterface $responseFactory,
Negotiator $negotiator,
?LoggerInterface $logger = null
protected Negotiator $negotiator,
?LoggerInterface $logger = null,
) {
$this->negotiator = $negotiator;

parent::__construct($callableResolver, $responseFactory, $logger);
}

/**
* @param array<string, string|ErrorRendererInterface> $errorRenderers
* @param array<string, ErrorRendererInterface|class-string<ErrorRendererInterface>> $errorRenderers
*/
public function setErrorRenderers(array $errorRenderers): void
{
Expand All @@ -109,9 +105,9 @@ public function setErrorRenderers(array $errorRenderers): void
}

/**
* @param string|ErrorRendererInterface $errorRenderer
* @param ErrorRendererInterface|class-string<ErrorRendererInterface> $errorRenderer
*/
public function setErrorRenderer(string $contentType, $errorRenderer): void
public function setErrorRenderer(string $contentType, ErrorRendererInterface|string $errorRenderer): void
{
$this->errorRenderers[$contentType] = $errorRenderer;
}
Expand All @@ -133,12 +129,12 @@ protected function determineContentType(ServerRequestInterface $request): ?strin
if ($selected instanceof BaseAccept) {
$contentType = $selected->getType();
}
} catch (NegotiateException $exception) {
} catch (NegotiateException) {
// @ignoreException
}
}

if (mb_strpos($contentType, '/*+') !== false) {
if (str_contains($contentType, '/*+')) {
$contentType = str_replace('/*+', '/', $contentType);
}

Expand Down
Loading

0 comments on commit c1c0dd6

Please sign in to comment.