maybeEmitInstance() public static method

public static maybeEmitInstance ( CodeBase $code_base, Context $context, phan\IssueInstance $issue_instance ) : void
$code_base CodeBase The code base within which we're operating
$context Phan\Language\Context The context in which the instance was found
$issue_instance phan\IssueInstance An issue instance to emit
return void
Beispiel #1
0
 /**
  * Check to see if the given Clazz is a duplicate
  *
  * @return null
  */
 public static function analyzePropertyTypes(CodeBase $code_base, Clazz $clazz)
 {
     foreach ($clazz->getPropertyList($code_base) as $property) {
         try {
             $union_type = $property->getUnionType();
         } catch (IssueException $exception) {
             Issue::maybeEmitInstance($code_base, $property->getContext(), $exception->getIssueInstance());
             continue;
         }
         // Look at each type in the parameter's Union Type
         foreach ($union_type->getTypeSet() as $type) {
             // If its a native type or a reference to
             // self, its OK
             if ($type->isNativeType() || $type->isSelfType()) {
                 continue;
             }
             if ($type instanceof TemplateType) {
                 if ($property->isStatic()) {
                     Issue::maybeEmit($code_base, $property->getContext(), Issue::TemplateTypeStaticProperty, $property->getFileRef()->getLineNumberStart(), (string) $property->getFQSEN());
                 }
             } else {
                 // Make sure the class exists
                 $type_fqsen = $type->asFQSEN();
                 if (!$code_base->hasClassWithFQSEN($type_fqsen) && !$type instanceof TemplateType && (!$property->hasDefiningFQSEN() || $property->getDefiningFQSEN() == $property->getFQSEN())) {
                     Issue::maybeEmit($code_base, $property->getContext(), Issue::UndeclaredTypeProperty, $property->getFileRef()->getLineNumberStart(), (string) $property->getFQSEN(), (string) $type_fqsen);
                 }
             }
         }
     }
 }
Beispiel #2
0
 /**
  * Visit a node with kind `\ast\AST_PROP`
  *
  * @param Node $node
  * A node of the type indicated by the method name that we'd
  * like to figure out the type that it produces.
  *
  * @return UnionType
  * The set of types that are possibly produced by the
  * given node
  */
 public function visitProp(Node $node) : UnionType
 {
     try {
         $property = (new ContextNode($this->code_base, $this->context, $node))->getProperty($node->children['prop']);
         return $property->getUnionType();
     } catch (IssueException $exception) {
         Issue::maybeEmitInstance($this->code_base, $this->context, $exception->getIssueInstance());
     } catch (CodeBaseException $exception) {
         $property_name = $node->children['prop'];
         $this->emitIssue(Issue::UndeclaredProperty, $node->lineno ?? 0, "{$exception->getFQSEN()}->{$property_name}");
     } catch (UnanalyzableException $exception) {
         // Swallow it. There are some constructs that we
         // just can't figure out.
     } catch (NodeException $exception) {
         // Swallow it. There are some constructs that we
         // just can't figure out.
     }
     return new UnionType();
 }
