Skip to content

Commit

Permalink
[TypeDeclaration] Improve param by caller type #2 (#5787)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba authored Mar 7, 2021
1 parent e362a51 commit ba14449
Show file tree
Hide file tree
Showing 87 changed files with 1,608 additions and 1,326 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
],
"files": [
"vendor/nette/forms/src/Forms/Controls/SubmitButton.php",
"packages/node-type-resolver/tests/PerNodeTypeResolver/VariableTypeResolver/Fixture/this_class.php.inc",
"rules/restoration/tests/Rector/Use_/RestoreFullyQualifiedNameRector/Source/ShortClassOnly.php",
"rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/AnotherClass.php",
"rules/coding-style/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/Source/Foo.php",
Expand Down
2 changes: 2 additions & 0 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer;
use Symplify\CodingStandard\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer;
use Symplify\CodingStandard\Fixer\Commenting\RemoveCommentedCodeFixer;
use Symplify\CodingStandard\Fixer\LineLength\DocBlockLineLengthFixer;
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
use Symplify\EasyCodingStandard\ValueObject\Option;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
Expand Down Expand Up @@ -69,6 +70,7 @@

// buggy - @todo fix on Symplify master
RemoveCommentedCodeFixer::class,
DocBlockLineLengthFixer::class,

// breaks on-purpose annotated variables
ReturnAssignmentFixer::class,
Expand Down
39 changes: 37 additions & 2 deletions packages/node-collector/src/NodeCollector/NodeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\TypeWithClassName;
Expand All @@ -49,8 +50,8 @@
use ReflectionMethod;

/**
* This service contains all the parsed nodes. E.g. all the functions, method call, classes, static calls etc.
* It's useful in case of context analysis, e.g. find all the usage of class method to detect, if the method is used.
* This service contains all the parsed nodes. E.g. all the functions, method call, classes, static calls etc. It's
* useful in case of context analysis, e.g. find all the usage of class method to detect, if the method is used.
*/
final class NodeRepository
{
Expand All @@ -76,6 +77,7 @@ final class NodeRepository

/**
* E.g. [$this, 'someLocalMethod']
*
* @var array<string, array<string, ArrayCallable[]>>
*/
private $arrayCallablesByTypeAndMethod = [];
Expand Down Expand Up @@ -150,6 +152,7 @@ public function __construct(

/**
* To prevent circular reference
*
* @required
*/
public function autowireNodeRepository(NodeTypeResolver $nodeTypeResolver): void
Expand Down Expand Up @@ -831,7 +834,12 @@ private function resolveNodeClassTypes(Node $node): Type
private function addCallByType(Node $node, Type $classType, string $methodName): void
{
if ($classType instanceof TypeWithClassName) {
if ($classType instanceof ThisType) {
$classType = $classType->getStaticObjectType();
}

$this->callsByTypeAndMethod[$classType->getClassName()][$methodName][] = $node;
$this->addParentTypeWithClassName($classType, $node, $methodName);
}

if ($classType instanceof UnionType) {
Expand Down Expand Up @@ -862,4 +870,31 @@ private function resolveClassReflectionsFromClassTypes(array $classTypes): array

return $classReflections;
}

/**
* @param MethodCall|StaticCall $node
*/
private function addParentTypeWithClassName(
TypeWithClassName $typeWithClassName,
Node $node,
string $methodName
): void {
// include also parent types
if (! $typeWithClassName instanceof ObjectType) {
return;
}

if (! $this->reflectionProvider->hasClass($typeWithClassName->getClassName())) {
return;
}

$classReflection = $this->reflectionProvider->getClass($typeWithClassName->getClassName());
if (! $classReflection instanceof ClassReflection) {
return;
}

foreach ($classReflection->getAncestors() as $ancestorClassReflection) {
$this->callsByTypeAndMethod[$ancestorClassReflection->getName()][$methodName][] = $node;
}
}
}

This file was deleted.

Loading

0 comments on commit ba14449

Please sign in to comment.