コード例 #1
0
ファイル: DepthFirstVisitor.php プロジェクト: hslatman/phan
 /**
  * Visit a node with kind `\ast\AST_CLOSURE`
  *
  * @param Node $node
  * A node to parse
  *
  * @return Context
  * A new or an unchanged context resulting from
  * parsing the node
  */
 public function visitClosure(Node $node) : Context
 {
     $closure_fqsen = FullyQualifiedFunctionName::fromClosureInContext($this->context);
     $method = Method::fromNode($this->context, $this->code_base, $node);
     // Override the FQSEN with the found alternate ID
     $method->setFQSEN($closure_fqsen);
     // Make the closure reachable by FQSEN from anywhere
     $this->code_base->addMethod($method);
     // If we have a 'this' variable in our current scope,
     // pass it down into the closure
     $context = $this->context->withScope(new Scope());
     if ($context->getScope()->hasVariableWithName('this')) {
         $context = $context->addScopeVariable($this->context->getScope()->getVariableWithName('this'));
     }
     if (!empty($node->children['uses']) && $node->children['uses']->kind == \ast\AST_CLOSURE_USES) {
         $uses = $node->children['uses'];
         foreach ($uses->children as $use) {
             if ($use->kind != \ast\AST_CLOSURE_VAR) {
                 Log::err(Log::EVAR, "You can only have variables in a closure use() clause", $this->context->getFile(), $node->lineno);
                 continue;
             }
             $variable_name = AST::variableName($use->children['name']);
             if (empty($variable_name)) {
                 continue;
             }
             $variable = null;
             // Check to see if the variable exists in this scope
             if (!$this->context->getScope()->hasVariableWithName($variable_name)) {
                 // If this is not pass-by-reference variable we
                 // have a problem
                 if (!($use->flags & \ast\flags\PARAM_REF)) {
                     Log::err(Log::EVAR, "Variable \${$variable_name} is not defined", $this->context->getFile(), $node->lineno);
                     continue;
                 } else {
                     // If the variable doesn't exist, but its
                     // a pass-by-reference variable, we can
                     // just create it
                     $variable = Variable::fromNodeInContext($use, $this->context, $this->code_base, false);
                 }
             } else {
                 $variable = $this->context->getScope()->getVariableWithName($variable_name);
                 // If this isn't a pass-by-reference variable, we
                 // clone the variable so state within this scope
                 // doesn't update the outer scope
                 if (!($use->flags & \ast\flags\PARAM_REF)) {
                     $variable = clone $variable;
                 }
             }
             // Pass the variable into a new scope
             $context = $context->withScopeVariable($variable);
         }
     }
     // Add all parameters to the scope
     if (!empty($node->children['params']) && $node->children['params']->kind == \ast\AST_PARAM_LIST) {
         $params = $node->children['params'];
         foreach ($params->children as $param) {
             // Read the parameter
             $parameter = Parameter::fromNode($this->context, $this->code_base, $param);
             // Add it to the scope
             $context = $context->withScopeVariable($parameter);
         }
     }
     return $context->withClosureFQSEN($closure_fqsen);
 }
コード例 #2
0
ファイル: ParseVisitor.php プロジェクト: tmli3b3rm4n/phan
 /**
  * Visit a node with kind `\ast\AST_FUNC_DECL`
  *
  * @param Node $node
  * A node to parse
  *
  * @return Context
  * A new or an unchanged context resulting from
  * parsing the node
  */
 public function visitFuncDecl(Node $node) : Context
 {
     $function_name = $node->name;
     // Hunt for an un-taken alternate ID
     $alternate_id = 0;
     $function_fqsen = null;
     do {
         $function_fqsen = FullyQualifiedFunctionName::fromStringInContext($function_name, $this->context)->withNamespace($this->context->getNamespace())->withAlternateId($alternate_id++);
     } while ($this->code_base->hasMethod($function_fqsen));
     $method = Method::fromNode($this->context->withLineNumberStart($node->lineno ?? 0)->withLineNumberEnd($node->endLineno ?? 0), $this->code_base, $node);
     $method->setFQSEN($function_fqsen);
     $this->code_base->addMethod($method);
     // Send the context into the method
     $context = $this->context->withMethodFQSEN($function_fqsen);
     // Add each method parameter to the scope. We clone it
     // so that changes to the variable don't alter the
     // parameter definition
     foreach ($method->getParameterList() as $parameter) {
         $context->addScopeVariable(clone $parameter);
     }
     return $context;
 }