Skip to content

Commit e711030

Browse files
author
Kirill Nesmeyanov
committed
Add phan 5.x-dev, phpstan 2.1.x-dev and psalm 5.x-dev type aliases for native type printer
1 parent eb067bd commit e711030

File tree

4 files changed

+194
-70
lines changed

4 files changed

+194
-70
lines changed

resources/aliases/phan.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* List of builtin type aliases of phan 5.x-dev
7+
*/
8+
return [
9+
// int
10+
'integer' => 'int',
11+
'non-zero-int' => 'int',
12+
// string
13+
'callable-string' => 'string',
14+
'class-string' => 'string',
15+
'lowercase-string' => 'string',
16+
'non-empty-string' => 'string',
17+
'non-empty-lowercase-string' => 'string',
18+
'numeric-string' => 'string',
19+
// bool
20+
'boolean' => 'bool',
21+
// float
22+
'double' => 'float',
23+
// array
24+
'associative-array' => 'array',
25+
'callable-array' => 'array',
26+
'list' => 'array',
27+
'non-empty-array' => 'array',
28+
'non-empty-associative-array' => 'array',
29+
'non-empty-list' => 'array',
30+
// object
31+
'callable-object' => 'object',
32+
// mixed
33+
'non-empty-mixed' => 'mixed',
34+
'non-null-mixed' => 'mixed',
35+
'phan-intersection-type' => 'mixed',
36+
'resource' => 'mixed',
37+
// never
38+
'no-return' => 'never',
39+
'never-return' => 'never',
40+
'never-returns' => 'never',
41+
// other
42+
'array-key' => 'int|string',
43+
'scalar' => 'bool|float|int|string',
44+
];

resources/aliases/phpstan.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* List of builtin type aliases of phpstan 2.1.x-dev
7+
*/
8+
return [
9+
// int
10+
'integer' => 'int',
11+
'positive-int' => 'int',
12+
'negative-int' => 'int',
13+
'non-positive-int' => 'int',
14+
'non-negative-int' => 'int',
15+
'non-zero-int' => 'int',
16+
'int-mask' => 'int',
17+
'int-mask-of' => 'int',
18+
// string
19+
'lowercase-string' => 'string',
20+
'uppercase-string' => 'string',
21+
'literal-string' => 'string',
22+
'class-string' => 'string',
23+
'interface-string' => 'string',
24+
'trait-string' => 'string',
25+
'enum-string' => 'string',
26+
'callable-string' => 'string',
27+
'numeric-string' => 'string',
28+
'truthy-string' => 'string',
29+
'non-falsy-string' => 'string',
30+
'non-empty-string' => 'string',
31+
'non-empty-lowercase-string' => 'string',
32+
'non-empty-uppercase-string' => 'string',
33+
'non-empty-literal-string' => 'string',
34+
// bool
35+
'boolean' => 'bool',
36+
// float
37+
'double' => 'float',
38+
// array
39+
'associative-array' => 'array',
40+
'non-empty-array' => 'array',
41+
'callable-array' => 'array',
42+
'list' => 'array',
43+
'non-empty-list' => 'array',
44+
// object
45+
'callable-object' => 'object',
46+
'$this' => 'object',
47+
// callable
48+
'pure-callable' => 'callable',
49+
'pure-closure' => '\Closure',
50+
// mixed
51+
'resource' => 'mixed',
52+
'open-resource' => 'mixed',
53+
'closed-resource' => 'mixed',
54+
'non-empty-mixed' => 'mixed',
55+
'key-of' => 'mixed',
56+
'value-of' => 'mixed',
57+
'template-type' => 'mixed',
58+
// never
59+
'noreturn' => 'never',
60+
'never-return' => 'never',
61+
'never-returns' => 'never',
62+
'no-return' => 'never',
63+
'empty' => 'never',
64+
// other
65+
'array-key' => 'int|string',
66+
'scalar' => 'bool|float|int|string',
67+
'non-empty-scalar' => 'bool|float|int|string',
68+
'empty-scalar' => 'bool|float|int|string',
69+
'number' => 'float|int',
70+
'numeric' => 'float|int|string',
71+
'__stringandstringable' => 'string|\Stringable',
72+
];

