/** * Initializes a reflection for the property * * @param string|array $unusedFunctionName Name of the function/method * @param string $parameterName Name of the parameter to reflect * @param Param $parameterNode Parameter definition node * @param int $parameterIndex Index of parameter * @param \ReflectionFunctionAbstract $declaringFunction */ public function __construct($unusedFunctionName, $parameterName, Param $parameterNode = null, $parameterIndex = 0, \ReflectionFunctionAbstract $declaringFunction = null) { // Let's unset original read-only property to have a control over it via __get unset($this->name); $this->parameterNode = $parameterNode; $this->parameterIndex = $parameterIndex; $this->declaringFunction = $declaringFunction; if ($this->isDefaultValueAvailable()) { if ($declaringFunction instanceof \ReflectionMethod) { $context = $declaringFunction->getDeclaringClass(); } else { $context = $declaringFunction; } $expressionSolver = new NodeExpressionResolver($context); $expressionSolver->process($this->parameterNode->default); $this->defaultValue = $expressionSolver->getValue(); $this->isDefaultValueConstant = $expressionSolver->isConstant(); $this->defaultValueConstantName = $expressionSolver->getConstantName(); } }
/** * {@inheritDoc} */ public function enterNode(Node $node) { // There may be internal closures, we do not need to look at them if ($node instanceof Node\Expr\Closure) { return NodeTraverser::DONT_TRAVERSE_CHILDREN; } if ($node instanceof Node\Stmt\Static_) { $expressionSolver = new NodeExpressionResolver($this->context); $staticVariables = $node->vars; foreach ($staticVariables as $staticVariable) { $expr = $staticVariable->default; if ($expr) { $expressionSolver->process($expr); $value = $expressionSolver->getValue(); } else { $value = null; } $this->staticVariables[$staticVariable->name] = $value; } } return null; }
/** * @inheritDoc */ public function getValue($object = null) { if (!isset($object)) { $solver = new NodeExpressionResolver($this->getDeclaringClass()); if (!isset($this->propertyNode->default)) { return null; } $solver->process($this->propertyNode->default); return $solver->getValue(); } $this->initializeInternalReflection(); return parent::getValue($object); }
/** * Returns list of constants from the class * * @return array */ private function findConstants() { $constants = array(); $expressionSolver = new NodeExpressionResolver($this); // constants can be only top-level nodes in the class, so we can scan them directly foreach ($this->classLikeNode->stmts as $classLevelNode) { if ($classLevelNode instanceof ClassConst) { $nodeConstants = $classLevelNode->consts; if (!empty($nodeConstants)) { foreach ($nodeConstants as $nodeConstant) { $expressionSolver->process($nodeConstant->value); $constants[$nodeConstant->name] = $expressionSolver->getValue(); } } } } return $constants; }