File: utils-types.php

package info (click to toggle)
php-nette-utils 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: php: 4,219; xml: 9; makefile: 4
file content (105 lines) | stat: -rw-r--r-- 2,478 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

/**
 * PHPStan type tests for Utils.
 * Run: vendor/bin/phpstan analyse tests/types
 */

declare(strict_types=1);

use Nette\Utils\ArrayHash;
use Nette\Utils\Arrays;
use Nette\Utils\Html;
use Nette\Utils\Strings;
use function PHPStan\Testing\assertType;


/** @param ArrayHash<mixed> $hash */
function testArrayHash(ArrayHash $hash): void
{
	foreach ($hash as $key => $value) {
		assertType('(int|string)', $key);
		assertType('mixed', $value);
	}

	assertType('mixed', $hash['key']);
}


function testHtml(Html $html): void
{
	foreach ($html as $key => $child) {
		assertType('int', $key);
		assertType('Nette\Utils\Html|string', $child);
	}

	assertType('Nette\Utils\Html|string', $html[0]);
}


function testArraysSome(): void
{
	$result = Arrays::some([1, 2, 3], function ($value, $key) {
		assertType('1|2|3', $value);
		assertType('0|1|2', $key);
		return $value > 2;
	});
	assertType('bool', $result);
}


function testArraysEvery(): void
{
	$result = Arrays::every([1, 2, 3], function ($value, $key) {
		assertType('1|2|3', $value);
		assertType('0|1|2', $key);
		return $value > 0;
	});
	assertType('bool', $result);
}


function testArraysMap(): void
{
	$result = Arrays::map([1, 2, 3], function ($value) {
		assertType('1|2|3', $value);
		return $value * 2;
	});
	assertType('array<0|1|2, float|int>', $result);
}


function testStringsSplit(): void
{
	$withoutOffset = Strings::split('a,b', '#(,)#');
	assertType('list<string>', $withoutOffset);

	$withOffset = Strings::split('a,b', '#(,)#', captureOffset: true);
	assertType('list<array{string, int}>', $withOffset);
}


function testStringsMatch(): void
{
	$withoutOffset = Strings::match('hello', '#l+#');
	assertType('array<string|null>|null', $withoutOffset);

	$withOffset = Strings::match('hello', '#l+#', captureOffset: true);
	assertType('array<array{string, int}|null>|null', $withOffset);
}


function testStringsMatchAll(): void
{
	$withoutOffset = Strings::matchAll('hello', '#l+#');
	assertType('list<array<string|null>>', $withoutOffset);

	$withOffset = Strings::matchAll('hello', '#l+#', captureOffset: true);
	assertType('list<array<array{string, int}|null>>', $withOffset);

	$lazy = Strings::matchAll('hello', '#l+#', lazy: true);
	assertType('Generator<int, array<string|null>, mixed, mixed>', $lazy);

	$lazyWithOffset = Strings::matchAll('hello', '#l+#', captureOffset: true, lazy: true);
	assertType('Generator<int, array<array{string, int}|null>, mixed, mixed>', $lazyWithOffset);
}