Skip to content

Commit

Permalink
Merge pull request #390 from dedoc/feat/method-call-inference-improve…
Browse files Browse the repository at this point in the history
…ments

Method calls inference improvements
  • Loading branch information
romalytvynenko committed May 22, 2024
2 parents dcb7145 + b54e909 commit dbbf57b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Infer/Handler/ArrayItemHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function leave(Node\Expr\ArrayItem $node, Scope $scope)
$scope->setType(
$node,
new ArrayItemType_(
$node->key->value ?? null,
$node->key ? ($scope->getType($node->key)->value ?? null) : null, // @todo handle cases when key is something dynamic
$scope->getType($node->value),
isOptional: false,
shouldUnpack: $node->unpack,
Expand Down
18 changes: 18 additions & 0 deletions src/Infer/Services/ReferenceTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace Dedoc\Scramble\Infer\Services;

use Dedoc\Scramble\Infer\Analyzer\ClassAnalyzer;
use Dedoc\Scramble\Infer\Context;
use Dedoc\Scramble\Infer\Definition\ClassDefinition;
use Dedoc\Scramble\Infer\Definition\FunctionLikeDefinition;
use Dedoc\Scramble\Infer\Extensions\Event\MethodCallEvent;
use Dedoc\Scramble\Infer\Scope\Index;
use Dedoc\Scramble\Infer\Scope\Scope;
use Dedoc\Scramble\Support\Type\CallableStringType;
Expand Down Expand Up @@ -169,6 +171,22 @@ private function resolveMethodCallReferenceType(Scope $scope, MethodCallReferenc
throw new \LogicException('Should not happen.');
}

// Attempting extensions broker before potentially giving up on type inference
if (($calleeType instanceof TemplateType || $calleeType instanceof ObjectType)) {
$unwrappedType = $calleeType instanceof TemplateType && $calleeType->is
? $calleeType->is
: $calleeType;

if ($unwrappedType instanceof ObjectType && $returnType = Context::getInstance()->extensionsBroker->getMethodReturnType(new MethodCallEvent(
instance: $unwrappedType,
name: $type->methodName,
scope: $scope,
arguments: $type->arguments,
))) {
return $returnType;
}
}

// (#TName).listTableDetails()
if ($calleeType instanceof TemplateType) {
// This maybe is not a good idea as it make references bleed into the fully analyzed
Expand Down

0 comments on commit dbbf57b

Please sign in to comment.