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

Provide support for templated value-of enum values #9655

Merged
merged 1 commit into from
Apr 16, 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 @@ -3,6 +3,7 @@
namespace Psalm\Internal\Analyzer\Statements\Expression\Call\StaticMethod;

use PhpParser;
use PhpParser\Node\Expr\StaticCall;
use Psalm\CodeLocation;
use Psalm\Codebase;
use Psalm\Config;
Expand Down Expand Up @@ -465,7 +466,7 @@ public static function analyze(
private static function getMethodReturnType(
StatementsAnalyzer $statements_analyzer,
Codebase $codebase,
PhpParser\Node\Expr\StaticCall $stmt,
StaticCall $stmt,
MethodIdentifier $method_id,
array $args,
TemplateResult $template_result,
Expand Down Expand Up @@ -493,40 +494,14 @@ private static function getMethodReturnType(
[$template_type->param_name]
[$template_type->defining_class],
)) {
if ($template_type->param_name === 'TFunctionArgCount') {
$template_result->lower_bounds[$template_type->param_name] = [
'fn-' . strtolower((string)$method_id) => [
new TemplateBound(
Type::getInt(false, count($stmt->getArgs())),
),
],
];
} elseif ($template_type->param_name === 'TPhpMajorVersion') {
$template_result->lower_bounds[$template_type->param_name] = [
'fn-' . strtolower((string)$method_id) => [
new TemplateBound(
Type::getInt(false, $codebase->getMajorAnalysisPhpVersion()),
),
],
];
} elseif ($template_type->param_name === 'TPhpVersionId') {
$template_result->lower_bounds[$template_type->param_name] = [
'fn-' . strtolower((string) $method_id) => [
new TemplateBound(
Type::getInt(
false,
$codebase->analysis_php_version_id,
),
),
],
];
} else {
$template_result->lower_bounds[$template_type->param_name] = [
($template_type->defining_class) => [
new TemplateBound(Type::getNever()),
],
];
}
$template_result->lower_bounds[$template_type->param_name]
= self::resolveTemplateResultLowerBound(
$codebase,
$stmt,
$class_storage,
$method_id,
$template_type,
);
}
}
}
Expand Down Expand Up @@ -632,4 +607,68 @@ private static function hasStaticInType(Type\TypeNode $type): bool
$visitor->traverse($type);
return $visitor->matches();
}

/**
* @return non-empty-array<string,non-empty-list<TemplateBound>>
*/
private static function resolveTemplateResultLowerBound(
Codebase $codebase,
StaticCall $stmt,
ClassLikeStorage $class_storage,
MethodIdentifier $method_id,
TTemplateParam $template_type
): array {
if ($template_type->param_name === 'TFunctionArgCount') {
return [
'fn-' . strtolower((string)$method_id) => [
new TemplateBound(
Type::getInt(false, count($stmt->getArgs())),
),
],
];
}

if ($template_type->param_name === 'TPhpMajorVersion') {
return [
'fn-' . strtolower((string)$method_id) => [
new TemplateBound(
Type::getInt(false, $codebase->getMajorAnalysisPhpVersion()),
),
],
];
}

if ($template_type->param_name === 'TPhpVersionId') {
return [
'fn-' . strtolower((string) $method_id) => [
new TemplateBound(
Type::getInt(
false,
$codebase->analysis_php_version_id,
),
),
],
];
}

if (isset(
$class_storage->template_extended_params[$template_type->defining_class][$template_type->param_name],
)) {
$extended_param_type = $class_storage->template_extended_params[
$template_type->defining_class
][$template_type->param_name];

return [
($template_type->defining_class) => [
new TemplateBound($extended_param_type),
],
];
}

return [
($template_type->defining_class) => [
new TemplateBound(Type::getNever()),
],
];
}
}
2 changes: 0 additions & 2 deletions src/Psalm/Internal/Codebase/Methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,6 @@ public function getMethodReturnType(
return null;
}

$candidate_type = null;

foreach ($class_storage->overridden_method_ids[$appearing_method_name] as $overridden_method_id) {
$overridden_storage = $this->getStorage($overridden_method_id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ private function useTemplatedType(
$storage->template_type_uses_count[$generic_class_lc] = count($atomic_type->type_params);

foreach ($atomic_type->type_params as $type_param) {
$used_type_parameters[] = $type_param;
$used_type_parameters[] = $type_param->replaceClassLike('self', $storage->name);
}

$storage->template_extended_offsets[$atomic_type->value] = $used_type_parameters;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function isContainedBy(
} elseif ($container_type_part instanceof Scalar) {
return UnionTypeComparator::isContainedBy(
$codebase,
$input_type_part->type,
TValueOf::getValueType($input_type_part->type, $codebase) ?? $input_type_part->type,
new Union([$container_type_part]),
false,
false,
Expand Down
6 changes: 6 additions & 0 deletions src/Psalm/Type/Atomic/TValueOf.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ function (EnumCaseStorage $case): Atomic {
));
}

protected function getChildNodeKeys(): array
{
return ['type'];
}


public function getKey(bool $include_extra = true): string
{
return 'value-of<' . $this->type . '>';
Expand Down
40 changes: 40 additions & 0 deletions tests/ValueOfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,46 @@ function foobar(int|string $arg): void
'ignored_issues' => [],
'php_version' => '8.1',
],
'valueOfTemplatedEnum' => [
'code' => '<?php
/**
* @template TValue of string|int
* @psalm-require-implements BackedEnum
*/
trait ValuesFromEnumTrait
{
/**
* @return list<TValue>
*/
public static function values(): array
{
$cases = self::cases();
return array_map(
static fn (BackedEnum $enum) => $enum->value,
$cases
);
}
}

enum Bar: string
{
/**
* @use ValuesFromEnumTrait<value-of<self>>
*/
use ValuesFromEnumTrait;

case BAZ = "baz";
}


$values = Bar::values();
',
'assertions' => [
'$values===' => 'list<\'baz\'>',
],
'ignored_issues' => [],
'php_version' => '8.1',
],
];
}

Expand Down