Bug #46008

Reflection should resolve relative namespaces

Added by Christopher Hlubek over 2 years ago. Updated about 2 years ago.

Status:Resolved Start date:2013-03-04
Priority:Should have Due date:
Assigned To:Sebastian Kurfuerst % Done:

100%

Category:Reflection
Target version:-
PHP Version: Complexity:
Has patch:No Affected Flow version:Git master

Description

Annotations (e.g. @var) with relative class names are not resolved correctly under the namespace of the given class.
To have a correct handling of namespaces in PHP we should support the usage of relative class names in these places.

 1namespace MyVendor\MyPackage\Domain\Model;
 2
 3class Foo {
 4
 5    /**
 6     * @var Bar
 7     */
 8    protected $bar;
 9
10}
1namespace MyVendor\MyPackage\Domain\Model;
2
3class Bar {
4
5}

Since this is a problem for some people (e.g. also PhpStorm uses relative class names when auto-completing) I consider this a bug we should solve in the framework.


Related issues

related to TYPO3.Flow - Bug #50909: Dependency injection for imported namespaces fails for ab... Resolved 2013-08-08
related to TYPO3.Flow - Bug #57034: Dependency injection for imported namespaces fails for co... Resolved 2014-03-18
related to Core - Bug #57856: @inject does not work relative to current namespace Under Review 2014-04-12

Associated revisions

Revision bc8f7164
Added by Sebastian Kurfuerst about 2 years ago

[FEATURE] Resolve relative namespaces and use statements in @var & @param annotations

Reflection should expand shortened or namespaced @var & @param annotations
to their fully qualified class name.

This has a direct benefit in the following areas (all covered by tests):

  • Reflection returns the fully qualified class name for @var and @param annotations
  • Reflection returns the fully qualified property types for Domain Models
    inside the class schema
  • Dependency Injection using @Flow\Inject works with shortened class names
  • Property mapping works for shortened class names in type hint & @param annotation

Resolves: #46008
Releases: master
Change-Id: I28fd7c7993ea79714cc62032fcc332ac8cf9d4bc

Revision 751b4b55
Added by Bastian Waidelich almost 2 years ago

[BUGFIX] Fix resolving of relative namespaces in parent classes

With I28fd7c7993ea79714cc62032fcc332ac8cf9d4bc the ReflectionService
resolves relative and aliased namespaces automatically.
This doesn't work for inherited classes though because the code only
considered the current class, not it's parents.

With this change code like this will work::

use TYPO3\Flow\Security\Context;
class SomeParentClass {
/**
  • @var Context
  • @Flow\Inject
    */
    protected $securityContext;
// ...
}
class SomeChildClass extends SomeParentClass {
}

Change-Id: Ie4b931a03a94d506890e996b9d8bae7d0d1a2e14
Fixes: #50909
Related: #46008
Releases: master

Revision ca5dd2ca
Added by Bastian Waidelich over 1 year ago

[BUGFIX] Fix resolving of imported collection types

This change extends the ReflectionService so that it is able to
expand our custom notation for collection types::

CollectionType<ElementType>

Background:

In the Coding Guidelines we encourage developers to import namespaces
in order to increase readability.

