Skip to content

Commit

Permalink
All FunctionReflection implementations have getFileName()
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Oct 31, 2021
1 parent c836a42 commit 5ddca42
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Analyser/MutatingScope.php
Expand Up @@ -2831,6 +2831,7 @@ public function enterClassMethod(
new PhpMethodFromParserNodeReflection(
$this->getClassReflection(),
$classMethod,
$this->getFile(),
$templateTypeMap,
$this->getRealParameterTypes($classMethod),
array_map(static function (Type $type): Type {
Expand Down Expand Up @@ -2940,6 +2941,7 @@ public function enterFunction(
return $this->enterFunctionLike(
new PhpFunctionFromParserNodeReflection(
$function,
$this->getFile(),
$templateTypeMap,
$this->getRealParameterTypes($function),
array_map(static function (Type $type): Type {
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/FunctionReflection.php
Expand Up @@ -11,6 +11,8 @@ interface FunctionReflection

public function getName(): string;

public function getFileName(): ?string;

/**
* @return \PHPStan\Reflection\ParametersAcceptor[]
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Reflection/Native/NativeFunctionReflection.php
Expand Up @@ -46,6 +46,11 @@ public function getName(): string
return $this->name;
}

public function getFileName(): ?string
{
return null;
}

/**
* @return \PHPStan\Reflection\ParametersAcceptor[]
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Reflection/Php/PhpFunctionFromParserNodeReflection.php
Expand Up @@ -19,6 +19,8 @@ class PhpFunctionFromParserNodeReflection implements \PHPStan\Reflection\Functio

private \PhpParser\Node\FunctionLike $functionLike;

private string $fileName;

private \PHPStan\Type\Generic\TemplateTypeMap $templateTypeMap;

/** @var \PHPStan\Type\Type[] */
Expand Down Expand Up @@ -66,6 +68,7 @@ class PhpFunctionFromParserNodeReflection implements \PHPStan\Reflection\Functio
*/
public function __construct(
FunctionLike $functionLike,
string $fileName,
TemplateTypeMap $templateTypeMap,
array $realParameterTypes,
array $phpDocParameterTypes,
Expand All @@ -81,6 +84,7 @@ public function __construct(
)
{
$this->functionLike = $functionLike;
$this->fileName = $fileName;
$this->templateTypeMap = $templateTypeMap;
$this->realParameterTypes = $realParameterTypes;
$this->phpDocParameterTypes = $phpDocParameterTypes;
Expand All @@ -100,6 +104,11 @@ protected function getFunctionLike(): FunctionLike
return $this->functionLike;
}

public function getFileName(): string
{
return $this->fileName;
}

public function getName(): string
{
if ($this->functionLike instanceof ClassMethod) {
Expand Down
2 changes: 2 additions & 0 deletions src/Reflection/Php/PhpMethodFromParserNodeReflection.php
Expand Up @@ -40,6 +40,7 @@ class PhpMethodFromParserNodeReflection extends PhpFunctionFromParserNodeReflect
public function __construct(
ClassReflection $declaringClass,
ClassMethod $classMethod,
string $fileName,
TemplateTypeMap $templateTypeMap,
array $realParameterTypes,
array $phpDocParameterTypes,
Expand Down Expand Up @@ -79,6 +80,7 @@ public function __construct(

parent::__construct(
$classMethod,
$fileName,
$templateTypeMap,
$realParameterTypes,
$phpDocParameterTypes,
Expand Down

7 comments on commit 5ddca42

@ondrejmirtes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/cc @TomasVotruba :)

@TomasVotruba
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳 👍 Thank you

@TomasVotruba
Copy link
Contributor

@TomasVotruba TomasVotruba commented on 5ddca42 Oct 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, I use this interface in Rector to detect getFileName() method:
https://github.com/phpstan/phpstan-src/blob/master/src/Reflection/ReflectionWithFilename.php

Could the FunctionReflection extend it?

@ondrejmirtes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to remove ReflectionWithFilename interface today, it no longer serves any function. It only has two childs - ClassReflection and PhpFunctionReflection.

Since this change, all ClassReflections and FunctionReflections have getFileName().

@TomasVotruba
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine with me 👍 , I'll update the code after you commit the change then.

@ondrejmirtes
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: 3738fcd

@TomasVotruba
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated 👍

Please sign in to comment.