Beispiel #3
0
 /**
  * @param Node $node
  * A node to parse
  *
  * @return Context
  * A new or an unchanged context resulting from
  * parsing the node
  */
 public function visitProp(Node $node) : Context
 {
     $property_name = $node->children['prop'];
     // Things like $foo->$bar
     if (!is_string($property_name)) {
         return $this->context;
     }
     assert(is_string($property_name), "Property must be string in context {$this->context}");
     try {
         $class_list = (new ContextNode($this->code_base, $this->context, $node->children['expr']))->getClassList();
     } catch (CodeBaseException $exception) {
         // This really shouldn't happen since the code
         // parsed cleanly. This should fatal.
         // throw $exception;
         return $this->context;
     } catch (\Exception $exception) {
         // If we can't figure out what kind of a class
         // this is, don't worry about it
         return $this->context;
     }
     foreach ($class_list as $clazz) {
         // Check to see if this class has the property or
         // a setter
         if (!$clazz->hasPropertyWithName($this->code_base, $property_name)) {
             if (!$clazz->hasMethodWithName($this->code_base, '__set')) {
                 continue;
             }
         }
         try {
             $property = $clazz->getPropertyByNameInContext($this->code_base, $property_name, $this->context);
         } catch (IssueException $exception) {
             Issue::maybeEmitInstance($this->code_base, $this->context, $exception->getIssueInstance());
             return $this->context;
         }
         if (!$this->right_type->canCastToExpandedUnionType($property->getUnionType(), $this->code_base)) {
             $this->emitIssue(Issue::TypeMismatchProperty, $node->lineno ?? 0, (string) $this->right_type, "{$clazz->getFQSEN()}::{$property->getName()}", (string) $property->getUnionType());
             return $this->context;
         }
         // After having checked it, add this type to it
         $property->getUnionType()->addUnionType($this->right_type);
         return $this->context;
     }
     if (Config::get()->allow_missing_properties) {
         try {
             // Create the property
             (new ContextNode($this->code_base, $this->context, $node))->getOrCreateProperty($property_name);
         } catch (\Exception $exception) {
             // swallow it
         }
     } elseif (!empty($class_list)) {
         $this->emitIssue(Issue::UndeclaredProperty, $node->lineno ?? 0, $property_name);
     } else {
         // If we hit this part, we couldn't figure out
         // the class, so we ignore the issue
     }
     return $this->context;
 }
Beispiel #4
0
 /**
  * @param Node $node
  * A node to parse
  *
  * @return Context
  * A new or an unchanged context resulting from
  * parsing the node
  */
 public function visitProp(Node $node) : Context
 {
     $property_name = $node->children['prop'];
     // Things like $foo->$bar
     if (!is_string($property_name)) {
         return $this->context;
     }
     assert(is_string($property_name), "Property must be string");
     try {
         $class_list = (new ContextNode($this->code_base, $this->context, $node->children['expr']))->getClassList();
     } catch (CodeBaseException $exception) {
         // This really shouldn't happen since the code
         // parsed cleanly. This should fatal.
         // throw $exception;
         return $this->context;
     } catch (\Exception $exception) {
         // If we can't figure out what kind of a class
         // this is, don't worry about it
         return $this->context;
     }
     foreach ($class_list as $clazz) {
         // Check to see if this class has the property or
         // a setter
         if (!$clazz->hasPropertyWithName($this->code_base, $property_name)) {
             if (!$clazz->hasMethodWithName($this->code_base, '__set')) {
                 continue;
             }
         }
         try {
             $property = $clazz->getPropertyByNameInContext($this->code_base, $property_name, $this->context);
         } catch (IssueException $exception) {
             Issue::maybeEmitInstance($this->code_base, $this->context, $exception->getIssueInstance());
             return $this->context;
         }
         if (!$this->right_type->canCastToExpandedUnionType($property->getUnionType(), $this->code_base)) {
             $this->emitIssue(Issue::TypeMismatchProperty, $node->lineno ?? 0, (string) $this->right_type, "{$clazz->getFQSEN()}::{$property->getName()}", (string) $property->getUnionType());
             return $this->context;
         } else {
             // If we're assigning to an array element then we don't
             // know what the constitutation of the parameter is
             // outside of the scope of this assignment, so we add to
             // its union type rather than replace it.
             if ($this->is_dim_assignment) {
                 $property->getUnionType()->addUnionType($this->right_type);
             }
         }
         // After having checked it, add this type to it
         $property->getUnionType()->addUnionType($this->right_type);
         return $this->context;
     }
     $std_class_fqsen = FullyQualifiedClassName::getStdClassFQSEN();
     if (Config::get()->allow_missing_properties || !empty($class_list) && $class_list[0]->getFQSEN() == $std_class_fqsen) {
         try {
             // Create the property
             $property = (new ContextNode($this->code_base, $this->context, $node))->getOrCreateProperty($property_name);
             $property->getUnionType()->addUnionType($this->right_type);
         } catch (\Exception $exception) {
             // swallow it
         }
     } elseif (!empty($class_list)) {
         $this->emitIssue(Issue::UndeclaredProperty, $node->lineno ?? 0, "{$class_list[0]->getFQSEN()}->{$property_name}");
     } else {
         // If we hit this part, we couldn't figure out
         // the class, so we ignore the issue
     }
     return $this->context;
 }
