Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"jean85/pretty-package-versions": "^1.2",
"nette/robot-loader": "^3.1",
"nette/utils": "^2.5|^3.0",
"nikic/php-parser": "^4.2.3",
"nikic/php-parser": "^4.2.4",
"phpstan/phpdoc-parser": "^0.3.5",
"phpstan/phpstan": "^0.11.13",
"phpstan/phpstan-phpunit": "^0.11.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,16 @@ public function refactor(Node $node): ?Node
$this->collectPropertyFetchToParams($constructMethod);

// replace them in property fetches with particular class methods and use variable instead
foreach ($node->stmts as $classStmt) {
if (! $classStmt instanceof ClassMethod) {
foreach ($node->getMethods() as $classMethod) {
if ($this->isName($classMethod, '__construct')) {
continue;
}

if ($this->isName($classStmt, '__construct')) {
if (! $classMethod->isPublic()) {
continue;
}

if (! $classStmt->isPublic()) {
continue;
}

$this->replacePropertyFetchByInjectedVariables($classStmt);
$this->replacePropertyFetchByInjectedVariables($classMethod);
}

// collect all property fetches that are relevant to original constructor properties
Expand Down
12 changes: 4 additions & 8 deletions packages/DeadCode/src/Doctrine/DoctrineEntityManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,17 @@ public function resolveRelationPropertyNames(Class_ $class): array
{
$manyToOnePropertyNames = [];

foreach ($class->stmts as $stmt) {
if (! $stmt instanceof Property) {
foreach ($class->getProperties() as $property) {
if ($property->getDocComment() === null) {
continue;
}

if ($stmt->getDocComment() === null) {
continue;
}

$phpDocInfo = $this->docBlockManipulator->createPhpDocInfoFromNode($stmt);
$phpDocInfo = $this->docBlockManipulator->createPhpDocInfoFromNode($property);
if ($phpDocInfo->getDoctrineRelationTagValueNode() === null) {
continue;
}

$manyToOnePropertyNames[] = $this->nameResolver->getName($stmt);
$manyToOnePropertyNames[] = $this->nameResolver->getName($property);
}

return $manyToOnePropertyNames;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\DeadCode\Doctrine\DoctrineEntityManipulator;
use Rector\DeadCode\UnusedNodeResolver\ClassUnusedPrivateClassMethodResolver;
Expand Down Expand Up @@ -153,16 +152,12 @@ public function refactor(Node $node): ?Node
*/
private function removeClassMethodsByNames(Class_ $class, array $unusedMethodNames): Class_
{
foreach ($class->stmts as $classStmt) {
if (! $classStmt instanceof ClassMethod) {
foreach ($class->getMethods() as $classMethod) {
if (! $this->isNames($classMethod, $unusedMethodNames)) {
continue;
}

if (! $this->isNames($classStmt, $unusedMethodNames)) {
continue;
}

$this->removeNodeFromStatements($class, $classStmt);
$this->removeNodeFromStatements($class, $classMethod);
}

return $class;
Expand All @@ -186,24 +181,20 @@ private function resolveUnusedPrivatePropertyNames(Class_ $class): array
*/
private function removeClassPrivatePropertiesByNames(Class_ $class, array $unusedPropertyNames): Class_
{
foreach ($class->stmts as $stmt) {
if (! $stmt instanceof Property) {
foreach ($class->getProperties() as $property) {
if (! $this->isNames($property, $unusedPropertyNames)) {
continue;
}

if (! $this->isNames($stmt, $unusedPropertyNames)) {
continue;
}

$this->removeNodeFromStatements($class, $stmt);
$this->removeNodeFromStatements($class, $property);

// remove "$this->someProperty = new ArrayCollection()"
$propertyName = $this->getName($stmt);
$propertyName = $this->getName($property);
if (isset($this->collectionByPropertyName[$propertyName])) {
$this->removeNode($this->collectionByPropertyName[$propertyName]);
}

$this->removeInversedByOrMappedByOnRelatedProperty($stmt);
$this->removeInversedByOrMappedByOnRelatedProperty($property);
}

return $class;
Expand All @@ -227,11 +218,7 @@ private function getOtherRelationProperty(Property $property): ?Property
return null;
}

foreach ($relatedEntityClass->stmts as $relatedEntityClassStmt) {
if (! $relatedEntityClassStmt instanceof Property) {
continue;
}

foreach ($relatedEntityClass->getProperties() as $relatedEntityClassStmt) {
if (! $this->isName($relatedEntityClassStmt, $otherProperty)) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,12 @@ public function refactor(Node $node): ?Node
}

// traverse relations and see which of them have freshly added uuid on the other side
foreach ($node->stmts as $classStmt) {
if (! $classStmt instanceof Property) {
foreach ($node->getProperties() as $property) {
if ($this->shouldSkipProperty($node, $property)) {
continue;
}

if ($this->shouldSkipProperty($node, $classStmt)) {
continue;
}

$node->stmts[] = $this->createMirrorNullable($classStmt);
$node->stmts[] = $this->createMirrorNullable($property);
}

return $node;
Expand Down Expand Up @@ -173,12 +169,8 @@ private function refactorToOnePropertyPhpDocInfo(PhpDocInfo $propertyPhpDocInfo)

private function hasClassPropertyName(Class_ $node, string $uuidPropertyName): bool
{
foreach ($node->stmts as $stmt) {
if (! $stmt instanceof Property) {
continue;
}

if (! $this->isName($stmt, $uuidPropertyName)) {
foreach ($node->getProperties() as $property) {
if (! $this->isName($property, $uuidPropertyName)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\Legacy\NodeAnalyzer\SingletonClassMethodAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -97,19 +95,17 @@ public function refactor(Node $node): ?Node
*/
private function matchStaticPropertyFetchAndGetSingletonMethodName(Class_ $class): ?array
{
foreach ((array) $class->stmts as $classStmt) {
if ($classStmt instanceof ClassMethod) {
if (! $classStmt->isStatic()) {
continue;
}

$staticPropertyFetch = $this->singletonClassMethodAnalyzer->matchStaticPropertyFetch($classStmt);
if ($staticPropertyFetch === null) {
return null;
}
foreach ($class->getMethods() as $classMethod) {
if (! $classMethod->isStatic()) {
continue;
}

return [$this->getName($staticPropertyFetch), $this->getName($classStmt)];
$staticPropertyFetch = $this->singletonClassMethodAnalyzer->matchStaticPropertyFetch($classMethod);
if ($staticPropertyFetch === null) {
return null;
}

return [$this->getName($staticPropertyFetch), $this->getName($classMethod)];
}

return null;
Expand All @@ -120,40 +116,32 @@ private function refactorClassStmts(
string $getSingletonMethodName,
string $singletonPropertyName
): Class_ {
foreach ($node->stmts as $classStmt) {
if (! $classStmt instanceof ClassMethod) {
foreach ($node->getMethods() as $property) {
if ($this->isName($property, $getSingletonMethodName)) {
$this->removeNodeFromStatements($node, $property);
continue;
}

if ($this->isName($classStmt, $getSingletonMethodName)) {
$this->removeNodeFromStatements($node, $classStmt);
if (! $this->isNames($property, ['__construct', '__clone', '__wakeup'])) {
continue;
}

if (! $this->isNames($classStmt, ['__construct', '__clone', '__wakeup'])) {
continue;
}

if (! $classStmt->isPublic()) {
if (! $property->isPublic()) {
// remove non-public empty
if ($classStmt->stmts === []) {
$this->removeNodeFromStatements($node, $classStmt);
if ($property->stmts === []) {
$this->removeNodeFromStatements($node, $property);
} else {
$this->makePublic($classStmt);
$this->makePublic($property);
}
}
}

foreach ($node->stmts as $classStmt) {
if (! $classStmt instanceof Property) {
continue;
}

if (! $this->isName($classStmt, $singletonPropertyName)) {
foreach ($node->getProperties() as $property) {
if (! $this->isName($property, $singletonPropertyName)) {
continue;
}

$this->removeNodeFromStatements($node, $classStmt);
$this->removeNodeFromStatements($node, $property);
}

return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Manipulator\ClassManipulator;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
Expand All @@ -20,16 +19,8 @@ final class NetteTesterClassToPHPUnitClassRector extends AbstractRector
*/
private $netteTesterTestCaseClass;

/**
* @var ClassManipulator
*/
private $classManipulator;

public function __construct(
ClassManipulator $classManipulator,
string $netteTesterTestCaseClass = 'Tester\TestCase'
) {
$this->classManipulator = $classManipulator;
public function __construct(string $netteTesterTestCaseClass = 'Tester\TestCase')
{
$this->netteTesterTestCaseClass = $netteTesterTestCaseClass;
}

Expand Down Expand Up @@ -132,11 +123,9 @@ private function processUnderTestRun(MethodCall $methodCall): void

private function processMethods(Class_ $class): void
{
$methods = $this->classManipulator->getMethods($class);

foreach ($methods as $method) {
if ($this->isNames($method, ['setUp', 'tearDown'])) {
$this->makeProtected($method);
foreach ($class->getMethods() as $classMethod) {
if ($this->isNames($classMethod, ['setUp', 'tearDown'])) {
$this->makeProtected($classMethod);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Rector\NetteToSymfony\Route\RouteInfoFactory;
use Rector\NodeContainer\ParsedNodesByType;
use Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer\DocBlockManipulator;
use Rector\PhpParser\Node\Manipulator\ClassManipulator;
use Rector\PhpParser\Node\Manipulator\ClassMethodManipulator;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
Expand Down Expand Up @@ -48,11 +47,6 @@ final class RouterListToControllerAnnotationsRector extends AbstractRector
*/
private $parsedNodesByType;

/**
* @var ClassManipulator
*/
private $classManipulator;

/**
* @var DocBlockManipulator
*/
Expand All @@ -70,7 +64,6 @@ final class RouterListToControllerAnnotationsRector extends AbstractRector

public function __construct(
ParsedNodesByType $parsedNodesByType,
ClassManipulator $classManipulator,
ClassMethodManipulator $classMethodManipulator,
DocBlockManipulator $docBlockManipulator,
RouteInfoFactory $routeInfoFactory,
Expand All @@ -81,7 +74,6 @@ public function __construct(
$this->routeListClass = $routeListClass;
$this->routerClass = $routerClass;
$this->parsedNodesByType = $parsedNodesByType;
$this->classManipulator = $classManipulator;
$this->docBlockManipulator = $docBlockManipulator;
$this->routeAnnotationClass = $routeAnnotationClass;
$this->routeInfoFactory = $routeInfoFactory;
Expand Down Expand Up @@ -260,24 +252,23 @@ private function resolveControllerClassMethod(RouteInfo $routeInfo): ?ClassMetho
return null;
}

return $this->classManipulator->getMethod($classNode, $routeInfo->getMethod());
return $classNode->getMethod($routeInfo->getMethod());
}

private function completeImplicitRoutes(): void
{
$presenterClasses = $this->parsedNodesByType->findClassesBySuffix('Presenter');

foreach ($presenterClasses as $presenterClass) {
foreach ((array) $presenterClass->stmts as $classStmt) {
if ($this->shouldSkipClassStmt($classStmt)) {
foreach ($presenterClass->getMethods() as $classMethod) {
if ($this->shouldSkipClassStmt($classMethod)) {
continue;
}

/** @var ClassMethod $classStmt */
$path = $this->resolvePathFromClassAndMethodNodes($presenterClass, $classStmt);
$path = $this->resolvePathFromClassAndMethodNodes($presenterClass, $classMethod);
$phpDocTagNode = new SymfonyRoutePhpDocTagNode($this->routeAnnotationClass, $path);

$this->docBlockManipulator->addTag($classStmt, $phpDocTagNode);
$this->docBlockManipulator->addTag($classMethod, $phpDocTagNode);
}
}
}
Expand Down
Loading