Issues (14)

check-top-packagist.php (1 issue)

Labels
Severity
1
#!/usr/bin/env php
2
<?php
3
4
declare(strict_types=1);
5
6
declare(ticks=1); // ensures signal checks happen between statements
7
8
require __DIR__ . '/vendor/autoload.php';
9
10
use SavinMikhail\DistSizeOptimizer\Command\CheckCommand;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Output\BufferedOutput;
13
14
const ANALYZED_FILE = __DIR__ . '/var/analyzed.json';
15
const RESULT_FILE = __DIR__ . '/var/results.json';
16
17
$interrupted = false;
18
19
pcntl_signal(SIGINT, static function () use (&$interrupted): void {
20
    echo "\n⛔️ Interrupted. Finishing up...\n";
21
    $interrupted = true;
22
});
23
24
function loadAnalyzed(): array
25
{
26
    if (!file_exists(ANALYZED_FILE)) {
27
        return [];
28
    }
29
30
    return json_decode(file_get_contents(ANALYZED_FILE), true, 512, JSON_THROW_ON_ERROR);
31
}
32
33
function fetchTopPackages(int $limit = 1_000): array
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_STRING, expecting ')' on line 33 at column 40
Loading history...
34
{
35
    $packages = [];
36
    $perPage = 100;
37
    $pages = (int) ceil($limit / $perPage);
38
39
    for ($page = 1; $page <= $pages; ++$page) {
40
        $url = "https://packagist.org/explore/popular.json?per_page={$perPage}&page={$page}";
41
        $json = file_get_contents($url);
42
43
        if (!$json) {
44
            throw new RuntimeException("Failed to fetch Packagist data for page {$page}");
45
        }
46
47
        $data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
48
        $pagePackages = array_map(static fn($pkg) => $pkg['name'], $data['packages']);
49
        $packages = array_merge($packages, $pagePackages);
50
51
        // Stop early if we already have enough
52
        if (count($packages) >= $limit) {
53
            break;
54
        }
55
    }
56
57
    return array_slice($packages, 0, $limit);
58
}
59
60
function runExportIgnoreCheck(string $package): array
61
{
62
    $command = new CheckCommand();
63
    $input = new ArrayInput([
64
        'package' => $package,
65
        '--json' => true,
66
    ]);
67
    $output = new BufferedOutput();
68
69
    try {
70
        $exitCode = $command->run($input, $output);
71
        $result = json_decode($output->fetch(), true);
72
73
        if ($exitCode === 0) {
74
            return ['status' => '✅ OK', 'details' => null];
75
        }
76
77
        if (isset($result['files']) && count($result['files']) > 0 || isset($result['directories']) && count($result['directories']) > 0) {
78
            return ['status' => '❌ Missing export-ignore', 'details' => $result];
79
        }
80
81
        return ['status' => '⚠️ Failed', 'details' => null];
82
    } catch (Exception $e) {
83
        return ['status' => '💥 Error: ' . $e->getMessage(), 'details' => null];
84
    }
85
}
86
87
function saveFailures(array $failures): void
88
{
89
    if (count($failures) === 0) {
90
        echo "\n✅ No export-ignore issues found. Great job, open source!\n";
91
92
        return;
93
    }
94
95
    file_put_contents(RESULT_FILE, json_encode($failures, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
96
    echo "\n📝 Saved detailed results to: " . RESULT_FILE . "\n";
97
}
98
99
echo "🔍 Checking top Packagist packages...\n";
100
101
$packages = fetchTopPackages(1_000);
102
103
$results = [];
104
$failures = [];
105
106
$total = count($packages);
107
$analyzed = loadAnalyzed();
108
$newPackages = array_filter($packages, static fn($p) => !isset($analyzed[$p]));
109
110
foreach (array_values($newPackages) as $i => $package) {
111
    if ($interrupted) {
112
        break;
113
    }
114
115
    echo "📦 [{$i}/{$total}] Checking {$package}...\n";
116
    $check = runExportIgnoreCheck($package);
117
    $results[$package] = $check['status'];
118
119
    if ($check['status'] === '❌ Missing export-ignore' && isset($check['details'])) {
120
        $failures[$package] = $check['details'];
121
    }
122
}
123
124
echo "\n📊 Results:\n";
125
foreach ($results as $package => $status) {
126
    echo "  {$package}: {$status}\n";
127
}
128
129
saveFailures($failures);
130
131
$allAnalyzed = array_merge($analyzed, $results);
132
file_put_contents(ANALYZED_FILE, json_encode($allAnalyzed, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
133