Commit b67fcd4c authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Détection des traits déclarés et non utilisés

parent 6be48884
Loading
Loading
Loading
Loading
+70 −0
Original line number Diff line number Diff line
<?php

/**
 * @var $this       \Application\View\Renderer\PhpRenderer
 * @var $controller \Laminas\Mvc\Controller\AbstractController
 * @var $container  \Psr\Container\ContainerInterface
 * @var $viewName   string
 * @var $viewFile   string
 */

?>
    <h1>Traits déclarés et non utilisés</h1>

    <div class="alert alert-warning">Attention : dans certains cas, le trait n'est pas utilisé dans le fichier courant, mais utilisé de manière déportée. Dans
        ce cas, il ne faut pas supprimer les "use".
    </div>
    <div class="alert alert-warning">Attention aussi : les "use" avec des "as" sont mal gérés : fichiers à purger avec parcimonie, donc!
    </div>

<?php
$introspection = \UnicaenCode\Util::introspection();


$traits = array_flip($introspection->getTraits());
foreach ($traits as $trait => $null) {
    $traitParams    = $introspection->getTraitParams($trait);
    $traits[$trait] = $traitParams;
    if (!$traitParams['aware']) {
        unset($traits[$trait]);
    }
    if (str_contains($traitParams['class'], '\Entity')) {
        unset($traits[$trait]);
    }
}


$modules = $introspection->getModules();
foreach ($modules as $module) {
    $files = $introspection->getSrcs($module['name'], true);
    echo '<h2>' . $module['name'] . '</h2>';

    echo '<table class="table table-condensed table-bordered table-hover">';
    echo '<tr><th>Fichier</th><th>Trait</th><td>Non déclaré</td><td>Non utilisé</td></tr>';
    foreach ($files as $file) {
        $c    = file_get_contents($file);
        $file = substr($file, strlen($module['absPath']) + 2 + strlen($module['srcDir']));
        foreach ($traits as $trait) {
            if (str_contains($c, $trait['class'])) {

                $class = 'use ' . \UnicaenCode\Util::classClassname($trait['class']) . ';';

                $notDeclared = !str_contains($c, $class);

                $notUsed = true;
                if ($trait['getter'] && str_contains($c, '$this->' . $trait['getter'] . '(')) {
                    $notUsed = false;
                }
                if ($trait['setter'] && str_contains($c, '$this->' . $trait['setter'] . '(')) {
                    $notUsed = false;
                }


                if ($notDeclared || $notUsed) {
                    echo '<tr><td>' . $file . '</td><td>' . $trait['class'] . '</td><td>' . ($notDeclared ? 'X' : '') . '</td><td>' . ($notUsed ? 'X' : '') . '</td></tr>';
                }
            }
        }
    }
    echo '</table>';
}
 No newline at end of file
+35 −4
Original line number Diff line number Diff line
@@ -177,7 +177,7 @@ class IntrospectionService
            }
        }

        return $cti;
        return array_values($cti);
    }


@@ -191,7 +191,7 @@ class IntrospectionService
            }
        }

        return $cti;
        return array_values($cti);
    }


@@ -205,7 +205,7 @@ class IntrospectionService
            }
        }

        return $cti;
        return array_values($cti);
    }


@@ -249,7 +249,6 @@ class IntrospectionService
    public function getViews(string $modulename): array
    {
        $module       = $this->getModule($modulename);
        $viewDir      = '/view';
        $templatePath = $module['absPath'] . '/' . $module['viewDir'];

        $rdi = new \RecursiveDirectoryIterator(
@@ -272,6 +271,36 @@ class IntrospectionService



    public function getSrcs(string $modulename, bool $absPaths = false): array
    {
        $module       = $this->getModule($modulename);
        $templatePath = $module['absPath'] . '/' . $module['srcDir'];

        $rdi = new \RecursiveDirectoryIterator(
            $templatePath,
            \RecursiveDirectoryIterator::FOLLOW_SYMLINKS | \RecursiveDirectoryIterator::SKIP_DOTS
        );
        $rii = new \RecursiveIteratorIterator($rdi, \RecursiveIteratorIterator::LEAVES_ONLY);

        $srcs = [];
        foreach ($rii as $file) {
            if (strtolower($file->getExtension()) != 'php') {
                continue;
            }

            if ($absPaths){
                $srcs[] = $file->getPathname();
            }else{
                $srcs[] = substr($file->getPathname(), strlen($templatePath . '/'));
            }

        }

        return $srcs;
    }



    /**
     * Retourne la liste des aides de vues accessibles depuis le ServiceManager et correspondant aux critères suivants :
     * Les chaînes retournées sont les noms de classe des aides de vues
@@ -415,6 +444,8 @@ class IntrospectionService
            'relPath'   => substr($path, strlen($appPath) + 1),
            'inVendor'  => $inVendor,
            'viewDir'   => 'view',
            'srcDir'    => 'src',
            'configDir' => 'config',
        ];
    }
}
 No newline at end of file