misc + cs fixes

This commit is contained in:
TomasVotruba 2017-09-08 01:18:46 +02:00
parent a6644b78bf
commit 073231b5df
26 changed files with 120 additions and 111 deletions

View File

@ -4,15 +4,17 @@ namespace Rector\NodeTypeResolver\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeVisitorAbstract;
use Rector\Node\Attribute;
/**
* In class, in interface, in trait, in method or in function?
* In class, in interface, in trait, in method or in function.
*/
final class ScopeResolver extends NodeVisitorAbstract
{
@ -21,6 +23,9 @@ final class ScopeResolver extends NodeVisitorAbstract
*/
private $currentScope;
/**
* @param Node[] $nodes
*/
public function beforeTraverse(array $nodes): void
{
$this->currentScope = null;
@ -28,18 +33,12 @@ final class ScopeResolver extends NodeVisitorAbstract
public function enterNode(Node $node): void
{
$this->resolveClassLikeScope($node);
if ($node instanceof Function_) {
$this->currentScope = 'scope_function';
}
if (($node instanceof Class_ && $node->isAnonymous()) || $node instanceof Property) {
$this->currentScope = 'scope_class';
}
if ($node instanceof Interface_) {
$this->currentScope = 'scope_interface';
}
if ($node instanceof ClassMethod) {
$this->currentScope = 'scope_class_method';
}
@ -48,4 +47,34 @@ final class ScopeResolver extends NodeVisitorAbstract
$node->setAttribute(Attribute::SCOPE, $this->currentScope);
}
}
public function leaveNode(Node $node): void
{
if ($node instanceof ClassLike) {
if ($node instanceof Class_ && $node->isAnonymous()) {
return;
}
$this->currentScope = null;
}
if ($node instanceof ClassMethod || $node instanceof Function_) {
$this->currentScope = null;
}
}
private function resolveClassLikeScope(Node $node): void
{
if (($node instanceof Class_ && $node->isAnonymous()) || $node instanceof Property) {
$this->currentScope = 'scope_class';
}
if ($node instanceof Interface_) {
$this->currentScope = 'scope_interface';
}
if ($node instanceof Trait_) {
$this->currentScope = 'scope_trait';
}
}
}

View File

