Skip to content

Commit

Permalink
Merge pull request #28 from siwinski/pr-nikic-php-parser-1
Browse files Browse the repository at this point in the history
Update nikic/php-parser to 1.0.*
  • Loading branch information
Ocramius committed Sep 15, 2014
2 parents 92a67dd + f76b4a6 commit 2ff2c2a
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 68 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"require": {
"php": "~5.4",
"zendframework/zend-stdlib": "2.*",
"nikic/php-parser": "0.9.*",
"ocramius/code-generator-utils": "0.2.*"
"nikic/php-parser": "1.0.*",
"ocramius/code-generator-utils": "0.3.*"
},
"require-dev": {
"phpunit/phpunit": "~4.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

namespace GeneratedHydrator\ClassGenerator\Hydrator\PropertyGenerator;

use PHPParser_Node_Stmt_Class;
use PHPParser_Node_Stmt_Property;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use ReflectionProperty;
use CodeGenerationUtils\Inflector\Util\UniqueIdentifierGenerator;

Expand All @@ -30,7 +31,7 @@
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
class PropertyAccessor extends PHPParser_Node_Stmt_Property
class PropertyAccessor extends Property
{
/**
* @var \ReflectionProperty
Expand All @@ -48,8 +49,8 @@ public function __construct(ReflectionProperty $accessedProperty, $nameSuffix)
$name = UniqueIdentifierGenerator::getIdentifier($originalName . $nameSuffix);

parent::__construct(
PHPParser_Node_Stmt_Class::MODIFIER_PRIVATE,
array(new \PHPParser_Node_Stmt_PropertyProperty($name))
Class_::MODIFIER_PRIVATE,
array(new PropertyProperty($name))
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/GeneratedHydrator/ClassGenerator/HydratorGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use CodeGenerationUtils\Visitor\ClassImplementorVisitor;
use CodeGenerationUtils\Visitor\MethodDisablerVisitor;
use GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor;
use PHPParser_NodeTraverser;
use PhpParser\NodeTraverser;
use ReflectionClass;

/**
Expand All @@ -38,12 +38,12 @@
class HydratorGenerator
{
/**
* Generates an AST of {@see \PHPParser_Node[]} out of a given reflection class
* Generates an AST of {@see \PhpParser\Node[]} out of a given reflection class
* and a map of properties to be used to
*
* @param \ReflectionClass $originalClass
*
* @return \PHPParser_Node[]
* @return \PhpParser\Node[]
*/
public function generate(ReflectionClass $originalClass)
{
Expand All @@ -52,7 +52,7 @@ public function generate(ReflectionClass $originalClass)
$ast = $builder->fromReflection($originalClass);

// step 1: remove methods that are not used
$cleaner = new PHPParser_NodeTraverser();
$cleaner = new NodeTraverser();

$cleaner->addVisitor(
new MethodDisablerVisitor(
Expand All @@ -65,7 +65,7 @@ function () {
$ast = $cleaner->traverse($ast);

// step 2: implement new methods and interfaces, extend original class
$implementor = new PHPParser_NodeTraverser();
$implementor = new NodeTraverser();

$implementor->addVisitor(new HydratorMethodsVisitor($originalClass));
$implementor->addVisitor(new ClassExtensionVisitor($originalClass->getName(), $originalClass->getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace GeneratedHydrator\CodeGenerator\Visitor;

use GeneratedHydrator\ClassGenerator\Hydrator\PropertyGenerator\PropertyAccessor;
use PHPParser_Lexer;
use PHPParser_Node;
use PHPParser_Node_Param;
use PHPParser_Node_Stmt_Class;
use PHPParser_Node_Stmt_ClassMethod;
use PHPParser_NodeVisitorAbstract;
use PHPParser_Parser;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Parser;
use ReflectionClass;
use ReflectionProperty;

Expand All @@ -19,7 +19,7 @@
* @author Marco Pivetta <ocramius@gmail.com>
* @license MIT
*/
class HydratorMethodsVisitor extends PHPParser_NodeVisitorAbstract
class HydratorMethodsVisitor extends NodeVisitorAbstract
{
/**
* @var ReflectionClass
Expand Down Expand Up @@ -50,13 +50,13 @@ public function __construct(ReflectionClass $reflectedClass)
}

/**
* @param PHPParser_Node $node
* @param PhpParser\Node $node
*
* @return null|PHPParser_Node_Stmt_Class|void
* @return null|PhpParser\Node\Stmt\Class_|void
*/
public function leaveNode(PHPParser_Node $node)
public function leaveNode(Node $node)
{
if (! $node instanceof PHPParser_Node_Stmt_Class) {
if (! $node instanceof Class_) {
return null;
}

Expand All @@ -68,9 +68,9 @@ public function leaveNode(PHPParser_Node $node)
}

/**
* @param PHPParser_Node_Stmt_ClassMethod $method
* @param PhpParser\Node\Stmt\ClassMethod $method
*/
private function replaceConstructor(PHPParser_Node_Stmt_ClassMethod $method)
private function replaceConstructor(ClassMethod $method)
{
$method->params = array();

Expand All @@ -87,19 +87,19 @@ private function replaceConstructor(PHPParser_Node_Stmt_ClassMethod $method)
. "}, null, " . var_export($className, true) . ");";
}

$parser = new PHPParser_Parser(new PHPParser_Lexer());
$parser = new Parser(new Lexer());

$method->stmts = $parser->parse('<?php ' . implode("\n", $bodyParts));
}

/**
* @param PHPParser_Node_Stmt_ClassMethod $method
* @param PhpParser\Node\Stmt\ClassMethod $method
*/
private function replaceHydrate(PHPParser_Node_Stmt_ClassMethod $method)
private function replaceHydrate(ClassMethod $method)
{
$method->params = array(
new PHPParser_Node_Param('data', null, 'array'),
new PHPParser_Node_Param('object'),
new Param('data', null, 'array'),
new Param('object'),
);

$body = '';
Expand All @@ -122,19 +122,19 @@ private function replaceHydrate(PHPParser_Node_Stmt_ClassMethod $method)

$body .= "\nreturn \$object;";

$parser = new PHPParser_Parser(new PHPParser_Lexer());
$parser = new Parser(new Lexer());

$method->stmts = $parser->parse('<?php ' . $body);
}

/**
* @param PHPParser_Node_Stmt_ClassMethod $method
* @param PhpParser\Node\Stmt\ClassMethod $method
*/
private function replaceExtract(PHPParser_Node_Stmt_ClassMethod $method)
private function replaceExtract(ClassMethod $method)
{
$parser = new PHPParser_Parser(new PHPParser_Lexer());
$parser = new Parser(new Lexer());

$method->params = array(new PHPParser_Node_Param('object'));
$method->params = array(new Param('object'));

if (empty($this->accessibleProperties) && empty($this->propertyWriters)) {
// no properties to hydrate
Expand Down Expand Up @@ -185,24 +185,24 @@ private function replaceExtract(PHPParser_Node_Stmt_ClassMethod $method)
/**
* Finds or creates a class method (and eventually attaches it to the class itself)
*
* @param PHPParser_Node_Stmt_Class $class
* @param PhpParser\Node\Stmt\Class_ $class
* @param string $name name of the method
*
* @return PHPParser_Node_Stmt_ClassMethod
* @return PhpParser\Node\Stmt\ClassMethod
*/
private function findOrCreateMethod(PHPParser_Node_Stmt_Class $class, $name)
private function findOrCreateMethod(Class_ $class, $name)
{
$foundMethods = array_filter(
$class->getMethods(),
function (PHPParser_Node_Stmt_ClassMethod $method) use ($name) {
function (ClassMethod $method) use ($name) {
return $name === $method->name;
}
);

$method = reset($foundMethods);

if (!$method) {
$class->stmts[] = $method = new PHPParser_Node_Stmt_ClassMethod($name);
$class->stmts[] = $method = new ClassMethod($name);
}

return $method;
Expand Down
4 changes: 2 additions & 2 deletions src/GeneratedHydrator/Factory/HydratorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use CodeGenerationUtils\Visitor\ClassRenamerVisitor;
use GeneratedHydrator\Configuration;
use GeneratedHydrator\ClassGenerator\HydratorGenerator;
use PHPParser_NodeTraverser;
use PhpParser\NodeTraverser;
use ReflectionClass;

/**
Expand Down Expand Up @@ -60,7 +60,7 @@ public function getHydratorClass()
$generator = new HydratorGenerator();
$originalClass = new ReflectionClass($realClassName);
$generatedAst = $generator->generate($originalClass);
$traverser = new PHPParser_NodeTraverser();
$traverser = new NodeTraverser();

$traverser->addVisitor(new ClassRenamerVisitor($originalClass, $hydratorClassName));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
use GeneratedHydrator\ClassGenerator\HydratorGenerator;
use CodeGenerationUtils\Inflector\Util\UniqueIdentifierGenerator;
use CodeGenerationUtils\GeneratorStrategy\EvaluatingGeneratorStrategy;
use PHPParser_NodeTraverser;
use PhpParser\NodeTraverser;
use PHPUnit_Framework_TestCase;
use ReflectionClass;

Expand All @@ -47,7 +47,7 @@ public function testGeneratesValidCode($className)
$generatedClassName = UniqueIdentifierGenerator::getIdentifier('HydratorGeneratorTest');
$originalClass = new ReflectionClass($className);
$generatorStrategy = new EvaluatingGeneratorStrategy();
$traverser = new PHPParser_NodeTraverser();
$traverser = new NodeTraverser();

$traverser->addVisitor(new ClassRenamerVisitor($originalClass, $generatedClassName));
$generatorStrategy->generate($traverser->traverse($generator->generate($originalClass)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
namespace GeneratedHydratorTest\CodeGenerator\Visitor;

use GeneratedHydrator\CodeGenerator\Visitor\HydratorMethodsVisitor;
use PHPParser_Lexer;
use PHPParser_Node;
use PHPParser_Node_Stmt_Class;
use PHPParser_Parser;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Parser;
use PHPUnit_Framework_TestCase;
use CodeGenerationUtils\Inflector\Util\UniqueIdentifierGenerator;
use ReflectionClass;
Expand All @@ -41,14 +44,14 @@ class HydratorMethodsVisitorTest extends PHPUnit_Framework_TestCase
* @dataProvider classAstProvider
*
* @param string $className
* @param PHPParser_Node_Stmt_Class $classNode
* @param PhpParser\Node\Stmt\Class_ $classNode
* @param string[] $properties
*/
public function testBasicCodeGeneration($className, PHPParser_Node_Stmt_Class $classNode, array $properties)
public function testBasicCodeGeneration($className, Class_ $classNode, array $properties)
{
$visitor = new HydratorMethodsVisitor(new ReflectionClass($className));

/* @var $modifiedAst PHPParser_Node_Stmt_Class */
/* @var $modifiedAst PhpParser\Node\Stmt\Class_ */
$modifiedNode = $visitor->leaveNode($classNode);

$this->assertMethodExistence('hydrate', $modifiedNode);
Expand All @@ -61,18 +64,18 @@ public function testBasicCodeGeneration($className, PHPParser_Node_Stmt_Class $c
* Verifies that a method was correctly added to by the visitor
*
* @param string $methodName
* @param PHPParser_Node_Stmt_Class $class
* @param PhpParser\Node\Stmt\Class_ $class
*/
private function assertMethodExistence($methodName, PHPParser_Node_Stmt_Class $class)
private function assertMethodExistence($methodName, Class_ $class)
{
$members = $class->stmts;

$this->assertCount(
1,
array_filter(
$members,
function (PHPParser_Node $node) use ($methodName) {
return $node instanceof \PHPParser_Node_Stmt_ClassMethod
function (Node $node) use ($methodName) {
return $node instanceof ClassMethod
&& $methodName === $node->name;
}
)
Expand All @@ -82,20 +85,20 @@ function (PHPParser_Node $node) use ($methodName) {
/**
* Verifies that the given properties and only the given properties are added to the hydrator logic
*
* @param PHPParser_Node_Stmt_Class $class
* @param PhpParser\Node\Stmt\Class_ $class
* @param array $properties
*/
private function assertContainsPropertyAccessors(PHPParser_Node_Stmt_Class $class, array $properties)
private function assertContainsPropertyAccessors(Class_ $class, array $properties)
{
$lookupProperties = array_flip($properties);

foreach ($class->stmts as $method) {
if ($method instanceof \PHPParser_Node_Stmt_ClassMethod && $method->name === 'hydrate') {
if ($method instanceof ClassMethod && $method->name === 'hydrate') {
foreach ($method->stmts as $assignment) {
if ($assignment instanceof \PHPParser_Node_Expr_Assign) {
if ($assignment instanceof Assign) {
$var = $assignment->var;

if ($var instanceof \PHPParser_Node_Expr_PropertyFetch && is_string($var->name)) {
if ($var instanceof PropertyFetch && is_string($var->name)) {
if (! isset($lookupProperties[$var->name])) {
$this->fail(sprintf('Property "%s" should not be hydrated', $var->name));
}
Expand All @@ -106,12 +109,12 @@ private function assertContainsPropertyAccessors(PHPParser_Node_Stmt_Class $clas
}
}

if ($method instanceof \PHPParser_Node_Stmt_ClassMethod && $method->name === '__construct') {
if ($method instanceof ClassMethod && $method->name === '__construct') {
foreach ($method->stmts as $assignment) {
if ($assignment instanceof \PHPParser_Node_Expr_Assign) {
if ($assignment instanceof Assign) {
$var = $assignment->var;

if ($var instanceof \PHPParser_Node_Expr_PropertyFetch
if ($var instanceof PropertyFetch
&& preg_match('/(.*)Writer[a-zA-Z0-9]+/', $assignment->var->name, $matches)
) {
if (! isset($lookupProperties[$matches[1]])) {
Expand All @@ -136,11 +139,11 @@ private function assertContainsPropertyAccessors(PHPParser_Node_Stmt_Class $clas
}

/**
* @return \PHPParser_Node[][]
* @return \PhpParser\Node[][]
*/
public function classAstProvider()
{
$parser = new PHPParser_Parser(new PHPParser_Lexer());
$parser = new Parser(new Lexer());

$className = UniqueIdentifierGenerator::getIdentifier('Foo');
$classCode = 'class ' . $className . ' { private $bar; private $baz; protected $tab; '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
use CodeGenerationUtils\ReflectionBuilder\ClassBuilder;
use CodeGenerationUtils\Visitor\ClassRenamerVisitor;
use GeneratedHydrator\Configuration;
use PHPParser_NodeTraverser;
use PhpParser\NodeTraverser;
use PHPUnit_Framework_TestCase;
use ReflectionClass;

Expand Down Expand Up @@ -57,7 +57,7 @@ public function setUp()
$generatorStrategy = new EvaluatingGeneratorStrategy();
$reflection = new ReflectionClass('GeneratedHydratorTestAsset\\ClassWithMixedProperties');
$generator = new ClassBuilder();
$traverser = new PHPParser_NodeTraverser();
$traverser = new NodeTraverser();
$renamer = new ClassRenamerVisitor($reflection, $this->generatedClassName);

$traverser->addVisitor($renamer);
Expand Down

0 comments on commit 2ff2c2a

Please sign in to comment.