CapsLock-Studio/FilterClass

View on GitHub
php-filter-class

Summary

Maintainability
Test Coverage
#!/usr/bin/php
<?php

if (file_exists(__DIR__.'/../../autoload.php')) {
    require __DIR__.'/../../autoload.php';
} else {
    require __DIR__.'/vendor/autoload.php';
}

$default = ["f" => getcwd(), "t" => getcwd()];
$opts    = getopt("f:t:");
$opts    = array_merge($default, $opts);

$fromFolder = $opts["f"];
$toFolder   = $opts["t"];

$used   = [];
$unused = [];
$lines  = [];
$total  = 0;
$dead   = 0;

$filter = new CapsLockStudio\FilterClass\ClassAnalyzer([
    "fromPath" => $fromFolder,
    "toPath"   => $toFolder,
]);

$filter->analyze();

$unused = $filter->getUnusedCode();
$total  = $filter->getTotal();
$lines  = array_merge($lines, $filter->getLines());

$processed = [];
foreach ($unused as $class => $method) {
    $processed[$class] = isset($processed[$class]) ? $processed[$class] : $method;
    $processed[$class] = array_intersect($processed[$class], $method);
}

foreach ($processed as $class => $methods) {
    if ($methods) {
        echo "{$class}:", PHP_EOL;
        foreach ($methods as $method) {
            echo "\t{$method}", PHP_EOL;
            $dead += $lines[$class][$method];
        }

        echo PHP_EOL;
    }
}

echo "-----------------------------------", PHP_EOL;
echo "Dead code hit report(estimate):", PHP_EOL, PHP_EOL;
echo "dead code lines: {$dead}", PHP_EOL;
echo "total lines: {$total}", PHP_EOL;
echo "dead code percentage: " . (sprintf("%.2f", $total ? $dead / $total * 100 : 0)) . "%", PHP_EOL;