Skip to content
Permalink
Browse files

Fix dumb

  • Loading branch information
muglug committed Dec 27, 2019
1 parent 982fe62 commit 6a6a6ab2d7d3c7522c007a37a9a532335fcd654d
Showing with 87 additions and 87 deletions.
  1. +87 −87 src/Psalm/Internal/Visitor/ReflectorVisitor.php
@@ -1847,6 +1847,92 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_m

$storage->required_param_count = $required_param_count;

if (($stmt instanceof PhpParser\Node\Stmt\Function_
|| $stmt instanceof PhpParser\Node\Stmt\ClassMethod)
&& $stmt->stmts
) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
&& $storage instanceof MethodStorage
&& $class_storage
&& !$class_storage->mutation_free
&& count($stmt->stmts) === 1
&& !count($stmt->params)
&& $stmt->stmts[0] instanceof PhpParser\Node\Stmt\Return_
&& $stmt->stmts[0]->expr instanceof PhpParser\Node\Expr\PropertyFetch
&& $stmt->stmts[0]->expr->var instanceof PhpParser\Node\Expr\Variable
&& $stmt->stmts[0]->expr->var->name === 'this'
) {
$storage->mutation_free = true;
$storage->external_mutation_free = true;
$storage->mutation_free_inferred = true;
} elseif (strpos($stmt->name->name, 'assert') === 0) {
$var_assertions = [];

foreach ($stmt->stmts as $function_stmt) {
if ($function_stmt instanceof PhpParser\Node\Stmt\If_) {
$final_actions = \Psalm\Internal\Analyzer\ScopeAnalyzer::getFinalControlActions(
$function_stmt->stmts,
null,
$this->config->exit_functions,
false,
false
);

if ($final_actions !== [\Psalm\Internal\Analyzer\ScopeAnalyzer::ACTION_END]) {
$var_assertions = [];
break;
}

$if_clauses = \Psalm\Type\Algebra::getFormula(
\spl_object_id($function_stmt->cond),
$function_stmt->cond,
$this->fq_classlike_names
? $this->fq_classlike_names[count($this->fq_classlike_names) - 1]
: null,
$this->file_scanner,
null
);

$negated_formula = \Psalm\Type\Algebra::negateFormula($if_clauses);

$rules = \Psalm\Type\Algebra::getTruthsFromFormula($negated_formula);

if (!$rules) {
$var_assertions = [];
break;
}

foreach ($rules as $var_id => $rule) {
foreach ($rule as $rule_part) {
if (count($rule_part) > 1) {
continue 2;
}
}

if (isset($existing_params[$var_id])) {
$param_offset = $existing_params[$var_id];

$var_assertions[] = new \Psalm\Storage\Assertion(
$param_offset,
$rule
);
} elseif (strpos($var_id, '$this->') === 0) {
$var_assertions[] = new \Psalm\Storage\Assertion(
$var_id,
$rule
);
}
}
} else {
$var_assertions = [];
break;
}
}

$storage->assertions = $var_assertions;
}
}

if (!$this->scan_deep
&& ($stmt instanceof PhpParser\Node\Stmt\Function_
|| $stmt instanceof PhpParser\Node\Stmt\ClassMethod
@@ -1973,100 +2059,14 @@ private function registerFunctionLike(PhpParser\Node\FunctionLike $stmt, $fake_m

if ($storage instanceof MethodStorage) {
$storage->external_mutation_free = true;
$storage->mutation_free_inferred = false;
}
}

if ($storage instanceof MethodStorage && $docblock_info->external_mutation_free) {
$storage->external_mutation_free = true;
}

if (($stmt instanceof PhpParser\Node\Stmt\Function_
|| $stmt instanceof PhpParser\Node\Stmt\ClassMethod)
&& $stmt->stmts
) {
if ($stmt instanceof PhpParser\Node\Stmt\ClassMethod
&& $storage instanceof MethodStorage
&& $class_storage
&& !$class_storage->mutation_free
&& !$storage->mutation_free
&& count($stmt->stmts) === 1
&& !count($stmt->params)
&& $stmt->stmts[0] instanceof PhpParser\Node\Stmt\Return_
&& $stmt->stmts[0]->expr instanceof PhpParser\Node\Expr\PropertyFetch
&& $stmt->stmts[0]->expr->var instanceof PhpParser\Node\Expr\Variable
&& $stmt->stmts[0]->expr->var->name === 'this'
) {
$storage->mutation_free = true;
$storage->external_mutation_free = true;
$storage->mutation_free_inferred = true;
} elseif (strpos($stmt->name->name, 'assert') === 0) {
$var_assertions = [];

foreach ($stmt->stmts as $function_stmt) {
if ($function_stmt instanceof PhpParser\Node\Stmt\If_) {
$final_actions = \Psalm\Internal\Analyzer\ScopeAnalyzer::getFinalControlActions(
$function_stmt->stmts,
null,
$this->config->exit_functions,
false,
false
);

if ($final_actions !== [\Psalm\Internal\Analyzer\ScopeAnalyzer::ACTION_END]) {
$var_assertions = [];
break;
}

$if_clauses = \Psalm\Type\Algebra::getFormula(
\spl_object_id($function_stmt->cond),
$function_stmt->cond,
$this->fq_classlike_names
? $this->fq_classlike_names[count($this->fq_classlike_names) - 1]
: null,
$this->file_scanner,
null
);

$negated_formula = \Psalm\Type\Algebra::negateFormula($if_clauses);

$rules = \Psalm\Type\Algebra::getTruthsFromFormula($negated_formula);

if (!$rules) {
$var_assertions = [];
break;
}

foreach ($rules as $var_id => $rule) {
foreach ($rule as $rule_part) {
if (count($rule_part) > 1) {
continue 2;
}
}

if (isset($existing_params[$var_id])) {
$param_offset = $existing_params[$var_id];

$var_assertions[] = new \Psalm\Storage\Assertion(
$param_offset,
$rule
);
} elseif (strpos($var_id, '$this->') === 0) {
$var_assertions[] = new \Psalm\Storage\Assertion(
$var_id,
$rule
);
}
}
} else {
$var_assertions = [];
break;
}
}

$storage->assertions = $var_assertions;
}
}

if ($docblock_info->deprecated) {
$storage->deprecated = true;
}

0 comments on commit 6a6a6ab

Please sign in to comment.
You can’t perform that action at this time.