2016-05-09 14:56:07 +02:00
|
|
|
<?php
|
2016-08-13 20:20:46 +02:00
|
|
|
namespace Psalm\Checker;
|
2016-05-09 14:56:07 +02:00
|
|
|
|
|
|
|
use PhpParser;
|
|
|
|
|
|
|
|
class ScopeChecker
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Do all code paths in this list of statements exit the block (return/throw)
|
|
|
|
*
|
2016-12-11 19:48:11 +01:00
|
|
|
* @param array<PhpParser\Node\Stmt|PhpParser\Node\Expr> $stmts
|
|
|
|
* @param bool $check_continue - also looks for a continue
|
|
|
|
* @param bool $check_break
|
2016-05-09 14:56:07 +02:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-06-10 20:47:44 +02:00
|
|
|
public static function doesLeaveBlock(array $stmts, $check_continue = true, $check_break = true)
|
2016-05-09 14:56:07 +02:00
|
|
|
{
|
2016-06-13 07:48:29 +02:00
|
|
|
if (empty($stmts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-05-09 14:56:07 +02:00
|
|
|
for ($i = count($stmts) - 1; $i >= 0; $i--) {
|
|
|
|
$stmt = $stmts[$i];
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Return_ ||
|
|
|
|
$stmt instanceof PhpParser\Node\Stmt\Throw_ ||
|
2016-06-21 00:10:24 +02:00
|
|
|
$stmt instanceof PhpParser\Node\Expr\Exit_ ||
|
2016-06-10 20:47:44 +02:00
|
|
|
($check_continue && $stmt instanceof PhpParser\Node\Stmt\Continue_) ||
|
2016-11-02 07:29:00 +01:00
|
|
|
($check_break && $stmt instanceof PhpParser\Node\Stmt\Break_)
|
|
|
|
) {
|
2016-05-09 14:56:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
2016-06-20 22:18:31 +02:00
|
|
|
if ($stmt->else &&
|
|
|
|
self::doesLeaveBlock($stmt->stmts, $check_continue, $check_break) &&
|
2016-11-02 07:29:00 +01:00
|
|
|
self::doesLeaveBlock($stmt->else->stmts, $check_continue, $check_break)
|
|
|
|
) {
|
2016-05-09 14:56:07 +02:00
|
|
|
if (empty($stmt->elseifs)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->elseifs as $elseif) {
|
2016-06-10 20:47:44 +02:00
|
|
|
if (!self::doesLeaveBlock($elseif->stmts, $check_continue, $check_break)) {
|
2016-05-09 14:56:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Switch_) {
|
|
|
|
$has_left = false;
|
2016-05-09 14:56:07 +02:00
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
$has_default_leaver = false;
|
|
|
|
|
|
|
|
// iterate backwards in a case statement
|
|
|
|
for ($i = count($stmt->cases) - 1; $i >= 0; $i--) {
|
|
|
|
$case = $stmt->cases[$i];
|
|
|
|
|
2016-06-30 00:10:41 +02:00
|
|
|
$case_does_leave = self::doesEverBreakOrContinue($case->stmts, true);
|
2016-06-28 19:56:44 +02:00
|
|
|
|
|
|
|
if ($case_does_leave) {
|
|
|
|
$has_left = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$case_does_leave && !$has_left) {
|
2016-05-09 14:56:07 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-28 19:56:44 +02:00
|
|
|
|
|
|
|
if (!$case->cond && $case_does_leave) {
|
|
|
|
$has_default_leaver = true;
|
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
return $has_default_leaver;
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Nop) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-06-17 00:52:12 +02:00
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-12-17 06:48:31 +01:00
|
|
|
* @param array<PhpParser\Node> $stmts
|
2017-02-21 22:52:27 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function doesEverBreak(array $stmts)
|
|
|
|
{
|
|
|
|
if (empty($stmts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = count($stmts) - 1; $i >= 0; $i--) {
|
|
|
|
$stmt = $stmts[$i];
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Break_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
|
|
|
if (self::doesEverBreak($stmt->stmts)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt->else && self::doesEverBreak($stmt->else->stmts)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->elseifs as $elseif) {
|
|
|
|
if (self::doesEverBreak($elseif->stmts)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<PhpParser\Node> $stmts
|
2016-12-17 06:48:31 +01:00
|
|
|
* @param bool $ignore_break
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-06-28 19:56:44 +02:00
|
|
|
public static function doesEverBreakOrContinue(array $stmts, $ignore_break = false)
|
2016-06-20 22:18:31 +02:00
|
|
|
{
|
|
|
|
if (empty($stmts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = count($stmts) - 1; $i >= 0; $i--) {
|
|
|
|
$stmt = $stmts[$i];
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Continue_ ||
|
|
|
|
(!$ignore_break && $stmt instanceof PhpParser\Node\Stmt\Break_)
|
|
|
|
) {
|
2016-06-20 22:18:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
2016-06-28 19:56:44 +02:00
|
|
|
if (self::doesEverBreakOrContinue($stmt->stmts, $ignore_break)) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-06-20 22:18:31 +02:00
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
if ($stmt->else && self::doesEverBreakOrContinue($stmt->else->stmts, $ignore_break)) {
|
2016-06-20 22:18:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
2016-06-28 19:56:44 +02:00
|
|
|
|
|
|
|
foreach ($stmt->elseifs as $elseif) {
|
|
|
|
if (self::doesEverBreakOrContinue($elseif->stmts, $ignore_break)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2016-06-20 22:18:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Switch_) {
|
|
|
|
// iterate backwards
|
|
|
|
// in switch statements we only care here about continue
|
|
|
|
for ($i = count($stmt->cases) - 1; $i >= 0; $i--) {
|
|
|
|
$case = $stmt->cases[$i];
|
2016-06-20 22:18:31 +02:00
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
if (self::doesEverBreakOrContinue($case->stmts, true)) {
|
|
|
|
return true;
|
2016-06-20 22:18:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Nop) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-12-17 06:48:31 +01:00
|
|
|
* @param array<PhpParser\Node> $stmts
|
|
|
|
* @param bool $ignore_break
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-24 23:06:20 +02:00
|
|
|
public static function doesAlwaysBreakOrContinue(array $stmts, $ignore_break = false)
|
|
|
|
{
|
|
|
|
if (empty($stmts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = count($stmts) - 1; $i >= 0; $i--) {
|
|
|
|
$stmt = $stmts[$i];
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Continue_ ||
|
|
|
|
(!$ignore_break && $stmt instanceof PhpParser\Node\Stmt\Break_)
|
|
|
|
) {
|
2016-08-24 23:06:20 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
|
|
|
if (!self::doesAlwaysBreakOrContinue($stmt->stmts, $ignore_break)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$stmt->else || !self::doesAlwaysBreakOrContinue($stmt->else->stmts, $ignore_break)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->elseifs as $elseif) {
|
|
|
|
if (!self::doesAlwaysBreakOrContinue($elseif->stmts, $ignore_break)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Switch_) {
|
|
|
|
// iterate backwards
|
|
|
|
// in switch statements we only care here about continue
|
|
|
|
for ($i = count($stmt->cases) - 1; $i >= 0; $i--) {
|
|
|
|
$case = $stmt->cases[$i];
|
|
|
|
|
|
|
|
if (!self::doesAlwaysBreakOrContinue($case->stmts, true)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Nop) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-12-17 06:48:31 +01:00
|
|
|
* @param array<PhpParser\Node> $stmts
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-08-24 23:06:20 +02:00
|
|
|
public static function doesAlwaysReturnOrThrow(array $stmts)
|
2016-06-20 22:18:31 +02:00
|
|
|
{
|
|
|
|
if (empty($stmts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = count($stmts) - 1; $i >= 0; $i--) {
|
|
|
|
$stmt = $stmts[$i];
|
|
|
|
|
2016-06-21 00:10:24 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Return_ ||
|
|
|
|
$stmt instanceof PhpParser\Node\Stmt\Throw_ ||
|
|
|
|
$stmt instanceof PhpParser\Node\Expr\Exit_
|
|
|
|
) {
|
2016-06-20 22:18:31 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\If_) {
|
2016-11-02 07:29:00 +01:00
|
|
|
if ($stmt->else &&
|
|
|
|
self::doesAlwaysReturnOrThrow($stmt->stmts) &&
|
|
|
|
self::doesAlwaysReturnOrThrow($stmt->else->stmts)
|
|
|
|
) {
|
2016-06-20 22:18:31 +02:00
|
|
|
if (empty($stmt->elseifs)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($stmt->elseifs as $elseif) {
|
2016-08-24 23:06:20 +02:00
|
|
|
if (!self::doesAlwaysReturnOrThrow($elseif->stmts)) {
|
2016-06-20 22:18:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Switch_) {
|
|
|
|
$has_returned = false;
|
2016-06-28 20:28:45 +02:00
|
|
|
$has_default_terminator = false;
|
2016-06-28 19:56:44 +02:00
|
|
|
|
|
|
|
// iterate backwards in a case statement
|
|
|
|
for ($i = count($stmt->cases) - 1; $i >= 0; $i--) {
|
|
|
|
$case = $stmt->cases[$i];
|
2016-06-20 22:18:31 +02:00
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
if (self::doesEverBreakOrContinue($case->stmts)) {
|
2016-06-20 22:18:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-24 23:06:20 +02:00
|
|
|
$case_does_return = self::doesAlwaysReturnOrThrow($case->stmts);
|
2016-06-28 19:56:44 +02:00
|
|
|
|
|
|
|
if ($case_does_return) {
|
|
|
|
$has_returned = true;
|
2016-06-20 22:18:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
if (!$case_does_return && !$has_returned) {
|
|
|
|
return false;
|
2016-06-20 22:18:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
if (!$case->cond && $case_does_return) {
|
|
|
|
$has_default_terminator = true;
|
|
|
|
}
|
2016-06-20 22:18:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-28 19:56:44 +02:00
|
|
|
return $has_default_terminator;
|
2016-06-20 22:18:31 +02:00
|
|
|
}
|
|
|
|
|
2016-06-21 00:10:24 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\While_) {
|
2016-08-24 23:06:20 +02:00
|
|
|
if (self::doesAlwaysReturnOrThrow($stmt->stmts)) {
|
2016-06-21 00:10:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\TryCatch) {
|
2016-08-24 23:06:20 +02:00
|
|
|
if (self::doesAlwaysReturnOrThrow($stmt->stmts)) {
|
2016-06-21 00:10:24 +02:00
|
|
|
foreach ($stmt->catches as $catch) {
|
2016-08-24 23:06:20 +02:00
|
|
|
if (!self::doesAlwaysReturnOrThrow($catch->stmts)) {
|
2016-06-21 00:10:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 22:18:31 +02:00
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Nop) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:29:00 +01:00
|
|
|
/**
|
2016-12-17 06:48:31 +01:00
|
|
|
* @param array<PhpParser\Node> $stmts
|
2016-11-02 07:29:00 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-06-17 00:52:12 +02:00
|
|
|
public static function onlyThrows(array $stmts)
|
|
|
|
{
|
|
|
|
if (empty($stmts)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for ($i = count($stmts) - 1; $i >= 0; $i--) {
|
|
|
|
$stmt = $stmts[$i];
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Throw_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($stmt instanceof PhpParser\Node\Stmt\Nop) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2016-05-09 14:56:07 +02:00
|
|
|
}
|