Skip to content

Commit

Permalink
skip bool instanceof
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Apr 6, 2021
1 parent b2d6f72 commit 401dc65
Show file tree
Hide file tree
Showing 21 changed files with 88 additions and 104 deletions.
6 changes: 3 additions & 3 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,11 +491,11 @@ public function hasChanged(): bool

// has a single node with missing start_end
$phpDocNodeTraverser = new PhpDocNodeTraverser();
$changedPhpNodeVisitor = new ChangedPhpDocNodeVisitor();
$phpDocNodeTraverser->addPhpDocNodeVisitor($changedPhpNodeVisitor);
$changedPhpDocNodeVisitor = new ChangedPhpDocNodeVisitor();
$phpDocNodeTraverser->addPhpDocNodeVisitor($changedPhpDocNodeVisitor);
$phpDocNodeTraverser->traverse($this->phpDocNode);

return $changedPhpNodeVisitor->hasChanged();
return $changedPhpDocNodeVisitor->hasChanged();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ private function processSerializerTypeTagValueNode(array $oldToNewClasses, PhpDo
if ($className) {
if ($className === $oldClass) {
$doctrineAnnotationTagValueNode->changeSilentValue($newClass);
// $phpDocInfo->markAsChanged();
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function changeVarType(PhpDocInfo $phpDocInfo, Type $newType): void
if ($currentVarTagValueNode !== null) {
// only change type
$currentVarTagValueNode->type = $newPHPStanPhpDocType;
// $phpDocInfo->markAsChanged();
} else {
// add completely new one
$varTagValueNode = new VarTagValueNode($newPHPStanPhpDocType, '', '');
Expand All @@ -84,7 +83,6 @@ public function changeReturnType(PhpDocInfo $phpDocInfo, Type $newType): void
if ($currentReturnTagValueNode !== null) {
// only change type
$currentReturnTagValueNode->type = $newPHPStanPhpDocType;
// $phpDocInfo->markAsChanged();
} else {
// add completely new one
$returnTagValueNode = new ReturnTagValueNode($newPHPStanPhpDocType, '');
Expand Down Expand Up @@ -115,7 +113,6 @@ public function changeParamType(PhpDocInfo $phpDocInfo, Type $newType, Param $pa
}

$paramTagValueNode->type = $phpDocType;
// $phpDocInfo->markAsChanged();
} else {
$paramTagValueNode = $this->paramPhpDocNodeFactory->create($phpDocType, $param);
$phpDocInfo->addTagValueNode($paramTagValueNode);
Expand Down
16 changes: 4 additions & 12 deletions packages/BetterPhpDocParser/PhpDocNodeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use Rector\BetterPhpDocParser\Contract\BasePhpDocNodeVisitorInterface;
use Rector\BetterPhpDocParser\DataProvider\CurrentTokenIteratorProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\CloningPhpDocNodeVisitor;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Symplify\SimplePhpDocParser\Contract\PhpDocNodeVisitorInterface;
Expand All @@ -19,11 +18,6 @@
*/
final class PhpDocNodeMapper
{
/**
* @var TokenIteratorFactory
*/
private $tokenIteratorFactory;

/**
* @var PhpDocNodeVisitorInterface[]
*/
Expand All @@ -42,23 +36,21 @@ final class PhpDocNodeMapper
/**
* @var CloningPhpDocNodeVisitor
*/
private $cloningNodeVisitor;
private $cloningPhpDocNodeVisitor;

/**
* @param BasePhpDocNodeVisitorInterface[] $phpDocNodeVisitors
*/
public function __construct(
TokenIteratorFactory $tokenIteratorFactory,
CurrentTokenIteratorProvider $currentTokenIteratorProvider,
ParentConnectingPhpDocNodeVisitor $parentConnectingPhpDocNodeVisitor,
CloningPhpDocNodeVisitor $cloningNodeVisitor,
CloningPhpDocNodeVisitor $cloningPhpDocNodeVisitor,
array $phpDocNodeVisitors
) {
$this->tokenIteratorFactory = $tokenIteratorFactory;
$this->phpDocNodeVisitors = $phpDocNodeVisitors;
$this->currentTokenIteratorProvider = $currentTokenIteratorProvider;
$this->parentConnectingPhpDocNodeVisitor = $parentConnectingPhpDocNodeVisitor;
$this->cloningNodeVisitor = $cloningNodeVisitor;
$this->cloningPhpDocNodeVisitor = $cloningPhpDocNodeVisitor;
}

public function transform(PhpDocNode $phpDocNode, BetterTokenIterator $betterTokenIterator): void
Expand All @@ -70,7 +62,7 @@ public function transform(PhpDocNode $phpDocNode, BetterTokenIterator $betterTok
$parentPhpDocNodeTraverser->traverse($phpDocNode);

$cloningPhpDocNodeTraverser = new PhpDocNodeTraverser();
$cloningPhpDocNodeTraverser->addPhpDocNodeVisitor($this->cloningNodeVisitor);
$cloningPhpDocNodeTraverser->addPhpDocNodeVisitor($this->cloningPhpDocNodeVisitor);
$cloningPhpDocNodeTraverser->traverse($phpDocNode);

$phpDocNodeTraverser = new PhpDocNodeTraverser();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function enterNode(Node $node): ?Node
return null;
}

$bracketsAwareUnionTypeNode = new BracketsAwareIntersectionTypeNode($node->types);
$this->attributeMirrorer->mirror($node, $bracketsAwareUnionTypeNode);
$bracketsAwareIntersectionTypeNode = new BracketsAwareIntersectionTypeNode($node->types);
$this->attributeMirrorer->mirror($node, $bracketsAwareIntersectionTypeNode);

return $bracketsAwareUnionTypeNode;
return $bracketsAwareIntersectionTypeNode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use Rector\BetterPhpDocParser\PhpDocInfo\TokenIteratorFactory;
use Rector\BetterPhpDocParser\PhpDocNodeMapper;
use Rector\BetterPhpDocParser\ValueObject\Parser\BetterTokenIterator;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\BetterPhpDocParser\ValueObject\StartAndEnd;
Expand All @@ -31,11 +30,6 @@ final class BetterPhpDocParser extends PhpDocParser
*/
private $privatesCaller;

/**
* @var PhpDocNodeMapper
*/
private $phpDocNodeMapper;

/**
* @var DoctrineAnnotationDecorator
*/
Expand All @@ -49,14 +43,12 @@ final class BetterPhpDocParser extends PhpDocParser
public function __construct(
TypeParser $typeParser,
ConstExprParser $constExprParser,
PhpDocNodeMapper $phpDocNodeMapper,
TokenIteratorFactory $tokenIteratorFactory,
DoctrineAnnotationDecorator $doctrineAnnotationDecorator
) {
parent::__construct($typeParser, $constExprParser);

$this->privatesCaller = new PrivatesCaller();
$this->phpDocNodeMapper = $phpDocNodeMapper;
$this->doctrineAnnotationDecorator = $doctrineAnnotationDecorator;
$this->tokenIteratorFactory = $tokenIteratorFactory;
}
Expand Down
21 changes: 9 additions & 12 deletions packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ final class PhpDocInfoPrinter
/**
* @var PhpDocNodeTraverser
*/
private $changedPhpdocNodeTraverser;
private $changedPhpDocNodeTraverser;

public function __construct(
EmptyPhpDocDetector $emptyPhpDocDetector,
Expand All @@ -128,8 +128,8 @@ public function __construct(
$this->removeNodesStartAndEndResolver = $removeNodesStartAndEndResolver;
$this->changedPhpDocNodeVisitor = $changedPhpDocNodeVisitor;

$this->changedPhpdocNodeTraverser = new PhpDocNodeTraverser();
$this->changedPhpdocNodeTraverser->addPhpDocNodeVisitor($this->changedPhpDocNodeVisitor);
$this->changedPhpDocNodeTraverser = new PhpDocNodeTraverser();
$this->changedPhpDocNodeTraverser->addPhpDocNodeVisitor($this->changedPhpDocNodeVisitor);
}

public function printNew(PhpDocInfo $phpDocInfo): string
Expand Down Expand Up @@ -242,15 +242,12 @@ private function printDocChildNode(
}
}

if ($phpDocChildNode->value instanceof DoctrineAnnotationTagValueNode) {
$startAndEnd = $phpDocChildNode->value->getAttribute(PhpDocAttributeKey::START_AND_END);
if ($startAndEnd === null) {
$printedNode = $phpDocChildNode->name . $phpDocChildNode->value;
if ($phpDocChildNode->value instanceof DoctrineAnnotationTagValueNode && $shouldReprintChildNode) {
$printedNode = $phpDocChildNode->name . $phpDocChildNode->value;

// remove extra space between tags
$printedNode = Strings::replace($printedNode, self::TAG_AND_SPACE_REGEX, '$1(');
return self::NEWLINE_WITH_ASTERISK . $printedNode;
}
// remove extra space between tags
$printedNode = Strings::replace($printedNode, self::TAG_AND_SPACE_REGEX, '$1(');
return self::NEWLINE_WITH_ASTERISK . $printedNode;
}
}

Expand Down Expand Up @@ -373,7 +370,7 @@ private function correctPreviouslyReprintedFirstNode(int $key, StartAndEnd $star

private function shouldReprint(PhpDocChildNode $phpDocChildNode): bool
{
$this->changedPhpdocNodeTraverser->traverse($phpDocChildNode);
$this->changedPhpDocNodeTraverser->traverse($phpDocChildNode);
return $this->changedPhpDocNodeVisitor->hasChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,18 @@
namespace Rector\NodeTypeResolver\PhpDoc\NodeAnalyzer;

use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper;
use Rector\NodeTypeResolver\PhpDocNodeVisitor\NameImportingPhpDocNodeVisitor;
use Rector\PostRector\Collector\UseNodesToAddCollector;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\SimplePhpDocParser\PhpDocNodeTraverser;

final class DocBlockNameImporter
{
/**
* @var PhpDocNodeTraverser
*/
private $phpDocNodeTraverser;

/**
* @var StaticTypeMapper
*/
private $staticTypeMapper;

/**
* @var ClassNameImportSkipper
*/
private $classNameImportSkipper;

/**
* @var ParameterProvider
*/
private $parameterProvider;

/**
* @var UseNodesToAddCollector
*/
private $useNodesToAddCollector;

/**
* @var NameImportingPhpDocNodeVisitor
*/
private $nameImportingPhpDocNodeVisitor;

public function __construct(
ClassNameImportSkipper $classNameImportSkipper,
ParameterProvider $parameterProvider,
PhpDocNodeTraverser $phpDocNodeTraverser,
StaticTypeMapper $staticTypeMapper,
UseNodesToAddCollector $useNodesToAddCollector,
NameImportingPhpDocNodeVisitor $nameImportingPhpDocNodeVisitor
) {
$this->phpDocNodeTraverser = $phpDocNodeTraverser;
$this->staticTypeMapper = $staticTypeMapper;
$this->classNameImportSkipper = $classNameImportSkipper;
$this->parameterProvider = $parameterProvider;
$this->useNodesToAddCollector = $useNodesToAddCollector;
public function __construct(NameImportingPhpDocNodeVisitor $nameImportingPhpDocNodeVisitor)
{
$this->nameImportingPhpDocNodeVisitor = $nameImportingPhpDocNodeVisitor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public function replaceTagByAnother(PhpDocInfo $phpDocInfo, string $oldTag, stri

unset($phpDocNode->children[$key]);
$phpDocNode->children[] = new PhpDocTagNode($newTag, new GenericTagValueNode(''));
//
// $phpDocInfo->markAsChanged();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function enterNode(Node $node): ?Node
}

$phpParserNode = $this->currentNodeProvider->getNode();
if ($phpParserNode === null) {
if (! $phpParserNode instanceof \PhpParser\Node) {
throw new ShouldNotHappenException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\CodingStyle\ClassNameImport\ClassNameImportSkipper;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Configuration\Option;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\PostRector\Collector\UseNodesToAddCollector;
Expand All @@ -23,11 +22,6 @@ final class NameImportingPhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
*/
private $staticTypeMapper;

/**
* @var CurrentNodeProvider
*/
private $currentNodeProvider;

/**
* @var ParameterProvider
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\ObjectType;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Renaming\ValueObject\PseudoNamespaceToNamespace;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand All @@ -21,18 +20,13 @@ final class UnderscoreRenamePhpDocNodeVisitor extends AbstractPhpDocNodeVisitor
*/
private $staticTypeMapper;

/**
* @var CurrentNodeProvider
*/
private $currentNodeProvider;

/**
* @var PseudoNamespaceToNamespace|null
*/
private $pseudoNamespaceToNamespace;

/**
* @var \PhpParser\Node
* @var \PhpParser\Node|null
*/
private $currentPhpParserNode;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector\Fixture;

use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\String_;

final class SkipInstanceOf
{
public function run($value)
{
// a comment
if ($value instanceof String_ || $value instanceof DNumber) {
return null;
}

return 'another';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\Tests\EarlyReturn\Rector\If_\ChangeOrIfReturnToEarlyReturnRector\Fixture;

use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;

final class SkipInstanceOfMany
{
public function run($value)
{
// a comment
if ($value instanceof String_ || $value instanceof DNumber || $value instanceof LNumber) {
return null;
}

return 'another';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(ChangeOrIfReturnToEarlyReturnRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(ClassPropertyAssignToConstructorPromotionRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

$services->set(ChangeReadOnlyPropertyWithDefaultValueToConstantRector::class);
};
Loading

0 comments on commit 401dc65

Please sign in to comment.