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

Add first stab at restricting templates #1604

Closed
wants to merge 2 commits into from
Closed
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
5 changes: 5 additions & 0 deletions src/Psalm/Internal/Analyzer/MethodAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,11 @@ public static function compareMethods(
$template_types,
$codebase
);

$guide_method_storage_return_type->replaceTemplateTypesWithArgTypes(
$template_types,
$codebase
);
}

// treat void as null when comparing against docblock implementer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,7 @@ public static function fleshOutType(
$fleshed_out_type->possibly_undefined = $return_type->possibly_undefined;
$fleshed_out_type->by_ref = $return_type->by_ref;
$fleshed_out_type->initialized = $return_type->initialized;
$fleshed_out_type->had_template = $return_type->had_template;

return $fleshed_out_type;
}
Expand Down
31 changes: 31 additions & 0 deletions src/Psalm/Internal/Analyzer/TypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1605,6 +1605,37 @@ private static function isMatchingTypeContainedBy(
$allow_interface_equality
)) {
$all_types_contain = false;
} elseif ($input_type_part->value !== 'Generator'
&& $container_type_part->value !== 'Generator'
&& !$container_param->had_template
&& !$input_param->had_template
&& !$container_param->hasTemplate()
&& !$input_param->hasTemplate()
&& !$input_param->hasLiteralValue()
) {
// Make sure types are basically the same
if (!self::isContainedBy(
$codebase,
$container_param,
$input_param,
$container_param->ignore_nullable_issues,
$container_param->ignore_falsable_issues,
$has_scalar_match,
$type_coerced,
$type_coerced_from_mixed,
$to_string_cast,
$type_coerced_from_scalar,
$allow_interface_equality
) || $type_coerced
) {
if ($container_param->hasMixed() || $container_param->isArrayKey()) {
$type_coerced_from_mixed = true;
} else {
$all_types_contain = false;
}

$type_coerced = false;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Codebase/Populator.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,7 +905,7 @@ private function convertPhpStormGenericToPsalmGeneric(Type\Union $candidate, $is
{
$atomic_types = $candidate->getTypes();

if (isset($atomic_types['array']) && count($atomic_types) > 1) {
if (isset($atomic_types['array']) && count($atomic_types) > 1 && !isset($atomic_types['null'])) {
$iterator_name = null;
$generic_params = null;

Expand Down Expand Up @@ -947,6 +947,7 @@ private function convertPhpStormGenericToPsalmGeneric(Type\Union $candidate, $is
}

$candidate->removeType('array');
$candidate->removeType($iterator_name);
$candidate->addType($generic_iterator);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Psalm/Internal/LanguageServer/Server/TextDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public function didClose(TextDocumentIdentifier $textDocument)
*
* @param TextDocumentIdentifier $textDocument The text document
* @param Position $position The position inside the text document
* @psalm-return Promise<Location|Hover>
* @psalm-return Promise<Location>|Promise<Hover>
*/
public function definition(TextDocumentIdentifier $textDocument, Position $position): Promise
{
Expand Down Expand Up @@ -244,7 +244,7 @@ public function hover(TextDocumentIdentifier $textDocument, Position $position):
*
* @param TextDocumentIdentifier The text document
* @param Position $position The position
* @psalm-return Promise<CompletionItem[]|CompletionList>
* @psalm-return Promise<array<empty, empty>>|Promise<CompletionList>
*/
public function completion(TextDocumentIdentifier $textDocument, Position $position): Promise
{
Expand Down
Loading