/** * {@inheritdoc} */ public function resolveVariableType(Node $node) { if ($node instanceof Node\Expr\Assign) { // $x = ... if ($node->var instanceof Node\Expr\Variable) { // skips variable names like ${$node->nodeName} if (!is_string($node->var->name)) { return; } // $x = new X(); if ($node->expr instanceof Node\Expr\New_) { if ($node->expr->class instanceof Node\Name) { $type = $node->expr->class->toString(); $this->table->setSymbol($node->var->name, $type); $node->var->setAttribute('guessedType', $type); } } // $x = $y; if ($node->expr instanceof Node\Expr\Variable) { $type = $this->table->lookUp($node->expr->name)->type(); $node->var->setAttribute('guessedType', $type); $this->table->setSymbol($node->var->name, $type); } // $x = $this->x if ($node->expr instanceof Node\Expr\PropertyFetch) { $type = $this->table->lookUpClassProperty($node->expr->name)->type(); $node->var->setAttribute('guessedType', $type); $this->table->setSymbol($node->var->name, $type); } } } }
/** * {@inheritdoc} */ public function resolveVariableType(Node $node) { if ($node instanceof Node\Expr\PropertyFetch) { // $this->someProperty if ($node->var instanceof Node\Expr\Variable && $node->var->name === 'this') { $node->setAttribute('guessedType', $this->table->lookUpClassProperty($node->name)->type()); } // $x->someProperty } }
public function testLookUpClassPropertyReturnsSymbol() { $table = new SymbolTable(); $symbol = $this->prophesize('SensioLabs\\DeprecationDetector\\TypeGuessing\\SymbolTable\\Symbol'); $classScope = $this->prophesize('SensioLabs\\DeprecationDetector\\TypeGuessing\\SymbolTable\\TableScope'); $classScope->scope()->willReturn('CLASS_LIKE_SCOPE'); $classScope->findSymbol('property')->willReturn($symbol->reveal()); $table->enterScope($classScope->reveal()); $this->assertEquals($symbol->reveal(), $table->lookUpClassProperty('property')); }
/** * {@inheritdoc} */ public function resolveVariableType(Node $node) { if ($node instanceof Node\Expr\Assign) { // $this->x = ... // excluding $this->$x = ... if ($node->var instanceof Node\Expr\PropertyFetch) { // $stub[$key]->x = ... ; if not tested a php notice will occur // @TODO change to be able to use all types of properties like $x->x = 10 if ($node->var->var instanceof Node\Expr\ArrayDimFetch) { return; } // @TODO change to be able to use all types of properties like $x->x = 10 if ($node->var->var->name !== 'this' || !is_string($node->var->name)) { return; } // $this->x = new X(); if ($node->expr instanceof Node\Expr\New_) { if ($node->expr->class instanceof Node\Name) { $type = $node->expr->class->toString(); $node->var->setAttribute('guessedType', $type); $this->table->setClassProperty($node->var->name, $type); } } // $this->x = $y; if ($node->expr instanceof Node\Expr\Variable) { $type = $this->table->lookUp($node->expr->name)->type(); $node->var->setAttribute('guessedType', $type); $this->table->setClassProperty($node->var->name, $type); } // $this->x = $this->y; if ($node->expr instanceof Node\Expr\PropertyFetch) { $type = $this->table->lookUpClassProperty($node->expr->name)->type(); $node->var->setAttribute('guessedType', $type); $this->table->setClassProperty($node->var->name, $type); } } } }