|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codebuddyphp\Codebuddy\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Foundation\Application; |
| 7 | +use Symfony\Component\Process\Process; |
| 8 | + |
| 9 | +use function Termwind\render; |
| 10 | + |
| 11 | +class Report extends Command |
| 12 | +{ |
| 13 | + protected $signature = 'codebuddy:report'; |
| 14 | + |
| 15 | + protected $description = 'Calculate the Codebuddy Score for your codebase'; |
| 16 | + |
| 17 | + public function handle(): int |
| 18 | + { |
| 19 | + // @phpstan-ignore-next-line |
| 20 | + render(view('codebuddy::banner')); |
| 21 | + |
| 22 | + $this->info('🔍 Running Codebuddy analysis...'); |
| 23 | + |
| 24 | + // Version Check |
| 25 | + $this->checkVersions(); |
| 26 | + |
| 27 | + // Code Quality Scores |
| 28 | + $scores = [ |
| 29 | + 'formatting' => $this->runPint(), |
| 30 | + 'static_analysis' => $this->runPHPStan(), |
| 31 | + 'code_refactoring' => $this->runRector(), |
| 32 | + 'test_coverage' => $this->runPestCoverage(), |
| 33 | + ]; |
| 34 | + |
| 35 | + $totalScore = round(array_sum($scores) / count($scores)); |
| 36 | + |
| 37 | + $this->line("\n🚀 <info>Codebuddy Score: {$totalScore}/100</info>"); |
| 38 | + $this->table(['Metric', 'Score (%)'], [ |
| 39 | + ['Code Formatting (Pint)', $scores['formatting']], |
| 40 | + ['Static Analysis (PHPStan)', $scores['static_analysis']], |
| 41 | + ['Code Refactoring (Rector)', $scores['code_refactoring']], |
| 42 | + ['Test Coverage (PestPHP)', $scores['test_coverage']], |
| 43 | + ]); |
| 44 | + |
| 45 | + return Command::SUCCESS; |
| 46 | + } |
| 47 | + |
| 48 | + private function checkVersions(): void |
| 49 | + { |
| 50 | + $phpVersion = phpversion(); |
| 51 | + $laravelVersion = Application::VERSION; |
| 52 | + |
| 53 | + $this->info("\n🔍 Checking PHP and Laravel versions..."); |
| 54 | + |
| 55 | + $latestPhpVersion = $this->getLatestPhpVersion(); |
| 56 | + $latestLaravelVersion = $this->getLatestLaravelVersion(); |
| 57 | + |
| 58 | + $this->checkVersion('PHP', $phpVersion, $latestPhpVersion); |
| 59 | + $this->checkVersion('Laravel', $laravelVersion, $latestLaravelVersion); |
| 60 | + } |
| 61 | + |
| 62 | + private function checkVersion(string $name, string $current, ?string $latest) |
| 63 | + { |
| 64 | + if (! $latest) { |
| 65 | + $this->warn("⚠️ Unable to fetch the latest $name version."); |
| 66 | + |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (version_compare($current, $latest, '<')) { |
| 71 | + $majorDiff = explode('.', $latest)[0] - explode('.', $current)[0]; |
| 72 | + |
| 73 | + if ($majorDiff >= 2) { |
| 74 | + $this->error("🚨 $name is very outdated! ($current) - Latest: $latest"); |
| 75 | + } elseif ($majorDiff === 1) { |
| 76 | + $this->warn("⚠️ $name is slightly outdated ($current) - Latest: $latest"); |
| 77 | + } else { |
| 78 | + $this->info("✅ $name is up-to-date ($current)"); |
| 79 | + } |
| 80 | + } else { |
| 81 | + $this->info("✅ $name is up-to-date ($current)"); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private function getLatestPhpVersion(): ?string |
| 86 | + { |
| 87 | + try { |
| 88 | + $response = file_get_contents('https://www.php.net/releases/index.php?json'); |
| 89 | + $data = json_decode($response, true); |
| 90 | + |
| 91 | + if (! $data || ! is_array($data)) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + // Find the highest major version available |
| 96 | + $latestMajorVersion = max(array_keys($data)); |
| 97 | + |
| 98 | + // Fetch the latest version for that major version |
| 99 | + return $data[$latestMajorVersion]['version'] ?? null; |
| 100 | + } catch (\Exception $e) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private function getLatestLaravelVersion(): ?string |
| 106 | + { |
| 107 | + try { |
| 108 | + $response = file_get_contents('https://repo.packagist.org/p2/laravel/framework.json'); |
| 109 | + $data = json_decode($response, true); |
| 110 | + |
| 111 | + return $data['packages']['laravel/framework'][0]['version'] ?? null; |
| 112 | + } catch (\Exception $e) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private function runPint(): int |
| 118 | + { |
| 119 | + return $this->executeCommand(['vendor/bin/pint', '--test']) ? 100 : 80; |
| 120 | + } |
| 121 | + |
| 122 | + private function runPHPStan(): int |
| 123 | + { |
| 124 | + return $this->executeCommand(['vendor/bin/phpstan', 'analyse', '--level=max']) ? 90 : 60; |
| 125 | + } |
| 126 | + |
| 127 | + private function runRector(): int |
| 128 | + { |
| 129 | + return $this->executeCommand(['vendor/bin/rector', 'process', '--dry-run']) ? 95 : 70; |
| 130 | + } |
| 131 | + |
| 132 | + private function runPestCoverage(): int |
| 133 | + { |
| 134 | + return $this->executeCommand(['vendor/bin/pest', '--coverage']) ? 85 : 50; |
| 135 | + } |
| 136 | + |
| 137 | + private function executeCommand(array $command): bool |
| 138 | + { |
| 139 | + $process = new Process($command); |
| 140 | + $process->run(); |
| 141 | + |
| 142 | + return $process->isSuccessful(); |
| 143 | + } |
| 144 | +} |
0 commit comments