Beispiel #5
0
 /**
  * Analyze the parameters and arguments for a call
  * to the given method or function
  *
  * @param CodeBase $code_base
  * @param Method $method
  * @param Node $node
  *
  * @return null
  */
 private function analyzeCallToMethod(CodeBase $code_base, FunctionInterface $method, Node $node)
 {
     $method->addReference($this->context);
     // Create variables for any pass-by-reference
     // parameters
     $argument_list = $node->children['args'];
     foreach ($argument_list->children as $i => $argument) {
         if (!is_object($argument)) {
             continue;
         }
         $parameter = $method->getParameterForCaller($i);
         if (!$parameter) {
             continue;
         }
         // If pass-by-reference, make sure the variable exists
         // or create it if it doesn't.
         if ($parameter->isPassByReference()) {
             if ($argument->kind == \ast\AST_VAR) {
                 // We don't do anything with it; just create it
                 // if it doesn't exist
                 $variable = (new ContextNode($this->code_base, $this->context, $argument))->getOrCreateVariable();
             } elseif ($argument->kind == \ast\AST_STATIC_PROP || $argument->kind == \ast\AST_PROP) {
                 $property_name = $argument->children['prop'];
                 if (is_string($property_name)) {
                     // We don't do anything with it; just create it
                     // if it doesn't exist
                     try {
                         $property = (new ContextNode($this->code_base, $this->context, $argument))->getOrCreateProperty($argument->children['prop']);
                     } catch (IssueException $exception) {
                         Issue::maybeEmitInstance($this->code_base, $this->context, $exception->getIssueInstance());
                     } catch (\Exception $exception) {
                         // If we can't figure out what kind of a call
                         // this is, don't worry about it
                     }
                 } else {
                     // This is stuff like `Class->$foo`. I'm ignoring
                     // it.
                 }
             }
         }
     }
     // Confirm the argument types are clean
     ArgumentType::analyze($method, $node, $this->context, $this->code_base);
     // Take another pass over pass-by-reference parameters
     // and assign types to passed in variables
     foreach ($argument_list->children as $i => $argument) {
         if (!is_object($argument)) {
             continue;
         }
         $parameter = $method->getParameterForCaller($i);
         if (!$parameter) {
             continue;
         }
         if (Config::get()->dead_code_detection) {
             (new ArgumentVisitor($this->code_base, $this->context))($argument);
         }
         // If the parameter is pass-by-reference and we're
         // passing a variable in, see if we should pass
         // the parameter and variable types to eachother
         $variable = null;
         if ($parameter->isPassByReference()) {
             if ($argument->kind == \ast\AST_VAR) {
                 $variable = (new ContextNode($this->code_base, $this->context, $argument))->getOrCreateVariable();
             } elseif ($argument->kind == \ast\AST_STATIC_PROP || $argument->kind == \ast\AST_PROP) {
                 $property_name = $argument->children['prop'];
                 if (is_string($property_name)) {
                     // We don't do anything with it; just create it
                     // if it doesn't exist
                     try {
                         $variable = (new ContextNode($this->code_base, $this->context, $argument))->getOrCreateProperty($argument->children['prop']);
                     } catch (IssueException $exception) {
                         Issue::maybeEmitInstance($this->code_base, $this->context, $exception->getIssueInstance());
                     } catch (\Exception $exception) {
                         // If we can't figure out what kind of a call
                         // this is, don't worry about it
                     }
                 } else {
                     // This is stuff like `Class->$foo`. I'm ignoring
                     // it.
                 }
             }
             if ($variable) {
                 $variable->getUnionType()->addUnionType($parameter->getVariadicElementUnionType());
             }
         }
     }
     // If we're in quick mode, don't retest methods based on
     // parameter types passed in
     if (Config::get()->quick_mode) {
         return;
     }
     // We're going to hunt to see if any of the arguments
     // have a mismatch with the parameters. If so, we'll
     // re-check the method to see how the parameters impact
     // its return type
     $has_argument_parameter_mismatch = false;
     // Now that we've made sure the arguments are sufficient
     // for definitions on the method, we iterate over the
     // arguments again and add their types to the parameter
     // types so we can test the method again
     $argument_list = $node->children['args'];
     // We create a copy of the parameter list so we can switch
     // back to it after
     $original_parameter_list = $method->getParameterList();
     // Create a backup of the method's scope so that we can
     // reset it after f*****g with it below
     $original_method_scope = $method->getInternalScope();
     foreach ($argument_list->children as $i => $argument) {
         // TODO(Issue #376): Support inference on the child in **the set of vargs**, not just the first vararg
         // This is just testing the first vararg.
         // The implementer will also need to restore the original parameter list.
         $parameter = $original_parameter_list[$i] ?? null;
         if (!$parameter) {
             continue;
         }
         // If the parameter has no type, pass the
         // argument's type to it
         if ($parameter->getVariadicElementUnionType()->isEmpty()) {
             $has_argument_parameter_mismatch = true;
             // If this isn't an internal function or method
             // and it has no type, add the argument's type
             // to it so we can compare it to subsequent
             // calls
             if (!$parameter->isInternal()) {
                 $argument_type = UnionType::fromNode($this->context, $this->code_base, $argument);
                 // Clone the parameter in the original
                 // parameter list so we can reset it
                 // later
                 // TODO: If there are varargs and this is beyond the end, ensure last arg is cloned.
                 $original_parameter_list[$i] = clone $original_parameter_list[$i];
                 // Then set the new type on that parameter based
                 // on the argument's type. We'll use this to
                 // retest the method with the passed in types
                 $parameter->getVariadicElementUnionType()->addUnionType($argument_type);
                 if (!is_object($argument)) {
                     continue;
                 }
                 // If we're passing by reference, get the variable
                 // we're dealing with wrapped up and shoved into
                 // the scope of the method
                 if ($parameter->isPassByReference()) {
                     if ($original_parameter_list[$i]->isVariadic()) {
                         // For now, give up and work on it later.
                         // TODO(Issue #376): It's possible to have a parameter `&...$args`. Analysing that is going to be a problem.
                         // Is it possible to create `PassByReferenceVariableCollection extends Variable` or something similar?
                     } elseif ($argument->kind == \ast\AST_VAR) {
                         // Get the variable
                         $variable = (new ContextNode($this->code_base, $this->context, $argument))->getOrCreateVariable();
                         $pass_by_reference_variable = new PassByReferenceVariable($parameter, $variable);
                         $parameter_list = $method->getParameterList();
                         $parameter_list[$i] = $pass_by_reference_variable;
                         $method->setParameterList($parameter_list);
                         // Add it to the scope of the function wrapped
                         // in a way that makes it addressable as the
                         // parameter its mimicking
                         $method->getInternalScope()->addVariable($pass_by_reference_variable);
                     } else {
                         if ($argument->kind == \ast\AST_STATIC_PROP) {
                             // Get the variable
                             $property = (new ContextNode($this->code_base, $this->context, $argument))->getOrCreateProperty($argument->children['prop'] ?? '');
                             $pass_by_reference_variable = new PassByReferenceVariable($parameter, $property);
                             $parameter_list = $method->getParameterList();
                             $parameter_list[$i] = $pass_by_reference_variable;
                             $method->setParameterList($parameter_list);
                             // Add it to the scope of the function wrapped
                             // in a way that makes it addressable as the
                             // parameter its mimicking
                             $method->getInternalScope()->addVariable($pass_by_reference_variable);
                         }
                     }
                 } else {
                     // Overwrite the method's variable representation
                     // of the parameter with the parameter with the
                     // new type
                     $method->getInternalScope()->addVariable($parameter);
                 }
             }
         }
     }
     // Now that we know something about the parameters used
     // to call the method, we can reanalyze the method with
     // the types of the parameter, making sure we don't get
     // into an infinite loop of checking calls to the current
     // method in scope
     if ($has_argument_parameter_mismatch && !$method->isInternal() && (!$this->context->isInFunctionLikeScope() || $method->getFQSEN() !== $this->context->getFunctionLikeFQSEN())) {
         $method->analyze($method->getContext(), $code_base);
     }
     // Reset to the original parameter list after having
     // tested the parameters with the types passed in
     $method->setParameterList($original_parameter_list);
     // Reset the scope to its original version before we
     // put new parameters in it
     $method->setInternalScope($original_method_scope);
 }
