rector/rules/CodingStyle/Application/UseImportsRemover.php
Tomas Votruba 727b9f46f0 Updated Rector to commit bfa1891c50677b01136a9308fd3c3ecc12e267d9
bfa1891c50 [cleanup] Remove 73 unused public methods (#3245)
2022-12-23 17:10:25 +00:00

43 lines
1.1 KiB
PHP

<?php
declare (strict_types=1);
namespace Rector\CodingStyle\Application;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Use_;
final class UseImportsRemover
{
/**
* @param Stmt[] $stmts
* @param string[] $removedShortUses
* @return Stmt[]
*/
public function removeImportsFromStmts(array $stmts, array $removedShortUses) : array
{
foreach ($stmts as $stmtKey => $stmt) {
if (!$stmt instanceof Use_) {
continue;
}
$this->removeUseFromUse($removedShortUses, $stmt);
// nothing left → remove
if ($stmt->uses === []) {
unset($stmts[$stmtKey]);
}
}
return $stmts;
}
/**
* @param string[] $removedShortUses
*/
private function removeUseFromUse(array $removedShortUses, Use_ $use) : void
{
foreach ($use->uses as $usesKey => $useUse) {
foreach ($removedShortUses as $removedShortUse) {
if ($useUse->name->toString() === $removedShortUse) {
unset($use->uses[$usesKey]);
}
}
}
}
}