Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
-
Upload
james-titcumb -
Category
Technology
-
view
208 -
download
2
Transcript of Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
![Page 1: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/1.jpg)
Mirror, mirror on the wall: Building a new PHP reflection library
James TitcumbDutch PHP Conference 2016
![Page 2: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/2.jpg)
James Titcumbwww.jamestitcumb.comwww.roave.comwww.phphants.co.ukwww.phpsouthcoast.co.uk@asgrim
Who is this guy?
![Page 3: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/3.jpg)
Reflection
@asgrim
![Page 4: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/4.jpg)
![Page 5: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/5.jpg)
© 1937 Disney’s Snow White - disneyscreencaps.com
![Page 6: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/6.jpg)
![Page 7: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/7.jpg)
Mostly this...public function testSomething()
{
$myObj = new Thing();
$propReflection = new \ReflectionProperty($myObj, 'foo');
$propReflection->setAccessible(true);
$propReflection->setValue($myObj, 'whatever');
// ... whatever ...
}
![Page 8: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/8.jpg)
● Structure● Metadata● Values● Type introspection● Modification
Reflection
![Page 9: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/9.jpg)
How does it work?
![Page 10: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/10.jpg)
zend_object (zend_types.h)● zend_class_entry *ce (zend.h)
○ zval* static_members_table○ HashTable function_table○ HashTable properties_info○ HashTable constants_table○ zend_class_entry** interfaces○ zend_class_entry** traits○ (…other stuff…)
Roughly...
![Page 11: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/11.jpg)
ReflectionClass->hasMethod
GET_REFLECTION_OBJECT_PTR(ce);
lc_name = zend_str_tolower_dup(name, name_len);
if ((ce == zend_ce_closure && (name_len == sizeof(ZEND_INVOKE_FUNC_NAME)-1)
&& memcmp(lc_name, ZEND_INVOKE_FUNC_NAME, sizeof(ZEND_INVOKE_FUNC_NAME)-1) == 0)
|| zend_hash_str_exists(&ce->function_table, lc_name, name_len)) {
efree(lc_name);
RETURN_TRUE;
} else {
efree(lc_name);
RETURN_FALSE;
}
![Page 12: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/12.jpg)
Okay. What now?
![Page 13: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/13.jpg)
github.com/ /BetterReflection
Better Reflection!
![Page 14: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/14.jpg)
![Page 15: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/15.jpg)
What?
![Page 16: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/16.jpg)
Why?
![Page 17: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/17.jpg)
Features!
![Page 18: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/18.jpg)
How?
![Page 19: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/19.jpg)
![Page 20: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/20.jpg)
![Page 21: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/21.jpg)
Reflector
Source Locator
PhpParser
Reflection
![Page 22: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/22.jpg)
$reflection = new ReflectionClass(
\My\ExampleClass::class
);
$this->assertSame(
'ExampleClass',
$reflection->getShortName()
);
![Page 23: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/23.jpg)
$reflection = ReflectionClass::createFromName(
\My\ExampleClass::class
);
$this->assertSame(
'ExampleClass',
$reflection->getShortName()
);
![Page 24: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/24.jpg)
// In ReflectionClass :
public static function createFromName($className)
{
return ClassReflector::buildDefaultReflector()->reflect($className);
}
![Page 25: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/25.jpg)
// In ClassReflector :
public static function buildDefaultReflector()
{
return new self(new AggregateSourceLocator([
new PhpInternalSourceLocator(),
new EvaledCodeSourceLocator(),
new AutoloadSourceLocator(),
]));
}
![Page 26: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/26.jpg)
Reflector
Source Locator
PhpParser
Reflection
![Page 27: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/27.jpg)
Source Locators
● PhpInternalSourceLocator● EvaledCodeSourceLocator● AggregateSourceLocator● ClosureSourceLocator● ComposerSourceLocator● SingleFileSourceLocator● StringSourceLocator
![Page 28: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/28.jpg)
StringSourceLocatoruse BetterReflection\Reflector\ClassReflector;
use BetterReflection\SourceLocator\Type\StringSourceLocator;
$source = <<<EOF
<?php
class MyClassInString {}
EOF;
$reflector = new ClassReflector(new StringSourceLocator($source));
$classInfo = $reflector->reflect(MyClassInString::class);
![Page 29: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/29.jpg)
However…
![Page 30: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/30.jpg)
AutoloadSourceLocator
ReflectionClass::createFromName(new MyClass)
replace stream wrapper
disable error handling
call “class_exists”
restore stream wrapper
restore error handling
store attempted filename load
DO NOT LOAD FILE!
return stored filename
Read file and parse AST!
![Page 31: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/31.jpg)
What’s next?
Now we have CODE!
![Page 32: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/32.jpg)
Magic superpowers
![Page 33: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/33.jpg)
source: http://goo.gl/HORwLQ
![Page 34: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/34.jpg)
So what is AST?
![Page 35: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/35.jpg)
Reflector
Source Locator
PhpParser
Reflection
![Page 36: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/36.jpg)
<?php
use PhpParser\ParserFactory;
$parser = (new ParserFactory)
->create(ParserFactory::PREFER_PHP7);
print_r($parser->parse(
file_get_contents('ast-demo-src.php')
));
![Page 37: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/37.jpg)
<?php
echo "Hello world";
![Page 38: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/38.jpg)
Echo statement
`-- String, value "Hello world"
![Page 39: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/39.jpg)
<?php
echo "Hello " . "world";
![Page 40: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/40.jpg)
Echo statement
`-- Concat
|-- Left
| `-- String, value "Hello "
`-- Right
`-- String, value "world"
![Page 41: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/41.jpg)
<?php
$a = 5;
$b = 3;
echo $a + ($b * 2);
![Page 42: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/42.jpg)
Assign statement
|-- Variable $a
`-- Integer, value 5
Assign statement
|-- Variable $b
`-- Integer, value 3
Echo statement
`-- Add operation
|-- Left
| `-- Variable $a
`-- Right
`-- Multiply operation
|-- Left
| `-- Variable $b
`-- Right
`-- Integer, value 2
![Page 43: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/43.jpg)
So what?
![Page 44: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/44.jpg)
Reflector
Source Locator
PhpParser
Reflection
![Page 45: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/45.jpg)
AST to Reflection
![Page 46: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/46.jpg)
Benefits?
![Page 47: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/47.jpg)
<?php
class Foo
{
private $bar;
public function thing()
{
}
}
![Page 48: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/48.jpg)
Class, name Foo
|-- Statements
| |-- Property, name bar
| | |-- Type [private]
| | `-- Attributes [start line: 7, end line: 9]
| `-- Method, name thing
| |-- Type [public]
| |-- Parameters [...]
| |-- Statements [...]
| `-- Attributes [start line: 7, end line: 9]
`-- Attributes [start line: 3, end line: 10]
![Page 49: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/49.jpg)
php-ast extension
![Page 50: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/50.jpg)
Here be dragons!
Some voodoo...
![Page 51: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/51.jpg)
class MyClass
{
public function foo()
{
return 5;
}
}
![Page 52: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/52.jpg)
// Create the reflection first
// ***BEFORE*** class is loaded
$classInfo = ReflectionClass::createFromName('MyClass');
// Or use specific source locators as already shown
![Page 53: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/53.jpg)
// Override the body...!
$methodInfo = $classInfo->getMethod('foo');
$methodInfo->setBodyFromClosure(function () {
return 4;
});
![Page 54: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/54.jpg)
// Bring the code into context now
$printer = new CodePrinter();
$classCode = $printer->prettyPrint([
$classInfo->getAst(),
]);
eval($classCode);
![Page 55: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/55.jpg)
// Now create an instance, and call the
// method on this...
$c = new MyClass();
var_dump($c->foo()); // will be 4!!!
![Page 56: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/56.jpg)
![Page 57: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/57.jpg)
astkit
![Page 58: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/58.jpg)
$if = AstKit::parseString(<<<EOD
if (true) {
echo "This is a triumph.\n";
} else {
echo "The cake is a lie.\n";
}
EOD
);
$if->execute(); // First run, program is as-seen above
$const = $if->getChild(0)->getChild(0);
// Replace the "true" constant in the condition with false
$const->graft(0, false);
// Can also graft other AstKit nodes, instead of constants
$if->execute(); // Second run now takes the else path
![Page 59: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/59.jpg)
Difficulties...
![Page 60: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/60.jpg)
ReflectionClass implements Reflector {
/* Constants */
const integer IS_IMPLICIT_ABSTRACT = 16 ;
const integer IS_EXPLICIT_ABSTRACT = 32 ;
const integer IS_FINAL = 64 ;
/* Properties */
public $name ;
/* Methods */
public __construct ( mixed $argument )
public static string export ( mixed $argument [, bool $return = false ] )
public mixed getConstant ( string $name )
public array getConstants ( void )
public ReflectionMethod getConstructor ( void )
public array getDefaultProperties ( void )
public string getDocComment ( void )
public int getEndLine ( void )
public ReflectionExtension getExtension ( void )
public string getExtensionName ( void )
public string getFileName ( void )
public array getInterfaceNames ( void )
public array getInterfaces ( void )
public ReflectionMethod getMethod ( string $name )
public array getMethods ([ int $filter ] )
public int getModifiers ( void )
public string getName ( void )
public string getNamespaceName ( void )
public object getParentClass ( void )
public array getProperties ([ int $filter ] )
public ReflectionProperty getProperty ( string $name )
public string getShortName ( void )
public int getStartLine ( void )
public array getStaticProperties ( void )
Reflection API is a big!public mixed getStaticPropertyValue ( string $name [, mixed &$def_value ] )
public array getTraitAliases ( void )
public array getTraitNames ( void )
public array getTraits ( void )
public bool hasConstant ( string $name )
public bool hasMethod ( string $name )
public bool hasProperty ( string $name )
public bool implementsInterface ( string $interface )
public bool inNamespace ( void )
public bool isAbstract ( void )
public bool isAnonymous ( void )
public bool isCloneable ( void )
public bool isFinal ( void )
public bool isInstance ( object $object )
public bool isInstantiable ( void )
public bool isInterface ( void )
public bool isInternal ( void )
public bool isIterateable ( void )
public bool isSubclassOf ( string $class )
public bool isTrait ( void )
public bool isUserDefined ( void )
public object newInstance ( mixed $args [, mixed $... ] )
public object newInstanceArgs ([ array $args ] )
public object newInstanceWithoutConstructor ( void )
public void setStaticPropertyValue ( string $name , string $value )
public string __toString ( void )
}
![Page 61: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/61.jpg)
<?php
namespace ??????????;
use ?????????????????????????????????????;
class Foo
{
public function something()
{
throw new InvalidArgumentException('Oh noes!');
}
}
Type determination
![Page 62: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/62.jpg)
<?php
namespace My\Package;
use Some\Package\InvalidArgumentException;
class Foo
{
public function something()
{
throw new InvalidArgumentException('Oh noes!');
}
}
Type determination
![Page 63: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/63.jpg)
Type determination
● FindParameterType● FindPropertyType● FindReturnType● FindTypeFromAst
![Page 64: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/64.jpg)
$finder = new FindTypeFromAst();
$namespace = '';
if ($method->getDeclaringClass()->inNamespace()) {
$namespace = $method->getDeclaringClass()->getNamespaceName();
}
$type = $finder(
$className,
$method->getLocatedSource(),
$namespace
);
Type determination
![Page 65: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/65.jpg)
DocBlock Parent Traversal Type
Resolution
![Page 66: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/66.jpg)
DocBlock Parent Traversal Type Resolution
class Foo {
/**
* @return int
*/
public function myMethod() { /* ... */ }
}
class Bar extends Foo {
/**
* {@inheritDoc}
*/
public function myMethod() { /* ... */ }
}
It’s an “int” return type!
![Page 67: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/67.jpg)
DocBlock Parent Traversal Type Resolutioninterface Blammo {
/**
* @return string
*/
public function myMethod();
}
class Foo {
/**
* @return int
*/
public function myMethod() { /* ... */ }
}
class Bar extends Foo implements Blammo {
/**
* {@inheritDoc}
*/
public function myMethod() { /* ... */ }
}
Return type: ¯\_(ツ)_/¯
![Page 68: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/68.jpg)
Evaluating Modified Reflections
![Page 69: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/69.jpg)
Evaluating Modified Reflections
$methodInfo = $classInfo->getMethod('foo');
$methodInfo->setBodyFromClosure(function () {
// Nasty, evil, malicious code here !!!
});
$classCode = (new CodePrinter())->prettyPrint([
$classInfo->getAst(),
]);
eval($classCode);
![Page 70: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/70.jpg)
Reflecting Internal Functions
![Page 71: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/71.jpg)
![Page 72: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/72.jpg)
Reflecting Closures
![Page 73: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/73.jpg)
export
__toString
createFromName
createFromInstance
createFromNode
getShortName
getName
getNamespaceName
inNamespace
getMethods
getImmediateMethods
getMethod
hasMethod
getConstants
getConstant
hasConstant
getConstructor
getProperties
getProperty
hasProperty
getDefaultProperties
getFileName
getLocatedSource
Better Reflection API is BIGGERERgetStartLine
getEndLine
getParentClass
getDocComment
isInternal
isUserDefined
isAbstract
isFinal
getModifiers
isTrait
isInterface
getTraits
getTraitNames
getTraitAliases
getInterfaces
getImmediateInterfaces
getInterfaceNames
isInstance
isSubclassOf
implementsInterface
isInstantiable
isCloneable
isIterateable
__clone
getStaticPropertyValue
setStaticPropertyValue
getAst
setFinal
removeMethod
addMethod
addProperty
removeProperty
![Page 74: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/74.jpg)
(at least for now)
Out of scope
![Page 75: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/75.jpg)
It’s not fast :(
![Page 76: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/76.jpg)
![Page 77: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/77.jpg)
Reflecting from STDIN
![Page 78: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/78.jpg)
![Page 79: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/79.jpg)
HHVM
![Page 80: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/80.jpg)
Reflection(Zend)Extension
![Page 81: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/81.jpg)
Instantiation & Invocation
![Page 82: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/82.jpg)
Use Cases
![Page 83: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/83.jpg)
API diff tool
![Page 84: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/84.jpg)
What’s next?
![Page 85: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/85.jpg)
Your ideas welcome!¯\_(ツ)_/¯
![Page 86: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/86.jpg)
● https://github.com/nikic/PHP-Parser● https://github.com/nikic/php-ast● https://github.com/sgolemon/astkit● https://gist.github.
com/sgolemon/f65fafadd90ed26f05be
Resources
![Page 87: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/87.jpg)
github.com/ /BetterReflection
Better Reflection
![Page 88: Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)](https://reader034.fdocuments.in/reader034/viewer/2022042604/587fd30c1a28ab58248b50d7/html5/thumbnails/88.jpg)
Any questions? :)
https://joind.in/talk/eb95dJames Titcumb @asgrim