Skip to content

Commit

Permalink
[Nette] Skip constructor on magic template call
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 22, 2021
1 parent 10ea0e0 commit 6cfddae
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Nette\NodeFactory\ActionRenderFactory;
use Rector\Nette\TemplatePropertyAssignCollector;
use Rector\Nette\ValueObject\MagicTemplatePropertyCalls;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand Down Expand Up @@ -86,11 +89,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->isObjectType($node, 'Nette\Application\UI\Control')) {
return null;
}

if (! $node->isPublic()) {
if ($this->shouldSkip($node)) {
return null;
}

Expand All @@ -116,4 +115,22 @@ private function createRenderMethodCall(

return $this->actionRenderFactory->createThisRenderMethodCall($magicTemplatePropertyCalls);
}

private function shouldSkip(ClassMethod $classMethod): bool
{
$classLike = $classMethod->getAttribute(AttributeKey::CLASS_NODE);
if (! $classLike instanceof Class_) {
return true;
}

if (! $this->isObjectType($classLike, 'Nette\Application\UI\Control')) {
return true;
}

if ($this->isName($classMethod, MethodName::CONSTRUCT)) {
return true;
}

return ! $classMethod->isPublic();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Nette\Tests\Rector\ClassMethod\TemplateMagicAssignToExplicitVariableArrayRector\Fixture;

use Nette\Application\UI\Presenter;

class SkipConstructor extends Presenter
{
public function __construct($logger)
{
$this->logger = $logger;
}
}

0 comments on commit 6cfddae

Please sign in to comment.