本文整理汇总了PHP中Phan\Issue::maybeEmitInstance方法的典型用法代码示例。如果您正苦于以下问题:PHP Issue::maybeEmitInstance方法的具体用法?PHP Issue::maybeEmitInstance怎么用?PHP Issue::maybeEmitInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phan\Issue
的用法示例。
在下文中一共展示了Issue::maybeEmitInstance方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: analyzePropertyTypes
/**
* 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);
}
}
}
}
}
示例2: visitProp
/**
* 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();
}
示例3: visitProp
/**
* @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;
}
示例4: visitProp
/**
* @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;
}
示例5: analyzeCallToMethod
/**
* 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;
//.........这里部分代码省略.........
示例6: emitIssue
/**
* 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);
}
示例7: addProperty
/**
* 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);
}
示例8: visitProp
/**
* 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();
}
示例9: visitProp
/**
* 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;
}
示例10: analyzeNodeUnionTypeCast
/**
* 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;
}
示例11: visitMethodCall
/**
* @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;
}