Beispiel #6
0
 /**
  * Emit an issue if it is not suppressed
  *
  * @param CodeBase $code_base
  * The code base in which the issue was found
  *
  * @param Context $context
  * The context in which the issue was found
  *
  * @param string $issue_type
  * A name for the type of issue such as 'PhanPluginMyIssue'
  *
  * @param string $issue_message
  * The complete issue message to emit such as 'class with
  * fqsen \NS\Name is broken in some fashion'.
  *
  * @param int $severity
  * A value from the set {Issue::SEVERITY_LOW,
  * Issue::SEVERITY_NORMAL, Issue::SEVERITY_HIGH}.
  *
  * @param int $remediation_difficulty
  * A guess at how hard the issue will be to fix from the
  * set {Issue:REMEDIATION_A, Issue:REMEDIATION_B, ...
  * Issue::REMEDIATION_F} with F being the hardest.
  */
 public function emitIssue(CodeBase $code_base, Context $context, string $issue_type, string $issue_message, int $severity = Issue::SEVERITY_NORMAL, int $remediation_difficulty = Issue::REMEDIATION_B, int $issue_type_id = Issue::TYPE_ID_UNKNOWN)
 {
     $issue = new Issue($issue_type, Issue::CATEGORY_PLUGIN, $severity, $issue_message, $remediation_difficulty, $issue_type_id);
     $issue_instance = new IssueInstance($issue, $context->getFile(), $context->getLineNumberStart(), []);
     Issue::maybeEmitInstance($code_base, $context, $issue_instance);
 }
