Skip to content

Commit

Permalink
Finalize debuginfo method
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin v / vvval committed Jun 3, 2019
1 parent 57dad75 commit 6a59efe
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
5 changes: 5 additions & 0 deletions src/Expressions.php
Expand Up @@ -65,6 +65,11 @@ public static function resolvePropertyFetch(string $object, string $property): N
return new Node\Expr\PropertyFetch(new Node\Expr\Variable($object), $property);
}

public static function equalsFalse(Node\Expr $expr): Node\Expr\BinaryOp\Identical
{
return new Node\Expr\BinaryOp\Identical($expr, self::const('false'));
}

public static function notNull(Node\Expr $expr): Node\Expr\BinaryOp\NotIdentical
{
return new Node\Expr\BinaryOp\NotIdentical($expr, self::const('null'));
Expand Down
23 changes: 17 additions & 6 deletions src/Printer.php
Expand Up @@ -15,9 +15,13 @@ class Printer
{
private const RESOLVER_PROPERTY = '__resolver';
private const UNSET_PROPERTIES_CONST = 'UNSET_PROPERTIES';
private const RESOLVE_METHOD = '__resolve';
private const INIT_METHOD = '__init';

private const LOADED_METHOD = '__loaded';
private const ROLE_METHOD = '__role';
private const SCOPE_METHOD = '__scope';
private const RESOLVE_METHOD = '__resolve';

private const DEPENDENCIES = [
'orm' => ORMInterface::class,
'role' => 'string',
Expand All @@ -32,10 +36,10 @@ class Printer
];

private const PROMISE_METHODS = [
'__loaded' => 'bool',
'__role' => 'string',
'__scope' => 'array',
'__resolve' => null,
self::LOADED_METHOD => 'bool',
self::ROLE_METHOD => 'string',
self::SCOPE_METHOD => 'array',
self::RESOLVE_METHOD => null,
];

/** @var ConflictResolver */
Expand Down Expand Up @@ -120,7 +124,14 @@ public function make(\ReflectionClass $reflection, Declaration\DeclarationInterf
new Visitor\AddMagicSetMethod($property, self::RESOLVE_METHOD),
new Visitor\AddMagicIssetMethod($property, self::RESOLVE_METHOD, $unsetPropertiesConst),
new Visitor\AddMagicUnset($property, self::RESOLVE_METHOD, $unsetPropertiesConst),
new Visitor\AddMagicDebugInfoMethod($property, self::RESOLVE_METHOD, $structure->properties),
new Visitor\AddMagicDebugInfoMethod(
$property,
self::RESOLVE_METHOD,
self::LOADED_METHOD,
self::ROLE_METHOD,
self::SCOPE_METHOD,
$structure->properties
),
new Visitor\UpdatePromiseMethods($property),
new Visitor\AddProxiedMethods($property, $structure->methods, self::RESOLVE_METHOD),
];
Expand Down
46 changes: 37 additions & 9 deletions src/Visitor/AddMagicDebugInfoMethod.php
Expand Up @@ -16,13 +16,31 @@ class AddMagicDebugInfoMethod extends NodeVisitorAbstract
/** @var string */
private $resolveMethod;

/** @var string */
private $loadedMethod;

/** @var string */
private $roleMethod;

/** @var string */
private $scopeMethod;

/** @var array */
private $unsetPropertiesValues;

public function __construct(string $resolverProperty, string $resolveMethod, array $unsetPropertiesValues)
{
public function __construct(
string $resolverProperty,
string $resolveMethod,
string $loadedMethod,
string $roleMethod,
string $scopeMethod,
array $unsetPropertiesValues
) {
$this->resolverProperty = $resolverProperty;
$this->resolveMethod = $resolveMethod;
$this->loadedMethod = $loadedMethod;
$this->roleMethod = $roleMethod;
$this->scopeMethod = $scopeMethod;
$this->unsetPropertiesValues = $unsetPropertiesValues;
}

Expand All @@ -31,7 +49,6 @@ public function leaveNode(Node $node)
if ($node instanceof Node\Stmt\Class_) {
$method = new Builder\Method('__debuginfo');
$method->makePublic();
$method->addStmt(Expressions::resolveIntoVar('entity', 'this', $this->resolverProperty, $this->resolveMethod));
$method->addStmt($this->buildExpression());

$node->stmts[] = $method->getNode();
Expand All @@ -42,10 +59,18 @@ public function leaveNode(Node $node)

private function buildExpression(): Node\Stmt\If_
{
$if = new Node\Stmt\If_(Expressions::notNull(new Node\Expr\Variable('entity')));
$if->stmts[] = new Node\Stmt\Return_($this->resolvedProperties());
$if->else = new Node\Stmt\Else_();
$if->else->stmts[] = new Node\Stmt\Return_($this->unresolvedProperties());
$loaded = Expressions::resolveMethodCall('this', $this->resolverProperty, $this->loadedMethod);
$if = new Node\Stmt\If_(Expressions::equalsFalse($loaded));
$if->stmts[] = new Node\Stmt\Return_($this->unresolvedProperties('false'));
$if->else = new Node\Stmt\Else_([
Expressions::resolveIntoVar('entity', 'this', $this->resolverProperty, $this->resolveMethod),
new Node\Stmt\If_(Expressions::notNull(new Node\Expr\Variable('entity')), [
'stmts' => [new Node\Stmt\Return_($this->resolvedProperties())],
'else' => new Node\Stmt\Else_([
new Node\Stmt\Return_($this->unresolvedProperties('true'))
])
])
]);

return $if;
}
Expand All @@ -60,10 +85,13 @@ private function resolvedProperties(): Node\Expr\Array_
return $this->array($array);
}

private function unresolvedProperties(): Node\Expr\Array_
private function unresolvedProperties(string $loaded): Node\Expr\Array_
{
$array = [];
$array[] = $this->arrayItem(Expressions::const('true'), '~unresolved');
$array[] = $this->arrayItem(Expressions::const($loaded), ':loaded');
$array[] = $this->arrayItem(Expressions::const('false'), ':resolved');
$array[] = $this->arrayItem(Expressions::resolveMethodCall('this', $this->resolverProperty, $this->roleMethod), ':role');
$array[] = $this->arrayItem(Expressions::resolveMethodCall('this', $this->resolverProperty, $this->scopeMethod), ':scope');
foreach ($this->unsetPropertiesValues as $value) {
$array[] = $this->arrayItem(Expressions::const('null'), $value);
}
Expand Down
5 changes: 3 additions & 2 deletions src/Visitor/AddMagicIssetMethod.php
Expand Up @@ -48,8 +48,9 @@ private function buildIssetExpression(): Node\Stmt\If_
new Node\Expr\Variable('entity'),
new Node\Stmt\Return_(Expressions::issetFunc('entity', '{$name}'))
);
$if->else = new Node\Stmt\Else_();
$if->else->stmts[] = new Node\Stmt\Return_(Expressions::issetFunc('this', '{$name}'));
$if->else = new Node\Stmt\Else_([
new Node\Stmt\Return_(Expressions::issetFunc('this', '{$name}'))
]);

return $if;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Visitor/AddMagicUnset.php
Expand Up @@ -48,8 +48,9 @@ private function buildUnsetExpression(): Node\Stmt\If_
new Node\Expr\Variable('entity'),
new Node\Stmt\Expression(Expressions::unsetFunc('entity', '{$name}'))
);
$if->else = new Node\Stmt\Else_();
$if->else->stmts[] = new Node\Stmt\Expression(Expressions::unsetFunc('this', '{$name}'));
$if->else = new Node\Stmt\Else_([
new Node\Stmt\Expression(Expressions::unsetFunc('this', '{$name}'))
]);

return $if;
}
Expand Down

0 comments on commit 6a59efe

Please sign in to comment.