Skip to content

Commit

Permalink
inliner var
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 23, 2022
1 parent 28c02ae commit 9bc9b0f
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ public function testResolvesClass(string $filePath): void
foreach ($properties as $property) {
/** @var Property $property */
$phpDoc = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
/** @var VarTagValueNode $varTag */
$varTag = $phpDoc->getByType(VarTagValueNode::class)[0];
$value = $varTag->type->__toString();

$varTagValueNode = $phpDoc->getVarTagValueNode();
$this->assertInstanceOf(VarTagValueNode::class, $varTagValueNode);

$value = $varTagValueNode->type->__toString();
$propertyName = strtolower($this->nodeNameResolver->getName($property));

$result = $this->classAnnotationMatcher->resolveTagToKnownFullyQualifiedName($value, $property);
Expand Down
16 changes: 1 addition & 15 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PropertyTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\TemplateTagValueNode;
Expand Down Expand Up @@ -421,19 +420,6 @@ public function hasChanged(): bool
return $changedPhpDocNodeVisitor->hasChanged();
}

/**
* @return string[]
*/
public function getMethodTagNames(): array
{
$methodTagNames = [];
foreach ($this->phpDocNode->getMethodTagValues() as $methodTagValueNode) {
$methodTagNames[] = $methodTagValueNode->methodName;
}

return $methodTagNames;
}

public function makeMultiLined(): void
{
$this->isSingleLine = false;
Expand All @@ -444,7 +430,7 @@ public function getNode(): \PhpParser\Node
return $this->node;
}

public function resolveNameForPhpDocTagValueNode(PhpDocTagValueNode $phpDocTagValueNode): ?string
private function resolveNameForPhpDocTagValueNode(PhpDocTagValueNode $phpDocTagValueNode): ?string
{
foreach (self::TAGS_TYPES_TO_NAMES as $tagValueNodeType => $name) {
/** @var class-string<PhpDocTagNode> $tagValueNodeType */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,18 @@
namespace Rector\BetterPhpDocParser\PhpDocManipulator;

use PhpParser\Node;
use PhpParser\Node\Stmt\Expression;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\Type\MixedType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\ValueObject\Type\FullyQualifiedIdentifierTypeNode;
use Rector\Core\PhpParser\Node\BetterNodeFinder;

final class VarAnnotationManipulator
{
public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly BetterNodeFinder $betterNodeFinder
) {
}

public function decorateNodeWithInlineVarType(
Node $node,
TypeWithClassName $typeWithClassName,
string $variableName
): void {
$phpDocInfo = $this->resolvePhpDocInfo($node);

// already done
if ($phpDocInfo->getVarTagValueNode() !== null) {
return;
}

$fullyQualifiedIdentifierTypeNode = new FullyQualifiedIdentifierTypeNode($typeWithClassName->getClassName());

$varTagValueNode = new VarTagValueNode($fullyQualifiedIdentifierTypeNode, '$' . $variableName, '');
$phpDocInfo->addTagValueNode($varTagValueNode);
$phpDocInfo->makeSingleLined();
}

/**
* @api
*/
Expand All @@ -55,18 +29,4 @@ public function decorateNodeWithType(Node $node, Type $staticType): void
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
$this->phpDocTypeChanger->changeVarType($phpDocInfo, $staticType);
}

private function resolvePhpDocInfo(Node $node): PhpDocInfo
{
$currentStmt = $this->betterNodeFinder->resolveCurrentStatement($node);
if ($currentStmt instanceof Expression) {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($currentStmt);
} else {
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
}

$phpDocInfo->makeSingleLined();

return $phpDocInfo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function resolveAnnotationMethodCall(BetterTokenIterator $tokenIterator):
}

/**
* @api tests
* @see https://github.com/doctrine/annotations/blob/c66f06b7c83e9a2a7523351a9d5a4b55f885e574/lib/Doctrine/Common/Annotations/DocParser.php#L1215-L1224
* @return CurlyListNode|string|array<mixed>|ConstExprNode|DoctrineAnnotationTagValueNode
*/
Expand Down
14 changes: 0 additions & 14 deletions packages/NodeNestingScope/ValueObject/ControlStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,21 @@
namespace Rector\NodeNestingScope\ValueObject;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\While_;

final class ControlStructure
{
/**
* @var array<class-string<FunctionLike>>
*/
public const RETURN_ISOLATING_SCOPE_NODE_TYPES = [
Function_::class,
ClassMethod::class,
Closure::class,
ArrowFunction::class,
];

/**
* @var array<class-string<Node>>
*/
Expand Down
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -887,3 +887,7 @@ parameters:
path: packages-tests/BetterPhpDocParser/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php

- '#Symfony\\Component\\DependencyInjection\\Loader\\Configurator\\service not found#'

# false positive
- '#Public method "Rector\\PhpAttribute\\NodeFactory\\DoctrineAnnotationFactory\:\:createFromAttribute\(\)" is never used#'

3 changes: 3 additions & 0 deletions src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function __construct()
$this->configureCallValuesCollector = new ConfigureCallValuesCollector();
}

/**
* @api used in tests
*/
public function create(): ContainerInterface
{
return $this->createFromConfigs([]);
Expand Down

0 comments on commit 9bc9b0f

Please sign in to comment.