Skip to content

Commit 4d0d3ff

Browse files
authored
Update dependencies and fix PHPStan paths (#1)
1 parent 1c93b1d commit 4d0d3ff

File tree

5 files changed

+78
-70
lines changed

5 files changed

+78
-70
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ matrix:
88
- php: 7.1
99
env: CS_CHECK=1 STATIC_ANALYSIS=1
1010
- php: 7.2
11+
- php: 7.3
1112
env: CODE_COVERAGE=1
1213

1314
cache:

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
"php": "^7.1"
1515
},
1616
"require-dev": {
17-
"phpstan/phpstan": "^0.9.1",
18-
"phpstan/phpstan-phpunit": "^0.9.3",
19-
"phpunit/phpunit": "^7.0",
17+
"phpstan/phpstan": "^0.11",
18+
"phpstan/phpstan-phpunit": "^0.11",
19+
"phpunit/phpunit": "^7.5",
2020
"roave/security-advisories": "dev-master",
21-
"slam/php-cs-fixer-extensions": "^1.13",
22-
"slam/php-debug-r": "^1.2",
23-
"slam/phpstan-extensions": "^1.0",
24-
"symfony/console": "^4.0",
25-
"thecodingmachine/phpstan-strict-rules": "^0.9.0"
21+
"slam/php-cs-fixer-extensions": "^1.18",
22+
"slam/php-debug-r": "^1.6",
23+
"slam/phpstan-extensions": "^3.6",
24+
"symfony/console": "^4.3",
25+
"thecodingmachine/phpstan-strict-rules": "^0.11"
2626
},
2727
"autoload": {
2828
"psr-4": {

lib/ErrorHandler.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function getTerminalWidth(): int
8080
if (null === $this->terminalWidth) {
8181
$width = \getenv('COLUMNS');
8282

83-
if (false === $width and 1 === \preg_match('{rows.(\d+);.columns.(\d+);}i', \exec('stty -a 2> /dev/null | grep columns'), $match)) {
83+
if (false === $width && 1 === \preg_match('{rows.(\d+);.columns.(\d+);}i', \exec('stty -a 2> /dev/null | grep columns'), $match)) {
8484
$width = $match[2];
8585
}
8686

@@ -97,7 +97,7 @@ public function setErrorOutputStream($errorOutputStream): void
9797
}
9898

9999
$this->errorOutputStream = $errorOutputStream;
100-
$this->hasColorSupport = (\function_exists('posix_isatty') and @\posix_isatty($errorOutputStream));
100+
$this->hasColorSupport = (\function_exists('posix_isatty') && @\posix_isatty($errorOutputStream));
101101
}
102102

103103
public function getErrorOutputStream()
@@ -152,7 +152,7 @@ public function register(): void
152152
public function errorHandler($errno, $errstr = '', $errfile = '', $errline = 0): void
153153
{
154154
// Mandatory check for @ operator
155-
if (0 === \error_reporting() and ! isset($this->scream[$errno])) {
155+
if (0 === \error_reporting() && ! isset($this->scream[$errno])) {
156156
return;
157157
}
158158

@@ -183,12 +183,12 @@ public function exceptionHandler(\Throwable $exception): void
183183

184184
if (isset($line[$width])) {
185185
$lines[$i] = \mb_substr($line, 0, $width);
186-
if (isset($line[0]) and '#' !== $line[0]) {
186+
if (isset($line[0]) && '#' !== $line[0]) {
187187
\array_splice($lines, $i + 1, 0, ' ' . \mb_substr($line, $width));
188188
}
189189
}
190190

191-
$i += 1;
191+
++$i;
192192
}
193193

194194
$this->outputError(\PHP_EOL);
@@ -215,7 +215,7 @@ public function exceptionHandler(\Throwable $exception): void
215215
}
216216
// @codeCoverageIgnoreEnd
217217

218-
$ajax = (isset($_SERVER) and isset($_SERVER['X_REQUESTED_WITH']) and 'XMLHttpRequest' === $_SERVER['X_REQUESTED_WITH']);
218+
$ajax = (isset($_SERVER) && isset($_SERVER['X_REQUESTED_WITH']) && 'XMLHttpRequest' === $_SERVER['X_REQUESTED_WITH']);
219219
$output = '';
220220
if (! $ajax) {
221221
$output .= '<!DOCTYPE html><html><head><title>500: Errore interno</title></head><body>';
@@ -324,16 +324,16 @@ public function emailException(\Throwable $exception): void
324324
$username = null;
325325

326326
if ($this->logVariables()) {
327-
if (isset($_POST) and ! empty($_POST)) {
327+
if (isset($_POST) && ! empty($_POST)) {
328328
$bodyText .= '$_POST = ' . \print_r($_POST, true) . \PHP_EOL;
329329
}
330-
if (isset($_SESSION) and ! empty($_SESSION)) {
330+
if (isset($_SESSION) && ! empty($_SESSION)) {
331331
$sessionText = \print_r(\class_exists(DoctrineDebug::class) ? DoctrineDebug::export($_SESSION, 4) : $_SESSION, true);
332332
$bodyText .= '$_SESSION = ' . $sessionText . \PHP_EOL;
333333

334-
$count = 0;
334+
$count = 0;
335335
$username = \preg_replace('/.+\[([^\]]+)?username([^\]]+)?\] => ([\w\-\.]+).+/s', '\3', $sessionText, -1, $count);
336-
if (! isset($username[0]) or isset($username[255]) or 1 !== $count) {
336+
if (! isset($username[0]) || isset($username[255]) || 1 !== $count) {
337337
$username = null;
338338
}
339339
}
@@ -356,7 +356,7 @@ public function emailException(\Throwable $exception): void
356356
private function getExceptionCode(\Throwable $exception): string
357357
{
358358
$code = $exception->getCode();
359-
if ($exception instanceof \ErrorException and isset(static::$errors[$code])) {
359+
if ($exception instanceof \ErrorException && isset(static::$errors[$code])) {
360360
$code = static::$errors[$code];
361361
}
362362

phpstan.neon

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@ includes:
55
- vendor/slam/phpstan-extensions/conf/thecodingmachine-rules.neon
66

77
parameters:
8+
paths:
9+
- lib/
10+
- tests/
811
ignoreErrors:
912
- '#Class Doctrine\\Common\\Util\\Debug not found#'
1013
- '#Call to static method export\(\) on an unknown class Doctrine\\Common\\Util\\Debug#'
11-
- '#Constant ROOT_PATH not found#'
14+
- '#Function \w+ is unsafe to use, rely on .+ instead#'
15+
- '#Variable \$_\w+ in isset\(\) always exists and is not nullable#'
16+
- '#Parameter \#1 \$error_handler of function set_error_handler expects#'
17+
- '#Offset .(no_exception_thrown.*|undefined_index). does not exist on array#'

tests/ErrorHandlerTest.php

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ protected function setUp()
2121
{
2222
\ini_set('display_errors', (string) false);
2323
$this->backupErrorLog = \ini_get('error_log');
24-
$this->errorLog = __DIR__ . \DIRECTORY_SEPARATOR . 'error_log_test';
24+
$this->errorLog = __DIR__ . \DIRECTORY_SEPARATOR . 'error_log_test';
2525
\touch($this->errorLog);
2626
\ini_set('error_log', $this->errorLog);
2727

28-
$this->exception = new ErrorException(\uniqid('normal_'), \E_USER_NOTICE);
29-
$this->emailsSent = [];
30-
$this->errorHandler = new ErrorHandler(function ($subject, $body) {
28+
$this->exception = new ErrorException(\uniqid('normal_'), \E_USER_NOTICE);
29+
$this->emailsSent = [];
30+
$this->errorHandler = new ErrorHandler(function (string $subject, string $body) {
3131
$this->emailsSent[] = [
3232
'subject' => $subject,
33-
'body' => $body,
33+
'body' => $body,
3434
];
3535
});
3636

@@ -50,26 +50,26 @@ public function testDefaultConfiguration()
5050
$errorHandler = new ErrorHandler(function () {
5151
});
5252

53-
$this->assertTrue($errorHandler->isCli());
54-
$this->assertTrue($errorHandler->autoExit());
55-
$this->assertNotNull($errorHandler->getTerminalWidth());
56-
$this->assertSame(\STDERR, $errorHandler->getErrorOutputStream());
57-
$this->assertFalse($errorHandler->logErrors());
53+
static::assertTrue($errorHandler->isCli());
54+
static::assertTrue($errorHandler->autoExit());
55+
static::assertNotNull($errorHandler->getTerminalWidth());
56+
static::assertSame(\STDERR, $errorHandler->getErrorOutputStream());
57+
static::assertFalse($errorHandler->logErrors());
5858

5959
$errorHandler->setCli(false);
6060
$errorHandler->setAutoExit(false);
6161
$errorHandler->setTerminalWidth($width = \mt_rand(1, 999));
6262
$errorHandler->setErrorOutputStream($memoryStream = \fopen('php://memory', 'r+'));
6363
$errorHandler->setLogErrors(true);
6464

65-
$this->assertFalse($errorHandler->isCli());
66-
$this->assertFalse($errorHandler->autoExit());
67-
$this->assertSame($width, $errorHandler->getTerminalWidth());
68-
$this->assertSame($memoryStream, $errorHandler->getErrorOutputStream());
69-
$this->assertTrue($errorHandler->logErrors());
65+
static::assertFalse($errorHandler->isCli());
66+
static::assertFalse($errorHandler->autoExit());
67+
static::assertSame($width, $errorHandler->getTerminalWidth());
68+
static::assertSame($memoryStream, $errorHandler->getErrorOutputStream());
69+
static::assertTrue($errorHandler->logErrors());
7070

7171
$errorHandler->setErrorOutputStream(\uniqid('not_a_stream_'));
72-
$this->assertSame($memoryStream, $errorHandler->getErrorOutputStream());
72+
static::assertSame($memoryStream, $errorHandler->getErrorOutputStream());
7373
}
7474

7575
/**
@@ -97,9 +97,9 @@ public function testScream()
9797
\E_USER_WARNING => true,
9898
];
9999

100-
$this->assertEmpty($this->errorHandler->getScreamSilencedErrors());
100+
static::assertEmpty($this->errorHandler->getScreamSilencedErrors());
101101
$this->errorHandler->setScreamSilencedErrors($scream);
102-
$this->assertSame($scream, $this->errorHandler->getScreamSilencedErrors());
102+
static::assertSame($scream, $this->errorHandler->getScreamSilencedErrors());
103103

104104
$this->errorHandler->register();
105105

@@ -115,13 +115,14 @@ public function testScream()
115115
public function testHandleCliException()
116116
{
117117
$memoryStream = \fopen('php://memory', 'r+');
118+
static::assertIsResource($memoryStream);
118119
$this->errorHandler->setErrorOutputStream($memoryStream);
119120

120121
$this->errorHandler->exceptionHandler($this->exception);
121122

122123
\fseek($memoryStream, 0);
123124
$output = \stream_get_contents($memoryStream);
124-
$this->assertContains($this->exception->getMessage(), $output);
125+
static::assertContains($this->exception->getMessage(), $output);
125126
}
126127

127128
public function testHandleWebExceptionWithDisplay()
@@ -134,10 +135,10 @@ public function testHandleWebExceptionWithDisplay()
134135
$this->errorHandler->exceptionHandler($this->exception);
135136
$output = \ob_get_clean();
136137

137-
$this->assertContains($this->exception->getMessage(), $output);
138+
static::assertContains($this->exception->getMessage(), $output);
138139

139140
$errorLogContent = \file_get_contents($this->errorLog);
140-
$this->assertContains($this->exception->getMessage(), $errorLogContent);
141+
static::assertContains($this->exception->getMessage(), $errorLogContent);
141142
}
142143

143144
public function testHandleWebExceptionWithoutDisplay()
@@ -150,10 +151,10 @@ public function testHandleWebExceptionWithoutDisplay()
150151
$this->errorHandler->exceptionHandler($this->exception);
151152
$output = \ob_get_clean();
152153

153-
$this->assertNotContains($this->exception->getMessage(), $output);
154+
static::assertNotContains($this->exception->getMessage(), $output);
154155

155156
$errorLogContent = \file_get_contents($this->errorLog);
156-
$this->assertContains($this->exception->getMessage(), $errorLogContent);
157+
static::assertContains($this->exception->getMessage(), $errorLogContent);
157158
}
158159

159160
public function testLogErrorAndException()
@@ -162,7 +163,7 @@ public function testLogErrorAndException()
162163

163164
$this->errorHandler->logException($this->exception);
164165

165-
$this->assertSame(0, \filesize($this->errorLog));
166+
static::assertSame(0, \filesize($this->errorLog));
166167

167168
$this->errorHandler->setLogErrors(true);
168169

@@ -172,8 +173,8 @@ public function testLogErrorAndException()
172173

173174
$errorLogContent = \file_get_contents($this->errorLog);
174175

175-
$this->assertContains($exception->getMessage(), $errorLogContent);
176-
$this->assertContains($this->exception->getMessage(), $errorLogContent);
176+
static::assertContains($exception->getMessage(), $errorLogContent);
177+
static::assertContains($this->exception->getMessage(), $errorLogContent);
177178
}
178179

179180
public function testEmailException()
@@ -182,54 +183,54 @@ public function testEmailException()
182183

183184
$this->errorHandler->emailException($this->exception);
184185

185-
$this->assertEmpty($this->emailsSent);
186+
static::assertEmpty($this->emailsSent);
186187

187188
$this->errorHandler->setLogErrors(true);
188189

189-
$key = \uniqid(__FUNCTION__);
190+
$key = \uniqid(__FUNCTION__);
190191
$_SESSION = [$key => \uniqid()];
191-
$_POST = [$key => \uniqid()];
192+
$_POST = [$key => \uniqid()];
192193

193194
$this->errorHandler->emailException($this->exception);
194195

195-
$this->assertNotEmpty($this->emailsSent);
196+
static::assertNotEmpty($this->emailsSent);
196197
$message = \current($this->emailsSent);
197-
$this->assertNotEmpty($message);
198+
static::assertNotEmpty($message);
198199

199200
$messageText = $message['body'];
200-
$this->assertContains($this->exception->getMessage(), $messageText);
201-
$this->assertContains($_SESSION[$key], $messageText);
202-
$this->assertContains($_POST[$key], $messageText);
201+
static::assertContains($this->exception->getMessage(), $messageText);
202+
static::assertContains($_SESSION[$key], $messageText);
203+
static::assertContains($_POST[$key], $messageText);
203204
}
204205

205206
public function testCanHideVariablesFromEmail()
206207
{
207-
$this->assertTrue($this->errorHandler->logVariables());
208+
static::assertTrue($this->errorHandler->logVariables());
208209
$this->errorHandler->setLogVariables(false);
209-
$this->assertFalse($this->errorHandler->logVariables());
210+
static::assertFalse($this->errorHandler->logVariables());
210211

211212
$this->errorHandler->setLogErrors(true);
212213

213-
$key = \uniqid(__FUNCTION__);
214+
$key = \uniqid(__FUNCTION__);
214215
$_SESSION = [$key => \uniqid()];
215-
$_POST = [$key => \uniqid()];
216+
$_POST = [$key => \uniqid()];
216217

217218
$this->errorHandler->emailException($this->exception);
218219

219-
$this->assertNotEmpty($this->emailsSent);
220+
static::assertNotEmpty($this->emailsSent);
220221
$message = \current($this->emailsSent);
221-
$this->assertNotEmpty($message);
222+
static::assertNotEmpty($message);
222223

223224
$messageText = $message['body'];
224-
$this->assertContains($this->exception->getMessage(), $messageText);
225-
$this->assertNotContains($_SESSION[$key], $messageText);
226-
$this->assertNotContains($_POST[$key], $messageText);
225+
static::assertContains($this->exception->getMessage(), $messageText);
226+
static::assertNotContains($_SESSION[$key], $messageText);
227+
static::assertNotContains($_POST[$key], $messageText);
227228
}
228229

229230
public function testErroriNellInvioDellaMailVengonoComunqueLoggati()
230231
{
231-
$mailError = \uniqid('mail_not_sent_');
232-
$mailCallback = function ($body, $text) use ($mailError) {
232+
$mailError = \uniqid('mail_not_sent_');
233+
$mailCallback = static function () use ($mailError) {
233234
throw new ErrorException($mailError, \E_USER_ERROR);
234235
};
235236
$errorHandler = new ErrorHandler($mailCallback);
@@ -238,8 +239,8 @@ public function testErroriNellInvioDellaMailVengonoComunqueLoggati()
238239
$errorHandler->emailException($this->exception);
239240

240241
$errorLogContent = \file_get_contents($this->errorLog);
241-
$this->assertNotContains($this->exception->getMessage(), $errorLogContent);
242-
$this->assertContains($mailError, $errorLogContent);
242+
static::assertNotContains($this->exception->getMessage(), $errorLogContent);
243+
static::assertContains($mailError, $errorLogContent);
243244
}
244245

245246
public function testUsernameInEmailSubject()
@@ -252,7 +253,7 @@ public function testUsernameInEmailSubject()
252253

253254
$message = \current($this->emailsSent);
254255

255-
$this->assertContains($username, $message['subject']);
256+
static::assertContains($username, $message['subject']);
256257
}
257258

258259
public function testTerminalWidthByEnv()
@@ -263,14 +264,14 @@ public function testTerminalWidthByEnv()
263264
$errorHandler = new ErrorHandler(function () {
264265
});
265266

266-
$this->assertSame($width, $errorHandler->getTerminalWidth());
267+
static::assertSame($width, $errorHandler->getTerminalWidth());
267268

268269
\putenv('COLUMNS');
269270

270271
$errorHandler = new ErrorHandler(function () {
271272
});
272273

273274
$terminal = new Terminal();
274-
$this->assertSame($terminal->getWidth(), $errorHandler->getTerminalWidth());
275+
static::assertSame($terminal->getWidth(), $errorHandler->getTerminalWidth());
275276
}
276277
}

0 commit comments

Comments
 (0)