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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ You can:

### READMEs for Subpackages

- [BetterReflection](/packages/BetterReflection/README.md)
- [DeprecationExtractor](/packages/DeprecationExtractor/README.md)
- [NodeTraverserQueue](/packages/NodeTraverserQueue/README.md)
- [NodeTypeResolver](/packages/NodeTypeResolver/README.md)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"php": "^7.1",
"beberlei/assert": "^2.7",
"nette/utils": "^2.4",
"nikic/php-parser": "4.0.x-dev#ed8a744c as 3.1.1",
"nikic/php-parser": "4.0.x-dev#3193f7a as 3.1.1",
"rector/better-reflection": "^3.0",
"symfony/console": "^3.3",
"symfony/dependency-injection": "^3.3",
Expand Down
6 changes: 6 additions & 0 deletions packages/BetterReflection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Better Reflection

This package add 2 features:

- ignores missing functions without crashing
- `SmartClassReflector` always knows about file it's parsing thanks to `CurrentFileProvider`
2 changes: 1 addition & 1 deletion src/Builder/ConstructorMethodBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\BuilderFactory;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\NodeFactory\NodeFactory;
use Rector\Node\NodeFactory;

final class ConstructorMethodBuilder
{
Expand Down
2 changes: 1 addition & 1 deletion src/Builder/Contrib/Nette/RouterFactoryClassBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Rector\NodeFactory\NodeFactory;
use Rector\Node\NodeFactory;

final class RouterFactoryClassBuilder
{
Expand Down
2 changes: 1 addition & 1 deletion src/Builder/MethodBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use Rector\NodeFactory\NodeFactory;
use Rector\Node\NodeFactory;

final class MethodBuilder
{
Expand Down
2 changes: 2 additions & 0 deletions src/Builder/StatementGlue.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ private function addStatementToClassBeforeTypes(Class_ $classNode, Node $node, s
}

/**
* @todo decouple to statements added
*
* @param int|string $key
*/
private function insertBefore(Class_ $classNode, Node $node, $key): void
Expand Down
4 changes: 4 additions & 0 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public function __construct()
parent::__construct(self::NAME);
}

/**
* This method override adds option to
* load custom config via --config in any command.
*/
protected function getDefaultInputDefinition(): InputDefinition
{
return new InputDefinition([
Expand Down
36 changes: 25 additions & 11 deletions src/DependencyInjection/Extension/RectorsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,7 @@ public function load(array $configs, ContainerBuilder $containerBuilder): void
return;
}

$rectors = [];

foreach ($configs as $config) {
// this magic will merge array recursively
// without making any extra duplications; array_merge only doesn't work
$rectors = array_merge(
$rectors,
$config,
array_replace_recursive($rectors, $config)
);
}
$rectors = $this->mergeAllConfigsRecursively($configs);

$rectors = $this->rectorClassNormalizer->normalizer($rectors);

Expand All @@ -61,4 +51,28 @@ public function load(array $configs, ContainerBuilder $containerBuilder): void
$rectorDefinition->setArguments([$arguments]);
}
}

/**
* This magic will merge array recursively
* without making any extra duplications.
*
* Only array_merge doesn't work in this case.
*
* @param mixed[] $configs
* @return mixed[]
*/
private function mergeAllConfigsRecursively(array $configs): array
{
$mergedConfigs = [];

foreach ($configs as $config) {
$mergedConfigs = array_merge(
$mergedConfigs,
$config,
array_replace_recursive($mergedConfigs, $config)
);
}

return $mergedConfigs;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php declare(strict_types=1);

namespace Rector\Builder\Naming;
namespace Rector\Naming;

final class NameResolver
final class PropertyNaming
{
public function resolvePropertyNameFromType(string $serviceType): string
public function typeToName(string $serviceType): string
{
$serviceNameParts = explode('\\', $serviceType);
$lastNamePart = array_pop($serviceNameParts);
Expand Down
8 changes: 8 additions & 0 deletions src/Node/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ final class Attribute
*/
public const SCOPE_NODE = 'scopeNode';

/**
* System name.
* Do not change this even if you want!
*
* @var string
*/
public const ORIGINAL_NODE = 'origNode';

/**
* System name to be found in @see \PhpParser\NodeVisitor\NameResolver
* Do not change this even if you want!
Expand Down
12 changes: 11 additions & 1 deletion src/NodeFactory/NodeFactory.php → src/Node/NodeFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace Rector\NodeFactory;
namespace Rector\Node;

use Nette\NotImplementedException;
use PhpParser\BuilderHelpers;
Expand All @@ -24,6 +24,7 @@
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\DeclareDeclare;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\TraitUse;

final class NodeFactory
Expand Down Expand Up @@ -244,6 +245,15 @@ public function createVariablePropertyArrayFetch(
);
}

/**
* Creates:
* - namespace NamespaceName;
*/
public function createNamespace(string $namespace): Namespace_
{
return new Namespace_(new Name($namespace));
}

private function createInternalConstant(string $value): ConstFetch
{
return new ConstFetch(new Name($value));
Expand Down
3 changes: 2 additions & 1 deletion src/NodeVisitor/PropertyToClassAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\ConstructorMethodBuilder;
use Rector\Builder\PropertyBuilder;
use Rector\Node\Attribute;

/**
* Adds new properties to class and to contructor.
Expand Down Expand Up @@ -65,7 +66,7 @@ private function processClass(Class_ $classNode, string $className): Class_
}

// prevents offset errors
$classNode->setAttribute('origNode', null);
$classNode->setAttribute(Attribute::ORIGINAL_NODE, null);

foreach ($propertiesForClass as $propertyType => $propertyName) {
$this->constructorMethodBuilder->addPropertyAssignToClass($classNode, $propertyType, $propertyName);
Expand Down
3 changes: 2 additions & 1 deletion src/NodeVisitor/StatementToMethodAdder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeVisitorAbstract;
use Rector\Builder\Method\MethodStatementCollector;
use Rector\Node\Attribute;

/**
* Adds new statements to method.
Expand Down Expand Up @@ -58,7 +59,7 @@ private function processClassMethod(ClassMethod $classMethodNode): ClassMethod

/** @var Node $parentNode */
$parentNode = $classMethodNode->getAttribute('parentNode');
$parentNode->setAttribute('origNode', null);
$parentNode->setAttribute(Attribute::ORIGINAL_NODE, null);

return $classMethodNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\MethodArgumentAnalyzer;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rector/Contrib/Nette/DI/SetInjectToAddTagRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Rector\Rector\Contrib\Nette\DI;

use PhpParser\Node;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;

final class SetInjectToAddTagRector extends AbstractRector
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\Naming\NameResolver;
use Rector\Contract\Bridge\ServiceTypeForNameProviderInterface;
use Rector\Naming\PropertyNaming;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;

/**
Expand All @@ -22,9 +22,9 @@
final class GetServiceToConstructorInjectionRector extends AbstractRector
{
/**
* @var NameResolver
* @var PropertyNaming
*/
private $nameResolver;
private $propertyNaming;

/**
* @var ClassPropertyCollector
Expand All @@ -47,13 +47,13 @@ final class GetServiceToConstructorInjectionRector extends AbstractRector
private $serviceTypeForNameProvider;

public function __construct(
NameResolver $nameResolver,
PropertyNaming $propertyNaming,
ClassPropertyCollector $classPropertyCollector,
NodeFactory $nodeFactory,
MethodCallAnalyzer $methodCallAnalyzer,
ServiceTypeForNameProviderInterface $serviceTypeForNameProvider
) {
$this->nameResolver = $nameResolver;
$this->propertyNaming = $propertyNaming;
$this->classPropertyCollector = $classPropertyCollector;
$this->nodeFactory = $nodeFactory;
$this->methodCallAnalyzer = $methodCallAnalyzer;
Expand All @@ -77,7 +77,7 @@ public function refactor(Node $methodCallNode): ?Node
return null;
}

$propertyName = $this->nameResolver->resolvePropertyNameFromType($serviceType);
$propertyName = $this->propertyNaming->typeToName($serviceType);

$this->classPropertyCollector->addPropertyForClass(
(string) $methodCallNode->getAttribute(Attribute::CLASS_NAME),
Expand Down
2 changes: 1 addition & 1 deletion src/Rector/Contrib/Nette/Form/FormCallbackRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Node\NodeFactory;
use Rector\Rector\AbstractRector;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Rector/Contrib/Nette/Form/FormSetRequiredRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Rector/Contrib/Nette/Utils/MagicMethodRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ public function refactor(Node $classNode): ?Node
$this->docBlockAnalyzer->removeAnnotationFromNode($classNode, 'method', $methodName);
}

$classNode->setAttribute(Attribute::ORIGINAL_NODE, null);

return $classNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use PhpParser\Node\Stmt\Class_;
use Rector\Builder\StatementGlue;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Node\NodeFactory;
use Rector\Rector\AbstractRector;

/**
Expand Down Expand Up @@ -63,6 +63,8 @@ public function refactor(Node $classNode): ?Node

$this->removeParentClass($classNode);

$classNode->setAttribute(Attribute::ORIGINAL_NODE, null);

return $classNode;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Rector/Contrib/PHPUnit/ExceptionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use PhpParser\Node\Stmt\Expression;
use Rector\Builder\Method\MethodStatementCollector;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Name\FullyQualified;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\Naming\NameResolver;
use Rector\Contract\Bridge\ServiceTypeForNameProviderInterface;
use Rector\Naming\PropertyNaming;
use Rector\Node\Attribute;
use Rector\Node\NodeFactory;
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;

/**
Expand Down Expand Up @@ -47,9 +47,9 @@ final class CommandToConstructorInjectionRector extends AbstractRector
private $classPropertyCollector;

/**
* @var NameResolver
* @var PropertyNaming
*/
private $nameResolver;
private $propertyNaming;

/**
* @var NodeFactory
Expand All @@ -68,13 +68,13 @@ final class CommandToConstructorInjectionRector extends AbstractRector

public function __construct(
ClassPropertyCollector $classPropertyCollector,
NameResolver $nameResolver,
PropertyNaming $propertyNaming,
NodeFactory $nodeFactory,
SymfonyContainerCallsAnalyzer $symfonyContainerCallsAnalyzer,
ServiceTypeForNameProviderInterface $serviceTypeForNameProvider
) {
$this->classPropertyCollector = $classPropertyCollector;
$this->nameResolver = $nameResolver;
$this->propertyNaming = $propertyNaming;
$this->nodeFactory = $nodeFactory;
$this->symfonyContainerCallsAnalyzer = $symfonyContainerCallsAnalyzer;
$this->serviceTypeForNameProvider = $serviceTypeForNameProvider;
Expand Down Expand Up @@ -108,7 +108,7 @@ public function refactor(Node $methodCallNode): ?Node
return null;
}

$propertyName = $this->nameResolver->resolvePropertyNameFromType($serviceType);
$propertyName = $this->propertyNaming->typeToName($serviceType);

$this->classPropertyCollector->addPropertyForClass(
(string) $methodCallNode->getAttribute(Attribute::CLASS_NAME),
Expand Down
Loading