Skip to content

Commit

Permalink
Cleanup (#1268)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Nov 18, 2021
1 parent 59809f3 commit 1258576
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 177 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\Annotation;

use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
use Rector\Php80\ValueObject\AnnotationToAttribute;

final class InverseJoinColumnCorrector
{
public function correctInverseJoinColumn(
AnnotationToAttribute $annotationToAttribute,
DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode
): void {
$docNodeValue = $doctrineAnnotationTagValueNode->getValue('inverseJoinColumns');
if ($annotationToAttribute->getTag() !== 'Doctrine\ORM\Mapping\JoinTable') {
return;
}

if (! $docNodeValue instanceof CurlyListNode) {
return;
}

$values = [$docNodeValue->getValues(), $docNodeValue->getOriginalValues()];

foreach ($values as $value) {
if (! array_key_exists(0, $value) && ! ($value[0] instanceof DoctrineAnnotationTagValueNode)) {
continue;
}

$identifierTypeNode = $value[0]->identifierTypeNode;
$identifierTypeNode->setAttribute(
PhpDocAttributeKey::RESOLVED_CLASS,
'Doctrine\ORM\Mapping\InverseJoinColumn'
);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeFirstClassCallableSyntaxRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DowngradeFirstClassCallableSyntaxRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
<?php

declare(strict_types=1);

namespace Rector\Tests\DowngradePhp81\Rector\FuncCall\DowngradeFirstClassCallableSyntaxRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class DowngradeFirstClassCallableSyntaxRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Rector\Compatibility\NodeAnalyzer;

use PhpParser\Node\Expr;
use PhpParser\Node\NullableType;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\Core\PhpParser\Node\Value\ValueResolver;

final class RequiredAnnotationPropertyAnalyzer
{
public function __construct(
private ValueResolver $valueResolver
) {
}

public function isRequiredProperty(PhpDocInfo $phpDocInfo, Property $property): bool
{
if ($phpDocInfo->hasByAnnotationClass('Doctrine\Common\Annotations\Annotation\Required')) {
return true;
}

// sometimes property has default null, but @var says its not null - that's due to nullability of typed properties
// in that case, we should treat property as required
$firstProperty = $property->props[0];
if (! $firstProperty->default instanceof Expr) {
return false;
}

if (! $this->valueResolver->isNull($firstProperty->default)) {
return false;
}

$varTagValueNode = $phpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
return false;
}

if ($varTagValueNode->type instanceof NullableTypeNode) {
return false;
}

return $property->type instanceof NullableType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
namespace Rector\Compatibility\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Ast\Type\NullableTypeNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\Compatibility\NodeAnalyzer\RequiredAnnotationPropertyAnalyzer;
use Rector\Compatibility\ValueObject\PropertyWithPhpDocInfo;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
Expand All @@ -39,7 +36,8 @@ final class AttributeCompatibleAnnotationRector extends AbstractRector
public function __construct(
private PhpAttributeAnalyzer $phpAttributeAnalyzer,
private PhpDocTagRemover $phpDocTagRemover,
private ParamTagRemover $paramTagRemover
private ParamTagRemover $paramTagRemover,
private RequiredAnnotationPropertyAnalyzer $requiredAnnotationPropertyAnalyzer
) {
}

Expand Down Expand Up @@ -127,7 +125,7 @@ public function refactor(Node $node): ?Node
}

$propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($property);
if (! $this->isRequiredProperty($propertyPhpDocInfo, $property)) {
if (! $this->requiredAnnotationPropertyAnalyzer->isRequiredProperty($propertyPhpDocInfo, $property)) {
continue;
}

Expand Down Expand Up @@ -195,35 +193,6 @@ private function createConstructParams(array $requiredPropertiesWithPhpDocInfos)
return $params;
}

private function isRequiredProperty(PhpDocInfo $propertyPhpDocInfo, Property $property): bool
{
if ($propertyPhpDocInfo->hasByAnnotationClass('Doctrine\Common\Annotations\Annotation\Required')) {
return true;
}

// sometimes property has default null, but @var says its not null - that's due to nullability of typed properties
// in that case, we should treat property as required
$firstProperty = $property->props[0];
if (! $firstProperty->default instanceof Expr) {
return false;
}

if (! $this->valueResolver->isNull($firstProperty->default)) {
return false;
}

$varTagValueNode = $propertyPhpDocInfo->getVarTagValueNode();
if (! $varTagValueNode instanceof VarTagValueNode) {
return false;
}

if ($varTagValueNode->type instanceof NullableTypeNode) {
return false;
}

return $property->type instanceof NullableType;
}

/**
* @param PropertyWithPhpDocInfo[] $requiredPropertiesWithPhpDocInfos
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\VariadicPlaceholder;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -79,7 +79,7 @@ private function createCallback(FuncCall|MethodCall|StaticCall $node): Expr
if ($node instanceof FuncCall) {
return $node->name instanceof Name ? new String_($node->name->toString()) : $node->name;
}

if ($node instanceof MethodCall) {
$object = $node->var;
$method = $node->name instanceof Identifier ? new String_($node->name->toString()) : $node->name;
Expand Down
9 changes: 3 additions & 6 deletions rules/Php80/Rector/Class_/AnnotationToAttributeRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use Rector\BetterPhpDocParser\Annotation\ChangeResolvedClassInParticularContextForAnnotation;
use Rector\BetterPhpDocParser\Annotation\InverseJoinColumnCorrector;
use Rector\BetterPhpDocParser\AnnotationAnalyzer\DoctrineAnnotationTagValueNodeAnalyzer;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
Expand Down Expand Up @@ -68,7 +68,7 @@ public function __construct(
private ConvertedAnnotationToAttributeParentRemover $convertedAnnotationToAttributeParentRemover,
private AttrGroupsFactory $attrGroupsFactory,
private DoctrineAnnotationTagValueNodeAnalyzer $doctrineAnnotationTagValueNodeAnalyzer,
private ChangeResolvedClassInParticularContextForAnnotation $changeResolvedClassInParticularContextForAnnotation
private InverseJoinColumnCorrector $inverseJoinColumnCorrector,
) {
}

Expand Down Expand Up @@ -245,10 +245,7 @@ private function processDoctrineAnnotationClasses(PhpDocInfo $phpDocInfo): array
continue;
}

$this->changeResolvedClassInParticularContextForAnnotation->changeResolvedClassIfNeed(
$annotationToAttribute,
$docNode
);
$this->inverseJoinColumnCorrector->correctInverseJoinColumn($annotationToAttribute, $docNode);

if ($this->doctrineAnnotationTagValueNodeAnalyzer->isNested(
$docNode,
Expand Down

0 comments on commit 1258576

Please sign in to comment.