Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop php doc info visitor #5250

Merged
merged 4 commits into from Jan 19, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/better-php-doc-parser/src/PhpDocInfo/PhpDocInfo.php
Expand Up @@ -125,6 +125,7 @@ public function getOriginalContent(): string
public function addPhpDocTagNode(PhpDocChildNode $phpDocChildNode): void
{
$this->phpDocNode->children[] = $phpDocChildNode;
$this->markAsChanged();
}

public function addTagValueNodeWithShortName(ShortNameAwareTagInterface $shortNameAwareTag): void
Expand Down Expand Up @@ -328,6 +329,8 @@ public function removeByType(string $type): void
}

unset($this->phpDocNode->children[$key]);

$this->markAsChanged();
}
}

Expand Down
Expand Up @@ -4,6 +4,7 @@

namespace Rector\BetterPhpDocParser\PhpDocManipulator;

use PHPStan\PhpDocParser\Ast\PhpDoc\ParamTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Naming\Contract\RenameValueObjectInterface;
use Rector\Naming\ValueObject\ParamRename;
Expand All @@ -29,7 +30,7 @@ public function renameParameterNameInDocBlock(RenameValueObjectInterface $rename

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);
$paramTagValueNode = $phpDocInfo->getParamTagValueNodeByName($renameValueObject->getCurrentName());
if ($paramTagValueNode === null) {
if (! $paramTagValueNode instanceof ParamTagValueNode) {
return;
}

Expand Down
Expand Up @@ -10,7 +10,6 @@
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\HttpKernel\RectorKernel;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;
Expand Down Expand Up @@ -45,13 +44,7 @@ protected function setUp(): void
protected function createPhpDocInfoFromDocCommentAndNode(string $docComment, Node $node): PhpDocInfo
{
$node->setDocComment(new Doc($docComment));

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if ($phpDocInfo === null) {
throw new ShouldNotHappenException();
}

return $phpDocInfo;
return $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
}

protected function yieldFilesFromDirectory(string $directory, string $suffix = '*.php'): Iterator
Expand Down
Expand Up @@ -17,7 +17,6 @@
use Rector\NodeTypeResolver\NodeVisitor\FunctionLikeParamArgPositionNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\FunctionMethodAndClassNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\NamespaceNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\PhpDocInfoNodeVisitor;
use Rector\NodeTypeResolver\NodeVisitor\StatementNodeVisitor;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
use Symplify\SmartFileSystem\SmartFileInfo;
Expand Down Expand Up @@ -64,11 +63,6 @@ final class NodeScopeAndMetadataDecorator
*/
private $configuration;

/**
* @var PhpDocInfoNodeVisitor
*/
private $phpDocInfoNodeVisitor;

/**
* @var NodeConnectingVisitor
*/
Expand All @@ -92,7 +86,6 @@ public function __construct(
NamespaceNodeVisitor $namespaceNodeVisitor,
NodeCollectorNodeVisitor $nodeCollectorNodeVisitor,
PHPStanNodeScopeResolver $phpStanNodeScopeResolver,
PhpDocInfoNodeVisitor $phpDocInfoNodeVisitor,
StatementNodeVisitor $statementNodeVisitor,
NodeConnectingVisitor $nodeConnectingVisitor,
FunctionLikeParamArgPositionNodeVisitor $functionLikeParamArgPositionNodeVisitor,
Expand All @@ -106,7 +99,6 @@ public function __construct(
$this->fileInfoNodeVisitor = $fileInfoNodeVisitor;
$this->nodeCollectorNodeVisitor = $nodeCollectorNodeVisitor;
$this->configuration = $configuration;
$this->phpDocInfoNodeVisitor = $phpDocInfoNodeVisitor;
$this->nodeConnectingVisitor = $nodeConnectingVisitor;
$this->functionLikeParamArgPositionNodeVisitor = $functionLikeParamArgPositionNodeVisitor;
$this->firstLevelNodeVisitor = $firstLevelNodeVisitor;
Expand Down Expand Up @@ -148,7 +140,6 @@ public function decorateNodesFromFile(array $nodes, SmartFileInfo $smartFileInfo
$nodeTraverser->addVisitor($this->nodeConnectingVisitor);
$nodeTraverser->addVisitor($this->functionMethodAndClassNodeVisitor);
$nodeTraverser->addVisitor($this->namespaceNodeVisitor);
$nodeTraverser->addVisitor($this->phpDocInfoNodeVisitor);
$nodeTraverser->addVisitor($this->firstLevelNodeVisitor);
$nodeTraverser->addVisitor($this->functionLikeParamArgPositionNodeVisitor);

Expand Down

This file was deleted.

18 changes: 4 additions & 14 deletions packages/node-type-resolver/src/PhpDoc/PhpDocTypeRenamer.php
Expand Up @@ -10,7 +10,7 @@
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\TypeNode;
use PHPStan\Type\ObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Generic\ValueObject\PseudoNamespaceToNamespace;
use Rector\StaticTypeMapper\StaticTypeMapper;
use Symplify\SimplePhpDocParser\PhpDocNodeTraverser;
Expand All @@ -27,27 +27,17 @@ final class PhpDocTypeRenamer
*/
private $staticTypeMapper;

/**
* @var PhpDocInfoFactory
*/
private $phpDocInfoFactory;

public function __construct(
PhpDocNodeTraverser $phpDocNodeTraverser,
StaticTypeMapper $staticTypeMapper,
PhpDocInfoFactory $phpDocInfoFactory
) {
public function __construct(PhpDocNodeTraverser $phpDocNodeTraverser, StaticTypeMapper $staticTypeMapper)
{
$this->phpDocNodeTraverser = $phpDocNodeTraverser;
$this->staticTypeMapper = $staticTypeMapper;
$this->phpDocInfoFactory = $phpDocInfoFactory;
}

public function changeUnderscoreType(
PhpDocInfo $phpDocInfo,
Node $node,
PseudoNamespaceToNamespace $pseudoNamespaceToNamespace
): void {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);

$attributeAwarePhpDocNode = $phpDocInfo->getPhpDocNode();
$phpParserNode = $node;

Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Expand Up @@ -56,6 +56,7 @@ parameters:
- packages/better-php-doc-parser/src/ValueObject/PhpDocNode/AbstractTagValueNode.php
- packages/testing/src/PHPUnit/*.php
- packages/node-type-resolver/tests/NodeVisitor/FunctionMethodAndClassNodeVisitor/FunctionMethodAndClassNodeVisitorTest.php
- rules/downgrade-php70/src/Rector/FunctionLike/AbstractDowngradeParamDeclarationRector.php

# phsptan bug
- utils/phpstan-extensions/src/Rule/KeepRectorNamespaceForRectorRule.php
Expand Down
Expand Up @@ -99,18 +99,18 @@ public function refactor(Node $node): ?Node
}

$toBeProcessedTypes = [];
foreach ($docParamTypes as $key => $docParamType) {
foreach ($docParamTypes as $paramName => $docParamType) {
if (! $this->isExclusivelyObjectType($docParamType)) {
continue;
}

/** @var ObjectType|UnionType $docParamType */
$assertionTypes = $this->getToBeProcessedTypes($params, $key, $docParamType);
$assertionTypes = $this->getToBeProcessedTypes($params, $paramName, $docParamType);
if ($assertionTypes === null) {
continue;
}

$variableName = ltrim($key, '$');
$variableName = ltrim($paramName, '$');
$toBeProcessedTypes[$variableName] = $assertionTypes;
}

Expand Down
Expand Up @@ -39,9 +39,10 @@ final class CallableThisArrayToAnonymousFunctionRector extends AbstractRector
*/
private $anonymousFunctionFactory;

public function __construct(CallableClassMethodMatcher $callableClassMethodMatcher,
AnonymousFunctionFactory $anonymousFunctionFactory)
{
public function __construct(
CallableClassMethodMatcher $callableClassMethodMatcher,
AnonymousFunctionFactory $anonymousFunctionFactory
) {
$this->callableClassMethodMatcher = $callableClassMethodMatcher;
$this->anonymousFunctionFactory = $anonymousFunctionFactory;
}
Expand Down
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Property;
use PhpParser\NodeTraverser;
use PHPStan\Type\MixedType;
use PHPStan\Type\NullType;
Expand Down Expand Up @@ -180,7 +181,7 @@ private function resolvePropertyFetchType(PropertyFetch $propertyFetch): Type
}

$property = $classLike->getProperty($propertyName);
if ($property === null) {
if (! $property instanceof Property) {
return new MixedType();
}

Expand Down
Expand Up @@ -9,7 +9,6 @@
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\MixedType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\PHPStan\TypeComparator;
Expand Down Expand Up @@ -105,10 +104,7 @@ public function refactor(Node $node): ?Node
}
}

if ($phpDocInfo instanceof PhpDocInfo && $this->typeComparator->isSubtype(
$constType,
$phpDocInfo->getVarType()
)) {
if ($this->typeComparator->isSubtype($constType, $phpDocInfo->getVarType())) {
return null;
}

Expand Down
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\Core\Rector\AbstractRector;
use Rector\DoctrineCodeQuality\NodeAnalyzer\SetterClassMethodAnalyzer;
use Rector\DoctrineCodeQuality\NodeManipulator\PropertyTypeManipulator;
Expand Down Expand Up @@ -104,7 +105,7 @@ public function getNodeTypes(): array
public function refactor(Node $node): ?Node
{
$property = $this->setterClassMethodAnalyzer->matchDateTimeSetterProperty($node);
if ($property === null) {
if (! $property instanceof Property) {
return null;
}

Expand Down
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Doctrine\Property_\ManyToOneTagValueNode;
use Rector\Core\Rector\AbstractRector;
use Rector\DoctrineCodeQuality\NodeAnalyzer\SetterClassMethodAnalyzer;
Expand Down Expand Up @@ -102,7 +103,7 @@ public function refactor(Node $node): ?Node
}

$property = $this->setterClassMethodAnalyzer->matchNullalbeClassMethodProperty($node);
if ($property === null) {
if (! $property instanceof Property) {
return null;
}

Expand Down
Expand Up @@ -203,7 +203,7 @@ private function resolveCollectionSetterAssignType(ClassMethod $classMethod): ?T
}

$property = $this->matchPropertyFetchToClassProperty($propertyFetches[0]);
if ($property === null) {
if (! $property instanceof Property) {
return null;
}

Expand Down
Expand Up @@ -116,13 +116,6 @@ private function removeVersionedTagFromProperties(Class_ $class): void
{
foreach ($class->getProperties() as $property) {
$propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);

$hasTypeVersionedTagValueNode = $propertyPhpDocInfo->hasByType(VersionedTagValueNode::class);

if (! $hasTypeVersionedTagValueNode) {
continue;
}

$propertyPhpDocInfo->removeByType(VersionedTagValueNode::class);
}
}
Expand Down
20 changes: 11 additions & 9 deletions rules/doctrine/src/NodeFactory/EntityUuidNodeFactory.php
Expand Up @@ -12,6 +12,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use Ramsey\Uuid\Uuid;
use Rector\BetterPhpDocParser\Contract\Doctrine\DoctrineTagNodeInterface;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Doctrine\Property_\GeneratedValueTagValueNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocNode\Doctrine\Property_\IdTagValueNode;
Expand Down Expand Up @@ -71,8 +72,10 @@ public function createUuidPropertyDefaultValueAssign(string $uuidVariableName):

private function decoratePropertyWithUuidAnnotations(Property $property, bool $isNullable, bool $isId): void
{
$this->clearVarAndOrmAnnotations($property);
$this->replaceIntSerializerTypeWithString($property);
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);

$this->clearVarAndOrmAnnotations($phpDocInfo);
$this->replaceIntSerializerTypeWithString($phpDocInfo);

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);

Expand All @@ -99,26 +102,25 @@ private function decoratePropertyWithUuidAnnotations(Property $property, bool $i
$phpDocInfo->addTagValueNodeWithShortName($generatedValueTagValueNode);
}

private function clearVarAndOrmAnnotations(Property $property): void
private function clearVarAndOrmAnnotations(PhpDocInfo $phpDocInfo): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);

$phpDocInfo->removeByType(VarTagValueNode::class);
$phpDocInfo->removeByType(DoctrineTagNodeInterface::class);
}

/**
* See https://github.com/ramsey/uuid-doctrine/issues/50#issuecomment-348123520.
*/
private function replaceIntSerializerTypeWithString(Property $property): void
private function replaceIntSerializerTypeWithString(PhpDocInfo $phpDocInfo): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);

$serializerTypeTagValueNode = $phpDocInfo->getByType(SerializerTypeTagValueNode::class);
if (! $serializerTypeTagValueNode instanceof SerializerTypeTagValueNode) {
return;
}

$serializerTypeTagValueNode->replaceName('int', 'string');
$hasReplaced = $serializerTypeTagValueNode->replaceName('int', 'string');
if ($hasReplaced) {
$phpDocInfo->markAsChanged();
}
}
}
Expand Up @@ -67,7 +67,6 @@ private function refactorParam(Param $param, FunctionLike $functionLike): void
}

$this->decorateWithDocBlock($functionLike, $param);

$param->type = null;
}

Expand All @@ -76,19 +75,18 @@ private function refactorParam(Param $param, FunctionLike $functionLike): void
*/
private function decorateWithDocBlock(FunctionLike $functionLike, Param $param): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);

if ($param->type === null) {
return;
}

$type = $this->staticTypeMapper->mapPhpParserNodePHPStanType($param->type);

if ($type instanceof IterableType) {
$type = new UnionType([$type, new IntersectionType([new ObjectType(Traversable::class)])]);
}

$paramName = $this->getName($param->var) ?? '';

$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike);
$this->phpDocTypeChanger->changeParamType($phpDocInfo, $type, $param, $paramName);
}
}