Skip to content

Commit

Permalink
[DowngradePhp72] Improve DowngradeParameterTypeWideningRector (#1150)
Browse files Browse the repository at this point in the history
* [DowngradePhp72] Improve DowngradeParameterTypeWideningRector

* check early for no params, private, or magic early

* clean up unneded $param->type === null check as already checked in hasParamAlreadyNonTyped()
  • Loading branch information
samsonasik committed Nov 5, 2021
1 parent 502cc04 commit 1e94a70
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,31 @@ public function configure(array $configuration): void

private function shouldSkip(ClassReflection $classReflection, ClassMethod $classMethod): bool
{
if ($classMethod->params === []) {
return true;
}

if ($classMethod->isPrivate()) {
return true;
}

if ($classMethod->isMagic()) {
return true;
}

if ($this->sealedClassAnalyzer->isSealedClass($classReflection)) {
return true;
}

if ($this->isSafeType($classReflection, $classMethod)) {
if ($this->autowiredClassMethodOrPropertyAnalyzer->detect($classMethod)) {
return true;
}

if ($classMethod->isPrivate()) {
if ($this->hasParamAlreadyNonTyped($classMethod)) {
return true;
}

return $this->shouldSkipClassMethod($classMethod);
return $this->isSafeType($classReflection, $classMethod);
}

private function processRemoveParamTypeFromMethod(
Expand Down Expand Up @@ -206,30 +218,13 @@ private function removeParamTypeFromMethod(ClassMethod $classMethod, int $paramP
return;
}

// It already has no type => nothing to do - check original param, as it could have been removed by this rule
if ($param->type === null) {
return;
}

// Add the current type in the PHPDoc
$this->nativeParamToPhpDocDecorator->decorate($classMethod, $param);
$param->type = null;
}

private function shouldSkipClassMethod(ClassMethod $classMethod): bool
private function hasParamAlreadyNonTyped(ClassMethod $classMethod): bool
{
if ($classMethod->isMagic()) {
return true;
}

if ($classMethod->params === []) {
return true;
}

if ($this->autowiredClassMethodOrPropertyAnalyzer->detect($classMethod)) {
return true;
}

foreach ($classMethod->params as $param) {
if ($param->type !== null) {
return false;
Expand All @@ -241,13 +236,14 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool

private function isSafeType(ClassReflection $classReflection, ClassMethod $classMethod): bool
{
$classReflectionName = $classReflection->getName();
foreach ($this->safeTypes as $safeType) {
if ($classReflection->isSubclassOf($safeType)) {
return true;
}

// skip self too
if ($classReflection->getName() === $safeType) {
if ($classReflectionName === $safeType) {
return true;
}
}
Expand All @@ -262,7 +258,7 @@ private function isSafeType(ClassReflection $classReflection, ClassMethod $class
}

// skip self too
if ($classReflection->getName() === $safeType) {
if ($classReflectionName === $safeType) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer;
use Symfony\Contracts\Service\Attribute\Required;

final class AutowiredClassMethodOrPropertyAnalyzer
{
public function __construct(
private PhpDocInfoFactory $phpDocInfoFactory,
private NodeNameResolver $nodeNameResolver
private PhpAttributeAnalyzer $phpAttributeAnalyzer
) {
}

Expand All @@ -26,17 +26,6 @@ public function detect(ClassMethod | Param | Property $node): bool
return true;
}

foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attribute) {
if ($this->nodeNameResolver->isNames(
$attribute->name,
[Required::class, 'Nette\DI\Attributes\Inject']
)) {
return true;
}
}
}

return false;
return $this->phpAttributeAnalyzer->hasPhpAttributes($node, [Required::class, 'Nette\DI\Attributes\Inject']);
}
}

0 comments on commit 1e94a70

Please sign in to comment.