resources/aliases/psalm.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* List of builtin type aliases of psalm 5.x-dev
7+
*/
8+
return [
9+
// int
10+
'integer' => 'int',
11+
'positive-int' => 'int',
12+
'non-positive-int' => 'int',
13+
'negative-int' => 'int',
14+
'non-negative-int' => 'int',
15+
'literal-int' => 'int',
16+
// string
17+
'non-empty-string' => 'string',
18+
'truthy-string' => 'string',
19+
'non-falsy-string' => 'string',
20+
'lowercase-string' => 'string',
21+
'non-empty-lowercase-string' => 'string',
22+
'class-string' => 'string',
23+
'interface-string' => 'string',
24+
'enum-string' => 'string',
25+
'trait-string' => 'string',
26+
'callable-string' => 'string',
27+
'numeric-string' => 'string',
28+
'literal-string' => 'string',
29+
'non-empty-literal-string' => 'string',
30+
// bool
31+
'boolean' => 'bool',
32+
// float
33+
'double' => 'float',
34+
'real' => 'float',
35+
// array
36+
'associative-array' => 'array',
37+
'non-empty-array' => 'array',
38+
'callable-array' => 'array',
39+
'list' => 'array',
40+
'non-empty-list' => 'array',
41+
'class-string-map' => 'array',
42+
'public-properties-of' => 'array',
43+
'protected-properties-of' => 'array',
44+
'private-properties-of' => 'array',
45+
'properties-of' => 'array',
46+
// object
47+
'$this' => 'object',
48+
'callable-object' => 'object',
49+
'stringable-object' => '\Stringable',
50+
// callable
51+
'pure-callable' => 'callable',
52+
// mixed
53+
'resource' => 'mixed',
54+
'resource (closed)' => 'mixed',
55+
'closed-resource' => 'mixed',
56+
'non-empty-mixed' => 'mixed',
57+
'key-of' => 'mixed',
58+
// never
59+
'never-return' => 'never',
60+
'never-returns' => 'never',
61+
'no-return' => 'never',
62+
'empty' => 'never',
63+
// other
64+
'array-key' => 'int|string',
65+
'scalar' => 'bool|float|int|string',
66+
'non-empty-scalar' => 'bool|float|int|string',
67+
'empty-scalar' => 'bool|float|int|string',
68+
];

src/NativeTypePrinter.php

Lines changed: 10 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,87 +26,27 @@ class NativeTypePrinter extends PrettyPrinter
2626
/**
2727
* @var array<non-empty-string, non-empty-string>
2828
*/
29-
private array $aliases = [
30-
// never
31-
'never-return' => 'never',
32-
'never-returns' => 'never',
33-
'no-return' => 'never',
34-
'empty' => 'never',
35-
'noreturn' => 'never',
36-
// mixed
37-
'value-of' => 'mixed',
38-
'resource' => 'mixed',
39-
'resource (closed)' => 'mixed',
40-
'closed-resource' => 'mixed',
41-
'non-empty-mixed' => 'mixed',
42-
// int
43-
'int-mask' => 'int',
44-
'integer' => 'int',
45-
'int-mask-of' => 'int',
46-
'literal-int' => 'int',
47-
'positive-int' => 'int',
48-
'negative-int' => 'int',
49-
'non-positive-int' => 'int',
50-
'non-negative-int' => 'int',
51-
'non-zero-int' => 'int',
52-
// float
53-
'double' => 'float',
54-
// bool
55-
'boolean' => 'bool',
56-
// array
57-
'__always-list' => 'array',
58-
'associative-array' => 'array',
59-
'list' => 'array',
60-
'non-empty-list' => 'array',
61-
'non-empty-array' => 'array',
62-
// string
63-
'class-string' => 'string',
64-
'interface-string' => 'string',
65-
'trait-string' => 'string',
66-
'enum-string' => 'string',
67-
'numeric-string' => 'string',
68-
'literal-string' => 'string',
69-
'non-empty-literal-string' => 'string',
70-
'non-empty-string' => 'string',
71-
'lowercase-string' => 'string',
72-
'non-empty-lowercase-string' => 'string',
73-
'truthy-string' => 'string',
74-
'non-falsy-string' => 'string',
75-
// object
76-
'stringable-object' => 'object',
77-
// string|\Stringable
78-
'__stringandstringable' => 'string|stringable',
79-
// int|string
80-
'array-key' => 'int|string',
81-
'key-of' => 'int|string',
82-
// float|int
83-
'number' => 'float|int',
84-
// float|int|string
85-
'numeric' => 'float|int|string',
86-
// bool|float|int|string
87-
'scalar' => 'bool|float|int|string',
88-
'non-empty-scalar' => 'bool|float|int|string',
89-
'empty-scalar' => 'bool|float|int|string',
90-
// callable
91-
'callable-array' => 'callable',
92-
'callable-object' => 'callable',
93-
'callable-string' => 'callable',
94-
'pure-callable' => 'callable',
95-
'pure-Closure' => \Closure::class,
96-
];
29+
private array $aliases = [];
9730

9831
/**
99-
* @param array<non-empty-string, non-empty-string> $aliases
32+
* @param iterable<non-empty-string, non-empty-string> $aliases
10033
* @param non-empty-string $newLine
10134
* @param non-empty-string $indention
10235
*/
10336
public function __construct(
104-
array $aliases = [],
37+
iterable $aliases = [],
10538
string $newLine = self::DEFAULT_NEW_LINE_DELIMITER,
10639
string $indention = self::DEFAULT_INDENTION,
10740
) {
10841
parent::__construct($newLine, $indention);
10942

43+
// preload phan type aliases
44+
$this->aliases += require __DIR__ . '/../resources/aliases/phan.php';
45+
// preload psalm type aliases
46+
$this->aliases += require __DIR__ . '/../resources/aliases/psalm.php';
47+
// preload phpstan type aliases
48+
$this->aliases += require __DIR__ . '/../resources/aliases/phpstan.php';
49+
11050
foreach ($aliases as $alias => $type) {
11151
$this->addTypeAlias($alias, $type);
11252
}

0 commit comments

Comments
 (0)