/
Type.php
122 lines (93 loc) 路 2.6 KB
/
Type.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\PhpGenerator;
use Nette;
/**
* PHP return, property and parameter types.
*/
class Type
{
public const
String = 'string',
Int = 'int',
Float = 'float',
Bool = 'bool',
Array = 'array',
Object = 'object',
Callable = 'callable',
Iterable = 'iterable',
Void = 'void',
Never = 'never',
Mixed = 'mixed',
True = 'true',
False = 'false',
Null = 'null',
Self = 'self',
Parent = 'parent',
Static = 'static';
/** @deprecated use Type::String */
public const STRING = self::String;
/** @deprecated use Type::Int */
public const INT = self::Int;
/** @deprecated use Type::Float */
public const FLOAT = self::Float;
/** @deprecated use Type::Bool */
public const BOOL = self::Bool;
/** @deprecated use Type::Array */
public const ARRAY = self::Array;
/** @deprecated use Type::Object */
public const OBJECT = self::Object;
/** @deprecated use Type::Callable */
public const CALLABLE = self::Callable;
/** @deprecated use Type::Iterable */
public const ITERABLE = self::Iterable;
/** @deprecated use Type::Void */
public const VOID = self::Void;
/** @deprecated use Type::Never */
public const NEVER = self::Never;
/** @deprecated use Type::Mixed */
public const MIXED = self::Mixed;
/** @deprecated use Type::False */
public const FALSE = self::False;
/** @deprecated use Type::Null */
public const NULL = self::Null;
/** @deprecated use Type::Self */
public const SELF = self::Self;
/** @deprecated use Type::Parent */
public const PARENT = self::Parent;
/** @deprecated use Type::Static */
public const STATIC = self::Static;
public static function nullable(string $type, bool $nullable = true): string
{
if (str_contains($type, '&')) {
return $nullable
? throw new Nette\InvalidArgumentException('Intersection types cannot be nullable.')
: $type;
}
$nnType = preg_replace('#^\?|^null\||\|null(?=\||$)#i', '', $type);
$always = (bool) preg_match('#^(null|mixed)$#i', $nnType);
if ($nullable) {
return match (true) {
$always, $type !== $nnType => $type,
str_contains($type, '|') => $type . '|null',
default => '?' . $type,
};
} else {
return $always
? throw new Nette\InvalidArgumentException("Type $type cannot be not nullable.")
: $nnType;
}
}
public static function union(string ...$types): string
{
return implode('|', $types);
}
public static function intersection(string ...$types): string
{
return implode('&', $types);
}
}