Skip to content

Commit ede5984

Browse files
committed
Raise phpstan level to max
1 parent 605487f commit ede5984

File tree

11 files changed

+175
-197
lines changed

11 files changed

+175
-197
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 0
2+
level: max
33
paths:
44
- src
55
- tests

src/Debug.php

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
use Fbn\Debug\Logger;
1515
use Psr\Log\LoggerInterface;
1616

17-
/**
18-
* Main class.
19-
*/
2017
class Debug
2118
{
2219
/**
@@ -33,8 +30,6 @@ class Debug
3330
private $defaultFlags;
3431

3532
/**
36-
* The logger instance.
37-
*
3833
* @var LoggerInterface
3934
*/
4035
private $logger;
@@ -47,23 +42,19 @@ class Debug
4742
private static $instance;
4843

4944
/**
50-
* Constructor.
51-
*
52-
* @param int $defaultFlags Default flags for tweaking the output.
53-
* @param LoggerInterface $logger The logger instance.
54-
*
55-
* @return void
45+
* @param int $defaultFlags Default flags for tweaking the output.
46+
* @param LoggerInterface $logger
5647
*/
57-
final private function __construct(int $defaultFlags, LoggerInterface $logger)
58-
{
48+
final private function __construct(
49+
int $defaultFlags,
50+
LoggerInterface $logger
51+
) {
5952
$this->defaultFlags = $defaultFlags;
6053
$this->logger = $logger;
6154
}
6255

6356
/**
6457
* Get singleton instance.
65-
*
66-
* @return Debug
6758
*/
6859
public static function getInstance(): Debug
6960
{
@@ -87,22 +78,28 @@ public static function getInstance(): Debug
8778
* If `logger` is specified, `log_file` will be ignored. If neither is present,
8879
* nothing will be logged.
8980
*
90-
* @param array $settings Settings.
91-
*
92-
* @return void
81+
* @param array<string,mixed> $settings
9382
*/
94-
public static function init(array $settings = [])
83+
public static function init(array $settings = []): void
9584
{
9685
$defaultFlags = 0;
97-
98-
if (isset($settings['use_vardump']) && $settings['use_vardump']) {
86+
if (
87+
isset($settings['use_vardump'])
88+
&& $settings['use_vardump'] === true
89+
) {
9990
$defaultFlags |= self::USE_VARDUMP;
10091
}
101-
if (isset($settings['use_htmlentities']) && $settings['use_htmlentities']) {
92+
if (
93+
isset($settings['use_htmlentities'])
94+
&& $settings['use_htmlentities'] === true
95+
) {
10296
$defaultFlags |= self::USE_HTMLENTITIES;
10397
}
10498

105-
if (isset($settings['logger']) && $settings['logger'] instanceof LoggerInterface) {
99+
if (
100+
isset($settings['logger'])
101+
&& $settings['logger'] instanceof LoggerInterface
102+
) {
106103
$logger = $settings['logger'];
107104
} else {
108105
if (isset($settings['log_file'])) {
@@ -117,14 +114,12 @@ public static function init(array $settings = [])
117114
}
118115

119116
/**
120-
* Print debug value
117+
* Print debug value.
121118
*
122-
* @param mixed $var The variable to analyse.
123-
* @param int|null $flags Flags for tweaking the output.
124-
*
125-
* @return void
119+
* @param mixed $var The variable to analyse.
120+
* @param int|null $flags Flags for tweaking the output.
126121
*/
127-
public function printValue($var, int $flags = null)
122+
public function printValue($var, ?int $flags = null): void
128123
{
129124
if ($flags === null) {
130125
$flags = $this->defaultFlags;
@@ -140,12 +135,11 @@ public function printValue($var, int $flags = null)
140135
/**
141136
* Return debug value.
142137
*
143-
* @param mixed $var The variable to analyse.
144-
* @param int|null $flags Flags for tweaking the output.
145-
*
138+
* @param mixed $var The variable to analyse.
139+
* @param int|null $flags Flags for tweaking the output.
146140
* @return mixed
147141
*/
148-
public function debugValue($var, int $flags = null)
142+
public function debugValue($var, ?int $flags = null)
149143
{
150144
if ($flags === null) {
151145
$flags = $this->defaultFlags;
@@ -162,6 +156,12 @@ public function debugValue($var, int $flags = null)
162156
$output = ob_get_contents();
163157
ob_end_clean();
164158

159+
if ($output === false) {
160+
throw new \UnexpectedValueException(
161+
'Unable to get debug value of variable'
162+
);
163+
}
164+
165165
if (($flags & self::USE_HTMLENTITIES) === self::USE_HTMLENTITIES) {
166166
$output = htmlentities($output, ENT_NOQUOTES);
167167
}
@@ -172,20 +172,16 @@ public function debugValue($var, int $flags = null)
172172
/**
173173
* Log debug value.
174174
*
175-
* @param mixed $var The variable to analyse.
176-
* @param int|null $flags Flags for tweaking the output.
177-
*
178-
* @return mixed
175+
* @param mixed $var The variable to analyse.
176+
* @param int|null $flags Flags for tweaking the output.
179177
*/
180-
public function logValue($var, int $flags = null)
178+
public function logValue($var, ?int $flags = null): void
181179
{
182180
$this->logger->debug(static::debugValue($var, $flags));
183181
}
184182

185183
/**
186184
* Check whether we are in a CLI environment.
187-
*
188-
* @return bool
189185
*/
190186
protected function isCli(): bool
191187
{

src/Logger.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,19 @@ final class Logger extends AbstractLogger
2626
public const DATE_FORMAT = 'Y-m-d H:i:s.u';
2727

2828
/**
29-
* Logfile.
30-
*
3129
* @var string|null
3230
*/
3331
private $logfile;
3432

3533
/**
36-
* Date format.
37-
*
3834
* @var string
3935
* @see DATE_FORMAT
4036
*/
4137
private $dateFormat;
4238

4339
/**
44-
* Constructor.
45-
*
46-
* @param string|null $logfile The log file.
47-
* @param string $dateFormat The date format.
48-
*
49-
* @return void
40+
* @param string|null $logfile
41+
* @param string $dateFormat
5042
*/
5143
public function __construct(
5244
string $logfile = null,
@@ -59,13 +51,11 @@ public function __construct(
5951
/**
6052
* Logs with an arbitrary level.
6153
*
62-
* @param mixed $level
54+
* @param mixed $level
6355
* @param string $message
64-
* @param array $context
65-
*
66-
* @return void
56+
* @param mixed[] $context
6757
*/
68-
public function log($level, $message, array $context = [])
58+
public function log($level, $message, array $context = []): void
6959
{
7060
if ($level !== LogLevel::DEBUG || $this->logfile == null) {
7161
return;

src/debug-functions.php

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,61 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
/**
13-
* Debug functions.
14-
*/
15-
1612
use Fbn\Debug\Debug;
1713

1814
/**
1915
* Initialize singleton instance.
2016
*
21-
* @param array $settings Settings.
22-
*
23-
* @return void
24-
* @see Debug::init()
17+
* @param array<string,mixed> $settings
18+
* @see Debug::init()
2519
*/
26-
function dbginit(array $settings = [])
20+
function dbginit(array $settings = []): void
2721
{
2822
Debug::init($settings);
2923
}
3024

3125
/**
32-
* Print debug value
33-
*
34-
* @param mixed $var The variable to analyse.
35-
* @param int|null $flags Flags for tweaking the output.
26+
* Print debug value.
3627
*
37-
* @return void
28+
* @param mixed $var The variable to analyse.
29+
* @param int|null $flags Flags for tweaking the output.
3830
*/
39-
function dbg($var, int $flags = null)
31+
function dbg($var, ?int $flags = null): void
4032
{
4133
Debug::getInstance()->printValue($var, $flags);
4234
}
4335

4436
/**
4537
* Return debug value.
4638
*
47-
* @param mixed $var The variable to analyse.
48-
* @param int|null $flags Flags for tweaking the output.
49-
*
39+
* @param mixed $var The variable to analyse.
40+
* @param int|null $flags Flags for tweaking the output.
5041
* @return mixed
5142
*/
52-
function dbgr($var, int $flags = null)
43+
function dbgr($var, ?int $flags = null)
5344
{
5445
return Debug::getInstance()->debugValue($var, $flags);
5546
}
5647

5748
/**
5849
* Log debug value.
5950
*
60-
* @param mixed $var The variable to analyse.
61-
* @param int|null $flags Flags for tweaking the output.
62-
*
63-
* @return void
51+
* @param mixed $var The variable to analyse.
52+
* @param int|null $flags Flags for tweaking the output.
6453
*/
65-
function dbglog($var, int $flags = null)
54+
function dbglog($var, int $flags = null): void
6655
{
6756
Debug::getInstance()->logValue($var, $flags);
6857
}
6958

7059
/**
7160
* Print debug value and die.
7261
*
73-
* @param mixed $var The variable to analyse.
74-
* @param int|null $flags Flags for tweaking the output.
75-
*
76-
* @return void
62+
* @param mixed $var The variable to analyse.
63+
* @param int|null $flags Flags for tweaking the output.
7764
* @codeCoverageIgnore
7865
*/
79-
function dbgdie($var, int $flags = null)
66+
function dbgdie($var, ?int $flags = null): void
8067
{
8168
dbg($var, $flags);
8269
exit;
@@ -85,12 +72,10 @@ function dbgdie($var, int $flags = null)
8572
/**
8673
* Throw exception with debug value.
8774
*
88-
* @param mixed $var The variable to analyse.
89-
* @param int|null $flags Flags for tweaking the output.
90-
*
91-
* @return void
75+
* @param mixed $var The variable to analyse.
76+
* @param int|null $flags Flags for tweaking the output.
9277
*/
93-
function dbgthrow($var, int $flags = null)
78+
function dbgthrow($var, int $flags = null): void
9479
{
9580
throw new \Exception(dbgr($var, $flags));
9681
}

tests/DbgTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,31 @@
1111

1212
namespace Fbn\Debug\Test;
1313

14-
/**
15-
* Tests for function `dbg`.
16-
*/
1714
class DbgTest extends \PHPUnit\Framework\TestCase
1815
{
19-
public function testPrintNumber()
16+
public function testPrintNumber(): void
2017
{
2118
$var = TestHelper::randomInt();
2219

2320
self::assertEquals($var, $this->captureOutput($var));
2421
}
2522

26-
public function testPrintString()
23+
public function testPrintString(): void
2724
{
2825
$var = TestHelper::randomString();
2926

3027
self::assertEquals($var, $this->captureOutput($var));
3128
}
3229

33-
public function testPrintArray()
30+
public function testPrintArray(): void
3431
{
3532
$var = TestHelper::randomArray();
3633
$expected = TestHelper::makeArrayOutput($var);
3734

3835
self::assertEquals($expected, $this->captureOutput($var));
3936
}
4037

41-
public function testPrintArrayNonCli()
38+
public function testPrintArrayNonCli(): void
4239
{
4340
TestDebug::init();
4441

@@ -51,17 +48,20 @@ public function testPrintArrayNonCli()
5148
/**
5249
* Capture and return output of function `dbg`.
5350
*
54-
* @param mixed $var The variable to analyse.
55-
* @param int $flags Flags for tweaking the output.
56-
*
57-
* @return string
51+
* @param mixed $var The variable to analyse.
52+
* @param int|null $flags Flags for tweaking the output.
5853
*/
59-
private function captureOutput($var, int $flags = null)
54+
private function captureOutput($var, ?int $flags = null): string
6055
{
6156
ob_start();
6257
dbg($var, $flags);
6358
$output = ob_get_contents();
6459
ob_end_clean();
60+
61+
if ($output === false) {
62+
throw new \UnexpectedValueException('Unable to capture output');
63+
}
64+
6565
return $output;
6666
}
6767
}

0 commit comments

Comments
 (0)