Skip to content

Latest commit

 

History

History
56 lines (38 loc) · 2.08 KB

features.md

File metadata and controls

56 lines (38 loc) · 2.08 KB

Feature examples

Reflecting on classes not yet loaded

In most cases, creating reflections using a built-in SourceLocator or the ReflectionClass::createFromName() technique doesn't attempt to load code. This means if a class has not already been declared, you can safely assume that Better Reflection won't load the class.

Note that if the class is already loaded, then this Better Reflection does not then unload that class - this is not currently possible in PHP.

Reflecting on things within a string

See Loading a class from a string

Analysing types from DocBlocks

See types documentation

AST extraction from Reflections

See AST extraction documentation

PHP 7 Parameter Type Declarations and Return Type Declarations

These act in the same way as the core reflection API, except they return a \Roave\BetterReflection\Reflection\ReflectionType instance (which does not extend \ReflectionType).

$reflectionType = $parameterInfo->getType();

However, Better Reflection also gives the ability to change, and remove type declarations. Removing these types might be useful if you want to make code written for PHP 7 work in PHP 5 for example, or setting new types to do the opposite. For example, you might want to set the PHP 7 return type declaration to that defined in the PHP DocBlock.

// Change a function to ensure it returns an integer
$functionInfo->setReturnType('int');

// If there is only one type defined in the DocBlock, set it as the return type
$returnTypes = $functionInfo->getDocBlockReturnTypes();
if (count($returnTypes) === 1) {
    $functionInfo->setReturnType((string) $returnTypes[0]);
}

// Remove the return type declaration
$functionInfo->removeReturnType();

You can do similar things with parameter types also:

$parameterInfo->setType('int');
$parameterInfo->removeType();