Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support anonymous object template replacement #9664

Merged
merged 4 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static function analyze(
true,
false,
true,
true,
false,
true,
),
$stmt_type,
Expand Down
93 changes: 69 additions & 24 deletions src/Psalm/Internal/Type/Comparator/KeyedArrayComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
namespace Psalm\Internal\Type\Comparator;

use Psalm\Codebase;
use Psalm\Internal\Type\TemplateInferredTypeReplacer;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Type;
use Psalm\Type\Atomic;
use Psalm\Type\Atomic\TGenericObject;
use Psalm\Type\Atomic\TKeyedArray;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TObjectWithProperties;
use Psalm\Type\Union;

use function array_keys;
use function is_string;
Expand Down Expand Up @@ -285,36 +289,20 @@ public static function isContainedByObjectWithProperties(
): bool {
$all_types_contain = true;

foreach ($container_type_part->properties as $property_name => $container_property_type) {
if (!is_string($property_name)) {
continue;
}

if (!$codebase->classlikes->classOrInterfaceExists($input_type_part->value)) {
$all_types_contain = false;
$input_object_with_keys = self::coerceToObjectWithProperties(
$codebase,
$input_type_part,
$container_type_part,
);

continue;
}

if (!$codebase->properties->propertyExists(
$input_type_part->value . '::$' . $property_name,
true,
)) {
foreach ($container_type_part->properties as $property_name => $container_property_type) {
if (!$input_object_with_keys || !isset($input_object_with_keys->properties[$property_name])) {
$all_types_contain = false;

continue;
}

$property_declaring_class = (string) $codebase->properties->getDeclaringClassForProperty(
$input_type_part . '::$' . $property_name,
true,
);

$class_storage = $codebase->classlike_storage_provider->get($property_declaring_class);

$input_property_storage = $class_storage->properties[$property_name];

$input_property_type = $input_property_storage->type ?: Type::getMixed();
$input_property_type = $input_object_with_keys->properties[$property_name];

$property_type_comparison = new TypeComparisonResult();

Expand Down Expand Up @@ -354,4 +342,61 @@ public static function isContainedByObjectWithProperties(

return $all_types_contain;
}

public static function coerceToObjectWithProperties(
Codebase $codebase,
TNamedObject $input_type_part,
TObjectWithProperties $container_type_part
): ?TObjectWithProperties {
$storage = $codebase->classlikes->getStorageFor($input_type_part->value);

if (!$storage) {
return null;
}

$inferred_lower_bounds = [];

if ($input_type_part instanceof TGenericObject) {
foreach ($storage->template_types ?? [] as $template_name => $templates) {
foreach (array_keys($templates) as $offset => $defining_at) {
$inferred_lower_bounds[$template_name][$defining_at] =
$input_type_part->type_params[$offset];
}
}
}

foreach ($storage->template_extended_params ?? [] as $defining_at => $templates) {
foreach ($templates as $template_name => $template_atomic) {
$inferred_lower_bounds[$template_name][$defining_at] = $template_atomic;
}
}

$properties = [];

foreach ($storage->appearing_property_ids as $property_name => $property_id) {
if (!isset($container_type_part->properties[$property_name])) {
continue;
}

$property_type = $codebase->properties->hasStorage($property_id)
? $codebase->properties->getStorage($property_id)->type
: null;

$properties[$property_name] = $property_type ?? Type::getMixed();
}

$replaced_object = TemplateInferredTypeReplacer::replace(
new Union([
new TObjectWithProperties($properties),
]),
new TemplateResult(
$storage->template_types ?? [],
$inferred_lower_bounds,
),
$codebase,
);

/** @var TObjectWithProperties */
return $replaced_object->getSingleAtomic();
}
}
17 changes: 17 additions & 0 deletions src/Psalm/Internal/Type/TemplateStandinTypeReplacer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Codebase\Methods;
use Psalm\Internal\Type\Comparator\CallableTypeComparator;
use Psalm\Internal\Type\Comparator\KeyedArrayComparator;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Type;
use Psalm\Type\Atomic;
Expand Down Expand Up @@ -581,6 +582,22 @@ private static function findMatchingAtomicTypesForTemplate(
}
}

if ($atomic_input_type instanceof TNamedObject
&& $base_type instanceof TObjectWithProperties
) {
$object_with_keys = KeyedArrayComparator::coerceToObjectWithProperties(
$codebase,
$atomic_input_type,
$base_type,
);

if ($object_with_keys) {
$matching_atomic_types[$object_with_keys->getId()] = $object_with_keys;
}

continue;
}

if ($atomic_input_type instanceof TTemplateParam) {
$matching_atomic_types = array_merge(
$matching_atomic_types,
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type/Atomic/TObjectWithProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function replaceTemplateTypesWithStandins(
foreach ($this->properties as $offset => $property) {
$input_type_param = null;

if ($input_type instanceof TKeyedArray
if ($input_type instanceof TObjectWithProperties
&& isset($input_type->properties[$offset])
) {
$input_type_param = $input_type->properties[$offset];
Expand Down
128 changes: 128 additions & 0 deletions tests/FunctionCallTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,134 @@ function mergeIterable(iterable $lhs, iterable $rhs): iterable
'$iterable===' => 'iterable<int, int|string>',
],
],
'inferTypeFromAnonymousObjectWithTemplatedProperty' => [
'code' => '<?php
/** @template T */
final class Value
{
/** @param T $value */
public function __construct(public readonly mixed $value) {}
}
/**
* @template T
* @param object{value: T} $object
* @return T
*/
function getValue(object $object): mixed
{
return $object->value;
}
/**
* @template T
* @param object{value: object{value: T}} $object
* @return T
*/
function getNestedValue(object $object): mixed
{
return $object->value->value;
}
$object = new Value(new Value(42));
$value = getValue($object);
$nestedValue = getNestedValue($object);',
'assertions' => [
'$value===' => 'Value<42>',
'$nestedValue===' => '42',
],
'ignored_issues' => [],
'php_version' => '8.1',
],
'inferTypeFromAnonymousObjectWithTemplatedPropertyFromTemplatedAncestor' => [
'code' => '<?php
/** @template T */
abstract class AbstractValue
{
/** @param T $value */
public function __construct(public readonly mixed $value) {}
}
/**
* @template TValue
* @extends AbstractValue<TValue>
*/
final class ConcreteValue extends AbstractValue
{
/**
* @param TValue $value
*/
public function __construct(mixed $value)
{
parent::__construct($value);
}
}
/**
* @template T
* @param object{value: T} $object
* @return T
*/
function getValue(object $object): mixed
{
return $object->value;
}
/**
* @template T
* @param object{value: object{value: T}} $object
* @return T
*/
function getNestedValue(object $object): mixed
{
return $object->value->value;
}
$object = new ConcreteValue(new ConcreteValue(42));
$value = getValue($object);
$nestedValue = getNestedValue($object);',
'assertions' => [
'$value===' => 'ConcreteValue<42>',
'$nestedValue===' => '42',
],
'ignored_issues' => [],
'php_version' => '8.1',
],
'inferTypeFromAnonymousObjectWithTemplatedPropertyFromConcreteAncestor' => [
'code' => '<?php
/** @template T */
abstract class AbstractValue
{
/** @param T $value */
public function __construct(public readonly mixed $value) {}
}
/** @extends AbstractValue<int> */
final class IntValue extends AbstractValue {}
final class Nested
{
public function __construct(public readonly IntValue $value) {}
}
/**
* @template T
* @param object{value: T} $object
* @return T
*/
function getValue(object $object): mixed
{
return $object->value;
}
/**
* @template T
* @param object{value: object{value: T}} $object
* @return T
*/
function getNestedValue(object $object): mixed
{
return $object->value->value;
}
$object = new Nested(new IntValue(42));
$value = getValue($object);
$nestedValue = getNestedValue($object);',
'assertions' => [
'$value===' => 'IntValue',
'$nestedValue===' => 'int',
],
'ignored_issues' => [],
'php_version' => '8.1',
],
'countShapedArrays' => [
'code' => '<?php
/** @var array{a?: int} */
Expand Down