Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,7 @@ public function create(): Reader
AnnotationRegistry::registerLoader('class_exists');

// generated
if (class_exists(ConstantPreservingAnnotationReader::class) && class_exists(
ConstantPreservingDocParser::class
)) {
$docParser = new ConstantPreservingDocParser();
$annotationReader = new ConstantPreservingAnnotationReader($docParser);
} else {
// fallback for testing incompatibilities
$annotationReader = new AnnotationReader(new DocParser());
}
$annotationReader = $this->createAnnotationReader();

// without this the reader will try to resolve them and fails with an exception
// don't forget to add it to "stubs/Doctrine/Empty" directory, because the class needs to exists
Expand All @@ -67,4 +59,21 @@ public function create(): Reader

return $annotationReader;
}

/**
* @return AnnotationReader|ConstantPreservingAnnotationReader
*/
private function createAnnotationReader(): Reader
{
// these 2 classes are generated by "bin/rector sync-annotation-parser" command
if (class_exists(ConstantPreservingAnnotationReader::class) && class_exists(
ConstantPreservingDocParser::class
)) {
$docParser = new ConstantPreservingDocParser();
return new ConstantPreservingAnnotationReader($docParser);
}

// fallback for testing incompatibilities
return new AnnotationReader(new DocParser());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use Rector\DoctrineAnnotationGenerated\PhpDocNode\ConstantReferenceIdentifierRestorer;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\ClassExistenceStaticHelper;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -34,10 +35,19 @@ final class NodeAnnotationReader
*/
private $nodeNameResolver;

public function __construct(Reader $reader, NodeNameResolver $nodeNameResolver)
{
/**
* @var ConstantReferenceIdentifierRestorer
*/
private $constantReferenceIdentifierRestorer;

public function __construct(
Reader $reader,
NodeNameResolver $nodeNameResolver,
ConstantReferenceIdentifierRestorer $constantReferenceIdentifierRestorer
) {
$this->reader = $reader;
$this->nodeNameResolver = $nodeNameResolver;
$this->constantReferenceIdentifierRestorer = $constantReferenceIdentifierRestorer;
}

/**
Expand Down Expand Up @@ -69,11 +79,12 @@ public function readMethodAnnotation(ClassMethod $classMethod, string $annotatio
}

$this->alreadyProvidedAnnotations[] = $objectHash;
$this->constantReferenceIdentifierRestorer->restoreObject($methodAnnotation);

return $methodAnnotation;
}
} catch (AnnotationException $annotationException) {
// unable to laod
// unable to load
return null;
}

Expand All @@ -87,7 +98,14 @@ public function readClassAnnotation(Class_ $class, string $annotationClassName)
{
$classReflection = $this->createClassReflectionFromNode($class);

return $this->reader->getClassAnnotation($classReflection, $annotationClassName);
$annotation = $this->reader->getClassAnnotation($classReflection, $annotationClassName);
if ($annotation === null) {
return null;
}

$this->constantReferenceIdentifierRestorer->restoreObject($annotation);

return $annotation;
}

/**
Expand All @@ -105,6 +123,7 @@ public function readPropertyAnnotation(Property $property, string $annotationCla

/** @var object[] $propertyAnnotations */
$propertyAnnotations = $this->reader->getPropertyAnnotations($propertyReflection);

foreach ($propertyAnnotations as $propertyAnnotation) {
if (! is_a($propertyAnnotation, $annotationClassName, true)) {
continue;
Expand All @@ -116,6 +135,7 @@ public function readPropertyAnnotation(Property $property, string $annotationCla
}

$this->alreadyProvidedAnnotations[] = $objectHash;
$this->constantReferenceIdentifierRestorer->restoreObject($propertyAnnotation);

return $propertyAnnotation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public function __toString(): string
public static function createFromAnnotationAndAnnotationContent(
GeneratedValue $generatedValue,
string $annotationContent
) {
): self {
$items = get_object_vars($generatedValue);

return new self($items, $annotationContent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected function doTestPrintedPhpDocInfo(string $filePath, string $tagValueNod
$errorMessage = $this->createErrorMessage($filePath);
$this->assertSame($originalDocCommentText, $printedPhpDocInfo, $errorMessage);

$this->doTestContainsTagValueNodeType($nodeWithPhpDocInfo, $tagValueNodeType);
$this->doTestContainsTagValueNodeType($nodeWithPhpDocInfo, $tagValueNodeType, $filePath);
}

protected function yieldFilesFromDirectory(string $directory, string $suffix = '*.php'): Iterator
Expand Down Expand Up @@ -107,10 +107,13 @@ private function createErrorMessage(string $filePath): string
return 'Caused by: ' . $fileInfo->getRelativeFilePathFromCwd() . PHP_EOL;
}

private function doTestContainsTagValueNodeType(Node $node, string $tagValueNodeType): void
private function doTestContainsTagValueNodeType(Node $node, string $tagValueNodeType, string $filePath): void
{
/** @var PhpDocInfo $phpDocInfo */
$phpDocInfo = $node->getAttribute(AttributeKey::PHP_DOC_INFO);
$this->assertTrue($phpDocInfo->hasByType($tagValueNodeType));

$message = (new SmartFileInfo($filePath))->getRelativeFilePathFromCwd();

$this->assertTrue($phpDocInfo->hasByType($tagValueNodeType), $message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Property;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\EntityTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\TableTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Property_\ColumnTagValueNode;
Expand Down Expand Up @@ -40,5 +41,8 @@ final class TagValueToPhpParserNodeMap
TableTagValueNode::class => Class_::class,
CustomIdGeneratorTagValueNode::class => Property::class,
GeneratedValueTagValueNode::class => Property::class,

// special case for constants
GenericTagValueNode::class => Property::class,
];
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class AssertChoiceNonQuoteValues
{
/**
* @Assert\Choice({chalet, apartment})
* @Assert\Choice({"chalet", "apartment"})
*/
public $type;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class AssertQuoteChoice
const CHOICE_ONE = 'choice_one';
const CHOICE_TWO = 'choice_two';
/**
* @Assert\Choice({SomeEntity::CHOICE_ONE, SomeEntity::CHOICE_TWO})
* @Assert\Choice({AssertQuoteChoice::CHOICE_ONE, AssertQuoteChoice::CHOICE_TWO})
*/
private $someChoice = self::CHOICE_ONE;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
final class AssertStringType
{
/**
* @Assert\Type(string)
* @Assert\Type("string")
*/
public $anotherProperty;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Fixture\ConstantReference;

use Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Source\ApiFilter;
use Doctrine\ORM\Mapping as ORM;

final class Book
{
/**
* @ApiFilter(Book::class)
* @ORM\Column()
*/
public $filterable;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@
class GeneratedValueWithStrategy
{
/**
* @ORM\GeneratedValue(AUTO)
* @ORM\GeneratedValue("AUTO")
*/
private $implicit;

/**
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $explicit;

/**
* @ORM\GeneratedValue(strategy=AUTO)
*/
private $explicitWithoutQuotes;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Fixture\DoctrineGeneratedValue;

use Doctrine\ORM\Mapping as ORM;

class GeneratedValueWithStrategyString
{
/**
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $explicit;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Fixture\SymfonyRoute;

use Symfony\Component\Routing\Annotation\Route;
use Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Source\TestController;

final class RouteName
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Fixture\SymfonyRoute;

use Symfony\Component\Routing\Annotation\Route;
use Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Source\MyController;

final class RouteNameWithMethodAndClassConstant
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Source;

use Rector\Core\Exception\ShouldNotHappenException;

/**
* @Annotation
* @Target({"PROPERTY", "CLASS"})
*/
class ApiFilter
{
public function __construct($options = [])
{
if(! class_exists($options['value'])) {
throw new ShouldNotHappenException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Source;

final class MyController
{
public const ROUTE_NAME = 'some_route_name';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Rector\BetterPhpDocParser\Tests\PhpDocParser\TagValueNodeReprint\Source;

final class TestController
{
public const ROUTE_NAME = 'some_route_name';
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Iterator;
use Nette\Utils\Strings;
use PHPStan\PhpDocParser\Ast\PhpDoc\GenericTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\EntityTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Class_\TableTagValueNode;
use Rector\BetterPhpDocParser\PhpDocNode\Doctrine\Property_\ColumnTagValueNode;
Expand Down Expand Up @@ -61,6 +62,9 @@ private function getDirectoriesByTagValueNodes(): array
TableTagValueNode::class => __DIR__ . '/Fixture/DoctrineTable',
CustomIdGeneratorTagValueNode::class => __DIR__ . '/Fixture/DoctrineCustomIdGenerator',
GeneratedValueTagValueNode::class => __DIR__ . '/Fixture/DoctrineGeneratedValue',

// special case
GenericTagValueNode::class => __DIR__ . '/Fixture/ConstantReference',
];
}
}
8 changes: 8 additions & 0 deletions packages/doctrine-annotation-generated/config/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
_defaults:
public: true
autowire: true
autoconfigure: true

Rector\DoctrineAnnotationGenerated\:
resource: '../src'
Loading