Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get use statement from class

Not quite sure of the best title but I will explain what I am asking as best I can. Assume I have the following file:

MyCustomClass.php

<?php    

namespace MyNamespace;

use FooNamespace\FooClass;
use BarNamespace\BarClass as Bar;
use BazNamespace\BazClass as BazSpecial;

class MyCustomClass {

    protected $someDependencies = [];

    public function __construct(FooClass $foo, Bar $bar) {

        $someDependencies[] = $foo;
        $someDependencies[] = $bar;
    }
}

Now if I were to use reflection, I could get the fully qualified class names from the type hints in the construct.

However, I would recieve FooNamespace\FooClass and BarNamespace\BarClass. Not, FooNamespace\FooClass and BarNamespace\Bar. I would also get no reference to BazNamespace\BazClass.

Basically, my question is: How can I get the fully qualified names from MyCustomClass.php while only knowing FooClass, Bar, and, BazSpecial?

I do not want to use a file parser as this will eat performance. I want to be able to do something like:

$class = new ReflectionClass('MyCustomClass');
...
$class->getUsedClass('FooClass'); // FooNamespace\FooClass
$class->getUsedClass('Bar'); // BarNamespace\BarClass
$class->getUsedClass('BazSpecial'); // BazNamespace\BazClass

How would I go about doing this?

like image 461
Ozzy Avatar asked May 18 '15 16:05

Ozzy


1 Answers

Seeing as no one has answered, I assume there is not an easy way to achieve this. I have therefore created my own class called ExtendedReflectionClass which achieves what I need.

I have created a gist with the class file and a readme, which is at the bottom so get scrolling!.

ExtendedReflectionClass

Usage example:

require 'ExtendedReflectionClass.php';
require 'MyCustomClass.php';

$class = new ExtendedReflectionClass('MyNamespace\Test\MyCustomClass');

$class->getUseStatements();    
// [
//     [
//         'class' => 'FooNamespace\FooClass',
//         'as' => 'FooClass'
//     ],
//     [
//         'class' => 'BarNamespace\BarClass',
//         'as' => 'Bar'
//     ],
//     [
//         'class' => 'BazNamespace\BazClass',
//         'as' => 'BazSpecial'
//     ]
// ]

$class->hasUseStatement('FooClass'); // true
$class->hasUseStatement('BarNamespace\BarClass'); // true
$class->hasUseStatement('BazSpecial'); // true

$class->hasUseStatement('SomeNamespace\SomeClass'); // false
like image 99
Ozzy Avatar answered Nov 14 '22 02:11

Ozzy