AST + Better Reflection (PHP Benelux 2016 Unconference)

50 %
50 %
Information about AST + Better Reflection (PHP Benelux 2016 Unconference)

Published on January 29, 2016

Author: asgrim1

Source: slideshare.net

1. AST + Better Reflection James Titcumb PHP Benelux 2016 Unconference

2. James Titcumb www.jamestitcumb.com www.roave.com www.phphants.co.uk www.phpsouthcoast.co.uk @asgrim Who is this guy?

3. Reflection

4. © 1937 Disney’s Snow White - disneyscreencaps.com

5. ● Structure ● Metadata ● Values ● Type introspection ● Modification Reflection

6. How does it work?

7. 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; }

8. Okay. What now?

9. github.com/ /BetterReflection Better Reflection!

10. What?

11. Why?

12. How?

13. Magic of the AST

14. source: http://goo.gl/HORwLQ

15. WTF is AST?

16. use PhpParserParserFactory; $parser = (new ParserFactory) ->create(ParserFactory::PREFER_PHP7); print_r($parser->parse( file_get_contents('ast-demo-src.php') ));

17. <?php echo "Hello world";

18. AST `-- Echo statement `-- String, value "Hello world"

19. <?php echo "Hello " . "world";

20. AST `-- Echo statement `-- Concat |-- Left | `-- String, value "Hello " `-- Right `-- String, value "world"

21. AST to Reflection

22. Benefits?

23. ReflectionProperty->getDocBlockTypes() Types!

24. Using Better Reflection

25. $reflection = new ReflectionClass( 'BetterReflectionTestFixtureExampleClass' ); $this->assertSame( 'ExampleClass', $reflection->getShortName() );

26. $reflection = ReflectionClass::createFromName( 'BetterReflectionTestFixtureExampleClass' ); $this->assertSame( 'ExampleClass', $reflection->getShortName() );

27. // In ReflectionClass : public static function createFromName($className) { return ClassReflector::buildDefaultReflector()->reflect($className); } // In ClassReflector : public static function buildDefaultReflector() { return new self(new AggregateSourceLocator([ new PhpInternalSourceLocator(), new EvaledCodeSourceLocator(), new AutoloadSourceLocator(), ])); }

28. 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!

29. (don’t try this at home kids!) Some voodoo...

30. class MyClass { public function foo() { return 5; } }

31. // Create the reflection first // ***BEFORE*** class is loaded $classInfo = ReflectionClass::createFromName('MyClass');

32. // Override the body...! $methodInfo = $classInfo->getMethod('foo'); $methodInfo->setBody(function () { return 4; });

33. // Save the class and require it $classCode = (new CodePrinter())->prettyPrint([$classInfo->getAst()]); $tmpFile = tempnam(sys_get_temp_dir(), 'br-monkey-patching'); file_put_contents($tmpFile, '<?php ' . $classCode); require_once($tmpFile); unlink($tmpFile);

34. // Now create an instance, and call the function... $c = new MyClass(); var_dump($c->foo()); // will be 4!!!

35. What’s in it for me?

36. The future...

37. ● DONE Currently: rewriting the internals ○ SourceLocators “might” return some code ?! ● DONE Returning AST/Code for method/fn Ideas/plans - DONE

38. ● WIP More compatibility core reflection ● WIP Reflect core fns with default params ○ This is not currently possible with core reflection ● WIP Modification & exporting ● WIP Reflecting on closures Ideas/plans - WIP

39. ● Adding a getColumn() function ● PHP 7 compatibility ● @inheritDoc traversal (to inherit type hints) ● Make it faster Ideas/plans

40. ¯_(ツ)_/¯

41. github.com/ /BetterReflection Better Reflection

42. Any questions? :) https://joind.in/talk/9a5d8 James Titcumb @asgrim

Add a comment

Related pages

Php | LinkedIn

PHP is much better than you think. ... 2016. Is Flipkart shifting from PHP to Node.JS? ... AST + Better Reflection (PHP Benelux 2016 Unconference)
Read more

Php*** | LinkedIn

PHP Freelance Developer/ Project Management at NSCF: National Scholastic Chess Foundation, Looking for PHP Development at Opensource PHP... Education
Read more

PHPBenelux Conference 2015 - PHP for and from the community

... his community efforts outside of the PHP Benelux ... unconference sponsor Ibuildings was very ... the PHPBenelux Conference 2015, ...
Read more

PHP: PHP Conferences around the world

Therefore we’d like to announce php[cruise] happening in 2016! ... We're in our fifth year and we're making things better all the time.
Read more

6" f/5 Newton - 2" Crayford focuser... - AST Optics ...

94% reflection on both mirrors for a brighter image; ... Steel Tube for a better stability ... 6" f/5 Newton - 2" Crayford focuser.
Read more

PHP: PHP 7 ChangeLog

PHP 7 ChangeLog Version 7.0.3 04 Feb 2016. ... for better performance, ... Added reflection support for return types and type declarations.
Read more

8" F5 Newton DS Crayford focuser - AST Optics - Astromarket

8" F5 Newton DS Crayford focuser heeft nog geen reviews. Deel uw ervaringen hierover door op Schrijf waardering te klikken.
Read more

MediaFire - Simple File Sharing and Storage.

MediaFire is a simple to use free service that lets you put all your photos, ... Life is better when shared with friends. ... ©2016 MediaFire Build 115091.
Read more

Common Compiler Infrastructure - Metadata - Home

Common Compiler Infrastructure: Metadata ... and System.Reflection.Emit APIs, but with much better ... AST components. Common Compiler Infrastructure ...
Read more

Vueling - Wikipedia, the free encyclopedia

... (from 4 May 2016) Paris ... Vueling Airlines, S.A. ... The merger was designed to create a carrier better able to compete in the competitive Spanish ...
Read more