Skip to content

Commit ad662e6

Browse files
committed
Code: modernize, PHP 8.1, nette/utils 4.x
1 parent a1d805d commit ad662e6

File tree

10 files changed

+190
-124
lines changed

10 files changed

+190
-124
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ For details on how to use this package, check out our [documentation](.docs).
3232

3333
## Versions
3434

35-
| State | Version | Branch | Nette | PHP |
36-
|-------------|---------|----------|--------|---------|
37-
| dev | `^0.4` | `master` | 3.0+ | `>=7.2` |
38-
| stable | `^0.3` | `master` | 2.4+ | `>=7.2` |
35+
| State | Version | Branch | Nette | PHP |
36+
|-------------|---------|----------|-------|---------|
37+
| dev | `^0.5` | `master` | 3.1+ | `>=8.1` |
38+
| stable | `^0.4` | `master` | 3.1+ | `>=8.1` |
3939

4040

4141
## Development

composer.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@
1818
}
1919
],
2020
"require": {
21-
"php": ">=7.2",
22-
"contributte/tracy": "^0.5.1",
23-
"contributte/utils": "^0.5.0"
21+
"php": ">=8.1",
22+
"contributte/tracy": "^0.6.0",
23+
"contributte/utils": "^0.6.0"
2424
},
2525
"require-dev": {
26-
"ninjify/qa": "^0.12",
27-
"ninjify/nunjuck": "^0.4"
26+
"contributte/phpstan": "^0.1.0",
27+
"contributte/phpunit": "^0.1.0",
28+
"contributte/qa": "^0.4.0"
2829
},
2930
"autoload": {
3031
"psr-4": {
@@ -34,11 +35,16 @@
3435
"src/shortcuts.php"
3536
]
3637
},
38+
"autoload-dev": {
39+
"psr-4": {
40+
"Tests\\": "tests"
41+
}
42+
},
3743
"minimum-stability": "dev",
3844
"prefer-stable": true,
3945
"extra": {
4046
"branch-alias": {
41-
"dev-master": "0.4.x-dev"
47+
"dev-master": "0.5.x-dev"
4248
}
4349
},
4450
"config": {

phpstan.neon

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
includes:
2+
- vendor/contributte/phpstan/phpstan.neon
3+
4+
parameters:
5+
level: 9
6+
phpVersion: 80100
7+
8+
scanDirectories:
9+
- src
10+
11+
fileExtensions:
12+
- php
13+
- phpt
14+
15+
paths:
16+
- src
17+
- .docs

ruleset.xml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
<?xml version="1.0"?>
2-
<ruleset name="Contributte">
3-
<!-- Extending rulesets -->
4-
<rule ref="vendor/ninjify/coding-standard/ruleset.xml"/>
5-
6-
<!-- Contributte -->
7-
<rule ref="Squiz.Classes.ClassFileName">
8-
<exclude name="Squiz.Classes.ClassFileName.NoMatch"/>
9-
</rule>
10-
11-
<!-- Exclude folders -->
12-
<exclude-pattern>/tests/tmp</exclude-pattern>
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset name="Contributte" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
3+
<!-- Rulesets -->
4+
<rule ref="./vendor/contributte/qa/ruleset-8.0.xml"/>
5+
6+
<!-- Rules -->
7+
<rule ref="SlevomatCodingStandard.Files.TypeNameMatchesFileName">
8+
<properties>
9+
<property name="rootNamespaces" type="array">
10+
<element key="src" value="Contributte\Dev"/>
11+
<element key="tests" value="Tests"/>
12+
</property>
13+
</properties>
14+
</rule>
15+
16+
<!-- Excludes -->
17+
<exclude-pattern>/tests/tmp</exclude-pattern>
1318
</ruleset>

src/Dev.php

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use RuntimeException;
88
use Tracy\Debugger;
99
use Tracy\Helpers;
10+
use Traversable;
1011

1112
class Dev
1213
{
@@ -35,9 +36,9 @@ public static function dd(): void
3536
/**
3637
* echo;die
3738
*
38-
* @param mixed $value
39+
* @param scalar $value
3940
*/
40-
public static function ed($value): void
41+
public static function ed(mixed $value): void
4142
{
4243
echo $value;
4344
die;
@@ -46,12 +47,12 @@ public static function ed($value): void
4647
/**
4748
* Foreach dump;
4849
*
49-
* @param mixed $values
50+
* @param array<mixed|mixed[]> $values
5051
*/
51-
public static function fd($values): void
52+
public static function fd(array $values): void
5253
{
53-
foreach ($values as $key => $value) {
54-
if (!is_array($value) && !is_scalar($value)) {
54+
foreach ($values as $value) {
55+
if ($value instanceof Traversable) {
5556
$value = iterator_to_array($value);
5657
}
5758

@@ -63,9 +64,9 @@ public static function fd($values): void
6364
/**
6465
* Foreach dump;die;
6566
*
66-
* @param mixed $values
67+
* @param mixed[] $values
6768
*/
68-
public static function fdd($values): void
69+
public static function fdd(array $values): void
6970
{
7071
self::fd($values);
7172
die;
@@ -74,16 +75,16 @@ public static function fdd($values): void
7475
/**
7576
* Table dump;
7677
*
77-
* @param mixed $values
78+
* @param mixed[] $values
7879
*/
79-
public static function td($values): void
80+
public static function td(array $values): void
8081
{
8182
echo "<table border=1 style='border-color:#DDD;border-collapse:collapse; font-family:Courier New; color:#222; font-size:13px' cellspacing=0 cellpadding=5>";
8283
$th = false;
83-
foreach ($values as $key => $value) {
84+
foreach ($values as $value) {
8485
if (!$th) {
8586
echo '<tr>';
86-
foreach ($value as $key2 => $value2) {
87+
foreach ((array) $value as $key2 => $value2) {
8788
echo '<th>' . $key2 . '</th>';
8889
}
8990

@@ -93,7 +94,7 @@ public static function td($values): void
9394
$th = true;
9495

9596
echo '<tr>';
96-
foreach ($value as $key2 => $value2) {
97+
foreach ((array) $value as $key2 => $value2) {
9798
echo '<td>' . $value2 . '</td>';
9899
}
99100

@@ -106,50 +107,41 @@ public static function td($values): void
106107
/**
107108
* Table dump;die;
108109
*
109-
* @param mixed $values
110+
* @param mixed[] $values
110111
*/
111-
public static function tdd($values): void
112+
public static function tdd(array $values): void
112113
{
113114
self::td($values);
114115
die;
115116
}
116117

117118
/**
118119
* Bar dump shortcut.
119-
*
120-
* @param mixed $var
121-
* @return mixed
122120
*/
123-
public static function bd($var, ?string $title = null)
121+
public static function bd(mixed $var, ?string $title = null): mixed
124122
{
125123
$trace = debug_backtrace();
126124
$traceTitle = (isset($trace[1]['class']) ? htmlspecialchars($trace[1]['class']) . '->' : null) .
127-
htmlspecialchars($trace[1]['function']) . '():' . $trace[0]['line'];
125+
htmlspecialchars($trace[1]['function']) . '():';
128126

129-
if (!is_scalar($title) && $title !== null) {
127+
if ($title === null) {
130128
foreach (func_get_args() as $arg) {
131129
Debugger::barDump($arg, $traceTitle);
132130
}
133131

134132
return $var;
135133
}
136134

137-
return Debugger::barDump($var, $title ?: $traceTitle);
135+
return Debugger::barDump($var, $title);
138136
}
139137

140138
/**
141139
* Function prints from where were method/function called
142-
*
143-
* @return mixed|void
144140
*/
145-
public static function wc(int $level = 1, bool $return = false, bool $fullTrace = false)
141+
public static function wc(int $level = 1, bool $return = false, bool $fullTrace = false): mixed
146142
{
147-
$o = function ($t) {
148-
return (isset($t->class) ? htmlspecialchars($t->class) . '->' : null) . htmlspecialchars($t->function) . '()';
149-
};
150-
$f = function ($t) {
151-
return isset($t->file) ? '(' . Helpers::editorLink($t->file, $t->line) . ')' : null;
152-
};
143+
$o = fn ($t) => (isset($t->class) ? htmlspecialchars($t->class) . '->' : null) . htmlspecialchars($t->function) . '()';
144+
$f = fn ($t) => isset($t->file) ? '(' . Helpers::editorLink($t->file, $t->line ?? null) . ')' : null;
153145

154146
$trace = debug_backtrace();
155147
$target = (object) $trace[$level];
@@ -166,10 +158,12 @@ public static function wc(int $level = 1, bool $return = false, bool $fullTrace
166158
}
167159

168160
if ($return) {
169-
return strip_tags($message);
161+
return strip_tags((string) $message);
170162
}
171163

172-
echo "<pre class='nette-dump'>" . nl2br($message) . '</pre>';
164+
echo "<pre class='nette-dump'>" . nl2br((string) $message) . '</pre>';
165+
166+
return null;
173167
}
174168

175169
/**
@@ -182,18 +176,16 @@ public static function fwc(int $level = 3, bool $return = false): void
182176

183177
/**
184178
* Convert script into shortcut; exit;
185-
*
186-
* @param mixed $code
187179
*/
188-
public static function ss($code): void
180+
public static function ss(string $code): void
189181
{
190182
$array = [
191183
"\t" => "\\t",
192184
"\n" => "\\n",
193185
];
194186

195187
echo strtr($code, $array);
196-
exit();
188+
exit;
197189
}
198190

199191
/**
@@ -217,9 +209,7 @@ public static function l(string $message): void
217209
*/
218210
public static function log(string $message): void
219211
{
220-
$message = array_map(function ($message) {
221-
return !is_scalar($message) ? Json::encode($message) : $message;
222-
}, func_get_args());
212+
$message = array_map(fn ($message) => !is_scalar($message) ? Json::encode($message) : $message, func_get_args());
223213

224214
Debugger::log(implode(', ', $message));
225215
}
@@ -229,7 +219,7 @@ public static function log(string $message): void
229219
*/
230220
public static function erd(): void
231221
{
232-
$e = new RuntimeException;
222+
$e = new RuntimeException();
233223
fd(func_get_args());
234224
echo '<hr />';
235225
fd($e->getTrace());
@@ -263,9 +253,12 @@ public static function cl(object $instance): object
263253

264254
/**
265255
* PHP callback workaround
256+
*
257+
* @return array{object, string}
266258
*/
267259
public static function callback(object $obj, string $method): array
268260
{
269261
return [$obj, $method];
270262
}
263+
271264
}

0 commit comments

Comments
 (0)