2016-06-14 07:23:57 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2016-07-26 00:37:44 +02:00
|
|
|
namespace Psalm;
|
2016-06-14 07:23:57 +02:00
|
|
|
|
2021-12-03 21:40:18 +01:00
|
|
|
use InvalidArgumentException;
|
2021-08-10 19:07:53 +02:00
|
|
|
use LogicException;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Config;
|
2020-07-22 01:40:35 +02:00
|
|
|
use Psalm\Internal\Type\Comparator\AtomicTypeComparator;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
|
2020-11-22 00:11:29 +01:00
|
|
|
use Psalm\Internal\Type\TypeCombiner;
|
2020-05-14 01:12:45 +02:00
|
|
|
use Psalm\Internal\Type\TypeParser;
|
|
|
|
use Psalm\Internal\Type\TypeTokenizer;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Plugin\EventHandler\Event\StringInterpreterEvent;
|
2021-08-10 19:07:53 +02:00
|
|
|
use Psalm\Type\Atomic;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TArray;
|
2019-01-05 06:15:53 +01:00
|
|
|
use Psalm\Type\Atomic\TArrayKey;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\TBool;
|
2018-03-05 22:06:06 +01:00
|
|
|
use Psalm\Type\Atomic\TClassString;
|
2021-12-13 04:45:57 +01:00
|
|
|
use Psalm\Type\Atomic\TClosure;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\TEmpty;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TFalse;
|
|
|
|
use Psalm\Type\Atomic\TFloat;
|
|
|
|
use Psalm\Type\Atomic\TInt;
|
2019-01-04 20:54:40 +01:00
|
|
|
use Psalm\Type\Atomic\TIterable;
|
2019-10-09 00:44:46 +02:00
|
|
|
use Psalm\Type\Atomic\TList;
|
2018-05-21 18:40:39 +02:00
|
|
|
use Psalm\Type\Atomic\TLiteralClassString;
|
2018-05-05 23:30:18 +02:00
|
|
|
use Psalm\Type\Atomic\TLiteralFloat;
|
|
|
|
use Psalm\Type\Atomic\TLiteralInt;
|
|
|
|
use Psalm\Type\Atomic\TLiteralString;
|
2021-04-01 05:16:21 +02:00
|
|
|
use Psalm\Type\Atomic\TLowercaseString;
|
2017-01-15 01:06:58 +01:00
|
|
|
use Psalm\Type\Atomic\TMixed;
|
|
|
|
use Psalm\Type\Atomic\TNamedObject;
|
2021-06-08 04:55:21 +02:00
|
|
|
use Psalm\Type\Atomic\TNever;
|
2021-12-13 04:45:57 +01:00
|
|
|
use Psalm\Type\Atomic\TNonEmptyList;
|
2021-04-01 05:16:21 +02:00
|
|
|
use Psalm\Type\Atomic\TNonEmptyLowercaseString;
|
|
|
|
use Psalm\Type\Atomic\TNonEmptyString;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TNull;
|
2018-02-16 01:50:50 +01:00
|
|
|
use Psalm\Type\Atomic\TNumeric;
|
2021-01-06 15:01:53 +01:00
|
|
|
use Psalm\Type\Atomic\TNumericString;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TObject;
|
2019-01-18 06:56:24 +01:00
|
|
|
use Psalm\Type\Atomic\TObjectWithProperties;
|
2021-12-13 04:45:57 +01:00
|
|
|
use Psalm\Type\Atomic\TPositiveInt;
|
2017-12-29 18:29:36 +01:00
|
|
|
use Psalm\Type\Atomic\TResource;
|
2019-12-09 23:42:22 +01:00
|
|
|
use Psalm\Type\Atomic\TScalar;
|
2018-08-21 17:40:29 +02:00
|
|
|
use Psalm\Type\Atomic\TSingleLetter;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TString;
|
2019-07-05 22:24:00 +02:00
|
|
|
use Psalm\Type\Atomic\TTemplateParam;
|
2017-12-09 20:53:39 +01:00
|
|
|
use Psalm\Type\Atomic\TTrue;
|
2017-05-19 06:48:26 +02:00
|
|
|
use Psalm\Type\Atomic\TVoid;
|
2016-11-02 07:29:00 +01:00
|
|
|
use Psalm\Type\Union;
|
2021-12-03 21:40:18 +01:00
|
|
|
use UnexpectedValueException;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
|
|
|
use function array_merge;
|
|
|
|
use function array_pop;
|
|
|
|
use function array_shift;
|
|
|
|
use function array_values;
|
|
|
|
use function explode;
|
2020-11-13 19:13:29 +01:00
|
|
|
use function get_class;
|
2021-06-08 04:55:21 +02:00
|
|
|
use function implode;
|
|
|
|
use function preg_quote;
|
|
|
|
use function preg_replace;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function stripos;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strlen;
|
|
|
|
use function strpos;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function strtolower;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function substr;
|
2016-06-14 07:23:57 +02:00
|
|
|
|
|
|
|
abstract class Type
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Parses a string type representation
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2019-02-07 18:25:57 +01:00
|
|
|
* @param array{int,int}|null $php_version
|
2021-12-13 16:28:14 +01:00
|
|
|
* @param array<string, array<string, Union>> $template_type_map
|
2016-06-14 07:23:57 +02:00
|
|
|
*/
|
2018-08-28 18:37:25 +02:00
|
|
|
public static function parseString(
|
2020-09-07 01:36:47 +02:00
|
|
|
string $type_string,
|
|
|
|
?array $php_version = null,
|
2018-12-18 05:29:27 +01:00
|
|
|
array $template_type_map = []
|
2020-09-04 22:26:33 +02:00
|
|
|
): Union {
|
2020-05-14 01:12:45 +02:00
|
|
|
return TypeParser::parseTokens(
|
|
|
|
TypeTokenizer::tokenize(
|
|
|
|
$type_string
|
|
|
|
),
|
2020-04-08 15:35:53 +02:00
|
|
|
$php_version,
|
|
|
|
$template_type_map
|
|
|
|
);
|
2018-02-04 18:23:32 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 05:38:10 +02:00
|
|
|
public static function getFQCLNFromString(
|
|
|
|
string $class,
|
|
|
|
Aliases $aliases
|
2021-12-05 18:51:26 +01:00
|
|
|
): string {
|
2018-06-14 19:49:16 +02:00
|
|
|
if ($class === '') {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new InvalidArgumentException('$class cannot be empty');
|
2018-02-04 18:23:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($class[0] === '\\') {
|
|
|
|
return substr($class, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$imported_namespaces = $aliases->uses;
|
|
|
|
|
|
|
|
if (strpos($class, '\\') !== false) {
|
|
|
|
$class_parts = explode('\\', $class);
|
|
|
|
$first_namespace = array_shift($class_parts);
|
|
|
|
|
|
|
|
if (isset($imported_namespaces[strtolower($first_namespace)])) {
|
|
|
|
return $imported_namespaces[strtolower($first_namespace)] . '\\' . implode('\\', $class_parts);
|
|
|
|
}
|
|
|
|
} elseif (isset($imported_namespaces[strtolower($class)])) {
|
|
|
|
return $imported_namespaces[strtolower($class)];
|
|
|
|
}
|
|
|
|
|
|
|
|
$namespace = $aliases->namespace;
|
|
|
|
|
|
|
|
return ($namespace ? $namespace . '\\' : '') . $class;
|
|
|
|
}
|
|
|
|
|
2019-06-01 02:21:34 +02:00
|
|
|
/**
|
2020-12-29 12:42:12 +01:00
|
|
|
* @param array<lowercase-string, string> $aliased_classes
|
2020-08-23 19:52:31 +02:00
|
|
|
*
|
|
|
|
* @psalm-pure
|
2019-06-01 02:21:34 +02:00
|
|
|
*/
|
|
|
|
public static function getStringFromFQCLN(
|
|
|
|
string $value,
|
|
|
|
?string $namespace,
|
|
|
|
array $aliased_classes,
|
2019-11-17 01:59:08 +01:00
|
|
|
?string $this_class,
|
2020-11-20 01:02:25 +01:00
|
|
|
bool $allow_self = false,
|
|
|
|
bool $was_static = false
|
2021-12-05 18:51:26 +01:00
|
|
|
): string {
|
2019-11-17 01:59:08 +01:00
|
|
|
if ($allow_self && $value === $this_class) {
|
2020-11-20 01:02:25 +01:00
|
|
|
if ($was_static) {
|
|
|
|
return 'static';
|
|
|
|
}
|
2019-06-01 02:21:34 +02:00
|
|
|
return 'self';
|
|
|
|
}
|
|
|
|
|
2019-06-11 16:33:52 +02:00
|
|
|
if (isset($aliased_classes[strtolower($value)])) {
|
|
|
|
return $aliased_classes[strtolower($value)];
|
|
|
|
}
|
|
|
|
|
2019-06-01 02:21:34 +02:00
|
|
|
if ($namespace && stripos($value, $namespace . '\\') === 0) {
|
2019-06-02 07:10:50 +02:00
|
|
|
$candidate = preg_replace(
|
2019-06-01 02:21:34 +02:00
|
|
|
'/^' . preg_quote($namespace . '\\') . '/i',
|
|
|
|
'',
|
|
|
|
$value
|
|
|
|
);
|
|
|
|
|
2019-06-02 07:10:50 +02:00
|
|
|
$candidate_parts = explode('\\', $candidate);
|
|
|
|
|
|
|
|
if (!isset($aliased_classes[strtolower($candidate_parts[0])])) {
|
|
|
|
return $candidate;
|
|
|
|
}
|
2020-09-20 14:55:28 +02:00
|
|
|
} elseif (!$namespace && strpos($value, '\\') === false) {
|
2019-06-01 02:21:34 +02:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2019-06-04 22:36:32 +02:00
|
|
|
if (strpos($value, '\\')) {
|
|
|
|
$parts = explode('\\', $value);
|
|
|
|
|
|
|
|
$suffix = array_pop($parts);
|
|
|
|
|
|
|
|
while ($parts) {
|
|
|
|
$left = implode('\\', $parts);
|
|
|
|
|
|
|
|
if (isset($aliased_classes[strtolower($left)])) {
|
|
|
|
return $aliased_classes[strtolower($left)] . '\\' . $suffix;
|
|
|
|
}
|
|
|
|
|
|
|
|
$suffix = array_pop($parts) . '\\' . $suffix;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 02:21:34 +02:00
|
|
|
return '\\' . $value;
|
|
|
|
}
|
|
|
|
|
2020-10-12 21:46:47 +02:00
|
|
|
public static function getInt(bool $from_calculation = false, ?int $value = null): Union
|
2016-06-15 01:22:29 +02:00
|
|
|
{
|
2018-05-18 17:02:50 +02:00
|
|
|
if ($value !== null) {
|
|
|
|
$union = new Union([new TLiteralInt($value)]);
|
2018-05-05 23:30:18 +02:00
|
|
|
} else {
|
|
|
|
$union = new Union([new TInt()]);
|
|
|
|
}
|
|
|
|
|
2018-04-25 05:02:20 +02:00
|
|
|
$union->from_calculation = $from_calculation;
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2018-04-25 05:02:20 +02:00
|
|
|
return $union;
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
|
|
|
|
2021-04-01 05:16:21 +02:00
|
|
|
public static function getLowercaseString(): Union
|
|
|
|
{
|
|
|
|
$type = new TLowercaseString();
|
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2022-01-23 23:26:24 +01:00
|
|
|
/** @deprecated will be removed in Psalm 5 */
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getPositiveInt(bool $from_calculation = false): Union
|
2020-07-26 21:51:55 +02:00
|
|
|
{
|
2021-12-13 04:45:57 +01:00
|
|
|
$union = new Union([new TPositiveInt()]);
|
2020-07-26 21:51:55 +02:00
|
|
|
$union->from_calculation = $from_calculation;
|
|
|
|
|
|
|
|
return $union;
|
|
|
|
}
|
|
|
|
|
2021-04-01 05:16:21 +02:00
|
|
|
public static function getNonEmptyLowercaseString(): Union
|
|
|
|
{
|
|
|
|
$type = new TNonEmptyLowercaseString();
|
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getNonEmptyString(): Union
|
|
|
|
{
|
|
|
|
$type = new TNonEmptyString();
|
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getNumeric(): Union
|
2018-02-16 01:50:50 +01:00
|
|
|
{
|
|
|
|
$type = new TNumeric;
|
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2021-01-06 15:01:53 +01:00
|
|
|
public static function getNumericString(): Union
|
|
|
|
{
|
|
|
|
$type = new TNumericString;
|
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public static function getString(?string $value = null): Union
|
2016-06-15 01:22:29 +02:00
|
|
|
{
|
2019-03-07 20:56:18 +01:00
|
|
|
$type = null;
|
|
|
|
|
2018-05-18 17:02:50 +02:00
|
|
|
if ($value !== null) {
|
2021-12-03 20:11:20 +01:00
|
|
|
$config = Config::getInstance();
|
2019-04-22 16:01:25 +02:00
|
|
|
|
2021-01-06 15:05:53 +01:00
|
|
|
$event = new StringInterpreterEvent($value);
|
|
|
|
|
|
|
|
$type = $config->eventDispatcher->dispatchStringInterpreter($event);
|
2019-03-07 20:56:18 +01:00
|
|
|
|
2020-06-13 22:08:51 +02:00
|
|
|
if (!$type) {
|
|
|
|
if (strlen($value) < $config->max_string_length) {
|
|
|
|
$type = new TLiteralString($value);
|
|
|
|
} else {
|
2021-12-13 04:45:57 +01:00
|
|
|
$type = new TNonEmptyString();
|
2020-06-13 22:08:51 +02:00
|
|
|
}
|
2019-03-07 20:56:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$type) {
|
2018-05-05 23:30:18 +02:00
|
|
|
$type = new TString();
|
|
|
|
}
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
return new Union([$type]);
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getSingleLetter(): Union
|
2018-08-21 17:40:29 +02:00
|
|
|
{
|
|
|
|
$type = new TSingleLetter;
|
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2020-10-12 21:46:47 +02:00
|
|
|
public static function getClassString(string $extends = 'object'): Union
|
2018-03-05 22:06:06 +01:00
|
|
|
{
|
2019-01-20 04:45:58 +01:00
|
|
|
return new Union([
|
|
|
|
new TClassString(
|
|
|
|
$extends,
|
|
|
|
$extends === 'object'
|
|
|
|
? null
|
|
|
|
: new TNamedObject($extends)
|
2019-07-05 22:24:00 +02:00
|
|
|
),
|
2019-01-20 04:45:58 +01:00
|
|
|
]);
|
2019-01-04 18:28:00 +01:00
|
|
|
}
|
2018-05-21 18:40:39 +02:00
|
|
|
|
2021-08-18 20:22:07 +02:00
|
|
|
public static function getLiteralClassString(string $class_type, bool $definite_class = false): Union
|
2019-01-04 18:28:00 +01:00
|
|
|
{
|
2021-08-18 20:22:07 +02:00
|
|
|
$type = new TLiteralClassString($class_type, $definite_class);
|
2018-03-05 22:06:06 +01:00
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getNull(): Union
|
2016-06-16 02:16:40 +02:00
|
|
|
{
|
2017-01-15 01:06:58 +01:00
|
|
|
$type = new TNull;
|
2016-06-16 02:16:40 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
return new Union([$type]);
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:46:47 +02:00
|
|
|
public static function getMixed(bool $from_loop_isset = false): Union
|
2016-06-15 01:22:29 +02:00
|
|
|
{
|
2018-12-08 19:18:55 +01:00
|
|
|
$type = new TMixed($from_loop_isset);
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
return new Union([$type]);
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getScalar(): Union
|
2019-12-09 23:06:00 +01:00
|
|
|
{
|
2019-12-09 23:35:50 +01:00
|
|
|
$type = new TScalar();
|
2019-12-09 23:06:00 +01:00
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2021-10-27 21:26:31 +02:00
|
|
|
/**
|
|
|
|
* @deprecated will be removed in Psalm 5. See getNever to retrieve a TNever that replaces TEmpty
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getEmpty(): Union
|
2018-05-18 17:02:50 +02:00
|
|
|
{
|
|
|
|
$type = new TEmpty();
|
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2021-05-13 00:46:37 +02:00
|
|
|
public static function getNever(): Union
|
|
|
|
{
|
|
|
|
$type = new TNever();
|
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getBool(): Union
|
2016-06-15 01:22:29 +02:00
|
|
|
{
|
2017-01-15 01:06:58 +01:00
|
|
|
$type = new TBool;
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
return new Union([$type]);
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public static function getFloat(?float $value = null): Union
|
2016-06-15 01:22:29 +02:00
|
|
|
{
|
2018-05-18 17:02:50 +02:00
|
|
|
if ($value !== null) {
|
|
|
|
$type = new TLiteralFloat($value);
|
2018-05-05 23:30:18 +02:00
|
|
|
} else {
|
|
|
|
$type = new TFloat();
|
|
|
|
}
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
return new Union([$type]);
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getObject(): Union
|
2016-06-15 01:22:29 +02:00
|
|
|
{
|
2017-01-15 01:06:58 +01:00
|
|
|
$type = new TObject;
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
return new Union([$type]);
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getClosure(): Union
|
2016-10-21 00:05:28 +02:00
|
|
|
{
|
2021-12-13 04:45:57 +01:00
|
|
|
$type = new TClosure('Closure');
|
2016-10-21 00:05:28 +02:00
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getArrayKey(): Union
|
2019-01-05 06:15:53 +01:00
|
|
|
{
|
|
|
|
$type = new TArrayKey();
|
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getArray(): Union
|
2016-06-15 01:22:29 +02:00
|
|
|
{
|
2017-01-15 01:06:58 +01:00
|
|
|
$type = new TArray(
|
2016-09-09 22:21:49 +02:00
|
|
|
[
|
2021-12-13 16:28:14 +01:00
|
|
|
new Union([new TArrayKey]),
|
|
|
|
new Union([new TMixed]),
|
2016-09-09 22:21:49 +02:00
|
|
|
]
|
|
|
|
);
|
2016-06-15 01:22:29 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
return new Union([$type]);
|
2016-06-15 01:22:29 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getEmptyArray(): Union
|
2016-09-12 06:02:26 +02:00
|
|
|
{
|
2018-05-03 19:56:30 +02:00
|
|
|
$array_type = new TArray(
|
|
|
|
[
|
2021-12-13 16:28:14 +01:00
|
|
|
new Union([new TEmpty]),
|
|
|
|
new Union([new TEmpty]),
|
2018-05-03 19:56:30 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2021-12-13 16:28:14 +01:00
|
|
|
return new Union([
|
2018-05-03 19:56:30 +02:00
|
|
|
$array_type,
|
2016-09-12 06:02:26 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getList(): Union
|
2019-10-10 16:57:43 +02:00
|
|
|
{
|
2021-12-13 16:28:14 +01:00
|
|
|
$type = new TList(new Union([new TMixed]));
|
2019-10-10 16:57:43 +02:00
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getNonEmptyList(): Union
|
2020-07-30 00:10:43 +02:00
|
|
|
{
|
2021-12-13 16:28:14 +01:00
|
|
|
$type = new TNonEmptyList(new Union([new TMixed]));
|
2020-07-30 00:10:43 +02:00
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getVoid(): Union
|
2016-06-16 02:16:40 +02:00
|
|
|
{
|
2017-01-15 01:06:58 +01:00
|
|
|
$type = new TVoid;
|
2016-06-16 02:16:40 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
return new Union([$type]);
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getFalse(): Union
|
2016-06-16 02:16:40 +02:00
|
|
|
{
|
2017-01-15 01:06:58 +01:00
|
|
|
$type = new TFalse;
|
2016-06-16 02:16:40 +02:00
|
|
|
|
2016-09-09 22:21:49 +02:00
|
|
|
return new Union([$type]);
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getTrue(): Union
|
2017-12-09 20:53:39 +01:00
|
|
|
{
|
|
|
|
$type = new TTrue;
|
|
|
|
|
|
|
|
return new Union([$type]);
|
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public static function getResource(): Union
|
2017-12-29 18:29:36 +01:00
|
|
|
{
|
|
|
|
return new Union([new TResource]);
|
|
|
|
}
|
|
|
|
|
2020-06-25 19:05:34 +02:00
|
|
|
/**
|
2021-12-13 16:28:14 +01:00
|
|
|
* @param non-empty-list<Union> $union_types
|
2020-06-25 19:05:34 +02:00
|
|
|
*/
|
2021-12-13 16:28:14 +01:00
|
|
|
public static function combineUnionTypeArray(array $union_types, ?Codebase $codebase): Union
|
2020-06-25 19:05:34 +02:00
|
|
|
{
|
|
|
|
$first_type = array_pop($union_types);
|
|
|
|
|
|
|
|
foreach ($union_types as $type) {
|
|
|
|
$first_type = self::combineUnionTypes($first_type, $type, $codebase);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $first_type;
|
|
|
|
}
|
|
|
|
|
2016-06-16 02:16:40 +02:00
|
|
|
/**
|
|
|
|
* Combines two union types into one
|
2016-11-02 07:29:00 +01:00
|
|
|
*
|
2018-11-28 21:12:08 +01:00
|
|
|
* @param int $literal_limit any greater number of literal types than this
|
|
|
|
* will be merged to a scalar
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2016-06-16 02:16:40 +02:00
|
|
|
*/
|
2018-11-28 16:41:49 +01:00
|
|
|
public static function combineUnionTypes(
|
2021-09-25 04:30:19 +02:00
|
|
|
?Union $type_1,
|
|
|
|
?Union $type_2,
|
2020-09-07 01:36:47 +02:00
|
|
|
?Codebase $codebase = null,
|
2018-11-28 21:12:08 +01:00
|
|
|
bool $overwrite_empty_array = false,
|
2018-12-08 19:18:55 +01:00
|
|
|
bool $allow_mixed_union = true,
|
2018-11-28 21:12:08 +01:00
|
|
|
int $literal_limit = 500
|
2020-09-04 22:26:33 +02:00
|
|
|
): Union {
|
2021-09-25 04:30:19 +02:00
|
|
|
if ($type_2 === null && $type_1 === null) {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new UnexpectedValueException('At least one type must be provided to combine');
|
2021-09-25 04:30:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($type_1 === null) {
|
|
|
|
return $type_2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($type_2 === null) {
|
|
|
|
return $type_1;
|
|
|
|
}
|
|
|
|
|
2019-06-26 06:14:06 +02:00
|
|
|
if ($type_1 === $type_2) {
|
|
|
|
return $type_1;
|
|
|
|
}
|
|
|
|
|
2018-12-08 19:18:55 +01:00
|
|
|
if ($type_1->isVanillaMixed() && $type_2->isVanillaMixed()) {
|
2021-11-07 07:47:33 +01:00
|
|
|
$combined_type = self::getMixed();
|
2018-03-23 18:14:00 +01:00
|
|
|
} else {
|
2018-12-08 19:18:55 +01:00
|
|
|
$both_failed_reconciliation = false;
|
2017-03-13 23:06:56 +01:00
|
|
|
|
2018-03-23 18:14:00 +01:00
|
|
|
if ($type_1->failed_reconciliation) {
|
|
|
|
if ($type_2->failed_reconciliation) {
|
|
|
|
$both_failed_reconciliation = true;
|
|
|
|
} else {
|
2020-09-30 18:28:13 +02:00
|
|
|
$type_2 = clone $type_2;
|
|
|
|
$type_2->parent_nodes += $type_1->parent_nodes;
|
|
|
|
|
2018-03-23 18:14:00 +01:00
|
|
|
return $type_2;
|
|
|
|
}
|
|
|
|
} elseif ($type_2->failed_reconciliation) {
|
2020-09-30 18:28:13 +02:00
|
|
|
$type_1 = clone $type_1;
|
|
|
|
$type_1->parent_nodes += $type_2->parent_nodes;
|
|
|
|
|
2018-03-23 18:14:00 +01:00
|
|
|
return $type_1;
|
2017-03-13 23:06:56 +01:00
|
|
|
}
|
|
|
|
|
2020-11-22 00:11:29 +01:00
|
|
|
$combined_type = TypeCombiner::combine(
|
2018-03-23 18:14:00 +01:00
|
|
|
array_merge(
|
2020-01-04 18:20:26 +01:00
|
|
|
array_values($type_1->getAtomicTypes()),
|
|
|
|
array_values($type_2->getAtomicTypes())
|
2018-11-28 16:41:49 +01:00
|
|
|
),
|
2019-01-05 06:15:53 +01:00
|
|
|
$codebase,
|
2018-11-28 21:12:08 +01:00
|
|
|
$overwrite_empty_array,
|
2018-12-08 19:18:55 +01:00
|
|
|
$allow_mixed_union,
|
2018-11-28 21:12:08 +01:00
|
|
|
$literal_limit
|
2018-03-23 18:14:00 +01:00
|
|
|
);
|
2017-01-27 07:23:12 +01:00
|
|
|
|
2018-03-23 18:14:00 +01:00
|
|
|
if (!$type_1->initialized || !$type_2->initialized) {
|
|
|
|
$combined_type->initialized = false;
|
|
|
|
}
|
2017-01-27 07:23:12 +01:00
|
|
|
|
2018-03-23 18:14:00 +01:00
|
|
|
if ($type_1->from_docblock || $type_2->from_docblock) {
|
|
|
|
$combined_type->from_docblock = true;
|
|
|
|
}
|
2017-03-19 19:39:05 +01:00
|
|
|
|
2018-04-25 05:02:20 +02:00
|
|
|
if ($type_1->from_calculation || $type_2->from_calculation) {
|
|
|
|
$combined_type->from_calculation = true;
|
|
|
|
}
|
|
|
|
|
2018-03-23 18:14:00 +01:00
|
|
|
if ($type_1->ignore_nullable_issues || $type_2->ignore_nullable_issues) {
|
|
|
|
$combined_type->ignore_nullable_issues = true;
|
|
|
|
}
|
2017-05-10 19:36:05 +02:00
|
|
|
|
2018-03-23 18:14:00 +01:00
|
|
|
if ($type_1->ignore_falsable_issues || $type_2->ignore_falsable_issues) {
|
|
|
|
$combined_type->ignore_falsable_issues = true;
|
|
|
|
}
|
2018-01-25 00:52:58 +01:00
|
|
|
|
2019-07-10 20:48:15 +02:00
|
|
|
if ($type_1->had_template && $type_2->had_template) {
|
|
|
|
$combined_type->had_template = true;
|
|
|
|
}
|
|
|
|
|
2020-03-14 06:09:12 +01:00
|
|
|
if ($type_1->reference_free && $type_2->reference_free) {
|
|
|
|
$combined_type->reference_free = true;
|
2019-08-31 15:49:32 +02:00
|
|
|
}
|
|
|
|
|
2018-03-23 18:14:00 +01:00
|
|
|
if ($both_failed_reconciliation) {
|
|
|
|
$combined_type->failed_reconciliation = true;
|
|
|
|
}
|
2017-03-13 23:06:56 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 06:36:56 +01:00
|
|
|
if ($type_1->possibly_undefined || $type_2->possibly_undefined) {
|
2018-03-17 21:53:11 +01:00
|
|
|
$combined_type->possibly_undefined = true;
|
|
|
|
}
|
|
|
|
|
2020-10-26 14:05:48 +01:00
|
|
|
if ($type_1->possibly_undefined_from_try || $type_2->possibly_undefined_from_try) {
|
|
|
|
$combined_type->possibly_undefined_from_try = true;
|
|
|
|
}
|
|
|
|
|
2020-05-22 04:47:58 +02:00
|
|
|
if ($type_1->parent_nodes || $type_2->parent_nodes) {
|
2020-09-28 06:45:02 +02:00
|
|
|
$combined_type->parent_nodes = $type_1->parent_nodes + $type_2->parent_nodes;
|
2019-08-04 16:37:36 +02:00
|
|
|
}
|
|
|
|
|
2020-09-30 18:28:13 +02:00
|
|
|
if ($type_1->by_ref || $type_2->by_ref) {
|
|
|
|
$combined_type->by_ref = true;
|
|
|
|
}
|
|
|
|
|
2017-01-27 07:23:12 +01:00
|
|
|
return $combined_type;
|
2016-06-16 02:16:40 +02:00
|
|
|
}
|
2019-05-17 00:36:36 +02:00
|
|
|
|
2019-05-24 18:48:37 +02:00
|
|
|
/**
|
|
|
|
* Combines two union types into one via an intersection
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function intersectUnionTypes(
|
|
|
|
Union $type_1,
|
2020-04-07 06:13:56 +02:00
|
|
|
Union $type_2,
|
|
|
|
Codebase $codebase
|
2020-09-04 22:26:33 +02:00
|
|
|
): ?Union {
|
2019-12-20 02:42:57 +01:00
|
|
|
$intersection_performed = false;
|
2021-09-25 02:34:21 +02:00
|
|
|
$type_1_mixed = $type_1->isMixed();
|
|
|
|
$type_2_mixed = $type_2->isMixed();
|
2019-12-20 02:42:57 +01:00
|
|
|
|
2021-09-25 02:34:21 +02:00
|
|
|
if ($type_1_mixed && $type_2_mixed) {
|
2021-11-07 07:47:33 +01:00
|
|
|
$combined_type = self::getMixed();
|
2019-05-24 18:48:37 +02:00
|
|
|
} else {
|
|
|
|
$both_failed_reconciliation = false;
|
|
|
|
|
|
|
|
if ($type_1->failed_reconciliation) {
|
|
|
|
if ($type_2->failed_reconciliation) {
|
|
|
|
$both_failed_reconciliation = true;
|
|
|
|
} else {
|
|
|
|
return $type_2;
|
|
|
|
}
|
|
|
|
} elseif ($type_2->failed_reconciliation) {
|
|
|
|
return $type_1;
|
|
|
|
}
|
|
|
|
|
2021-10-02 10:09:40 +02:00
|
|
|
if ($type_1_mixed) {
|
2019-05-24 18:48:37 +02:00
|
|
|
$combined_type = clone $type_2;
|
2019-12-20 02:42:57 +01:00
|
|
|
$intersection_performed = true;
|
2021-09-25 02:34:21 +02:00
|
|
|
} elseif ($type_2_mixed) {
|
2019-05-24 18:48:37 +02:00
|
|
|
$combined_type = clone $type_1;
|
2019-12-20 02:42:57 +01:00
|
|
|
$intersection_performed = true;
|
2019-05-24 18:48:37 +02:00
|
|
|
} else {
|
2021-08-10 19:07:53 +02:00
|
|
|
$combined_type = null;
|
|
|
|
foreach ($type_1->getAtomicTypes() as $type_1_atomic) {
|
|
|
|
foreach ($type_2->getAtomicTypes() as $type_2_atomic) {
|
2021-08-05 22:15:54 +02:00
|
|
|
$intersection_atomic = null;
|
|
|
|
$wider_type = null;
|
2020-04-07 06:13:56 +02:00
|
|
|
if ($type_1_atomic instanceof TNamedObject
|
|
|
|
&& $type_2_atomic instanceof TNamedObject
|
|
|
|
) {
|
2020-11-13 15:43:30 +01:00
|
|
|
if (($type_1_atomic->value === $type_2_atomic->value
|
|
|
|
&& get_class($type_1_atomic) === TNamedObject::class
|
|
|
|
&& get_class($type_2_atomic) !== TNamedObject::class)
|
|
|
|
) {
|
2021-08-05 22:15:54 +02:00
|
|
|
$intersection_atomic = clone $type_2_atomic;
|
|
|
|
$wider_type = $type_1_atomic;
|
2020-11-13 15:43:30 +01:00
|
|
|
$intersection_performed = true;
|
|
|
|
} elseif (($type_1_atomic->value === $type_2_atomic->value
|
|
|
|
&& get_class($type_2_atomic) === TNamedObject::class
|
|
|
|
&& get_class($type_1_atomic) !== TNamedObject::class)
|
|
|
|
) {
|
2021-08-05 22:15:54 +02:00
|
|
|
$intersection_atomic = clone $type_1_atomic;
|
|
|
|
$wider_type = $type_2_atomic;
|
2020-11-13 15:43:30 +01:00
|
|
|
$intersection_performed = true;
|
2021-08-05 22:15:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (null === $intersection_atomic) {
|
|
|
|
if (AtomicTypeComparator::isContainedBy(
|
2020-04-07 06:13:56 +02:00
|
|
|
$codebase,
|
|
|
|
$type_2_atomic,
|
2020-04-07 17:17:52 +02:00
|
|
|
$type_1_atomic
|
2020-04-07 06:13:56 +02:00
|
|
|
)) {
|
2021-08-05 22:15:54 +02:00
|
|
|
$intersection_atomic = clone $type_2_atomic;
|
|
|
|
$wider_type = $type_1_atomic;
|
2020-04-07 06:13:56 +02:00
|
|
|
$intersection_performed = true;
|
2020-07-22 01:40:35 +02:00
|
|
|
} elseif (AtomicTypeComparator::isContainedBy(
|
2020-04-07 06:13:56 +02:00
|
|
|
$codebase,
|
|
|
|
$type_1_atomic,
|
|
|
|
$type_2_atomic
|
|
|
|
)) {
|
2021-08-05 22:15:54 +02:00
|
|
|
$intersection_atomic = clone $type_1_atomic;
|
|
|
|
$wider_type = $type_2_atomic;
|
2020-04-07 06:13:56 +02:00
|
|
|
$intersection_performed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 19:07:53 +02:00
|
|
|
if (static::mayHaveIntersection($type_1_atomic)
|
|
|
|
&& static::mayHaveIntersection($type_2_atomic)
|
2019-05-24 18:48:37 +02:00
|
|
|
) {
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($intersection_atomic === null && $wider_type === null) {
|
2021-08-05 22:15:54 +02:00
|
|
|
$intersection_atomic = clone $type_1_atomic;
|
|
|
|
$wider_type = $type_2_atomic;
|
|
|
|
}
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($intersection_atomic === null || $wider_type === null) {
|
|
|
|
throw new LogicException(
|
|
|
|
'$intersection_atomic and $wider_type should be both set or null.'
|
|
|
|
.' Check the preceding code for errors.'
|
|
|
|
.' Did you forget to assign one of the variables?'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!static::mayHaveIntersection($intersection_atomic)
|
|
|
|
|| !static::mayHaveIntersection($wider_type)
|
|
|
|
) {
|
|
|
|
throw new LogicException(
|
|
|
|
'$intersection_atomic and $wider_type should be both support intersection.'
|
|
|
|
.' Check the preceding code for errors.'
|
|
|
|
);
|
|
|
|
}
|
2021-08-05 22:15:54 +02:00
|
|
|
if (!$intersection_atomic->extra_types) {
|
|
|
|
$intersection_atomic->extra_types = [];
|
2019-05-24 18:48:37 +02:00
|
|
|
}
|
|
|
|
|
2019-12-20 02:42:57 +01:00
|
|
|
$intersection_performed = true;
|
|
|
|
|
2021-08-05 22:15:54 +02:00
|
|
|
$wider_type_clone = clone $wider_type;
|
2019-05-24 18:48:37 +02:00
|
|
|
|
2021-08-05 22:15:54 +02:00
|
|
|
$wider_type_clone->extra_types = [];
|
2019-05-24 18:48:37 +02:00
|
|
|
|
2021-08-05 22:15:54 +02:00
|
|
|
$intersection_atomic->extra_types[$wider_type_clone->getKey()] = $wider_type_clone;
|
2019-05-24 18:48:37 +02:00
|
|
|
|
2021-08-05 22:15:54 +02:00
|
|
|
$wider_type_intersection_types = $wider_type->getIntersectionTypes();
|
2019-05-24 18:48:37 +02:00
|
|
|
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($wider_type_intersection_types !== null) {
|
2021-08-05 22:15:54 +02:00
|
|
|
foreach ($wider_type_intersection_types as $wider_type_intersection_type) {
|
|
|
|
$intersection_atomic->extra_types[$wider_type_intersection_type->getKey()]
|
|
|
|
= clone $wider_type_intersection_type;
|
2019-05-24 18:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 22:15:54 +02:00
|
|
|
if (null !== $intersection_atomic) {
|
2021-08-10 19:07:53 +02:00
|
|
|
if (null === $combined_type) {
|
|
|
|
$combined_type = new Union([$intersection_atomic]);
|
|
|
|
} else {
|
|
|
|
$combined_type->addType($intersection_atomic);
|
|
|
|
}
|
2019-05-26 19:11:43 +02:00
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 22:14:58 +02:00
|
|
|
//if a type is contained by the other, the intersection is the narrowest type
|
|
|
|
if (!$intersection_performed) {
|
|
|
|
$type_1_in_2 = UnionTypeComparator::isContainedBy($codebase, $type_1, $type_2);
|
|
|
|
$type_2_in_1 = UnionTypeComparator::isContainedBy($codebase, $type_2, $type_1);
|
|
|
|
if ($type_1_in_2) {
|
|
|
|
$intersection_performed = true;
|
|
|
|
$combined_type = $type_1;
|
|
|
|
} elseif ($type_2_in_1) {
|
|
|
|
$intersection_performed = true;
|
|
|
|
$combined_type = $type_2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($combined_type !== null) {
|
|
|
|
if (!$type_1->initialized && !$type_2->initialized) {
|
|
|
|
$combined_type->initialized = false;
|
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($type_1->possibly_undefined_from_try && $type_2->possibly_undefined_from_try) {
|
|
|
|
$combined_type->possibly_undefined_from_try = true;
|
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($type_1->from_docblock && $type_2->from_docblock) {
|
|
|
|
$combined_type->from_docblock = true;
|
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($type_1->from_calculation && $type_2->from_calculation) {
|
|
|
|
$combined_type->from_calculation = true;
|
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($type_1->ignore_nullable_issues && $type_2->ignore_nullable_issues) {
|
|
|
|
$combined_type->ignore_nullable_issues = true;
|
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($type_1->ignore_falsable_issues && $type_2->ignore_falsable_issues) {
|
|
|
|
$combined_type->ignore_falsable_issues = true;
|
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($both_failed_reconciliation) {
|
|
|
|
$combined_type->failed_reconciliation = true;
|
|
|
|
}
|
2019-05-24 18:48:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 02:42:57 +01:00
|
|
|
if (!$intersection_performed && $type_1->getId() !== $type_2->getId()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-08-10 19:07:53 +02:00
|
|
|
if ($type_1->possibly_undefined && $type_2->possibly_undefined && $combined_type !== null) {
|
2019-05-24 18:48:37 +02:00
|
|
|
$combined_type->possibly_undefined = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $combined_type;
|
|
|
|
}
|
2021-08-10 19:07:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @psalm-assert-if-true TIterable|TNamedObject|TTemplateParam|TObjectWithProperties $type
|
|
|
|
*/
|
|
|
|
private static function mayHaveIntersection(Atomic $type): bool
|
|
|
|
{
|
|
|
|
return $type instanceof TIterable
|
|
|
|
|| $type instanceof TNamedObject
|
|
|
|
|| $type instanceof TTemplateParam
|
|
|
|
|| $type instanceof TObjectWithProperties;
|
|
|
|
}
|
2016-06-14 07:23:57 +02:00
|
|
|
}
|