DocComment for the main class is incorrect when an anonymous class is used inside. #3

Closed
opened 2026-01-24 11:24:42 +01:00 by admin · 2 comments
Owner

Originally created by @xopoc14 on GitHub (Mar 21, 2019).

Example of the class:

<?php

namespace MyNameSpace;

/**
 * Class MyClass.
 *
 * @Annotation(
 *   id = "my_id"
 * )
 */
class MyClass {

  /**
   * {@inheritdoc}
   */
  public function initializeIterator() {


    $array = [
      ['name' => 'Jonathan','id' => '5'],
      ['name' => 'Abdul' ,'id' => '22'],
    ];

    $object = new \ArrayObject($array);

    $filter_iterator = new class ($object->getIterator()) extends \FilterIterator {
      public function accept() {
        $user = $this->getInnerIterator()->current();
        if( strcasecmp($user['name'],'abdul') == 0) {
          return false;
        }
        return true;
      }
    };

    return $filter_iterator;

  }

}

Then I try to get DocComment by next code:

$class = 'MyNameSpace\MyClass';

$finder = new \Doctrine\Common\Reflection\Psr0FindFile(['MyNameSpace' => ['my_folder']]);
$parser = new \Doctrine\Common\Reflection\StaticReflectionParser($class, $finder, FALSE);

$doctrine_doc = $parser->getDocComment();

expected result:

/**
 * Class MyClass.
 *
 * @Annotation(
 *   id = "my_id"
 * )
 */

actual result: empty string.

After debugging I found out that the parse overrides DocComment for the main class by DocComment of an anonymous class which is empty.

Originally created by @xopoc14 on GitHub (Mar 21, 2019). Example of the class: ``` <?php namespace MyNameSpace; /** * Class MyClass. * * @Annotation( * id = "my_id" * ) */ class MyClass { /** * {@inheritdoc} */ public function initializeIterator() { $array = [ ['name' => 'Jonathan','id' => '5'], ['name' => 'Abdul' ,'id' => '22'], ]; $object = new \ArrayObject($array); $filter_iterator = new class ($object->getIterator()) extends \FilterIterator { public function accept() { $user = $this->getInnerIterator()->current(); if( strcasecmp($user['name'],'abdul') == 0) { return false; } return true; } }; return $filter_iterator; } } ``` Then I try to get DocComment by next code: ``` $class = 'MyNameSpace\MyClass'; $finder = new \Doctrine\Common\Reflection\Psr0FindFile(['MyNameSpace' => ['my_folder']]); $parser = new \Doctrine\Common\Reflection\StaticReflectionParser($class, $finder, FALSE); $doctrine_doc = $parser->getDocComment(); ``` expected result: ``` /** * Class MyClass. * * @Annotation( * id = "my_id" * ) */ ``` actual result: empty string. After debugging I found out that the parse overrides DocComment for the main class by DocComment of an anonymous class which is empty.
admin added the Bug label 2026-01-24 11:24:42 +01:00
admin closed this issue 2026-01-24 11:24:42 +01:00
Author
Owner

@Ocramius commented on GitHub (Mar 24, 2019):

@xopoc14 would you be able to integrate a test scenario with the existing test suite (as a pull request) on 1.x?

@Ocramius commented on GitHub (Mar 24, 2019): @xopoc14 would you be able to integrate a test scenario with the existing test suite (as a pull request) on 1.x?
Author
Owner

@xopoc14 commented on GitHub (May 13, 2019):

@Ocramius added #20

@xopoc14 commented on GitHub (May 13, 2019): @Ocramius added #20
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/reflection#3