Skip to content

Commit cd919b7

Browse files
committed
Using psalm and phpstorm for simple static analysis to find type discrepancies and potential edge cases and bugs
1 parent ecf0ca7 commit cd919b7

10 files changed

+83
-121
lines changed

psalm.xml

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?xml version="1.0"?>
22
<psalm
3-
totallyTyped="false"
43
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
54
xmlns="https://getpsalm.org/schema/config"
65
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
@@ -32,7 +31,6 @@
3231
<MissingReturnType errorLevel="info" />
3332
<MissingPropertyType errorLevel="info" />
3433
<InvalidDocblock errorLevel="info" />
35-
<MisplacedRequiredParam errorLevel="info" />
3634

3735
<PropertyNotSetInConstructor errorLevel="info" />
3836
<MissingConstructor errorLevel="info" />

src/Arrays.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33

44
/**
55
* @param array $aArray
6-
* @param array ...$aKeys
6+
* @param string[] $aKeys
77
* @return array
88
*/
9-
function array_without_keys(array $aArray, ...$aKeys) {
9+
function array_without_keys(array $aArray, ...$aKeys): array {
1010
$aDiff = [];
1111
foreach($aKeys as $sKey) {
1212
$aDiff[$sKey] = 1;
@@ -34,7 +34,7 @@ function array_is_multi(array $aArray):bool {
3434
* @param string $sDelimiter
3535
* @return array
3636
*/
37-
function array_from_path($sPath, $mValue, $sDelimiter = '.') {
37+
function array_from_path(string $sPath, $mValue, $sDelimiter = '.'): array {
3838
$aPath = explode($sDelimiter, $sPath);
3939
$aResponse = $mValue;
4040
while(count($aPath) > 1) {

src/DateTime.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Returns DateTime object with proper microtime
1212
* @return DateTime
1313
*/
14-
function notNowButRightNow() {
14+
function notNowButRightNow(): DateTime {
1515
return DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', ''));
1616
}
1717

@@ -40,7 +40,7 @@ function minutes(DateTime $oFrom, DateTime $oTo):int {
4040
*
4141
* @return string
4242
*/
43-
function minutes_ago(DateTime $oFrom, DateTime $oTo) {
43+
function minutes_ago(DateTime $oFrom, DateTime $oTo): string {
4444
$oDiff = $oFrom->diff($oTo);
4545
$iDiff = $oDiff->d * 24 * 60;
4646
$iDiff += $oDiff->h * 60;
@@ -65,16 +65,16 @@ function formatSeconds( float $nSeconds , int $iMSPlaces = 0): string {
6565

6666
$nMS = $nSeconds - floor( $nSeconds );
6767
$nMSRounded = $iMSPlaces ? round($nMS, $iMSPlaces) : $nMS;
68-
$sMS = str_replace( "0.", '', $nMSRounded );
68+
$sMS = str_replace( "0.", '', '' . $nMSRounded);
6969

7070
if ( $nSeconds > 3600 )
7171
{
7272
$iHours = floor( $nSeconds / 3600 );
7373
}
7474
$nSeconds %= 3600;
7575

76-
return str_pad( $iHours, 2, '0', STR_PAD_LEFT )
77-
. gmdate( ':i:s', $nSeconds )
76+
return str_pad( '' . $iHours, 2, '0', STR_PAD_LEFT )
77+
. gmdate( ':i:s', (int) $nSeconds )
7878
. ($sMS ? ".$sMS" : '')
7979
;
8080
}

src/ErrorsToExceptions.php

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<?php
2+
/** @noinspection PhpMultipleClassesDeclarationsInOneFile */
3+
/** @noinspection PhpIllegalPsrClassPathInspection */
4+
/** @noinspection PhpUnhandledExceptionInspection */
5+
26
namespace Enobrev;
37

48
use ErrorException;
@@ -13,21 +17,21 @@
1317
if (0 === error_reporting()) { return; }
1418
switch($err_severity)
1519
{
16-
case E_ERROR: throw new ErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
17-
case E_WARNING: throw new WarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
18-
case E_PARSE: throw new ParseException ($err_msg, 0, $err_severity, $err_file, $err_line);
19-
case E_NOTICE: throw new NoticeException ($err_msg, 0, $err_severity, $err_file, $err_line);
20-
case E_CORE_ERROR: throw new CoreErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
21-
case E_CORE_WARNING: throw new CoreWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
22-
case E_COMPILE_ERROR: throw new CompileErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
23-
case E_COMPILE_WARNING: throw new CoreWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
24-
case E_USER_ERROR: throw new UserErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
25-
case E_USER_WARNING: throw new UserWarningException ($err_msg, 0, $err_severity, $err_file, $err_line);
26-
case E_USER_NOTICE: throw new UserNoticeException ($err_msg, 0, $err_severity, $err_file, $err_line);
27-
case E_STRICT: throw new StrictException ($err_msg, 0, $err_severity, $err_file, $err_line);
28-
case E_RECOVERABLE_ERROR: throw new RecoverableErrorException ($err_msg, 0, $err_severity, $err_file, $err_line);
29-
case E_DEPRECATED: throw new DeprecatedException ($err_msg, 0, $err_severity, $err_file, $err_line);
30-
case E_USER_DEPRECATED: throw new UserDeprecatedException ($err_msg, 0, $err_severity, $err_file, $err_line);
20+
case E_ERROR: throw new ErrorException ($err_msg, E_ERROR, $err_severity, $err_file, $err_line);
21+
case E_WARNING: throw new WarningException ($err_msg, E_WARNING, $err_severity, $err_file, $err_line);
22+
case E_PARSE: throw new ParseException ($err_msg, E_PARSE, $err_severity, $err_file, $err_line);
23+
case E_NOTICE: throw new NoticeException ($err_msg, E_NOTICE, $err_severity, $err_file, $err_line);
24+
case E_CORE_ERROR: throw new CoreErrorException ($err_msg, E_CORE_ERROR, $err_severity, $err_file, $err_line);
25+
case E_CORE_WARNING: throw new CoreWarningException ($err_msg, E_CORE_WARNING, $err_severity, $err_file, $err_line);
26+
case E_COMPILE_ERROR: throw new CompileErrorException ($err_msg, E_COMPILE_ERROR, $err_severity, $err_file, $err_line);
27+
case E_COMPILE_WARNING: throw new CoreWarningException ($err_msg, E_COMPILE_WARNING, $err_severity, $err_file, $err_line);
28+
case E_USER_ERROR: throw new UserErrorException ($err_msg, E_USER_ERROR, $err_severity, $err_file, $err_line);
29+
case E_USER_WARNING: throw new UserWarningException ($err_msg, E_USER_WARNING, $err_severity, $err_file, $err_line);
30+
case E_USER_NOTICE: throw new UserNoticeException ($err_msg, E_USER_NOTICE, $err_severity, $err_file, $err_line);
31+
case E_STRICT: throw new StrictException ($err_msg, E_STRICT, $err_severity, $err_file, $err_line);
32+
case E_RECOVERABLE_ERROR: throw new RecoverableErrorException ($err_msg, E_RECOVERABLE_ERROR, $err_severity, $err_file, $err_line);
33+
case E_DEPRECATED: throw new DeprecatedException ($err_msg, E_DEPRECATED, $err_severity, $err_file, $err_line);
34+
case E_USER_DEPRECATED: throw new UserDeprecatedException ($err_msg, E_USER_DEPRECATED, $err_severity, $err_file, $err_line);
3135
}
3236
});
3337

src/Headers.php

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
<?php
22
namespace Enobrev;
33

4-
/**
5-
* @return array
6-
*/
7-
function getHeaders() {
4+
function getHeaders(): array {
85
$aHeaders = [];
96
foreach ($_SERVER as $sName => $sValue) {
107
if (strpos($sName, 'HTTP_') === 0) {
@@ -14,17 +11,13 @@ function getHeaders() {
1411
return $aHeaders;
1512
}
1613

17-
/**
18-
* @return bool
19-
*/
20-
function isCli() {
14+
function isCli(): bool {
2115
return PHP_SAPI === 'cli'
2216
&& empty($_SERVER['REMOTE_ADDR']);
2317
}
2418

2519
/**
2620
* @return string
27-
* @psalm-suppress PossiblyFalseArgument
2821
*/
2922
function get_ip() {
3023
$bIsCLI = isCli();
@@ -51,10 +44,7 @@ function get_ip() {
5144
return $sIP;
5245
}
5346

54-
/**
55-
* @return bool
56-
*/
57-
function contentTypeIsNotHtml() {
47+
function contentTypeIsNotHtml(): bool {
5848
$aHeaders = headers_list();
5949
if (count($aHeaders)) {
6050
foreach($aHeaders as $sHeader) {

src/Log.php

+40-53
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,50 @@
11
<?php
22
namespace Enobrev;
33

4-
use Exception;
54
use Throwable;
65

76
use Adbar\Dot;
8-
use Monolog;
7+
use Laminas\Diactoros\ServerRequestFactory;
98
use Monolog\Formatter\LineFormatter;
109
use Monolog\Handler\SyslogHandler;
10+
use Monolog\Logger;
1111
use Psr\Http\Message\ServerRequestInterface;
12-
use Laminas\Diactoros\ServerRequestFactory;
1312

1413
class Log {
15-
/** @var Monolog\Logger */
16-
private static $oLog;
14+
private static ?Logger $oLog;
1715

18-
/** @var ServerRequestInterface */
19-
private static $oServerRequest;
16+
private static ServerRequestInterface $oServerRequest;
2017

21-
/** @var string */
22-
private static $sService = 'Enobrev_Logger_Replace_Me';
18+
private static string $sService = 'Enobrev_Logger_Replace_Me';
2319

24-
/** @var int */
25-
private static $iStackLimit = 5;
20+
private static int $iStackLimit = 5;
2621

27-
/** @var bool */
28-
private static $bMetrics = false;
22+
private static bool $bMetrics = false;
2923

30-
/** @var bool */
31-
private static $bJSONLogs = false;
24+
private static bool $bJSONLogs = false;
3225

33-
/** @var string */
34-
private static $sThreadHash;
26+
private static string $sThreadHash;
3527

36-
/** @var array */
37-
private static $aSpans = [];
28+
private static array $aSpans = [];
3829

3930
/** @var SpanMeta[] */
40-
private static $aSpanMetas = [];
31+
private static array $aSpanMetas = [];
4132

42-
/** @var int */
43-
private static $iGlobalIndex = 0;
33+
private static int $iGlobalIndex = 0;
4434

45-
/** @var array */
46-
private static $aDisabled = [
35+
private static array $aDisabled = [
4736
'd' => false,
4837
'dt' => false
4938
];
5039

5140
/**
52-
* @return Monolog\Logger
41+
* @return Logger
5342
*/
54-
private static function initLogger(): \Monolog\Logger {
43+
private static function initLogger(): Logger {
5544
if (self::$oLog === null) {
5645
register_shutdown_function([self::class, 'summary']);
5746

58-
self::$oLog = new Monolog\Logger(self::$sService);
47+
self::$oLog = new Logger(self::$sService);
5948

6049
if (self::$bJSONLogs) {
6150
$oFormatter = new LineFormatter('@cee: %context%');
@@ -219,13 +208,13 @@ public static function disableDT(bool $bDisabled = true): void {
219208
* @param array $aContext The log context
220209
* @return boolean Whether the record has been processed
221210
*/
222-
public static function d($sMessage, array $aContext = array()): bool {
211+
public static function d(string $sMessage, array $aContext = array()): bool {
223212
if (self::$aDisabled['d']) {
224213
self::justAddContext($aContext);
225214
return false;
226215
}
227216

228-
return self::addRecord(Monolog\Logger::DEBUG, $sMessage, $aContext);
217+
return self::addRecord(Logger::DEBUG, $sMessage, $aContext);
229218
}
230219

231220
/**
@@ -235,8 +224,8 @@ public static function d($sMessage, array $aContext = array()): bool {
235224
* @param array $aContext The log context
236225
* @return boolean Whether the record has been processed
237226
*/
238-
public static function i($sMessage, array $aContext = array()): bool {
239-
return self::addRecord(Monolog\Logger::INFO, $sMessage, $aContext);
227+
public static function i(string $sMessage, array $aContext = array()): bool {
228+
return self::addRecord(Logger::INFO, $sMessage, $aContext);
240229
}
241230

242231
/**
@@ -246,8 +235,8 @@ public static function i($sMessage, array $aContext = array()): bool {
246235
* @param array $aContext The log context
247236
* @return boolean Whether the record has been processed
248237
*/
249-
public static function n($sMessage, array $aContext = array()): bool {
250-
return self::addRecord(Monolog\Logger::NOTICE, $sMessage, $aContext);
238+
public static function n(string $sMessage, array $aContext = array()): bool {
239+
return self::addRecord(Logger::NOTICE, $sMessage, $aContext);
251240
}
252241

253242
/**
@@ -257,8 +246,8 @@ public static function n($sMessage, array $aContext = array()): bool {
257246
* @param array $aContext The log context
258247
* @return boolean Whether the record has been processed
259248
*/
260-
public static function w($sMessage, array $aContext = array()): bool {
261-
return self::addRecord(Monolog\Logger::WARNING, $sMessage, $aContext);
249+
public static function w(string $sMessage, array $aContext = array()): bool {
250+
return self::addRecord(Logger::WARNING, $sMessage, $aContext);
262251
}
263252

264253
/**
@@ -268,20 +257,20 @@ public static function w($sMessage, array $aContext = array()): bool {
268257
* @param array $aContext The log context
269258
* @return boolean Whether the record has been processed
270259
*/
271-
public static function e($sMessage, array $aContext = array()): bool {
272-
return self::addRecord(Monolog\Logger::ERROR, $sMessage, $aContext);
260+
public static function e(string $sMessage, array $aContext = array()): bool {
261+
return self::addRecord(Logger::ERROR, $sMessage, $aContext);
273262
}
274263

275264
/**
276265
* Adds a log record at the ERROR level.
277266
*
278267
* @param string $sMessage The log message
279-
* @param Exception $oThrowable The exception
268+
* @param Throwable $oThrowable The exception
280269
* @param array $aContext The log context
281270
*
282271
* @return boolean Whether the record has been processed
283272
*/
284-
public static function ex($sMessage, Throwable $oThrowable, array $aContext = array()): bool {
273+
public static function ex(string $sMessage, Throwable $oThrowable, array $aContext = array()): bool {
285274
$iTruncate = self::$iStackLimit;
286275
$aStack = $oThrowable->getTrace();
287276
$iStack = count($aStack);
@@ -315,10 +304,10 @@ public static function ex($sMessage, Throwable $oThrowable, array $aContext = ar
315304
'context' => self::getContextForOutput()
316305
];
317306

318-
return self::addRecord(Monolog\Logger::ERROR, $sMessage, $aContext);
307+
return self::addRecord(Logger::ERROR, $sMessage, $aContext);
319308
}
320309

321-
private static function replaceObjects(array $aArgs) {
310+
private static function replaceObjects(array $aArgs): array {
322311
$aOutput = [];
323312
foreach($aArgs as $sKey => $aArg) { // Do NOT use a reference here as getTrace returns references to the actual args in the call stack which can then modify the real vars in the stack
324313
if (is_object($aArg)) {
@@ -342,8 +331,8 @@ private static function replaceObjects(array $aArgs) {
342331
* @param array $aContext The log context
343332
* @return boolean Whether the record has been processed
344333
*/
345-
public static function c($sMessage, array $aContext = array()): bool {
346-
return self::addRecord(Monolog\Logger::CRITICAL, $sMessage, $aContext);
334+
public static function c(string $sMessage, array $aContext = array()): bool {
335+
return self::addRecord(Logger::CRITICAL, $sMessage, $aContext);
347336
}
348337

349338
/**
@@ -353,8 +342,8 @@ public static function c($sMessage, array $aContext = array()): bool {
353342
* @param array $aContext The log context
354343
* @return boolean Whether the record has been processed
355344
*/
356-
public static function a($sMessage, array $aContext = array()): bool {
357-
return self::addRecord(Monolog\Logger::ALERT, $sMessage, $aContext);
345+
public static function a(string $sMessage, array $aContext = array()): bool {
346+
return self::addRecord(Logger::ALERT, $sMessage, $aContext);
358347
}
359348

360349
/**
@@ -364,8 +353,8 @@ public static function a($sMessage, array $aContext = array()): bool {
364353
* @param array $aContext The log context
365354
* @return boolean Whether the record has been processed
366355
*/
367-
public static function em($sMessage, array $aContext = array()): bool {
368-
return self::addRecord(Monolog\Logger::EMERGENCY, $sMessage, $aContext);
356+
public static function em(string $sMessage, array $aContext = array()): bool {
357+
return self::addRecord(Logger::EMERGENCY, $sMessage, $aContext);
369358
}
370359

371360
/**
@@ -495,11 +484,9 @@ private static function getThreadHash(): string {
495484
return self::$sThreadHash;
496485
}
497486

498-
/** @var array */
499-
private static $aIndices = [];
487+
private static array $aIndices = [];
500488

501-
/** @var bool */
502-
private static $bJSONParsed = false;
489+
private static bool $bJSONParsed = false;
503490

504491
private static function parseJSONBodyForIndices(): void {
505492
if (self::$bJSONParsed) {
@@ -647,10 +634,10 @@ public static function summary(string $sOverrideName = 'Summary'): void {
647634
self::getCurrentSpan()
648635
);
649636

650-
self::initLogger()->addRecord(Monolog\Logger::INFO, $aMessage['--action'], $aMessage);
637+
self::initLogger()->addRecord(Logger::INFO, $aMessage['--action'], $aMessage);
651638
}
652639

653-
public static function getContextForOutput() {
640+
public static function getContextForOutput(): array {
654641
return self::$aSpanMetas[self::getCurrentRequestHash()]->Context->all();
655642
}
656643
}

0 commit comments

Comments
 (0)