Skip to content

Commit 6711745

Browse files
committed
issue#13, code quality
1 parent cb1c964 commit 6711745

19 files changed

+864
-106
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
clover.xml
22
coveralls-upload.json
3-
phpunit.xml
3+
.phpunit.result.cache
4+
.phpcs-cache
45

56
# Created by .ignore support plugin (hsz.mobi)
67
### JetBrains template
@@ -36,4 +37,4 @@ composer.phar
3637

3738
# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file
3839
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
39-
composer.lock
40+
composer.lock

LICENSE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
MIT License
22

3-
Copyright (c) 2020 Apidemia
3+
Copyright (c) 2023 Apidemia
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

77
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
88

9-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Logging Error Handler for DotKernel
1010
[![GitHub stars](https://img.shields.io/github/stars/dotkernel/dot-errorhandler)](https://github.com/dotkernel/dot-errorhandler/stargazers)
1111
[![GitHub license](https://img.shields.io/github/license/dotkernel/dot-errorhandler)](https://github.com/dotkernel/dot-errorhandler/blob/3.0/LICENSE)
1212

13+
[![SymfonyInsight](https://insight.symfony.com/projects/cf1f8d89-f230-4157-bc8b-7cce20c75454/big.svg)](https://insight.symfony.com/projects/cf1f8d89-f230-4157-bc8b-7cce20c75454)
1314
## Adding the error handler
1415

1516
* Add the composer package:

composer.json

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,48 @@
1919
"mezzio",
2020
"service-manager"
2121
],
22+
"config": {
23+
"sort-packages": true,
24+
"allow-plugins": {
25+
"dealerdirect/phpcodesniffer-composer-installer": true
26+
}
27+
},
2228
"require": {
23-
"php": "~7.4.0 || ~8.0.0 || ~8.1.0",
24-
"dotkernel/dot-log": "^3.2.0",
25-
"laminas/laminas-diactoros": "^2.8.0",
26-
"psr/http-message": "^1.0.1",
29+
"php": "~8.1.0 || ~8.2.0",
30+
"dotkernel/dot-log": "^3.3.0",
31+
"laminas/laminas-diactoros": "^2.25.2",
32+
"laminas/laminas-stratigility": "^3.10.0",
33+
"mezzio/mezzio": "^3.17.0",
34+
"psr/http-message": "^1.1",
35+
"psr/http-server-middleware": "^1.0.2",
2736
"psr/container": "^1.1.2"
2837
},
38+
"require-dev": {
39+
"laminas/laminas-coding-standard": "^2.5",
40+
"mikey179/vfsstream": "^1.6",
41+
"phpunit/phpunit": "^10.2.3",
42+
"vimeo/psalm": "^5.13"
43+
},
2944
"autoload": {
3045
"psr-4": {
3146
"Dot\\ErrorHandler\\": "src/"
3247
}
3348
},
34-
"minimum-stability": "stable"
49+
"autoload-dev": {
50+
"psr-4": {
51+
"Dot\\Tests\\": "tests/"
52+
}
53+
},
54+
"minimum-stability": "stable",
55+
"scripts": {
56+
"check": [
57+
"@cs-check",
58+
"@test"
59+
],
60+
"cs-check": "phpcs",
61+
"cs-fix": "phpcbf",
62+
"test": "phpunit --colors=always",
63+
"test-coverage": "phpunit --colors=always --coverage-clover clover.xml",
64+
"static-analysis": "psalm --shepherd --stats"
65+
}
3566
}

config/error-handling.global.php.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ return [
1313
'loggerEnabled' => true,
1414
'logger' => 'dot-log.default_logger'
1515
]
16-
];
16+
];

phpcs.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
4+
5+
<arg name="basepath" value="."/>
6+
<arg name="cache" value=".phpcs-cache"/>
7+
<arg name="colors"/>
8+
<arg name="extensions" value="php"/>
9+
<arg name="parallel" value="80"/>
10+
11+
<!-- Show progress -->
12+
<arg value="p"/>
13+
14+
<!-- Paths to check -->
15+
<file>config</file>
16+
<file>src</file>
17+
<file>tests</file>
18+
19+
<!-- Include all rules from the Laminas Coding Standard -->
20+
<rule ref="LaminasCodingStandard"/>
21+
</ruleset>

phpunit.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" bootstrap="./vendor/autoload.php" colors="true">
3+
<testsuites>
4+
<testsuite name="dot-errorhandler Test Suite">
5+
<directory>./tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
<coverage/>
9+
<source>
10+
<include>
11+
<directory suffix=".php">./src</directory>
12+
</include>
13+
</source>
14+
</phpunit>

psalm.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="4"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
findUnusedBaselineEntry="true"
9+
findUnusedCode="false"
10+
>
11+
<projectFiles>
12+
<directory name="src" />
13+
<ignoreFiles>
14+
<directory name="vendor" />
15+
</ignoreFiles>
16+
</projectFiles>
17+
</psalm>

src/ConfigProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66

77
class ConfigProvider
88
{
9-
public function __invoke()
9+
public function __invoke(): array
1010
{
1111
return [
1212
'dependencies' => [
13-
'aliases' => [
13+
'aliases' => [
1414
ErrorHandlerInterface::class => ErrorHandler::class,
1515
],
1616
'factories' => [
1717
LogErrorHandler::class => LogErrorHandlerFactory::class,
18-
ErrorHandler::class => ErrorHandlerFactory::class,
19-
]
18+
ErrorHandler::class => ErrorHandlerFactory::class,
19+
],
2020
],
2121
];
2222
}

src/ErrorHandler.php

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
namespace Dot\ErrorHandler;
66

77
use ErrorException;
8+
use Laminas\Stratigility\Middleware\ErrorResponseGenerator;
89
use Psr\Http\Message\ResponseInterface;
910
use Psr\Http\Message\ServerRequestInterface;
1011
use Psr\Http\Server\MiddlewareInterface;
1112
use Psr\Http\Server\RequestHandlerInterface;
1213
use Throwable;
13-
use Laminas\Stratigility\Exception\MissingResponseException;
1414

1515
use function error_reporting;
1616
use function in_array;
@@ -65,19 +65,11 @@
6565
*/
6666
class ErrorHandler implements MiddlewareInterface, ErrorHandlerInterface
6767
{
68-
/**
69-
* @var callable[]
70-
*/
71-
private $listeners = [];
72-
73-
/**
74-
* @var callable Routine that will generate the error response.
75-
*/
68+
/** @var callable[] */
69+
private array $listeners = [];
70+
/** @var callable Routine that will generate the error response. */
7671
private $responseGenerator;
77-
78-
/**
79-
* @var callable
80-
*/
72+
/** @var callable */
8173
private $responseFactory;
8274

8375
/**
@@ -87,9 +79,9 @@ class ErrorHandler implements MiddlewareInterface, ErrorHandlerInterface
8779
* @param null|callable $responseGenerator Callback that will generate the final
8880
* error response; if none is provided, ErrorResponseGenerator is used.
8981
*/
90-
public function __construct(callable $responseFactory, callable $responseGenerator = null)
82+
public function __construct(callable $responseFactory, ?callable $responseGenerator = null)
9183
{
92-
$this->responseFactory = function () use ($responseFactory) : ResponseInterface {
84+
$this->responseFactory = function () use ($responseFactory): ResponseInterface {
9385
return $responseFactory();
9486
};
9587
$this->responseGenerator = $responseGenerator ?: new ErrorResponseGenerator();
@@ -108,7 +100,7 @@ public function __construct(callable $responseFactory, callable $responseGenerat
108100
* listeners are ignored; use listeners for reporting purposes
109101
* only.
110102
*/
111-
public function attachListener(callable $listener) : void
103+
public function attachListener(callable $listener): void
112104
{
113105
if (in_array($listener, $this->listeners, true)) {
114106
return;
@@ -130,16 +122,12 @@ public function attachListener(callable $listener) : void
130122
* and returned instead; otherwise, the response returned by $next is
131123
* used.
132124
*/
133-
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
125+
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
134126
{
135127
set_error_handler($this->createErrorHandler());
136128

137129
try {
138130
$response = $handler->handle($request);
139-
140-
if (! $response instanceof ResponseInterface) {
141-
throw new MissingResponseException('Application did not return a response');
142-
}
143131
} catch (Throwable $e) {
144132
$response = $this->handleThrowable($e, $request);
145133
}
@@ -156,10 +144,10 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
156144
* triggers all listeners with the same arguments (but using the response
157145
* returned from createErrorResponse()), and then returns the response.
158146
*/
159-
private function handleThrowable(Throwable $e, ServerRequestInterface $request) : ResponseInterface
147+
public function handleThrowable(Throwable $e, ServerRequestInterface $request): ResponseInterface
160148
{
161149
$generator = $this->responseGenerator;
162-
$response = $generator($e, $request, ($this->responseFactory)());
150+
$response = $generator($e, $request, ($this->responseFactory)());
163151
$this->triggerListeners($e, $request, $response);
164152
return $response;
165153
}
@@ -169,12 +157,12 @@ private function handleThrowable(Throwable $e, ServerRequestInterface $request)
169157
*
170158
* Only raises exceptions for errors that are within the error_reporting mask.
171159
*/
172-
private function createErrorHandler() : callable
160+
public function createErrorHandler(): callable
173161
{
174162
/**
175163
* @throws ErrorException if error is not within the error_reporting mask.
176164
*/
177-
return function (int $errno, string $errstr, string $errfile, int $errline) : void {
165+
return function (int $errno, string $errstr, string $errfile, int $errline): void {
178166
if (! (error_reporting() & $errno)) {
179167
// error_reporting does not include this error
180168
return;
@@ -187,11 +175,11 @@ private function createErrorHandler() : callable
187175
/**
188176
* Trigger all error listeners.
189177
*/
190-
private function triggerListeners(
178+
public function triggerListeners(
191179
Throwable $error,
192180
ServerRequestInterface $request,
193181
ResponseInterface $response
194-
) : void {
182+
): void {
195183
foreach ($this->listeners as $listener) {
196184
$listener($error, $request, $response);
197185
}

0 commit comments

Comments
 (0)