Beispiel #7
0
 /**
  * Add a property to this class
  *
  * @param CodeBase $code_base
  * A reference to the code base in which the ancestor exists
  *
  * @param Property $property
  * The property to copy onto this class
  *
  * @param Option<Type>|None $type_option
  * A possibly defined type used to define template
  * parameter types when importing the property
  *
  * @return void
  */
 public function addProperty(CodeBase $code_base, Property $property, $type_option)
 {
     // Ignore properties we already have
     if ($this->hasPropertyWithName($code_base, $property->getName())) {
         return;
     }
     $property_fqsen = FullyQualifiedPropertyName::make($this->getFQSEN(), $property->getName());
     if ($property->getFQSEN() !== $property_fqsen) {
         $property = clone $property;
         $property->setDefiningFQSEN($property->getFQSEN());
         $property->setFQSEN($property_fqsen);
         try {
             // If we have a parent type defined, map the property's
             // type through it
             if ($type_option->isDefined() && $property->getUnionType()->hasTemplateType()) {
                 $property->setUnionType($property->getUnionType()->withTemplateParameterTypeMap($type_option->get()->getTemplateParameterTypeMap($code_base)));
             }
         } catch (IssueException $exception) {
             Issue::maybeEmitInstance($code_base, $property->getContext(), $exception->getIssueInstance());
         }
     }
     $code_base->addProperty($property);
 }
