Skip to content

Commit 487b50a

Browse files
authored
Flag to not send emails for 404 (#11)
* Flag to not send emails for 404 * YML fix
1 parent ef6bf5d commit 487b50a

9 files changed

+114
-82
lines changed

.github/dependabot.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
version: 2
22

33
updates:
4+
45
- package-ecosystem: "composer"
56
directory: "/"
67
schedule:
@@ -9,5 +10,15 @@ updates:
910
include: "scope"
1011
prefix: "composer"
1112
labels:
12-
- "dependency-update"
13+
- "enhancement"
1314
versioning-strategy: "widen"
15+
16+
- package-ecosystem: "github-actions"
17+
directory: "/"
18+
schedule:
19+
interval: "weekly"
20+
commit-message:
21+
include: "scope"
22+
prefix: "github-actions"
23+
labels:
24+
- "enhancement"

.github/workflows/integrate.yaml

+19-45
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
strategy:
1616
matrix:
1717
php-version:
18-
- "8.0"
18+
- "8.1"
1919

2020
steps:
2121
- name: "Checkout"
@@ -63,16 +63,21 @@ jobs:
6363
matrix:
6464
php-version:
6565
- "8.0"
66-
- "8.1"
67-
66+
code-coverage:
67+
- "none"
68+
include:
69+
- php-version: "8.1"
70+
code-coverage: "pcov"
71+
72+
6873
steps:
6974
- name: "Checkout"
7075
uses: "actions/checkout@v2"
7176

7277
- name: "Install PHP"
7378
uses: "shivammathur/setup-php@v2"
7479
with:
75-
coverage: "none"
80+
coverage: "${{ matrix.code-coverage }}"
7681
php-version: "${{ matrix.php-version }}"
7782
ini-values: zend.assertions=1
7883

@@ -91,53 +96,21 @@ jobs:
9196
run: "composer update --no-interaction --no-progress"
9297

9398
- name: "Run tests"
94-
timeout-minutes: 3
99+
if: ${{ matrix.code-coverage == 'none' }}
100+
timeout-minutes: 1
95101
run: "vendor/bin/phpunit --no-coverage --no-logging"
96102

97-
code-coverage:
98-
name: "Code Coverage"
99-
100-
runs-on: "ubuntu-latest"
101-
102-
strategy:
103-
matrix:
104-
php-version:
105-
- "8.0"
106-
107-
steps:
108-
- name: "Checkout"
109-
uses: "actions/checkout@v2"
110-
111-
- name: "Install PHP"
112-
uses: "shivammathur/setup-php@v2"
113-
with:
114-
coverage: "pcov"
115-
php-version: "${{ matrix.php-version }}"
116-
ini-values: zend.assertions=1
117-
118-
- name: "Get composer cache directory"
119-
id: composercache
120-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
121-
122-
- name: "Cache dependencies"
123-
uses: actions/cache@v2
124-
with:
125-
path: ${{ steps.composercache.outputs.dir }}
126-
key: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-${{ hashFiles('**/composer.json') }}
127-
restore-keys: ${{ runner.os }}-php-${{ matrix.php-version }}-${{ matrix.dependencies }}-composer-
128-
129-
- name: "Install dependencies"
130-
run: "composer update --no-interaction --no-progress"
131-
132103
- name: "Run tests"
133-
timeout-minutes: 3
134-
run: "vendor/bin/phpunit --coverage-clover=coverage.xml"
104+
if: ${{ matrix.code-coverage != 'none' }}
105+
timeout-minutes: 1
106+
run: "vendor/bin/phpunit"
135107

136108
- name: "Send code coverage report to Codecov.io"
109+
if: ${{ matrix.code-coverage != 'none' }}
137110
uses: codecov/codecov-action@v1
138111
with:
139112
token: ${{ secrets.CODECOV_TOKEN }}
140-
file: ./coverage.xml
113+
file: ./coverage/clover.xml
141114
fail_ci_if_error: true
142115

143116
coding-standards:
@@ -148,7 +121,7 @@ jobs:
148121
strategy:
149122
matrix:
150123
php-version:
151-
- "8.0"
124+
- "8.1"
152125

153126
steps:
154127
- name: "Checkout"
@@ -186,7 +159,7 @@ jobs:
186159
strategy:
187160
matrix:
188161
php-version:
189-
- "8.0"
162+
- "8.1"
190163

191164
steps:
192165
- name: "Checkout"
@@ -197,6 +170,7 @@ jobs:
197170
with:
198171
coverage: "none"
199172
php-version: "${{ matrix.php-version }}"
173+
ini-values: zend.assertions=1
200174
tools: cs2pr
201175

202176
- name: "Get composer cache directory"

.gitignore

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
.idea/
2-
coverage/
3-
vendor/
4-
.php-cs-fixer.cache
5-
.phpunit.result.cache
6-
composer.lock
1+
/.idea/
2+
/coverage/
3+
/vendor/
4+
/.php-cs-fixer.cache
5+
/.phpunit.result.cache
6+
/composer.lock

composer.json

+10-5
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
},
1616
"require-dev": {
1717
"malukenho/mcbumpface": "^1.1.5",
18-
"phpstan/phpstan": "^1.2.0",
19-
"phpstan/phpstan-phpunit": "^1.0.0",
20-
"phpunit/phpunit": "^9.5.10",
18+
"phpstan/phpstan": "^1.5.3",
19+
"phpstan/phpstan-phpunit": "^1.1.0",
20+
"phpstan/phpstan-strict-rules": "^1.1.0",
21+
"phpunit/phpunit": "^9.5.19",
2122
"slam/php-cs-fixer-extensions": "^3.1.0",
2223
"slam/php-debug-r": "^1.7.0",
23-
"slam/phpstan-extensions": "^6.0.0",
24-
"symfony/console": "^6.0.1"
24+
"symfony/console": "^6.0.5"
2525
},
2626
"autoload": {
2727
"psr-4": {
@@ -33,6 +33,11 @@
3333
"SlamTest\\ErrorHandler\\": "tests/"
3434
}
3535
},
36+
"config": {
37+
"allow-plugins": {
38+
"malukenho/mcbumpface": true
39+
}
40+
},
3641
"extra": {
3742
"mc-bumpface": {
3843
"stripVersionPrefixes": true

lib/ErrorHandler.php

+41-16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ final class ErrorHandler
3636
private bool $autoExit = true;
3737
private ?bool $cli = null;
3838
private ?int $terminalWidth = null;
39+
3940
/**
4041
* @var null|resource
4142
*/
@@ -44,23 +45,29 @@ final class ErrorHandler
4445
private ?bool $logErrors = null;
4546
private bool $logVariables = true;
4647
private ?bool $displayErrors = null;
48+
4749
/**
4850
* @var callable
4951
*/
5052
private $emailCallback;
53+
5154
/**
5255
* @var callable
5356
*/
5457
private $errorLogCallback = '\\error_log';
58+
5559
/**
5660
* @var array<int, bool>
5761
*/
5862
private array $scream = [];
63+
5964
/**
6065
* @var array<int, class-string<Throwable>>
6166
*/
6267
private array $exceptionsTypesFor404 = [];
6368

69+
private bool $shouldEmail404Exceptions = true;
70+
6471
public function __construct(callable $emailCallback)
6572
{
6673
$this->emailCallback = $emailCallback;
@@ -109,13 +116,15 @@ public function setTerminalWidth(int $terminalWidth): void
109116
public function getTerminalWidth(): int
110117
{
111118
if (null === $this->terminalWidth) {
112-
$width = \getenv('COLUMNS');
113-
114-
if (false === $width && 1 === \preg_match('{rows.(\d+);.columns.(\d+);}i', (string) \exec('stty -a 2> /dev/null | grep columns'), $match)) {
115-
$width = $match[2]; // @codeCoverageIgnore
119+
$width = (int) \getenv('COLUMNS');
120+
if (0 === $width && 1 === \preg_match('{rows.(\d+);.columns.(\d+);}i', (string) \exec('stty -a 2> /dev/null | grep columns'), $match)) {
121+
$width = (int) $match[2]; // @codeCoverageIgnore
122+
}
123+
if (0 === $width) {
124+
$width = 80; // @codeCoverageIgnore
116125
}
117126

118-
$this->setTerminalWidth((int) $width ?: 80);
127+
$this->setTerminalWidth($width);
119128
\assert(null !== $this->terminalWidth);
120129
}
121130

@@ -228,11 +237,11 @@ public function exceptionHandler(Throwable $exception): void
228237
if ($this->isCli()) {
229238
$currentEx = $exception;
230239
do {
231-
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 3 : 120;
240+
$width = 0 !== $this->getTerminalWidth() ? $this->getTerminalWidth() - 3 : 120;
232241
$lines = [
233242
'Message: ' . $currentEx->getMessage(),
234243
'',
235-
'Class: ' . \get_class($currentEx),
244+
'Class: ' . $currentEx::class,
236245
'Code: ' . $this->getExceptionCode($currentEx),
237246
'File: ' . $currentEx->getFile() . ':' . $currentEx->getLine(),
238247
];
@@ -273,7 +282,7 @@ public function exceptionHandler(Throwable $exception): void
273282
// @codeCoverageIgnoreStart
274283
if (! \headers_sent()) {
275284
$header = 'HTTP/1.1 500 Internal Server Error';
276-
if (\in_array(\get_class($exception), $this->exceptionsTypesFor404, true)) {
285+
if (\in_array($exception::class, $this->exceptionsTypesFor404, true)) {
277286
$header = 'HTTP/1.1 404 Not Found';
278287
}
279288
\header($header);
@@ -288,7 +297,7 @@ public function renderHtmlException(Throwable $exception): string
288297
$ajax = (isset($_SERVER['X_REQUESTED_WITH']) && 'XMLHttpRequest' === $_SERVER['X_REQUESTED_WITH']);
289298
$output = '';
290299
$errorType = '500: Internal Server Error';
291-
if (\in_array(\get_class($exception), $this->exceptionsTypesFor404, true)) {
300+
if (\in_array($exception::class, $this->exceptionsTypesFor404, true)) {
292301
$errorType = '404: Not Found';
293302
}
294303
if (! $ajax) {
@@ -309,7 +318,7 @@ public function renderHtmlException(Throwable $exception): string
309318
. '<b>Stack trace:</b><pre>%s</pre>'
310319
. '</div>%s',
311320
\htmlspecialchars($currentEx->getMessage()),
312-
\get_class($currentEx),
321+
$currentEx::class,
313322
$this->getExceptionCode($currentEx),
314323
$currentEx->getFile(),
315324
$currentEx->getLine(),
@@ -347,7 +356,7 @@ public function logException(Throwable $exception): void
347356
$output = \sprintf(
348357
'%s%s: %s in %s:%s%s%s',
349358
($i > 0 ? '{PR ' . $i . '} ' : ''),
350-
\get_class($exception),
359+
$exception::class,
351360
$exception->getMessage(),
352361
$exception->getFile(),
353362
$exception->getLine(),
@@ -363,7 +372,13 @@ public function logException(Throwable $exception): void
363372

364373
public function emailException(Throwable $exception): void
365374
{
366-
if (! $this->logErrors()) {
375+
if (
376+
! $this->logErrors()
377+
|| (
378+
! $this->shouldEmail404Exceptions
379+
&& \in_array($exception::class, $this->exceptionsTypesFor404, true)
380+
)
381+
) {
367382
return;
368383
}
369384

@@ -389,7 +404,7 @@ public function emailException(Throwable $exception): void
389404
$currentEx = $exception;
390405
do {
391406
$bodyArray = [
392-
'Class' => \get_class($currentEx),
407+
'Class' => $currentEx::class,
393408
'Code' => $this->getExceptionCode($currentEx),
394409
'Message' => $currentEx->getMessage(),
395410
'File' => $currentEx->getFile() . ':' . $currentEx->getLine(),
@@ -405,10 +420,10 @@ public function emailException(Throwable $exception): void
405420
$username = null;
406421

407422
if ($this->logVariables()) {
408-
if (! empty($_POST)) {
423+
if ([] !== $_POST) {
409424
$bodyText .= '$_POST = ' . \print_r($_POST, true) . \PHP_EOL;
410425
}
411-
if (isset($_SESSION) && ! empty($_SESSION)) {
426+
if (isset($_SESSION) && [] !== $_SESSION) {
412427
$sessionText = \print_r(\class_exists(DoctrineDebug::class) ? DoctrineDebug::export($_SESSION, 4) : $_SESSION, true);
413428
$bodyText .= '$_SESSION = ' . $sessionText . \PHP_EOL;
414429

@@ -422,7 +437,7 @@ public function emailException(Throwable $exception): void
422437

423438
$subject = \sprintf(
424439
'Error%s: %s',
425-
$username ? \sprintf(' [%s]', $username) : '',
440+
null !== $username ? \sprintf(' [%s]', $username) : '',
426441
$exception->getMessage()
427442
);
428443

@@ -465,4 +480,14 @@ public function get404ExceptionTypes(): array
465480
{
466481
return $this->exceptionsTypesFor404;
467482
}
483+
484+
public function setShouldEmail404Exceptions(bool $shouldEmail404Exceptions): void
485+
{
486+
$this->shouldEmail404Exceptions = $shouldEmail404Exceptions;
487+
}
488+
489+
public function shouldEmail404Exceptions(): bool
490+
{
491+
return $this->shouldEmail404Exceptions;
492+
}
468493
}

phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
parameters:
22
ignoreErrors:
3-
-
4-
message: "#^Cannot cast mixed to string\\.$#"
5-
count: 1
6-
path: lib/ErrorHandler.php
7-
83
-
94
message: "#^Expression \"\\$arrayPerVerificaErrori\\['undefined_index'\\]\" on a separate line does not do anything\\.$#"
105
count: 1

phpstan.neon

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
includes:
22
- vendor/phpstan/phpstan-phpunit/extension.neon
3-
- vendor/slam/phpstan-extensions/conf/slam-rules.neon
3+
- vendor/phpstan/phpstan-strict-rules/rules.neon
44
- phpstan-baseline.neon
55

66
parameters:

phpunit.xml

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
5-
bootstrap="./vendor/autoload.php"
65
colors="true"
76
verbose="true"
87
>
@@ -11,6 +10,8 @@
1110
<directory suffix=".php">./lib</directory>
1211
</include>
1312
<report>
13+
<clover outputFile="coverage/clover.xml" />
14+
<html outputDirectory="coverage/html" />
1415
<text outputFile="php://stdout" showOnlySummary="true"/>
1516
</report>
1617
</coverage>

0 commit comments

Comments
 (0)