Skip to content

Commit

Permalink
Allow usage of self:: accessor for constants
Browse files Browse the repository at this point in the history
ex: @annotation(self::VALUE)
Fixes doctrine#269
  • Loading branch information
bertrandseurot committed Oct 2, 2020
1 parent 2dceb19 commit 5f58768
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/Doctrine/Common/Annotations/AnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,10 @@ private function collectParsingMetadata(ReflectionClass $class)
$this->imports[$name] = array_merge(
self::$globalImports,
$this->phpParser->parseClass($class),
['__NAMESPACE__' => $class->getNamespaceName()]
[
'__NAMESPACE__' => $class->getNamespaceName(),
'self' => $name,
]
);

$this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
Expand Down
61 changes: 61 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/AnnotationReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,67 @@ public function testOmitNotRegisteredAnnotation(): void
self::assertEquals([], $annotations);
}

public function testClassAnnotationSupportsSelfAccessorForConstants(): void
{
$reader = $this->getReader();
$ref = new ReflectionClass(Fixtures\ClassWithAnnotationWithSelfConstantReference::class);

$annotations = $reader->getClassAnnotations($ref);

self::assertCount(1, $annotations);

$annotation = $annotations[0];
self::assertInstanceOf(Fixtures\AnnotationWithConstants::class, $annotation);
self::assertEquals(
$annotation->value,
Fixtures\ClassWithAnnotationWithSelfConstantReference::VALUE_FOR_CLASS
);
}

public function testPropertyAnnotationSupportsSelfAccessorForConstants(): void
{
$reader = $this->getReader();
$ref = new ReflectionClass(Fixtures\ClassWithAnnotationWithSelfConstantReference::class);

$classProperty = $ref->getProperty('classProperty');
$classAnnotation = $reader->getPropertyAnnotation($classProperty, Fixtures\AnnotationWithConstants::class);
self::assertNotNull($classAnnotation);
self::assertEquals(
$classAnnotation->value,
Fixtures\ClassWithAnnotationWithSelfConstantReference::VALUE_FOR_CLASS
);

$traitProperty = $ref->getProperty('traitProperty');
$traitAnnotation = $reader->getPropertyAnnotation($traitProperty, Fixtures\AnnotationWithConstants::class);
self::assertNotNull($traitAnnotation);
self::assertEquals(
$traitAnnotation->value,
Fixtures\ClassWithAnnotationWithSelfConstantReference::VALUE_FOR_TRAIT
);
}

public function testMethodAnnotationSupportsSelfAccessorForConstants(): void
{
$reader = $this->getReader();
$ref = new ReflectionClass(Fixtures\ClassWithAnnotationWithSelfConstantReference::class);

$classMethod = $ref->getMethod('classMethod');
$classAnnotation = $reader->getMethodAnnotation($classMethod, Fixtures\AnnotationWithConstants::class);
self::assertNotNull($classAnnotation);
self::assertEquals(
$classAnnotation->value,
Fixtures\ClassWithAnnotationWithSelfConstantReference::VALUE_FOR_CLASS
);

$traitMethod = $ref->getMethod('traitMethod');
$traitAnnotation = $reader->getMethodAnnotation($traitMethod, Fixtures\AnnotationWithConstants::class);
self::assertNotNull($traitAnnotation);
self::assertEquals(
$traitAnnotation->value,
Fixtures\ClassWithAnnotationWithSelfConstantReference::VALUE_FOR_TRAIT
);
}

/**
* @group 45
* @runInSeparateProcess
Expand Down
14 changes: 14 additions & 0 deletions tests/Doctrine/Tests/Common/Annotations/DocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,20 @@ public function testSupportClassConstants(string $docblock, $expected): void
self::assertEquals($expected, $annotation->value);
}

public function testSupportSelfAccessorForClassConstants(): void
{
$parser = $this->createTestParser();
$dockBlock = '@AnnotationWithConstants(self::SOME_VALUE)';
$parser->setImports([
'annotationwithconstants' => AnnotationWithConstants::class,
'self' => ClassWithConstants::class,
]);

$result = $parser->parse($dockBlock);
self::assertInstanceOf(AnnotationWithConstants::class, $annotation = $result[0]);
self::assertEquals(ClassWithConstants::SOME_VALUE, $annotation->value);
}

public function testWithoutConstructorWhenIsNotDefaultValue(): void
{
$parser = $this->createTestParser();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace Doctrine\Tests\Common\Annotations\Fixtures;


use Doctrine\Tests\Common\Annotations\Fixtures\Traits\TraitWithSelfConstantReferenceTrait;

/**
* @AnnotationWithConstants(self::VALUE_FOR_CLASS)
*/
class ClassWithAnnotationWithSelfConstantReference
{
public const VALUE_FOR_CLASS = 'ClassWithAnnotationWithSelfConstantReference.VALUE_FROM_CLASS';
public const VALUE_FOR_TRAIT = 'ClassWithAnnotationWithSelfConstantReference.VALUE_FOR_TRAIT';

use TraitWithSelfConstantReferenceTrait;

/**
* @AnnotationWithConstants(self::VALUE_FOR_CLASS)
*/
private $classProperty;


/**
* @AnnotationWithConstants(self::VALUE_FOR_CLASS)
*/
public function classMethod(): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php


namespace Doctrine\Tests\Common\Annotations\Fixtures\Traits;

use Doctrine\Tests\Common\Annotations\Fixtures\AnnotationWithConstants;

trait TraitWithSelfConstantReferenceTrait
{
/**
* @AnnotationWithConstants(self::VALUE_FOR_TRAIT)
*/
private $traitProperty;

/**
* @AnnotationWithConstants(self::VALUE_FOR_TRAIT)
*/
public function traitMethod(): void
{
}
}

0 comments on commit 5f58768

Please sign in to comment.