/** * @param Variable $variable * @return bool */ public function addVariable(Variable $variable) { $this->symbols[$variable->getName()] = $variable; return true; }
public function testGetTypeName() { $int = new Variable('a', 1, CompiledExpression::INTEGER); parent::assertSame("integer", $int->getTypeName()); $double = new Variable('b', 1, CompiledExpression::DOUBLE); parent::assertSame("double", $double->getTypeName()); $number = new Variable('c', 1, CompiledExpression::NUMBER); parent::assertSame("number", $number->getTypeName()); $arr = new Variable('d', [1, 2], CompiledExpression::ARR); parent::assertSame("array", $arr->getTypeName()); $object = new Variable('e', 1, CompiledExpression::OBJECT); parent::assertSame("object", $object->getTypeName()); $resource = new Variable('f', 1, CompiledExpression::RESOURCE); parent::assertSame("resource", $resource->getTypeName()); $callable = new Variable('g', 1, CompiledExpression::CALLABLE_TYPE); parent::assertSame("callable", $callable->getTypeName()); $boolean = new Variable('h', 1, CompiledExpression::BOOLEAN); parent::assertSame("boolean", $boolean->getTypeName()); $null = new Variable('i', 1, CompiledExpression::NULL); parent::assertSame("null", $null->getTypeName()); $unknown = new Variable('j', 1, CompiledExpression::UNKNOWN); parent::assertSame("unknown", $unknown->getTypeName()); }
public function testModifyReferencedVariables() { $context = $this->getContext(); /** * This variable is not needed for change */ $variableAValue = 1; $variableAType = CompiledExpression::INTEGER; $context->addVariable($variableA = new Variable('a', $variableAValue, $variableAType)); /** * $b = true; */ $context->addVariable($variableB = new Variable('b', true, CompiledExpression::BOOLEAN)); /** * $c = &$b; */ $variableC = new Variable('c'); $variableC->setReferencedTo($variableB); $context->addVariable($variableC); $newType = CompiledExpression::INTEGER; $newValue = 55; /** * $b = {$newValue}; * After it variable $c will change type and value */ $context->modifyReferencedVariables($variableB, $newType, $newValue); self::assertSame($newValue, $variableC->getValue()); self::assertSame($newType, $variableC->getType()); /** * Assert that variable $a was not changed */ self::assertSame($variableAValue, $variableA->getValue()); self::assertSame($variableAType, $variableA->getType()); }
/** * @param int $type * @param mixed $value */ public function modify($type, $value) { $this->type = (int) $type; $this->value = $value; if ($this->referencedTo) { $this->referencedTo->modify($type, $value); } }
/** * Compile methods to check it * * @param Context $context */ public function compile(Context $context) { $context->setScope($this); foreach ($this->methods as $method) { $context->clearSymbols(); if (!$method->isStatic()) { $thisPtr = new Variable('this', $this, CompiledExpression::OBJECT); $thisPtr->incGets(); $context->addVariable($thisPtr); } $method->compile($context); $symbols = $context->getSymbols(); if (count($symbols) > 0) { foreach ($symbols as $name => $variable) { if ($variable->isUnused()) { $context->warning('unused-variable', sprintf('Unused variable $%s in method %s()', $variable->getName(), $method->getName())); } } } } }
/** * @param string $name * @param null $defaultValue * @param int $type * @param bool|false $referenced */ public function __construct($name, $defaultValue = null, $type = CompiledExpression::UNKNOWN, $referenced = false) { parent::__construct($name, $defaultValue, $type); $this->referenced = $referenced; }
/** * @param string $name * @param mixed $defaultValue * @param int $type */ public function __construct($name, $defaultValue = null, $type = CompiledExpression::UNKNOWN) { parent::__construct($name, $defaultValue, $type); }
public function testReferenceToChange() { /** * $a = 1; * $b = &$a; */ $parentVariable = new Variable('a', 1, CompiledExpression::INTEGER); $variable = new Variable('b', $parentVariable->getValue(), $parentVariable->getType()); static::assertFalse($variable->isReferenced()); $variable->setReferencedTo($parentVariable); static::assertTrue($variable->isReferenced()); static::assertSame($parentVariable, $variable->getReferencedTo()); /** * $b = 55.00 */ $variable->modify(CompiledExpression::DOUBLE, 55.0); static::assertSame($variable->getValue(), $parentVariable->getValue()); static::assertSame($variable->getType(), $parentVariable->getType()); }
/** * @param Variable $variable */ public function add(Variable $variable) { $this->variables[$variable->getName()] = $variable; }
/** * @param Context $context * @return $this */ public function compile(Context $context) { if ($this->compiled) { return true; } $this->compiled = true; $context->setFilepath($this->filepath); $context->setScope($this); $context->getEventManager()->fire(Event\StatementBeforeCompile::EVENT_NAME, new Event\StatementBeforeCompile($this->statement, $context)); // Compile event for properties foreach ($this->properties as $property) { if (!$property->default) { continue; } // fire expression event for property default $context->getEventManager()->fire(Event\ExpressionBeforeCompile::EVENT_NAME, new Event\ExpressionBeforeCompile($property->default, $context)); } // Compile event for PropertyProperty foreach ($this->properties as $property) { $context->getEventManager()->fire(Event\StatementBeforeCompile::EVENT_NAME, new Event\StatementBeforeCompile($property, $context)); } // Compile event for constants foreach ($this->constants as $const) { $context->getEventManager()->fire(Event\StatementBeforeCompile::EVENT_NAME, new Event\StatementBeforeCompile($const, $context)); } // Compiler event for property statements foreach ($this->propertyStatements as $prop) { $context->getEventManager()->fire(Event\StatementBeforeCompile::EVENT_NAME, new Event\StatementBeforeCompile($prop, $context)); } // Compile each method foreach ($this->methods as $method) { $context->clearSymbols(); if (!$method->isStatic()) { $thisPtr = new Variable('this', $this, CompiledExpression::OBJECT); $thisPtr->incGets(); $context->addVariable($thisPtr); } $method->compile($context); $symbols = $context->getSymbols(); if (count($symbols) > 0) { foreach ($symbols as $name => $variable) { if ($variable->isUnused()) { $context->warning('unused-' . $variable->getSymbolType(), sprintf('Unused ' . $variable->getSymbolType() . ' $%s in method %s()', $variable->getName(), $method->getName())); } } } } return $this; }