With #46008 the ReflectionService is able to resolve relative and
imported namespace (with #50909 this also works for abstract classes).

But this does not work for our custom notation of Collection types yet.
With this fix relative and imported class names are supported also for
our custom notation of collection types and the above would be
expanded to::

\CollectionTypeNamespace\CollectionType<\ElementTypeNamespace\ElementType>

Change-Id: I4e228d6846776d2500d7e8aec2bc88df170bc9de
Fixes: #57034
Related: #46008
Related: #50909
Releases: master, 2.2, 2.1

Revision 0b45aa53
Added by Bastian Waidelich over 1 year ago

[BUGFIX] Fix resolving of imported collection types

This change extends the ReflectionService so that it is able to
expand our custom notation for collection types::

CollectionType&lt;ElementType&gt;

Background:

In the Coding Guidelines we encourage developers to import namespaces
in order to increase readability.

With #46008 the ReflectionService is able to resolve relative and
imported namespace (with #50909 this also works for abstract classes).

But this does not work for our custom notation of Collection types yet.
With this fix relative and imported class names are supported also for
our custom notation of collection types and the above would be
expanded to::

\CollectionTypeNamespace\CollectionType<\ElementTypeNamespace\ElementType>

Change-Id: I4e228d6846776d2500d7e8aec2bc88df170bc9de
Fixes: #57034
Related: #46008
Related: #50909
Releases: master, 2.2, 2.1

Revision 4935af46
Added by Bastian Waidelich over 1 year ago

[BUGFIX] Fix resolving of imported collection types

This change extends the ReflectionService so that it is able to
expand our custom notation for collection types::

CollectionType&lt;ElementType&gt;

Background:

In the Coding Guidelines we encourage developers to import namespaces
in order to increase readability.

With #46008 the ReflectionService is able to resolve relative and
imported namespace (with #50909 this also works for abstract classes).

But this does not work for our custom notation of Collection types yet.
With this fix relative and imported class names are supported also for
our custom notation of collection types and the above would be
expanded to::

\CollectionTypeNamespace\CollectionType<\ElementTypeNamespace\ElementType>

Change-Id: I4e228d6846776d2500d7e8aec2bc88df170bc9de
Fixes: #57034
Related: #46008
Related: #50909
Releases: master, 2.2, 2.1

History

#1 Updated by Bastian Waidelich over 2 years ago

Probably we should also consider use statements:

 1namespace MyVendor\MyPackage\Domain\Model;
 2
 3use MyVendor\MyPackage\Bar as Baz;
 4
 5class Foo {
 6
 7    /**
 8     * @var Baz
 9     */
10    protected $baz;
11}

but it seems PHP itself is buggy there https://bugs.php.net/bug.php?id=63665

#2 Updated by Bastian Waidelich over 2 years ago

Bastian Waidelich wrote:

but it seems PHP itself is buggy there https://bugs.php.net/bug.php?id=63665

This bug report seems is about something different it seems. This works as expected:

 1namespace Foo\Bar;
 2
 3class Baz {
 4
 5}
 6
 7namespace MyPackage;
 8use Foo\Bar\Baz as Quux;
 9
10class Test {
11
12    /**
13     * @param Quux $param
14     * @return void
15     */
16    public function test(Quux $param) {
17    }
18}
19
20$reflectionMethod = new \ReflectionMethod('MyPackage\Test', 'test');
21echo current($reflectionMethod->getParameters())->getClass();

Result:

Class [ class Foo\Bar\Baz ] { @@ test.php 4-6 - Constants [0] { } - Static properties [0] { } - Static methods [0] { } - Properties [0] { } - Methods [0] { } }

#3 Updated by Sebastian Kurfuerst over 2 years ago

  • Status changed from New to Accepted
  • Assigned To set to Sebastian Kurfuerst

I have a patch for this in the works; almost ready.

#4 Updated by Bastian Waidelich over 2 years ago

Sebastian Kurfuerst wrote:

I have a patch for this in the works; almost ready.

Great! Looking forward to this one

#5 Updated by Gerrit Code Review over 2 years ago

  • Status changed from Accepted to Under Review

Patch set 1 for branch master has been pushed to the review server.
It is available at https://review.typo3.org/20158

#6 Updated by Gerrit Code Review about 2 years ago

Patch set 2 for branch master has been pushed to the review server.
It is available at https://review.typo3.org/20158

#7 Updated by Gerrit Code Review about 2 years ago

Patch set 3 for branch master has been pushed to the review server.
It is available at https://review.typo3.org/20158

#8 Updated by Gerrit Code Review about 2 years ago

Patch set 4 for branch master has been pushed to the review server.
It is available at https://review.typo3.org/20158

#9 Updated by Sebastian Kurfuerst about 2 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100

Also available in: Atom PDF