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

Allow running new on template objects #9704

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@
use Psalm\Type\Union;

use function array_map;
use function array_merge;
use function array_shift;
use function array_values;
use function implode;
use function in_array;
Expand Down Expand Up @@ -703,15 +701,58 @@ private static function analyzeConstructorExpression(
}
}

$new_type = null;
$new_type = self::getNewType(
$statements_analyzer,
$codebase,
$context,
$stmt,
$stmt_class_type,
$config,
$can_extend,
);

$stmt_class_types = $stmt_class_type->getAtomicTypes();
if (!$has_single_class) {
if ($new_type) {
$statements_analyzer->node_data->setType($stmt, $new_type);
}

while ($stmt_class_types) {
$lhs_type_part = array_shift($stmt_class_types);
ArgumentsAnalyzer::analyze(
$statements_analyzer,
$stmt->getArgs(),
null,
null,
true,
$context,
);

return;
}
}
private static function getNewType(
StatementsAnalyzer $statements_analyzer,
Codebase $codebase,
Context $context,
PhpParser\Node\Expr\New_ $stmt,
Union $stmt_class_type,
Config $config,
bool &$can_extend
): ?Union {
$new_types = [];

foreach ($stmt_class_type->getAtomicTypes() as $lhs_type_part) {
if ($lhs_type_part instanceof TTemplateParam) {
$stmt_class_types = array_merge($stmt_class_types, $lhs_type_part->as->getAtomicTypes());
$as = self::getNewType(
$statements_analyzer,
$codebase,
$context,
$stmt,
$lhs_type_part->as,
$config,
$can_extend,
);
if ($as) {
$new_types []= new Union([$lhs_type_part->replaceAs($as)]);
}
continue;
}

Expand All @@ -735,7 +776,7 @@ private static function analyzeConstructorExpression(
);
}

$new_type = Type::combineUnionTypes($new_type, new Union([$new_type_part]));
$new_types []= new Union([$new_type_part]);

if ($lhs_type_part->as_type
&& $codebase->classlikes->classExists($lhs_type_part->as_type->value)
Expand Down Expand Up @@ -839,7 +880,7 @@ private static function analyzeConstructorExpression(
);
}

$new_type = Type::combineUnionTypes($new_type, new Union([$generated_type]));
$new_types []= new Union([$generated_type]);
}

continue;
Expand Down Expand Up @@ -876,7 +917,7 @@ private static function analyzeConstructorExpression(
) {
// do nothing
} elseif ($lhs_type_part instanceof TNamedObject) {
$new_type = Type::combineUnionTypes($new_type, new Union([$lhs_type_part]));
$new_types []= new Union([$lhs_type_part]);
continue;
} else {
IssueBuffer::maybeAdd(
Expand All @@ -889,24 +930,12 @@ private static function analyzeConstructorExpression(
);
}

$new_type = Type::combineUnionTypes($new_type, Type::getObject());
$new_types []= Type::getObject();
}

if (!$has_single_class) {
if ($new_type) {
$statements_analyzer->node_data->setType($stmt, $new_type);
}

ArgumentsAnalyzer::analyze(
$statements_analyzer,
$stmt->getArgs(),
null,
null,
true,
$context,
);

return;
if ($new_types) {
return Type::combineUnionTypeArray($new_types, $codebase);
}
return null;
}
}
23 changes: 23 additions & 0 deletions tests/ReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,29 @@ function produceParameters(array $configuration): array
}
',
],
'NewFromTemplateObject' => [
'code' => '<?php
/** @psalm-consistent-constructor */
class AggregateResult {}

/**
* @template T as AggregateResult
* @param T $type
* @return T
*/
function aggregate($type) {
$t = new $type;
return $t;
}


class b extends AggregateResult {}

$b = aggregate(new b);',
danog marked this conversation as resolved.
Show resolved Hide resolved
'assertions' => [
'$b===' => 'b',
],
],
];
}

Expand Down