Skip to content
Permalink
Browse files

Also replace templated intersection types that aren’t direct templates

Ref #1675
  • Loading branch information...
muglug committed May 24, 2019
1 parent 569d826 commit 62dacaf97b3ee49ebc130de4ad4bd89742e31fe3
@@ -802,7 +802,7 @@ public function replaceTemplateTypesWithStandins(
*
* @return void
*/
public function replaceTemplateTypesWithArgTypes(array $template_types)
public function replaceTemplateTypesWithArgTypes(array $template_types, ?Codebase $codebase)
{
// do nothing
}
@@ -230,20 +230,20 @@ public function replaceTemplateTypesWithStandins(
*
* @return void
*/
public function replaceTemplateTypesWithArgTypes(array $template_types)
public function replaceTemplateTypesWithArgTypes(array $template_types, ?Codebase $codebase)
{
if ($this->params) {
foreach ($this->params as $param) {
if (!$param->type) {
continue;
}
$param->type->replaceTemplateTypesWithArgTypes($template_types);
$param->type->replaceTemplateTypesWithArgTypes($template_types, $codebase);
}
}
if ($this->return_type) {
$this->return_type->replaceTemplateTypesWithArgTypes($template_types);
$this->return_type->replaceTemplateTypesWithArgTypes($template_types, $codebase);
}
}
@@ -192,14 +192,14 @@ public function replaceTemplateTypesWithStandins(
*
* @return void
*/
public function replaceTemplateTypesWithArgTypes(array $template_types)
public function replaceTemplateTypesWithArgTypes(array $template_types, ?Codebase $codebase)
{
foreach ($this->type_params as $type_param) {
$type_param->replaceTemplateTypesWithArgTypes($template_types);
$type_param->replaceTemplateTypesWithArgTypes($template_types, $codebase);
}
if ($this instanceof TGenericObject || $this instanceof TIterable) {
$this->replaceIntersectionTemplateTypesWithArgTypes($template_types);
$this->replaceIntersectionTemplateTypesWithArgTypes($template_types, $codebase);
}
}
}
@@ -3,6 +3,7 @@
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Codebase;
trait HasIntersectionTrait
{
@@ -73,7 +74,7 @@ public function getIntersectionTypes()
*
* @return void
*/
public function replaceIntersectionTemplateTypesWithArgTypes(array $template_types)
public function replaceIntersectionTemplateTypesWithArgTypes(array $template_types, ?Codebase $codebase)
{
if (!$this->extra_types) {
return;
@@ -91,6 +92,7 @@ public function replaceIntersectionTemplateTypesWithArgTypes(array $template_typ
}
}
} else {
$extra_type->replaceTemplateTypesWithArgTypes($template_types, $codebase);
$new_types[] = $extra_type;
}
}
@@ -328,7 +328,7 @@ public function replaceTemplateTypesWithStandins(
*
* @return void
*/
public function replaceTemplateTypesWithArgTypes(array $template_types)
public function replaceTemplateTypesWithArgTypes(array $template_types, ?Codebase $codebase)
{
foreach ($this->properties as $property) {
$property->replaceTemplateTypesWithArgTypes($template_types);
@@ -1,6 +1,7 @@
<?php
namespace Psalm\Type\Atomic;
use Psalm\Codebase;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Union;
@@ -125,8 +126,8 @@ public function canBeFullyExpressedInPhp()
*
* @return void
*/
public function replaceTemplateTypesWithArgTypes(array $template_types)
public function replaceTemplateTypesWithArgTypes(array $template_types, ?Codebase $codebase)
{
$this->replaceIntersectionTemplateTypesWithArgTypes($template_types);
$this->replaceIntersectionTemplateTypesWithArgTypes($template_types, $codebase);
}
}
@@ -1,6 +1,7 @@
<?php
namespace Psalm\Type\Atomic;
use Psalm\Codebase;
use Psalm\Type;
use Psalm\Type\Union;
@@ -120,8 +121,8 @@ public function canBeFullyExpressedInPhp()
*
* @return void
*/
public function replaceTemplateTypesWithArgTypes(array $template_types)
public function replaceTemplateTypesWithArgTypes(array $template_types, ?Codebase $codebase)
{
$this->replaceIntersectionTemplateTypesWithArgTypes($template_types);
$this->replaceIntersectionTemplateTypesWithArgTypes($template_types, $codebase);
}
}
@@ -1247,7 +1247,7 @@ public function replaceTemplateTypesWithArgTypes(array $template_types, Codebase
$is_mixed = false;
foreach ($this->types as $key => $atomic_type) {
$atomic_type->replaceTemplateTypesWithArgTypes($template_types);
$atomic_type->replaceTemplateTypesWithArgTypes($template_types, $codebase);
if ($atomic_type instanceof Type\Atomic\TTemplateParam) {
$keys_to_unset[] = $key;
@@ -2507,6 +2507,37 @@ public function foo() {
'$a' => 'C',
]
],
'returnTemplateIntersectionGenericObjectAndTemplate' => [
'<?php
/** @psalm-template Tp */
interface I {
/** @psalm-return Tp */
function getMe();
}
class C {}
/**
* @psalm-template T as object
*
* @psalm-param class-string<T> $className
*
* @psalm-return T&I<T>
*/
function makeConcrete(string $className) : object
{
return new class() extends C implements I {
public function getMe() {
return $this;
}
};
}
$a = makeConcrete(C::class);',
[
'$a' => 'C&I<C>'
]
],
];
}

0 comments on commit 62dacaf

Please sign in to comment.
You can’t perform that action at this time.