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

[Feature] Implementation of annotation getters for the ClassField joinpoint (#387) #388

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 src/Aop/Framework/ClassFieldAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Go\Aop\AspectException;
use Go\Aop\Intercept\FieldAccess;
use Go\Aop\Support\AnnotatedReflectionProperty;
use ReflectionProperty;

/**
Expand Down Expand Up @@ -67,7 +68,7 @@ public function __construct(string $className, string $fieldName, array $advices
{
parent::__construct($advices);

$this->reflectionProperty = $reflectionProperty = new ReflectionProperty($className, $fieldName);
$this->reflectionProperty = $reflectionProperty = new AnnotatedReflectionProperty($className, $fieldName);
// Give an access to protected field
if ($reflectionProperty->isProtected()) {
$reflectionProperty->setAccessible(true);
Expand All @@ -84,6 +85,8 @@ public function getAccessType(): int

/**
* Gets the field being accessed.
*
* @return ReflectionProperty|AnnotatedReflectionProperty the property which is being accessed.
*/
public function getField(): ReflectionProperty
{
Expand Down
4 changes: 2 additions & 2 deletions src/Aop/Support/AnnotatedReflectionMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* Extended version of ReflectionMethod with annotation support
*/
class AnnotatedReflectionMethod extends ReflectionMethod
class AnnotatedReflectionMethod extends ReflectionMethod implements AnnotationAccess
{
/**
* Annotation reader
Expand All @@ -28,7 +28,7 @@ class AnnotatedReflectionMethod extends ReflectionMethod
private static $annotationReader;

/**
* Gets a method annotation.
* Gets method annotation.
*
* @param string $annotationName The name of the annotation.
* @return mixed The Annotation or NULL, if the requested annotation does not exist.
Expand Down
60 changes: 60 additions & 0 deletions src/Aop/Support/AnnotatedReflectionProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types = 1);
/*
* Go! AOP framework
*
* @copyright Copyright 2014, Lisachenko Alexander <lisachenko.it@gmail.com>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Year isn't correct :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did copy/paste of docBlock.

Different classes have different years, I checked.

To be honest - I am ok for having author and/or copyright annotation, but year in docblock does not have any particular value, we could just remove it - it poses only unnecessary maintenance.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest - I am ok for having author and/or copyright annotation, but year in docblock does not have any particular value, we could just remove it - it poses only unnecessary maintenance.

Looks reasonable. Accepted, will do this for the master branch.

*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/

namespace Go\Aop\Support;

use Doctrine\Common\Annotations\Reader;
use Go\Core\AspectKernel;
use ReflectionProperty;

/**
* Extended version of ReflectionProperty with annotation support
*/
class AnnotatedReflectionProperty extends ReflectionProperty implements AnnotationAccess
{
/**
* Annotation reader
*
* @var Reader
*/
private static $annotationReader;

/**
* Gets property annotation.
*
* @param string $annotationName The name of the annotation.
* @return mixed The Annotation or NULL, if the requested annotation does not exist.
*/
public function getAnnotation(string $annotationName)
{
return self::getReader()->getPropertyAnnotation($this, $annotationName);
}

/**
* Gets the annotations applied to a property.
*/
public function getAnnotations(): array
{
return self::getReader()->getPropertyAnnotations($this);
}

/**
* Returns an annotation reader
*/
private static function getReader(): Reader
{
if (!self::$annotationReader) {
self::$annotationReader = AspectKernel::getInstance()->getContainer()->get('aspect.annotation.reader');
}

return self::$annotationReader;
}
}
31 changes: 31 additions & 0 deletions src/Aop/Support/AnnotationAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types = 1);
/*
* Go! AOP framework
*
* @copyright Copyright 2014, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/

namespace Go\Aop\Support;

/**
* Provides access to annotations from reflection class/property/method.
*/
interface AnnotationAccess
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for interface extraction refactoring

{
/**
* Gets annotation.
*
* @param string $annotationName The name of the annotation.
* @return mixed The Annotation or NULL, if the requested annotation does not exist.
*/
public function getAnnotation(string $annotationName);

/**
* Gets the annotations.
*/
public function getAnnotations(): array;
}
7 changes: 7 additions & 0 deletions tests/Go/Aop/Framework/AbstractMethodInvocationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

namespace Go\Aop\Framework;

use Go\Aop\Support\AnnotationAccess;

class AbstractMethodInvocationTest extends \PHPUnit_Framework_TestCase
{
/**
Expand All @@ -28,4 +30,9 @@ public function testStaticPartEqualsToReflectionMethod()
{
$this->assertInstanceOf('ReflectionMethod', $this->invocation->getStaticPart());
}

public function testProvidesAccessToAnnotations()
{
$this->assertInstanceOf(AnnotationAccess::class, $this->invocation->getMethod());
}
}
35 changes: 35 additions & 0 deletions tests/Go/Aop/Framework/ClassFieldAccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
declare(strict_types = 1);

namespace Go\Aop\Framework;

use Go\Aop\Support\AnnotationAccess;

class ClassFieldAccessTest extends \PHPUnit_Framework_TestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 for the test case

{
/**
* @var ClassFieldAccess
*/
protected $classField;

public function setUp()
{
$this->classField = new ClassFieldAccess(__CLASS__, 'classField', []);
}

public function testClassFiledReturnsProperty()
{
$this->assertEquals(__CLASS__, $this->classField->getField()->class);
$this->assertEquals('classField', $this->classField->getField()->name);
}

public function testStaticPartEqualsToReflectionMethod()
{
$this->assertInstanceOf('ReflectionProperty', $this->classField->getStaticPart());
}

public function testProvidesAccessToAnnotations()
{
$this->assertInstanceOf(AnnotationAccess::class, $this->classField->getField());
}
}