Skip to content

Commit 0125dee

Browse files
committed
improved native types
1 parent 19e09bd commit 0125dee

8 files changed

Lines changed: 22 additions & 18 deletions

File tree

src/Utils/Arrays.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public static function getKeyOffset(array $array, string|int $key): ?int
106106
/**
107107
* @deprecated use getKeyOffset()
108108
*/
109-
public static function searchKey(array $array, $key): ?int
109+
public static function searchKey(array $array, string|int $key): ?int
110110
{
111111
return self::getKeyOffset($array, $key);
112112
}
@@ -286,7 +286,7 @@ public static function isList(mixed $value): bool
286286
* Reformats table to associative tree. Path looks like 'field|field[]field->field=field'.
287287
* @param string|string[] $path
288288
*/
289-
public static function associate(array $array, $path): array|\stdClass
289+
public static function associate(array $array, string|array $path): array|\stdClass
290290
{
291291
$parts = is_array($path)
292292
? $path
@@ -482,7 +482,7 @@ public static function mapWithKeys(array $array, callable $transformer): array
482482
* Invokes all callbacks and returns array of results.
483483
* @param callable[] $callbacks
484484
*/
485-
public static function invoke(iterable $callbacks, ...$args): array
485+
public static function invoke(iterable $callbacks, mixed ...$args): array
486486
{
487487
$res = [];
488488
foreach ($callbacks as $k => $cb) {
@@ -497,7 +497,7 @@ public static function invoke(iterable $callbacks, ...$args): array
497497
* Invokes method on every object in an array and returns array of results.
498498
* @param object[] $objects
499499
*/
500-
public static function invokeMethod(iterable $objects, string $method, ...$args): array
500+
public static function invokeMethod(iterable $objects, string $method, mixed ...$args): array
501501
{
502502
$res = [];
503503
foreach ($objects as $k => $obj) {

src/Utils/Callback.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class Callback
2525
*/
2626
public static function invokeSafe(string $function, array $args, callable $onError): mixed
2727
{
28-
$prev = set_error_handler(function ($severity, $message, $file) use ($onError, &$prev, $function): ?bool {
28+
$prev = set_error_handler(function (int $severity, string $message, string $file, int $line) use ($onError, &$prev, $function): ?bool {
2929
if ($file === __FILE__) {
3030
$msg = ini_get('html_errors')
3131
? Html::htmlToText($message)
@@ -53,7 +53,7 @@ public static function invokeSafe(string $function, array $args, callable $onErr
5353
* @return callable
5454
* @throws Nette\InvalidArgumentException
5555
*/
56-
public static function check(mixed $callable, bool $syntax = false)
56+
public static function check(mixed $callable, bool $syntax = false): mixed
5757
{
5858
if (!is_callable($callable, $syntax)) {
5959
throw new Nette\InvalidArgumentException(
@@ -87,7 +87,7 @@ public static function toString(mixed $callable): string
8787
* @param callable $callable type check is escalated to ReflectionException
8888
* @throws \ReflectionException if callback is not valid
8989
*/
90-
public static function toReflection($callable): \ReflectionMethod|\ReflectionFunction
90+
public static function toReflection(mixed $callable): \ReflectionMethod|\ReflectionFunction
9191
{
9292
if ($callable instanceof \Closure) {
9393
$callable = self::unwrap($callable);

src/Utils/DateTime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public static function relativeToSeconds(string $relativeTime): int
142142
}
143143

144144

145-
private function apply(string $datetime, $timezone = null, bool $ctr = false): void
145+
private function apply(string $datetime, ?\DateTimeZone $timezone = null, bool $ctr = false): void
146146
{
147147
$relPart = '';
148148
$absPart = preg_replace_callback(

src/Utils/Html.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ final public function offsetSet($index, $child): void
634634
* Returns child node (\ArrayAccess implementation).
635635
* @param int $index
636636
*/
637-
final public function offsetGet($index): HtmlStringable|string
637+
final public function offsetGet($index): self|string
638638
{
639639
return $this->children[$index];
640640
}

src/Utils/Image.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public static function fromBlank(int $width, int $height, ImageColor|array|null
237237
* Returns the type of image from file.
238238
* @return ImageType::*|null
239239
*/
240-
public static function detectTypeFromFile(string $file, &$width = null, &$height = null): ?int
240+
public static function detectTypeFromFile(string $file, mixed &$width = null, mixed &$height = null): ?int
241241
{
242242
[$width, $height, $type] = Helpers::falseToNull(@getimagesize($file)); // @ - files smaller than 12 bytes causes read error
243243
return $type && isset(self::Formats[$type]) ? $type : null;
@@ -248,7 +248,7 @@ public static function detectTypeFromFile(string $file, &$width = null, &$height
248248
* Returns the type of image from string.
249249
* @return ImageType::*|null
250250
*/
251-
public static function detectTypeFromString(string $s, &$width = null, &$height = null): ?int
251+
public static function detectTypeFromString(string $s, mixed &$width = null, mixed &$height = null): ?int
252252
{
253253
[$width, $height, $type] = Helpers::falseToNull(@getimagesizefromstring($s)); // @ - strings smaller than 12 bytes causes read error
254254
return $type && isset(self::Formats[$type]) ? $type : null;
@@ -423,8 +423,8 @@ public function resize(int|string|null $width, int|string|null $height, int $mod
423423
public static function calculateSize(
424424
int $srcWidth,
425425
int $srcHeight,
426-
$newWidth,
427-
$newHeight,
426+
int|string|null $newWidth,
427+
int|string|null $newHeight,
428428
int $mode = self::OrSmaller,
429429
): array
430430
{

src/Utils/Strings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ private static function bytesToChars(string $s, array $groups): array
680680

681681

682682
/** @internal */
683-
public static function pcre(string $func, array $args)
683+
public static function pcre(string $func, array $args): mixed
684684
{
685685
$res = Callback::invokeSafe($func, $args, function (string $message) use ($args): void {
686686
// compile-time error, not detectable by preg_last_error

src/Utils/Type.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ public static function fromReflection(
4040
}
4141

4242

43-
private static function fromReflectionType(\ReflectionType $type, $of, bool $asObject): self|string
43+
private static function fromReflectionType(
44+
\ReflectionType $type,
45+
\ReflectionFunctionAbstract|\ReflectionParameter|\ReflectionProperty $of,
46+
bool $asObject,
47+
): self|string
4448
{
4549
if ($type instanceof \ReflectionNamedType) {
4650
$name = self::resolve($type->getName(), $of);
@@ -291,9 +295,9 @@ private function allowsAll(array $ourTypes, array $givenTypes): bool
291295
{
292296
return Arrays::every(
293297
$ourTypes,
294-
fn($ourType) => Arrays::some(
298+
fn(string $ourType) => Arrays::some(
295299
$givenTypes,
296-
fn($givenType) => Validators::isBuiltinType($ourType)
300+
fn(string $givenType) => Validators::isBuiltinType($ourType)
297301
? strcasecmp($ourType, $givenType) === 0
298302
: is_a($givenType, $ourType, allow_string: true),
299303
),

src/Utils/Validators.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static function assert(mixed $value, string $expected, string $label = 'v
120120
*/
121121
public static function assertField(
122122
array $array,
123-
$key,
123+
int|string $key,
124124
?string $expected = null,
125125
string $label = "item '%' in array",
126126
): void

0 commit comments

Comments
 (0)