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

Allow usage of self:: accessor for constants #368

Merged
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
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Doctrine\Tests\Common\Annotations\Fixtures;

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

/** @AnnotationWithConstants(self::VALUE_FOR_CLASS) */
class ClassWithAnnotationWithSelfConstantReference
{
use TraitWithSelfConstantReferenceTrait;

public const VALUE_FOR_CLASS = 'ClassWithAnnotationWithSelfConstantReference.VALUE_FROM_CLASS';
public const VALUE_FOR_TRAIT = 'ClassWithAnnotationWithSelfConstantReference.VALUE_FOR_TRAIT';

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

/**
* @return mixed
*
* @AnnotationWithConstants(self::VALUE_FOR_CLASS)
*/
public function classMethod()
{
return $this->classProperty;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

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

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

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

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