1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00
This commit is contained in:
Daniil Gentili 2021-10-14 10:56:54 +02:00
parent 24f1b60e53
commit 244a90ca4f
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
3 changed files with 104 additions and 1 deletions

View File

@ -14,6 +14,7 @@ use function array_map;
use function array_values; use function array_values;
use function count; use function count;
use function implode; use function implode;
use function strpos;
use function substr; use function substr;
trait GenericTrait trait GenericTrait
@ -102,6 +103,10 @@ trait GenericTrait
return $value_type_string . '[]'; return $value_type_string . '[]';
} }
$intersection_pos = strpos($base_value, '&');
if ($intersection_pos !== false) {
$base_value = substr($base_value, 0, $intersection_pos);
}
$type_params = $this->type_params; $type_params = $this->type_params;
//no need for special format if the key is not determined //no need for special format if the key is not determined

View File

@ -6,6 +6,7 @@ use Psalm\Type\Atomic;
use function array_merge; use function array_merge;
use function count; use function count;
use function implode; use function implode;
use function strrpos;
use function substr; use function substr;
/** /**
@ -69,7 +70,16 @@ class TGenericObject extends TNamedObject
int $php_major_version, int $php_major_version,
int $php_minor_version int $php_minor_version
): ?string { ): ?string {
return $this->toNamespacedString($namespace, $aliased_classes, $this_class, true); $result = $this->toNamespacedString($namespace, $aliased_classes, $this_class, true);
$intersection = strrpos($result, '&');
if ($intersection === false || (
($php_major_version === 8 && $php_minor_version >= 1) ||
($php_major_version >= 9)
)
) {
return $result;
}
return substr($result, $intersection+1);
} }
public function equals(Atomic $other_type, bool $ensure_source_equality): bool public function equals(Atomic $other_type, bool $ensure_source_equality): bool

View File

@ -872,6 +872,94 @@ class ReturnTypeManipulationTest extends FileManipulationTestCase
false, false,
true, true,
], ],
'Intersection80' => [
'<?php
/**
* @template T
*/
class container1 {}
/**
* @template TT
* @extends container1<TT>
*/
class container2 extends container1 {}
function ret() {
/** @var container1<int>&container2<int> $a */
$a = new container1;
return $a;
}
',
'<?php
/**
* @template T
*/
class container1 {}
/**
* @template TT
* @extends container1<TT>
*/
class container2 extends container1 {}
/**
* @return container1&container2
*
* @psalm-return container1<int>&container2<int>
*/
function ret(): container2 {
/** @var container1<int>&container2<int> $a */
$a = new container1;
return $a;
}
',
'8.0',
['MissingReturnType'],
false,
true,
],
'Intersection81' => [
'<?php
/**
* @template T
*/
class container1 {}
/**
* @template TT
* @extends container1<TT>
*/
class container2 extends container1 {}
function ret() {
/** @var container1<int>&container2<int> $a */
$a = new container1;
return $a;
}
',
'<?php
/**
* @template T
*/
class container1 {}
/**
* @template TT
* @extends container1<TT>
*/
class container2 extends container1 {}
/**
* @psalm-return container1<int>&container2<int>
*/
function ret(): container1&container2 {
/** @var container1<int>&container2<int> $a */
$a = new container1;
return $a;
}
',
'8.1',
['MissingReturnType'],
false,
true,
]
]; ];
} }
} }