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

Remove type-hiding name methods #5912

Merged
merged 8 commits into from
Mar 19, 2021
Merged
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
78 changes: 0 additions & 78 deletions packages/NodeNameResolver/NodeNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -184,22 +182,6 @@ public function getNames(array $nodes): array
return $names;
}

/**
* @param Node[] $nodes
*/
public function haveName(array $nodes, string $name): bool
{
foreach ($nodes as $node) {
if (! $this->isName($node, $name)) {
continue;
}

return true;
}

return false;
}

public function isLocalPropertyFetchNamed(Node $node, string $name): bool
{
if (! $node instanceof PropertyFetch) {
Expand All @@ -221,27 +203,6 @@ public function isLocalPropertyFetchNamed(Node $node, string $name): bool
return $this->isName($node->name, $name);
}

public function isLocalStaticPropertyFetchNamed(Node $node, string $name): bool
{
if (! $node instanceof StaticPropertyFetch) {
return false;
}

return $this->isName($node->name, $name);
}

/**
* @param string[] $names
*/
public function isFuncCallNames(Node $node, array $names): bool
{
if (! $node instanceof FuncCall) {
return false;
}

return $this->isNames($node, $names);
}

/**
* Ends with ucname
* Starts with adjective, e.g. (Post $firstPost, Post $secondPost)
Expand Down Expand Up @@ -287,18 +248,6 @@ public function isLocalMethodCallsNamed(Node $node, array $names): bool
return false;
}

/**
* @deprecated Helper function causes to lose the type on the outside. Better avoid it
*/
public function isFuncCallName(Node $node, string $name): bool
{
if (! $node instanceof FuncCall) {
return false;
}

return $this->isName($node, $name);
}

/**
* @deprecated Helper function causes to lose the type on the outside. Better avoid it
*/
Expand All @@ -319,33 +268,6 @@ public function isStaticCallNamed(Node $node, string $className, string $methodN
return $this->isName($node->name, $methodName);
}

/**
* @deprecated Helper function causes to lose the type on the outside. Better avoid it
* @param string[] $methodNames
*/
public function isStaticCallsNamed(Node $node, string $className, array $methodNames): bool
{
foreach ($methodNames as $methodName) {
if ($this->isStaticCallNamed($node, $className, $methodName)) {
return true;
}
}

return false;
}

/**
* @deprecated Helper function causes to lose the type on the outside. Better avoid it
*/
public function isVariableName(Node $node, string $name): bool
{
if (! $node instanceof Variable) {
return false;
}

return $this->isName($node, $name);
}

/**
* @param ObjectType[] $desiredObjectTypes
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,16 @@

namespace Rector\VendorLocker\NodeVendorLocker;

use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use Rector\NodeCollector\NodeCollector\NodeRepository;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\VendorLocker\Reflection\ClassReflectionAncestorAnalyzer;
use Rector\VendorLocker\Reflection\MethodReflectionContractAnalyzer;

final class ClassMethodParamVendorLockResolver
{
/**
* @var ClassReflectionAncestorAnalyzer
*/
private $classReflectionAncestorAnalyzer;

/**
* @var MethodReflectionContractAnalyzer
*/
private $methodReflectionContractAnalyzer;

/**
* @var NodeNameResolver
*/
Expand All @@ -35,14 +24,8 @@ final class ClassMethodParamVendorLockResolver
*/
private $nodeRepository;

public function __construct(
ClassReflectionAncestorAnalyzer $classReflectionAncestorAnalyzer,
MethodReflectionContractAnalyzer $methodReflectionContractAnalyzer,
NodeNameResolver $nodeNameResolver,
NodeRepository $nodeRepository
) {
$this->classReflectionAncestorAnalyzer = $classReflectionAncestorAnalyzer;
$this->methodReflectionContractAnalyzer = $methodReflectionContractAnalyzer;
public function __construct(NodeNameResolver $nodeNameResolver, NodeRepository $nodeRepository)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->nodeRepository = $nodeRepository;
}
Expand All @@ -59,58 +42,36 @@ public function isVendorLocked(ClassMethod $classMethod, int $paramPosition): bo
return false;
}

