2020-12-24 00:44:16 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-12-24 00:44:16 +01:00
|
|
|
namespace Rector\Php80\NodeAnalyzer;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2022-04-24 15:27:24 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2020-12-24 00:44:16 +01:00
|
|
|
use PhpParser\Node\Stmt\Break_;
|
2021-06-08 08:23:35 +00:00
|
|
|
use PhpParser\Node\Stmt\Case_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2020-12-24 00:44:16 +01:00
|
|
|
use PhpParser\Node\Stmt\Switch_;
|
2022-04-24 15:27:24 +00:00
|
|
|
use Rector\NodeTypeResolver\NodeTypeResolver;
|
|
|
|
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
|
2020-12-24 00:44:16 +01:00
|
|
|
final class SwitchAnalyzer
|
|
|
|
{
|
2022-04-24 15:27:24 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\NodeTypeResolver\NodeTypeResolver
|
|
|
|
*/
|
|
|
|
private $nodeTypeResolver;
|
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory
|
|
|
|
*/
|
|
|
|
private $typeFactory;
|
2022-05-27 11:51:31 +00:00
|
|
|
public function __construct(\Rector\NodeTypeResolver\NodeTypeResolver $nodeTypeResolver, \Rector\NodeTypeResolver\PHPStan\Type\TypeFactory $typeFactory)
|
2022-04-24 15:27:24 +00:00
|
|
|
{
|
|
|
|
$this->nodeTypeResolver = $nodeTypeResolver;
|
|
|
|
$this->typeFactory = $typeFactory;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Case_[] $cases
|
|
|
|
*/
|
|
|
|
public function hasDifferentTypeCases(array $cases) : bool
|
|
|
|
{
|
|
|
|
$types = [];
|
|
|
|
foreach ($cases as $case) {
|
2022-05-27 11:51:31 +00:00
|
|
|
if ($case->cond instanceof \PhpParser\Node\Expr) {
|
2022-04-24 15:27:24 +00:00
|
|
|
$types[] = $this->nodeTypeResolver->getType($case->cond);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($types === []) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
$uniqueTypes = $this->typeFactory->uniquateTypes($types);
|
|
|
|
return \count($uniqueTypes) > 1;
|
|
|
|
}
|
2022-05-27 11:51:31 +00:00
|
|
|
public function hasEachCaseBreak(\PhpParser\Node\Stmt\Switch_ $switch) : bool
|
2020-12-24 00:44:16 +01:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
$totalCases = \count($switch->cases);
|
2021-02-21 21:34:57 +07:00
|
|
|
if ($totalCases === 1) {
|
2021-06-08 13:06:20 +00:00
|
|
|
return $this->containsCaseReturn($switch->cases[0]);
|
2021-02-21 21:34:57 +07:00
|
|
|
}
|
|
|
|
foreach ($switch->cases as $key => $case) {
|
|
|
|
if ($key === $totalCases - 1) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-12-24 00:44:16 +01:00
|
|
|
}
|
2021-06-08 11:02:16 +00:00
|
|
|
if ($this->hasBreakOrReturnOrEmpty($case)) {
|
2021-06-08 08:23:35 +00:00
|
|
|
continue;
|
2021-02-21 21:34:57 +07:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2020-12-24 00:44:16 +01:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-12-24 00:44:16 +01:00
|
|
|
}
|
2022-05-27 11:51:31 +00:00
|
|
|
public function hasEachCaseSingleStmt(\PhpParser\Node\Stmt\Switch_ $switch) : bool
|
2020-12-24 00:44:16 +01:00
|
|
|
{
|
|
|
|
foreach ($switch->cases as $case) {
|
2022-03-31 11:04:24 +00:00
|
|
|
if ($case->cond === null) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-05-27 11:51:31 +00:00
|
|
|
$stmtsWithoutBreak = \array_filter($case->stmts, function (\PhpParser\Node $node) : bool {
|
|
|
|
return !$node instanceof \PhpParser\Node\Stmt\Break_;
|
2020-12-24 00:44:16 +01:00
|
|
|
});
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($stmtsWithoutBreak) !== 1) {
|
|
|
|
return \false;
|
2020-12-24 00:44:16 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2020-12-24 00:44:16 +01:00
|
|
|
}
|
2022-05-27 11:51:31 +00:00
|
|
|
public function hasDefaultSingleStmt(\PhpParser\Node\Stmt\Switch_ $switch) : bool
|
2021-06-07 21:47:57 +00:00
|
|
|
{
|
|
|
|
foreach ($switch->cases as $case) {
|
|
|
|
if ($case->cond === null) {
|
2022-05-27 11:51:31 +00:00
|
|
|
$stmtsWithoutBreak = \array_filter($case->stmts, function (\PhpParser\Node $node) : bool {
|
|
|
|
return !$node instanceof \PhpParser\Node\Stmt\Break_;
|
2022-03-31 11:04:24 +00:00
|
|
|
});
|
|
|
|
return \count($stmtsWithoutBreak) === 1;
|
2021-06-07 21:47:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return \false;
|
|
|
|
}
|
2022-05-27 11:51:31 +00:00
|
|
|
private function hasBreakOrReturnOrEmpty(\PhpParser\Node\Stmt\Case_ $case) : bool
|
2021-06-08 08:23:35 +00:00
|
|
|
{
|
2021-06-08 11:02:16 +00:00
|
|
|
if ($case->stmts === []) {
|
|
|
|
return \true;
|
|
|
|
}
|
2021-06-08 08:23:35 +00:00
|
|
|
foreach ($case->stmts as $caseStmt) {
|
2022-05-27 11:51:31 +00:00
|
|
|
if ($caseStmt instanceof \PhpParser\Node\Stmt\Break_) {
|
2021-06-08 08:23:35 +00:00
|
|
|
return \true;
|
|
|
|
}
|
2022-05-27 11:51:31 +00:00
|
|
|
if ($caseStmt instanceof \PhpParser\Node\Stmt\Return_) {
|
2021-06-08 08:23:35 +00:00
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \false;
|
|
|
|
}
|
2022-05-27 11:51:31 +00:00
|
|
|
private function containsCaseReturn(\PhpParser\Node\Stmt\Case_ $case) : bool
|
2021-06-08 13:06:20 +00:00
|
|
|
{
|
|
|
|
foreach ($case->stmts as $stmt) {
|
2022-05-27 11:51:31 +00:00
|
|
|
if ($stmt instanceof \PhpParser\Node\Stmt\Return_) {
|
2021-06-08 13:06:20 +00:00
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return \false;
|
|
|
|
}
|
2020-12-24 00:44:16 +01:00
|
|
|
}
|