Skip to content

Commit

Permalink
Merge pull request rectorphp#847 from rectorphp/php73-regex-regression
Browse files Browse the repository at this point in the history
[PHP 7.3] Add RegexDashEscapeRector
  • Loading branch information
TomasVotruba authored Dec 16, 2018
2 parents 3ce2e41 + 15d8b91 commit 3cc2646
Show file tree
Hide file tree
Showing 44 changed files with 947 additions and 408 deletions.
3 changes: 2 additions & 1 deletion config/level/php/php73.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ services:
Rector\Php\Rector\FuncCall\ArrayKeyFirstLastRector: ~
Rector\Php\Rector\FuncCall\SensitiveDefineRector: ~
Rector\Php\Rector\ConstFetch\SensitiveConstantNameRector: ~
Rector\Php\Rector\String\SensitiveHereNowDocRector: ~
Rector\Php\Rector\String_\SensitiveHereNowDocRector: ~

Rector\Rector\Function_\FunctionReplaceRector:
image2wbmp: 'imagewbmp'
Expand All @@ -24,3 +24,4 @@ services:
Rector\Php\Rector\FuncCall\StringifyStrNeedlesRector: ~
Rector\Php\Rector\FuncCall\JsonThrowOnErrorRector: ~
Rector\Php\Rector\FuncCall\TrailingCommaArgumentsRector: ~
Rector\Php\Rector\FuncCall\RegexDashEscapeRector: ~
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ private function shouldSkipForeach(Foreach_ $foreachNode): bool
return ! $foreachNode->stmts[0] instanceof If_;
}

private function shouldSkipIf(If_ $ifNode): bool
{
$ifCondition = $ifNode->cond;
return ! $ifCondition instanceof Identical && ! $ifCondition instanceof Equal;
}

private function isIfBodyABoolReturnNode(If_ $firstNodeInsideForeach): bool
{
$ifStatment = $firstNodeInsideForeach->stmts[0];
Expand Down Expand Up @@ -212,10 +218,4 @@ private function combineCommentsToNode(Node $originalNode, Node $newNode): void

$newNode->setAttribute('comments', [new Comment($commentContent)]);
}

