cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

Phalcon\Annotations can't read class annotation from parent

Open HanXHX opened this issue 9 years ago • 3 comments

Expected and Actual Behavior

Bug or feature? :)

Phalcon\Annotations can read method annotation from parent classes (if ommited in child class). However, it can't read class annotation.

Provide minimal script to reproduce the issue

<?php

use Phalcon\Annotations\Adapter\Memory as Annotation;

/**
 * @iam("animal")
 */ 
abstract class Animal
{
	/**
	 * @iam("animal")
	 */ 
	public function walk()
	{
		return 'walking';
	}
}


class Dog extends Animal 
{

}


$an = new Annotation();

// OK
var_dump($an->getMethod('Animal', 'walk')->get('iam'));
// OK
var_dump($an->getMethod('Dog', 'walk')->get('iam'));
// OK
var_dump($an->get('Animal')->getClassAnnotations()->get('iam'));
// Fail
var_dump($an->get('Dog')->getClassAnnotations()->get('iam'));

Expected:

If you get class annotations from child class ("Dog"), i expect to have parent annotation.

Details

  • Phalcon version: 3.0.2
  • PHP Version: PHP 7.0.13-0ubuntu0.16.04.1 (cli) ( NTS )
  • Operating System: Debian / Ubuntu
  • Installation type: Installing via package manager

HanXHX avatar Mar 21 '17 15:03 HanXHX

It just like Java Annotation without @Inherited

leupom avatar Mar 22 '17 01:03 leupom

The annotations in PHP are not inherited, are not part of the inheritance system.

It's not a problem of Phalcon but a feature of PHP itself. The annotations in Phalcon as other frameworks are built around the ReflectionClass functionality.

Consider the following full example:

/**
 * @iam('test')
 */
class A {

  /**
   * @iam('property')
   */
  protected $property;

  /**
   * @iam('method')
   */
  public function test() {
  }
}

class B extends A {

   public function test() {
   }
}

function info($reflection) {
  var_dump($reflection->getDocComment());
  var_dump(array_map(function($property) {
     return $property->getDocComment();
  }, $reflection->getProperties()));
  var_dump(array_map(function($method) {
     return $method->getDocComment();
  }, $reflection->getMethods()));
}

$reflection = new \ReflectionClass('A');
info($reflection);
$reflection = new \ReflectionClass('B');
info($reflection);

Here we have, for A:

  • Returns a class doc
  • Returns a property doc
  • Returns a method doc

And for B we have:

  • Doesn't return a class doc because B it's declared without it.
  • Returns a property doc because that property is declared in A, and in A it has a phpdoc associated.
  • Doesn't return a method doc because the method is overridden and doesn't have a phpdoc associated.

Implementing DocComment inheritance in Phalcon would be hard and barely useful. Aside of the crawling effort that would mean, we should consider different inheritance behaviours, such: what happens if the parent has two annotations for a class but the child wants to override only one of them? The system gets too complicated to be something functional.

I think it's not that bad having to redeclare the annotations in the extended class/overridden methods following how PHP expects them.

gguridi avatar Mar 22 '17 11:03 gguridi

Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues

stale[bot] avatar Apr 16 '18 20:04 stale[bot]