@ -26,6 +26,9 @@ final class ClassMethodDeprecation implements DeprecationInterface
*/
private $newArguments = [];
/**
* @param mixed[] $newArguments
*/
public function __construct(string $class, string $oldMethod, string $newMethod, array $newArguments = [])
{
$this->class = $class;

View File

@ -18,7 +18,7 @@ final class DeprecationFactory
* @var string
* @see https://regex101.com/r/WdGoyd/2
*/
private const CLASS_WITH_METHOD_PATTERN = '#^(?<classWithMethod>[A-Za-z]+[\\\\A-Za-z]+::[A-Za-z]+\([A-Za-z\']*\))#s';
private const CLASS_WITH_METHOD_PATTERN = '#^(?<classMethod>[A-Za-z]+[\\\\A-Za-z]+::[A-Za-z]+\([A-Za-z\']*\))#s';
/**
* Probably resolve by recursion, similar too
@ -26,12 +26,8 @@ final class DeprecationFactory
*/
public function createFromNode(Node $node, string $scope): DeprecationInterface
{
if ($scope === 'scope_class_method') {
// new class method deprecation
dump('tada');
die;
}
dump($node->getAttribute(Attribute::SCOPE));
die;
$message = '';
if ($node instanceof Concat) {
@ -42,6 +38,37 @@ final class DeprecationFactory
return $this->createFromMesssage($message, $scope);
}
public function tryToCreateClassMethodDeprecation(string $oldMessage, string $newMessage): ?DeprecationInterface
{
// try to find "SomeClass::methodCall()"
$matches = Strings::matchAll($oldMessage, self::CLASS_WITH_METHOD_PATTERN);
if (isset($matches[0]['classMethod'])) {
$oldClassWithMethod = $matches[0]['classMethod'];
}
// try to find "SomeClass::methodCall()"
$matches = Strings::matchAll($newMessage, self::CLASS_WITH_METHOD_PATTERN);
if (isset($matches[0]['classMethod'])) {
$newClassWithMethod = $matches[0]['classMethod'];
}
if (isset($oldClassWithMethod, $newClassWithMethod)) {
[$oldClass, $oldMethod] = explode('::', $oldClassWithMethod);
[$newClass, $newMethod] = explode('::', $newClassWithMethod);
if ($oldClass === $newClass) {
// simple method replacement
return new ClassMethodDeprecation(
$oldClass,
rtrim($oldMethod, '()'),
rtrim($newMethod, '()')
);
}
}
return null;
}
private function processConcatNode(Node $node, string $scope): string
{
if ($node instanceof Method) {
@ -63,37 +90,6 @@ final class DeprecationFactory
));
}
public function tryToCreateClassMethodDeprecation(string $oldMessage, string $newMessage): ?DeprecationInterface
{
// try to find "SomeClass::methodCall()"
$matches = Strings::matchAll($oldMessage, self::CLASS_WITH_METHOD_PATTERN);
if (isset($matches[0]['classWithMethod'])) {
$oldClassWithMethod = $matches[0]['classWithMethod'];
}
// try to find "SomeClass::methodCall()"
$matches = Strings::matchAll($newMessage, self::CLASS_WITH_METHOD_PATTERN);
if (isset($matches[0]['classWithMethod'])) {
$newClassWithMethod = $matches[0]['classWithMethod'];
}
if (isset($oldClassWithMethod, $newClassWithMethod)) {
[$oldClass, $oldMethod] = explode('::', $oldClassWithMethod);
[$newClass, $newMethod] = explode('::', $newClassWithMethod);
if ($oldClass === $newClass) {
// simple method replacement
return new ClassMethodDeprecation(
$oldClass,
rtrim($oldMethod, '()'),
rtrim($newMethod, '()')
);
}
}
return null;
}
private function findParentOfType(Node $node, string $type): Node
{
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);

View File

@ -9,9 +9,13 @@ use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\NodeVisitorAbstract;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\DocBlockAnalyzer;
use Rector\TriggerExtractor\Deprecation\DeprecationCollector;
use Rector\TriggerExtractor\Deprecation\DeprecationFactory;
/**
* Inspired by https://github.com/sensiolabs-de/deprecation-detector/blob/master/src/Visitor/Deprecation/FindDeprecatedTagsVisitor.php
*/
final class DeprecationDetector extends NodeVisitorAbstract
{
/**
@ -24,27 +28,28 @@ final class DeprecationDetector extends NodeVisitorAbstract
*/
private $deprecationFactory;
/**
* @var DocBlockAnalyzer
*/
private $docBlockAnalyzer;
public function __construct(
DeprecationCollector $deprecationCollector,
DeprecationFactory $triggerMessageResolver
DeprecationFactory $triggerMessageResolver,
DocBlockAnalyzer $docBlockAnalyzer
) {
$this->deprecationCollector = $deprecationCollector;
$this->deprecationFactory = $triggerMessageResolver;
$this->docBlockAnalyzer = $docBlockAnalyzer;
}
public function enterNode(Node $node): void
{
// @see https://github.com/sensiolabs-de/deprecation-detector/blob/master/src/Visitor/Deprecation/FindDeprecatedTagsVisitor.php
// if (! $this->isTriggerErrorUserDeprecated($node)) {
// return;
// }
if (! $this->hasTriggerErrorUserDeprecatedInside($node)) {
return;
}
if (! $this->hasDeprecatedDocComment($node)) {
if (! $this->docBlockAnalyzer->hasAnnotation($node, 'deprecated')) {
return;
}
@ -94,21 +99,4 @@ final class DeprecationDetector extends NodeVisitorAbstract
return $node->name->toString() === $name;
}
/**
* @todo extract to docblock analyzer
* use deprecated annotation check
* @param Node $node
*
* @return bool
*/
protected function hasDeprecatedDocComment(Node $node)
{
try {
$docBlock = new DocBlock((string) $node->getDocComment());
return count($docBlock->getTagsByName('deprecated')) > 0;
} catch (\Exception $e) {
return false;
}
}
}

View File

@ -34,8 +34,8 @@ final class RectorFactoryTest extends AbstractContainerAwareTestCase
$this->assertSame([
'Nette\DI\Definition' => [
'setClass' => 'setFactory'
]
'setClass' => 'setFactory',
],
], Assert::getObjectAttribute($firstRector, 'perClassOldToNewMethod'));
/** @var ConfigurableChangeMethodNameRector $secondRector */
@ -44,8 +44,8 @@ final class RectorFactoryTest extends AbstractContainerAwareTestCase
$this->assertSame([
'Nette\DI\Definition' => [
'setInject' => 'addTag'
]
'setInject' => 'addTag',
],
], Assert::getObjectAttribute($secondRector, 'perClassOldToNewMethod'));
}
}