private function shouldSkipIf(If_ $ifNode): bool
{
$ifCondition = $ifNode->cond;
return ! $ifCondition instanceof Identical && ! $ifCondition instanceof Equal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ function (Node $node) {
return $inArrayFuncCall;
}

private function shouldBeStrict(BinaryOp $binaryOpNode): bool
{
return $binaryOpNode instanceof Identical || $binaryOpNode instanceof NotIdentical;
}

private function resolveIsNot(BinaryOp $node, ConstFetch $boolConstFetchNode): bool
{
if ($node instanceof Identical || $node instanceof Equal) {
Expand All @@ -98,9 +103,4 @@ private function resolveIsNot(BinaryOp $node, ConstFetch $boolConstFetchNode): b

return $this->isTrue($boolConstFetchNode);
}

private function shouldBeStrict(BinaryOp $binaryOpNode): bool
{
return $binaryOpNode instanceof Identical || $binaryOpNode instanceof NotIdentical;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ public function __construct(NameResolver $nameResolver)
$this->nameResolver = $nameResolver;
}

public function addClass(string $name, Class_ $classNode): void
public function addClass(Class_ $classNode): void
{
$name = (string) $classNode->getAttribute(Attribute::CLASS_NAME);
$this->classes[$name] = $classNode;
}

Expand All @@ -47,13 +48,15 @@ public function findClass(string $name): ?Class_
return $this->classes[$name] ?? null;
}

public function addInterface(string $name, Interface_ $interfaceNode): void
public function addInterface(Interface_ $interfaceNode): void
{
$name = (string) $interfaceNode->getAttribute(Attribute::CLASS_NAME);
$this->interfaces[$name] = $interfaceNode;
}

public function addTrait(string $name, Trait_ $traitNode): void
public function addTrait(Trait_ $traitNode): void
{
$name = (string) $traitNode->getAttribute(Attribute::CLASS_NAME);
$this->traits[$name] = $traitNode;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Rector\NodeTypeResolver\Application;

use PhpParser\Node\Stmt\ClassConst;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\PhpParser\Node\Resolver\NameResolver;

final class ConstantNodeCollector
{
/**
* @var ClassConst[][]
*/
private $constantsByType = [];

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

public function __construct(NameResolver $nameResolver)
{
$this->nameResolver = $nameResolver;
}

public function addConstant(ClassConst $classConst): void
{
$className = (string) $classConst->getAttribute(Attribute::CLASS_NAME);
$constantName = $this->nameResolver->resolve($classConst);

$this->constantsByType[$className][$constantName] = $classConst;
}

public function findConstant(string $constantName, string $className): ?ClassConst
{
return $this->constantsByType[$className][$constantName] ?? null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\NodeTypeResolver\Node\Attribute;
use Rector\PhpParser\Node\Resolver\NameResolver;

final class FunctionLikeNodeCollector
Expand All @@ -28,9 +29,11 @@ public function __construct(NameResolver $nameResolver)
$this->nameResolver = $nameResolver;
}

public function addMethod(ClassMethod $classMethodNode, string $className): void
public function addMethod(ClassMethod $classMethodNode): void
{
$className = (string) $classMethodNode->getAttribute(Attribute::CLASS_NAME);
$methodName = $this->nameResolver->resolve($classMethodNode);

$this->methodsByType[$className][$methodName] = $classMethodNode;
}

Expand Down
21 changes: 6 additions & 15 deletions packages/NodeTypeResolver/src/NodeScopeAndMetadataDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\NodeVisitor\NameResolver;
use Rector\NodeTypeResolver\NodeVisitor\ClassAndMethodNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\ClassLikeNodeCollectorNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\ExpressionNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FileInfoNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FunctionLikeNodeCollectorNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\NamespaceNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\NodeCollectorNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\ParentAndNextNodeVisitor;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeScopeResolver;

Expand Down Expand Up @@ -53,14 +52,9 @@ final class NodeScopeAndMetadataDecorator
private $fileInfoNodeVisitor;

/**
* @var ClassLikeNodeCollectorNodeVisitor
* @var NodeCollectorNodeVisitor
*/
private $classLikeNodeCollectorNodeVisitor;

/**
* @var FunctionLikeNodeCollectorNodeVisitor
*/
private $functionLikeNodeCollectorNodeVisitor;
private $nodeCollectorNodeVisitor;

public function __construct(
NodeScopeResolver $nodeScopeResolver,
Expand All @@ -70,8 +64,7 @@ public function __construct(
NamespaceNodeVisitor $namespaceNodeVisitor,
ExpressionNodeVisitor $expressionNodeVisitor,
FileInfoNodeVisitor $fileInfoNodeVisitor,
ClassLikeNodeCollectorNodeVisitor $classLikeNodeCollectorNodeVisitor,
FunctionLikeNodeCollectorNodeVisitor $functionLikeNodeCollectorNodeVisitor
NodeCollectorNodeVisitor $nodeCollectorNodeVisitor
) {
$this->nodeScopeResolver = $nodeScopeResolver;
$this->parentAndNextNodeVisitor = $parentAndNextNodeVisitor;
Expand All @@ -80,8 +73,7 @@ public function __construct(
$this->namespaceNodeVisitor = $namespaceNodeVisitor;
$this->expressionNodeVisitor = $expressionNodeVisitor;
$this->fileInfoNodeVisitor = $fileInfoNodeVisitor;
$this->classLikeNodeCollectorNodeVisitor = $classLikeNodeCollectorNodeVisitor;
$this->functionLikeNodeCollectorNodeVisitor = $functionLikeNodeCollectorNodeVisitor;
$this->nodeCollectorNodeVisitor = $nodeCollectorNodeVisitor;
}

/**
Expand Down Expand Up @@ -111,8 +103,7 @@ public function decorateNodesFromFile(array $nodes, string $filePath): array
$nodeTraverser->addVisitor($this->namespaceNodeVisitor);
$nodeTraverser->addVisitor($this->expressionNodeVisitor);
$nodeTraverser->addVisitor($this->fileInfoNodeVisitor);
$nodeTraverser->addVisitor($this->classLikeNodeCollectorNodeVisitor);
$nodeTraverser->addVisitor($this->functionLikeNodeCollectorNodeVisitor);
$nodeTraverser->addVisitor($this->nodeCollectorNodeVisitor);

return $nodeTraverser->traverse($nodes);
}
Expand Down
43 changes: 32 additions & 11 deletions packages/NodeTypeResolver/src/NodeTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
Expand Down Expand Up @@ -45,6 +46,26 @@ public function isStringType(Node $node): bool
return $this->getNodeType($node) instanceof StringType;
}

public function isStringyType(Node $node): bool
{
$nodeType = $this->getNodeType($node);
if ($nodeType instanceof StringType || $nodeType instanceof ConstantStringType) {
return true;
}

if ($nodeType instanceof UnionType) {
foreach ($nodeType->getTypes() as $singleType) {
if (! $singleType instanceof StringType && ! $singleType instanceof ConstantStringType) {
return false;
}
}

return true;
}

return false;
}

public function isNullType(Node $node): bool
{
return $this->getNodeType($node) instanceof NullType;
Expand Down Expand Up @@ -96,6 +117,17 @@ public function isCountableType(Node $node): bool
return $nodeType instanceof ArrayType;
}

private function getNodeType(Node $node): ?Type
{
/** @var Scope|null $nodeScope */
$nodeScope = $node->getAttribute(Attribute::SCOPE);
if (! $node instanceof Expr || $nodeScope === null) {
return null;
}

return $nodeScope->getType($node);
}

/**
* Special case for "preg_match(), preg_match_all()" - with 3rd argument
* @covers https://github.com/rectorphp/rector/issues/786
Expand Down Expand Up @@ -134,15 +166,4 @@ private function correctPregMatchType(Node $node, Type $originalType): Type

return new ArrayType(new MixedType(), new MixedType());
}

private function getNodeType(Node $node): ?Type
{
/** @var Scope|null $nodeScope */
$nodeScope = $node->getAttribute(Attribute::SCOPE);
if (! $node instanceof Expr || $nodeScope === null) {
return null;
}

return $nodeScope->getType($node);
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 3cc2646

Please sign in to comment.