<commit_before><?php declare(strict_types=1);

namespace Wnx\LaravelStats\ShareableMetrics;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Wnx\LaravelStats\Classifier;
use Wnx\LaravelStats\Project;
use Wnx\LaravelStats\ShareableMetrics\Metrics\CodeTestRatio;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ComposerPsr4Sources;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ControllersCustomInheritance;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ControllersFormRequestInjection;
use Wnx\LaravelStats\ShareableMetrics\Metrics\InstalledPackages;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ModelsCustomInheritance;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ModelsFolder;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ModelsMassAssignment;
use Wnx\LaravelStats\ShareableMetrics\Metrics\NumberOfRoutes;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ProjectLinesOfCode;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ProjectLogicalLinesOfCode;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ProjectLogicalLinesOfCodePerMethod;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ProjectNumberOfClasses;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ScheduledTasks;
use Wnx\LaravelStats\ValueObjects\Component;

class CollectMetrics
{
    public function collect(Project $project): MetricsCollection
    {
        return new MetricsCollection([
            'project_metrics' => $this->getProjectMetrics($project),
            'component_metrics' => $this->getComponentMetrics($project)
        ]);
    }

    protected function getProjectMetrics(Project $project): Collection
    {
        $availableMetrics = collect([
            CodeTestRatio::class,
            ComposerPsr4Sources::class,
            ControllersCustomInheritance::class,
            ControllersFormRequestInjection::class,
            InstalledPackages::class,
            ModelsCustomInheritance::class,
            ModelsFolder::class,
            ModelsMassAssignment::class,
            NumberOfRoutes::class,
            ProjectLinesOfCode::class,
            ProjectLogicalLinesOfCode::class,
            ProjectLogicalLinesOfCodePerMethod::class,
            ProjectNumberOfClasses::class,
            ScheduledTasks::class,
        ])->map(function ($statClass) use ($project) {
            return new $statClass($project);
        });

        return $availableMetrics->map->toArray()->collapse();
    }

    protected function getComponentMetrics(Project $project): Collection
    {
        // Get the Names of "Core"-Components
        $coreClassifierNames = array_map(function ($classifier) {
            return (new $classifier)->name();
        }, Classifier::DEFAULT_CLASSIFIER);

        // Group Into Components
        $groupedByComponent = $project->classifiedClassesGroupedAndFilteredByComponentNames($coreClassifierNames)
            ->map(function ($classifiedClasses, $componentName) {
                return new Component($componentName, $classifiedClasses);
            });

        $metrics = [];

        /** @var Component $component */
        foreach ($groupedByComponent as $component) {
            $slug = Str::slug(strtolower($component->name), '_');

            $metrics["{$slug}"] = $component->getNumberOfClasses();
            $metrics["{$slug}_methods"] = $component->getNumberOfMethods();
            $metrics["{$slug}_loc"] = $component->getLinesOfCode();
            $metrics["{$slug}_lloc"] = $component->getLogicalLinesOfCode();<commit_msg>Add PROJECT_METRICS Constant<commit_after><?php declare(strict_types=1);

namespace Wnx\LaravelStats\ShareableMetrics;

use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Wnx\LaravelStats\Classifier;
use Wnx\LaravelStats\Project;
use Wnx\LaravelStats\ShareableMetrics\Metrics\CodeTestRatio;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ComposerPsr4Sources;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ControllersCustomInheritance;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ControllersFormRequestInjection;
use Wnx\LaravelStats\ShareableMetrics\Metrics\InstalledPackages;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ModelsCustomInheritance;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ModelsFolder;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ModelsMassAssignment;
use Wnx\LaravelStats\ShareableMetrics\Metrics\NumberOfRoutes;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ProjectLinesOfCode;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ProjectLogicalLinesOfCode;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ProjectLogicalLinesOfCodePerMethod;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ProjectNumberOfClasses;
use Wnx\LaravelStats\ShareableMetrics\Metrics\ScheduledTasks;
use Wnx\LaravelStats\ValueObjects\Component;

class CollectMetrics
{
    public const PROJECT_METRICS = [
        CodeTestRatio::class,
        ComposerPsr4Sources::class,
        ControllersCustomInheritance::class,
        ControllersFormRequestInjection::class,
        InstalledPackages::class,
        ModelsCustomInheritance::class,
        ModelsFolder::class,
        ModelsMassAssignment::class,
        NumberOfRoutes::class,
        ProjectLinesOfCode::class,
        ProjectLogicalLinesOfCode::class,
        ProjectLogicalLinesOfCodePerMethod::class,
        ProjectNumberOfClasses::class,
        ScheduledTasks::class,
    ];

    public function collect(Project $project): MetricsCollection
    {
        return new MetricsCollection([
            'project_metrics' => $this->getProjectMetrics($project),
            'component_metrics' => $this->getComponentMetrics($project)
        ]);
    }

    protected function getProjectMetrics(Project $project): Collection
    {
        return collect(self::PROJECT_METRICS)
            ->map(function ($metricClass) use ($project) {
                return new $metricClass($project);
            })
            ->map
            ->toArray()
            ->collapse();
    }

    protected function getComponentMetrics(Project $project): Collection
    {
        // Get the Names of "Core"-Components
        $coreClassifierNames = array_map(function ($classifier) {
            return (new $classifier)->name();
        }, Classifier::DEFAULT_CLASSIFIER);

        // Group Into Components
        $groupedByComponent = $project->classifiedClassesGroupedAndFilteredByComponentNames($coreClassifierNames)
            ->map(function ($classifiedClasses, $componentName) {
                return new Component($componentName, $classifiedClasses);
            });

        $metrics = [];

        /** @var Component $component */
        foreach ($groupedByComponent as $component) {
            $slug = Str::slug(strtolower($component->name), '_');

            $metrics["{$slug}"] = $component->getNumberOfClasses();
            $metrics["{$slug}_methods"] = $component->getNumberOfMethods();
            $metrics["{$slug}_loc"] = $component->getLinesOfCode();
            $metrics["{$slug}_lloc"] = $component->getLogicalLinesOfCode();
{'fraction_non_alphanumeric': 0.10496128477201032, 'fraction_numerical': 0.0008603383997705764, 'mean_word_length': 3.8203178991015894, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4809873', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1874, 'n_words': 271}