Beispiel #8
0
 /**
  * Visit a node with kind `\ast\AST_PROP`
  *
  * @param Node $node
  * A node of the type indicated by the method name that we'd
  * like to figure out the type that it produces.
  *
  * @return UnionType
  * The set of types that are possibly produced by the
  * given node
  */
 public function visitProp(Node $node) : UnionType
 {
     try {
         $property = (new ContextNode($this->code_base, $this->context, $node))->getProperty($node->children['prop']);
         // Map template types to concrete types
         if ($property->getUnionType()->hasTemplateType()) {
             // Get the type of the object calling the property
             $expression_type = UnionType::fromNode($this->context, $this->code_base, $node->children['expr']);
             $union_type = $property->getUnionType()->withTemplateParameterTypeMap($expression_type->getTemplateParameterTypeMap($this->code_base));
             return $union_type;
         }
         return $property->getUnionType();
     } catch (IssueException $exception) {
         Issue::maybeEmitInstance($this->code_base, $this->context, $exception->getIssueInstance());
     } catch (CodeBaseException $exception) {
         $property_name = $node->children['prop'];
         $this->emitIssue(Issue::UndeclaredProperty, $node->lineno ?? 0, "{$exception->getFQSEN()}->{$property_name}");
     } catch (UnanalyzableException $exception) {
         // Swallow it. There are some constructs that we
         // just can't figure out.
     } catch (NodeException $exception) {
         // Swallow it. There are some constructs that we
         // just can't figure out.
     }
     return new UnionType();
 }
 /**
  * Visit a node with kind `\ast\AST_PROP`
  *
  * @param Node $node
  * A node of the type indicated by the method name that we'd
  * like to figure out the type that it produces.
  *
  * @return Context
  * A new or an unchanged context resulting from
  * parsing the node
  */
 public function visitProp(Node $node) : Context
 {
     try {
         $property = (new ContextNode($this->code_base, $this->context, $node))->getProperty($node->children['prop']);
         // Mark that this property has been referenced from
         // this context
         $property->addReference($this->context);
     } catch (\Exception $exception) {
         // Swallow any exceptions. We'll log the errors
         // elsewhere.
     }
     if (isset($property)) {
         $this->analyzeNoOp($node, Issue::NoopProperty);
     } else {
         assert(isset($node->children['expr']) || isset($node->children['class']), "Property nodes must either have an expression or class");
         $class_list = [];
         try {
             // Get the set of classes that are being referenced
             $class_list = (new ContextNode($this->code_base, $this->context, $node->children['expr'] ?? $node->children['class']))->getClassList(true);
         } catch (IssueException $exception) {
             Issue::maybeEmitInstance($this->code_base, $this->context, $exception->getIssueInstance());
         }
         // Find out of any of them have a __get magic method
         $has_getter = array_reduce($class_list, function ($carry, $class) {
             return $carry || $class->hasGetMethod($this->code_base);
         }, false);
         // If they don't, then analyze for Noops.
         if (!$has_getter) {
             $this->analyzeNoOp($node, Issue::NoopProperty);
         }
     }
     return $this->context;
 }
Beispiel #10
0
 /**
  * Emit a log message if the type of the given
  * node cannot be cast to the given type
  *
  * @param Node|null|string|int $node
  * A node or whatever php-ast feels like returning
  *
  * @return bool
  * True if the cast is possible, else false
  */
 private static function analyzeNodeUnionTypeCast($node, Context $context, CodeBase $code_base, UnionType $cast_type, \Closure $issue_instance) : bool
 {
     // Get the type of the node
     $node_type = UnionType::fromNode($context, $code_base, $node);
     // See if it can be cast to the given type
     $can_cast = $node_type->canCastToUnionType($cast_type);
     // If it can't, emit the log message
     if (!$can_cast) {
         Issue::maybeEmitInstance($code_base, $context, $issue_instance($node_type));
     }
     return $can_cast;
 }
 /**
  * @param Node $node
  * A node to parse
  *
  * @return Context
  * A new or an unchanged context resulting from
  * parsing the node
  */
 public function visitMethodCall(Node $node) : Context
 {
     $method_name = $node->children['method'];
     if (!is_string($method_name)) {
         return $this->context;
     }
     try {
         $method = (new ContextNode($this->code_base, $this->context, $node))->getMethod($method_name, false);
     } catch (IssueException $exception) {
         Issue::maybeEmitInstance($this->code_base, $this->context, $exception->getIssueInstance());
         return $this->context;
     } catch (NodeException $exception) {
         // If we can't figure out the class for this method
         // call, cry YOLO and mark every method with that
         // name with a reference.
         if (Config::get()->dead_code_detection && Config::get()->dead_code_detection_prefer_false_negative) {
             foreach ($this->code_base->getMethodSetByName($method_name) as $method) {
                 $method->addReference($this->context);
             }
         }
         // Swallow it
         return $this->context;
     }
     // Check the call for paraemter and argument types
     $this->analyzeCallToMethod($this->code_base, $method, $node);
     return $this->context;
 }