Skip to content

Commit

Permalink
Use StatementsAnalyzer insteadof Codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
klimick committed May 4, 2024
1 parent 4a89b6a commit 4a41491
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 46 deletions.
11 changes: 6 additions & 5 deletions src/Psalm/ContextualTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Psalm;

use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Type\TemplateInferredTypeReplacer;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Internal\Type\TemplateStandinTypeReplacer;
Expand All @@ -14,21 +15,21 @@ final class ContextualTypeResolver
public function __construct(
private readonly Union $contextual_type,
private readonly TemplateResult $template_result,
private readonly Codebase $codebase,
private readonly StatementsAnalyzer $statements_analyzer,
) {
}

public function withContextualType(Union $type): self
{
return new self($type, $this->template_result, $this->codebase);
return new self($type, $this->template_result, $this->statements_analyzer);
}

public function resolve(): Union
{
return TemplateInferredTypeReplacer::replace(
$this->contextual_type,
$this->template_result,
$this->codebase,
$this->statements_analyzer->getCodebase(),
);
}

Expand All @@ -37,8 +38,8 @@ public function fillTemplateResult(Union $input_type): void
TemplateStandinTypeReplacer::fillTemplateResult(
$this->contextual_type,
$this->template_result,
$this->codebase,
null,
$this->statements_analyzer->getCodebase(),
$this->statements_analyzer,
$input_type,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psalm\Codebase;
use Psalm\Context;
use Psalm\ContextualTypeResolver;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Type\TemplateContextualBoundsCollector;
use Psalm\Internal\Type\TemplateResult;
use Psalm\Storage\FunctionLikeStorage;
Expand All @@ -23,7 +24,7 @@ final class CallLikeContextualTypeExtractor
{
public static function extract(
Context $context,
Codebase $codebase,
StatementsAnalyzer $statements_analyzer,
?FunctionLikeStorage $function_storage,
CollectedArgumentTemplates $collected_templates,
): ContextualTypeResolver {
Expand All @@ -38,12 +39,12 @@ public static function extract(
return new ContextualTypeResolver(
$empty_contextual_type,
$template_result_without_contextual_bounds,
$codebase,
$statements_analyzer,
);
}

$return_type = $function_storage->return_type
?? self::getReturnTypeFromDeclaringConstructor($codebase, $function_storage);
?? self::getReturnTypeFromDeclaringConstructor($statements_analyzer->getCodebase(), $function_storage);

if ($return_type === null) {
$template_result_without_contextual_bounds = new TemplateResult(
Expand All @@ -54,14 +55,14 @@ public static function extract(
return new ContextualTypeResolver(
$empty_contextual_type,
$template_result_without_contextual_bounds,
$codebase,
$statements_analyzer,
);
}

$template_result_with_contextual_bounds = new TemplateResult(
$collected_templates->template_types,
array_merge($collected_templates->lower_bounds, TemplateContextualBoundsCollector::collect(
$codebase,
$statements_analyzer,
$context->contextual_type_resolver->resolve(),
$return_type,
$collected_templates->template_types,
Expand All @@ -71,7 +72,7 @@ public static function extract(
return new ContextualTypeResolver(
$return_type,
$template_result_with_contextual_bounds,
$codebase,
$statements_analyzer,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static function analyze(

$context->contextual_type_resolver = CallLikeContextualTypeExtractor::extract(
$context,
$codebase,
$statements_analyzer,
$function_call_info->function_storage,
$collected_argument_templates,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static function analyze(
$was_contextual_type_resolver = $context->contextual_type_resolver;
$context->contextual_type_resolver = CallLikeContextualTypeExtractor::extract(
$context,
$codebase,
$statements_analyzer,
$method_storage,
$collected_argument_templates,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static function handleMagicMethod(
$was_contextual_type_resolver = $context->contextual_type_resolver;
$context->contextual_type_resolver = CallLikeContextualTypeExtractor::extract(
$context,
$codebase,
$statements_analyzer,
$pseudo_method_storage,
$collected_argument_templates,
);
Expand Down Expand Up @@ -310,7 +310,7 @@ public static function handleMissingOrMagicMethod(
$was_contextual_type_resolver = $context->contextual_type_resolver;
$context->contextual_type_resolver = CallLikeContextualTypeExtractor::extract(
$context,
$codebase,
$statements_analyzer,
$pseudo_method_storage,
$collected_argument_templates,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ private static function analyzeNamedConstructor(
$was_contextual_type_resolver = $context->contextual_type_resolver;
$context->contextual_type_resolver = CallLikeContextualTypeExtractor::extract(
$context,
$codebase,
$statements_analyzer,
$method_storage,
$collected_argument_templates,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static function analyze(
$was_contextual_type_resolver = $context->contextual_type_resolver;
$context->contextual_type_resolver = CallLikeContextualTypeExtractor::extract(
$context,
$codebase,
$statements_analyzer,
$method_storage,
$collected_argument_templates,
);
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Analyzer/Statements/ReturnAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public static function analyze(
? new ContextualTypeResolver(
$next_contextual_type,
new TemplateResult([], []),
$codebase,
$statements_analyzer,
)
: null;

Expand Down
51 changes: 23 additions & 28 deletions src/Psalm/Internal/Type/TemplateContextualBoundsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Psalm\Internal\Type;

use Psalm\Codebase;
use Psalm\Internal\Analyzer\StatementsAnalyzer;
use Psalm\Internal\Type\Comparator\CallableTypeComparator;
use Psalm\Type;
use Psalm\Type\Atomic;
Expand All @@ -25,36 +25,33 @@
*/
final class TemplateContextualBoundsCollector
{
private Codebase $codebase;

/** @var array<string, non-empty-array<string, non-empty-list<Atomic>>> */
private array $collected_atomics = [];

/** @var array<string, non-empty-array<string, Union>> */
private array $template_types;

/**
* @param array<string, non-empty-array<string, Union>> $template_types
*/
private function __construct(Codebase $codebase, array $template_types)
{
$this->codebase = $codebase;
$this->template_types = $template_types;
private function __construct(
private readonly StatementsAnalyzer $statements_analyzer,
private readonly array $template_types,
) {
}

/**
* @param array<string, non-empty-array<string, Union>> $template_types
* @return array<string, non-empty-array<string, Union>>
*/
public static function collect(
Codebase $codebase,
StatementsAnalyzer $statements_analyzer,
Union $contextual_type,
Union $return_type,
array $template_types,
): array {
$collector = new self($codebase, $template_types);
$collector = new self($statements_analyzer, $template_types);
$collector->collectUnion($contextual_type, $return_type);

$codebase = $statements_analyzer->getCodebase();

return array_map(
static fn(array $template_map): array => array_map(
static fn(array $collected_atomics): Union => TypeCombiner::combine($collected_atomics, $codebase),
Expand Down Expand Up @@ -107,15 +104,14 @@ private function collectTemplateType(Atomic $contextual_atomic, TTemplateParam $
*/
private function collectCallable(Atomic $contextual_atomic, Atomic $return_atomic): void
{
$codebase = $this->statements_analyzer->getCodebase();

if ($return_atomic instanceof TNamedObject
&& $return_atomic->value !== 'Closure'
&& $this->codebase->classOrInterfaceExists($return_atomic->value)
&& $this->codebase->methodExists($return_atomic->value . '::__invoke')
&& $codebase->classOrInterfaceExists($return_atomic->value)
&& $codebase->methodExists($return_atomic->value . '::__invoke')
) {
$return_atomic = CallableTypeComparator::getCallableFromAtomic(
$this->codebase,
$return_atomic,
);
$return_atomic = CallableTypeComparator::getCallableFromAtomic($codebase, $return_atomic);
}

if ($return_atomic instanceof TCallable || $return_atomic instanceof TClosure) {
Expand Down Expand Up @@ -171,12 +167,14 @@ private function collectKeyedArray(TKeyedArray $contextual_atomic, Atomic $retur

private function collectGenericObject(TGenericObject $contextual_atomic, Atomic $return_atomic): void
{
$codebase = $this->statements_analyzer->getCodebase();

if ($return_atomic instanceof TGenericObject
&& $this->codebase->classExists($return_atomic->value)
&& $this->codebase->classExtends($return_atomic->value, $contextual_atomic->value)
&& $codebase->classExists($return_atomic->value)
&& $codebase->classExtends($return_atomic->value, $contextual_atomic->value)
) {
$contextual_storage = $this->codebase->classlike_storage_provider->get($contextual_atomic->value);
$return_storage = $this->codebase->classlike_storage_provider->get($return_atomic->value);
$contextual_storage = $codebase->classlike_storage_provider->get($contextual_atomic->value);
$return_storage = $codebase->classlike_storage_provider->get($return_atomic->value);

$contextual_raw_atomic = $contextual_storage->getNamedObjectAtomic();
$return_raw_atomic = $return_storage->getNamedObjectAtomic();
Expand All @@ -190,15 +188,12 @@ private function collectGenericObject(TGenericObject $contextual_atomic, Atomic
TemplateStandinTypeReplacer::fillTemplateResult(
new Union([$contextual_raw_atomic]),
$template_result,
$this->codebase,
null,
$codebase,
$this->statements_analyzer,
new Union([$return_raw_atomic]),
);

$return_atomic = $contextual_raw_atomic->replaceTemplateTypesWithArgTypes(
$template_result,
$this->codebase,
);
$return_atomic = $contextual_raw_atomic->replaceTemplateTypesWithArgTypes($template_result, $codebase);
}

if ($return_atomic instanceof TIterable
Expand Down

0 comments on commit 4a41491

Please sign in to comment.