* - Uses `\Phan\Analysis\PreOrderAnalysisVisitor` for pre-order analysis of a node (E.g. entering a function to analyze)
* - Recursively analyzes child nodes
* - Uses `\Phan\Analysis\PostOrderAnalysisVisitor` for post-order analysis of a node (E.g. analyzing a statement with the updated Context and emitting issues)
* - If there is more than one possible child context, merges state from them (variable types)
* An AST node we'd like to analyze the statements for
*
* @return Context
* The updated context after visiting the node
*
* NOTE: This should never get called.
*/
publicfunctionvisitIfElem(Node$node):Context
{
$context=$this->context->withLineNumberStart(
$node->lineno
);
// NOTE: This is different from other analysis visitors because analyzing 'cond' with `||` has side effects
// after supporting `BlockAnalysisVisitor->visitBinaryOp()`
// TODO: Calling analyzeAndGetUpdatedContext before preOrderAnalyze is a hack.
// TODO: This is redundant and has worse knowledge of the specific types of blocks than ConditionVisitor does.
// TODO: Implement a hybrid BlockAnalysisVisitor+ConditionVisitor that will do a better job of inferences and reducing false positives? (and reduce the redundant work)
// E.g. the below code would update the context of BlockAnalysisVisitor in BlockAnalysisVisitor->visitBinaryOp()
//
// if (!(is_string($x) || $x === null)) {}
//
// But we want to let BlockAnalysisVisitor modify the context for cases such as the below:
* XXX this is complicated because we need to know for each `if`/`elseif` clause
*
* - What the side effects of executing the expression are on the chain (e.g. variable assignments, assignments by references)
* - What the context would be if that expression were truthy (ConditionVisitor)
* - What the context would be if that expression were falsey (NegatedConditionVisitor)
*
* The code in visitIfElem had to be inlined in order to properly modify the associated contexts.
* @suppress PhanSuspiciousTruthyCondition, PhanSuspiciousTruthyString the value Phan infers for conditions can be array literals. This seems to be handled properly.
*/
publicfunctionvisitIf(Node$node):Context
{
$context=$this->context->withLineNumberStart(
$node->lineno
);
$context=$this->preOrderAnalyze($context,$node);
// We collect all child context so that the
// PostOrderAnalysisVisitor can optionally operate on
// NOTE: This is different from other analysis visitors because analyzing 'cond' with `||` has side effects
// after supporting `BlockAnalysisVisitor->visitBinaryOp()`
// TODO: Calling analyzeAndGetUpdatedContext before preOrderAnalyze is a hack.
// TODO: This is redundant and has worse knowledge of the specific types of blocks than ConditionVisitor does.
// TODO: Implement a hybrid BlockAnalysisVisitor+ConditionVisitor that will do a better job of inferences and reducing false positives? (and reduce the redundant work)
// E.g. the below code would update the context of BlockAnalysisVisitor in BlockAnalysisVisitor->visitBinaryOp()
//
// if (!(is_string($x) || $x === null)) {}
//
// But we want to let BlockAnalysisVisitor modify the context for cases such as the below:
// 1. Update context with any side effects of left
// 2. Create a context to analyze the right-hand side with any inferences possible from left (e.g. ($x instanceof MyClass) && $x->foo()
// 3. Analyze the right-hand side
// 4. Merge the possibly evaluated $right_context for the right-hand side into the original context. (The left_node is guaranteed to have been evaluated, so it becomes $context)
// TODO: Warn about non-node, they're guaranteed to be always false or true
// 1. Update context with any side effects of left
// 2. Create a context to analyze the right-hand side with any inferences possible from left (e.g. (!($x instanceof MyClass)) || $x->foo()
// 3. Analyze the right-hand side
// 4. Merge the possibly evaluated $right_context for the right-hand side into the original context. (The left_node is guaranteed to have been evaluated, so it becomes $context)
// TODO: Warn about non-node, they're guaranteed to be always false or true