View File

@ -16,11 +16,11 @@ final class Attribute
*/
public const TYPE = 'type';
/**
* In class, in interface, in trait, in method or in function
*
* @var string
*/
/**
* In class, in interface, in trait, in method or in function
*
* @var string
*/
public const SCOPE = 'scope';
/**

View File

@ -15,7 +15,7 @@ final class DocBlockAnalyzer
return (bool) $docBlock->getAnnotationsOfType($annotation);
}
public function removeAnnotationFromNode(Node $node, string $annotation): Node
public function removeAnnotationFromNode(Node $node, string $annotation): void
{
$docBlock = $this->createDocBlockFromNode($node);
@ -25,8 +25,6 @@ final class DocBlockAnalyzer
}
$node->setDocComment(new Doc($docBlock->getContent()));
return $node;
}
private function createDocBlockFromNode(Node $node): DocBlock

View File

@ -7,6 +7,7 @@ use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\MethodCall;
@ -144,7 +145,7 @@ final class NodeFactory
'name' => $propertyName,
]);
$assign = new Node\Expr\Assign(
$assign = new Assign(
$this->createLocalPropertyFetch($propertyName),
$variable
);

View File

@ -6,10 +6,10 @@ use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use Rector\Rector\Set\SetNames;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Covers https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject.

View File

@ -5,11 +5,11 @@ namespace Rector\Rector\Contrib\Nette;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\MethodCall;
use Rector\Rector\Set\SetNames;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Covers https://forum.nette.org/cs/26672-missing-setrequired-true-false-on-field-abc-in-form

View File

@ -2,8 +2,8 @@
namespace Rector\Rector\Contrib\Nette;
use Rector\Rector\Set\SetNames;
use Rector\Rector\AbstractChangeMethodNameRector;
use Rector\Rector\Set\SetNames;
final class HtmlAddMethodRector extends AbstractChangeMethodNameRector
{

View File

@ -3,23 +3,17 @@
namespace Rector\Rector\Contrib\Nette;
use PhpCsFixer\DocBlock\DocBlock;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\NodeAnalyzer\DocBlockAnalyzer;
use Rector\Rector\Set\SetNames;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\DocBlockAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
final class InjectPropertyRector extends AbstractRector
{
/**
* @var string
*/
private const ANNOTATION_INJECT = 'inject';
/**
* @var ClassPropertyCollector
*/
@ -54,7 +48,7 @@ final class InjectPropertyRector extends AbstractRector
*/
public function refactor(Node $propertyNode): Node
{
$propertyNode = $this->docBlockAnalyzer->removeAnnotationFromNode($propertyNode, 'inject');
$this->docBlockAnalyzer->removeAnnotationFromNode($propertyNode, 'inject');
$propertyNode->flags = Class_::MODIFIER_PRIVATE;

View File

@ -2,8 +2,8 @@
namespace Rector\Rector\Contrib\Nette;
use Rector\Rector\Set\SetNames;
use Rector\Rector\AbstractClassReplacerRector;
use Rector\Rector\Set\SetNames;
final class NetteConfiguratorRector extends AbstractClassReplacerRector
{

View File

@ -6,10 +6,10 @@ use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use Rector\Builder\StatementGlue;
use Rector\Rector\Set\SetNames;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Covers https://doc.nette.org/en/2.4/migration-2-4#toc-nette-smartobject.

View File

@ -2,8 +2,8 @@
namespace Rector\Rector\Contrib\Nette;
use Rector\Rector\Set\SetNames;
use Rector\Rector\AbstractChangeMethodNameRector;
use Rector\Rector\Set\SetNames;
final class PhpGeneratorDocumentMethodRector extends AbstractChangeMethodNameRector
{

View File

@ -5,9 +5,9 @@ namespace Rector\Rector\Contrib\Nette;
use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\Set\SetNames;
use Rector\Node\Attribute;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
final class RemoveConfiguratorConstantsRector extends AbstractRector
{

View File

@ -2,8 +2,8 @@
namespace Rector\Rector\Contrib\PHPUnit;
use Rector\Rector\Set\SetNames;
use Rector\Rector\AbstractClassReplacerRector;
use Rector\Rector\Set\SetNames;
final class NamespaceClassRector extends AbstractClassReplacerRector
{

View File

@ -2,8 +2,8 @@
namespace Rector\Rector\Contrib\PHP_CodeSniffer;
use Rector\Rector\Set\SetNames;
use Rector\Rector\AbstractClassReplacerRector;
use Rector\Rector\Set\SetNames;
final class NamespaceClassRector extends AbstractClassReplacerRector
{

View File

@ -5,10 +5,10 @@ namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\Set\SetNames;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Ref: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#validator

View File

@ -2,8 +2,8 @@
namespace Rector\Rector\Contrib\Symfony;
use Rector\Rector\Set\SetNames;
use Rector\Rector\AbstractChangeParentClassRector;
use Rector\Rector\Set\SetNames;
/**
* Ref: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#validator

View File

@ -5,10 +5,10 @@ namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\MethodCall;
use Rector\Rector\Set\SetNames;
use Rector\Node\Attribute;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Converts all:

View File

@ -2,8 +2,8 @@
namespace Rector\Rector\Contrib\Symfony;
use Rector\Rector\Set\SetNames;
use Rector\Rector\AbstractClassReplacerRector;
use Rector\Rector\Set\SetNames;
/**
* Ref.: https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md#frameworkbundle

View File

@ -5,9 +5,9 @@ namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\Set\SetNames;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Converts all:

View File

@ -5,10 +5,10 @@ namespace Rector\Rector\Contrib\Symfony;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Scalar\String_;
use Rector\Rector\Set\SetNames;
use Rector\NodeAnalyzer\MethodCallAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
/**
* Converts all:

View File

@ -9,11 +9,11 @@ use PhpParser\Node\Name\FullyQualified;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\Kernel\ServiceFromKernelResolver;
use Rector\Builder\Naming\NameResolver;
use Rector\Rector\Set\SetNames;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
use Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source\LocalKernel;
/**

View File

@ -7,11 +7,11 @@ use PhpParser\Node\Expr\MethodCall;
use Rector\Builder\Class_\ClassPropertyCollector;
use Rector\Builder\Kernel\ServiceFromKernelResolver;
use Rector\Builder\Naming\NameResolver;
use Rector\Rector\Set\SetNames;
use Rector\Node\Attribute;
use Rector\NodeAnalyzer\SymfonyContainerCallsAnalyzer;
use Rector\NodeFactory\NodeFactory;
use Rector\Rector\AbstractRector;
use Rector\Rector\Set\SetNames;
use Rector\Tests\Rector\Contrib\SymfonyExtra\GetterToPropertyRector\Source\LocalKernel;
/**