if (! $this->classReflectionAncestorAnalyzer->hasAncestors($classReflection)) {
return false;
if ($classMethod->isMagic()) {
return true;
}

$methodName = $this->nodeNameResolver->getName($classMethod);

if ($classReflection->getParentClass() !== false) {
$vendorLock = $this->isParentClassVendorLocking(
$classReflection->getParentClass(),
$paramPosition,
$methodName
);
if ($vendorLock !== null) {
return $vendorLock;
foreach ($classReflection->getAncestors() as $ancestorClassReflection) {
// skip self
if ($ancestorClassReflection === $classReflection) {
continue;
}
}

if ($classReflection->isClass()) {
return $this->methodReflectionContractAnalyzer->hasInterfaceContract($classReflection, $methodName);
}

if ($classReflection->isInterface()) {
return $this->methodReflectionContractAnalyzer->hasInterfaceContract($classReflection, $methodName);
}
if (! $ancestorClassReflection->hasNativeMethod($methodName)) {
continue;
}

return false;
}
// class is vendor, its locking us
$classLike = $this->nodeRepository->findClassLike($ancestorClassReflection->getName());
if (! $classLike instanceof ClassLike) {
return true;
}

private function isParentClassVendorLocking(
ClassReflection $parentClassReflection,
int $paramPosition,
string $methodName
): ?bool {
$parentClass = $this->nodeRepository->findClass($parentClassReflection->getName());
if ($parentClass !== null) {
$parentClassMethod = $parentClass->getMethod($methodName);
// parent class method in local scope → it's ok
if ($parentClassMethod !== null) {
// parent method has no type → we cannot change it here
if (! isset($parentClassMethod->params[$paramPosition])) {
return false;
}
return $parentClassMethod->params[$paramPosition]->type === null;
$classMethod = $classLike->getMethod($methodName);
if (! $classMethod instanceof ClassMethod) {
continue;
}
}

if ($parentClassReflection->hasMethod($methodName)) {
// parent class method in external scope → it's not ok
// if not, look for it's parent parent
return true;
$paramType = $classMethod->params[$paramPosition]->type;
return $paramType !== null;
}

return null;
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,10 @@
use PHPStan\Type\MixedType;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\VendorLocker\Reflection\ClassReflectionAncestorAnalyzer;
use Rector\VendorLocker\Reflection\MethodReflectionContractAnalyzer;

final class ClassMethodReturnVendorLockResolver
{
/**
* @var ClassReflectionAncestorAnalyzer
*/
private $classReflectionAncestorAnalyzer;

/**
* @var MethodReflectionContractAnalyzer
*/
Expand All @@ -32,11 +26,9 @@ final class ClassMethodReturnVendorLockResolver
private $nodeNameResolver;

public function __construct(
ClassReflectionAncestorAnalyzer $classReflectionAncestorAnalyzer,
MethodReflectionContractAnalyzer $methodReflectionContractAnalyzer,
NodeNameResolver $nodeNameResolver
) {
$this->classReflectionAncestorAnalyzer = $classReflectionAncestorAnalyzer;
$this->methodReflectionContractAnalyzer = $methodReflectionContractAnalyzer;
$this->nodeNameResolver = $nodeNameResolver;
}
Expand All @@ -53,7 +45,7 @@ public function isVendorLocked(ClassMethod $classMethod): bool
return false;
}

if (! $this->classReflectionAncestorAnalyzer->hasAncestors($classReflection)) {
if (count($classReflection->getAncestors()) === 1) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@
use Rector\FamilyTree\Reflection\FamilyRelationsAnalyzer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\VendorLocker\Reflection\ClassReflectionAncestorAnalyzer;

final class PropertyTypeVendorLockResolver
{
/**
* @var ClassReflectionAncestorAnalyzer
*/
private $classReflectionAncestorAnalyzer;

/**
* @var NodeNameResolver
*/
Expand All @@ -30,11 +24,9 @@ final class PropertyTypeVendorLockResolver
private $familyRelationsAnalyzer;

public function __construct(
ClassReflectionAncestorAnalyzer $classReflectionAncestorAnalyzer,
NodeNameResolver $nodeNameResolver,
FamilyRelationsAnalyzer $familyRelationsAnalyzer
) {
$this->classReflectionAncestorAnalyzer = $classReflectionAncestorAnalyzer;
$this->nodeNameResolver = $nodeNameResolver;
$this->familyRelationsAnalyzer = $familyRelationsAnalyzer;
}
Expand All @@ -49,7 +41,7 @@ public function isVendorLocked(Property $property): bool
return false;
}

if (! $this->classReflectionAncestorAnalyzer->hasAncestors($classReflection)) {
if (count($classReflection->getAncestors()) === 1) {
return false;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ final class LocalChildClass extends AbstractLocalParentClass
{
}

/**
* @param int $number
*/
public function changeToo(int $number)
{
}
Expand Down
Loading