PSR-12, build script, fixes

This commit is contained in:
2022-12-24 00:55:13 +02:00
parent 348fc6f785
commit cf87d02ecb
15 changed files with 648 additions and 593 deletions

View File

@@ -6,7 +6,8 @@ use App\Dto\PackagistApiPackagePayload;
use Illuminate\Support\Facades\Http;
use LaravelZero\Framework\Commands\Command;
class CheckCommand extends Command {
class CheckCommand extends Command
{
protected $signature = 'check
{vendor : Package vendor (required)}
{package : Package name (required)}
@@ -17,119 +18,128 @@ class CheckCommand extends Command {
private string $vendor = '';
private string $package = '';
private string $filter = '';
private int $total_branches = 0;
private int $totalBranches = 0;
public function handle() : int {
$this->vendor = (string) $this->argument( 'vendor' );
$this->package = (string) $this->argument( 'package' );
$months = (int) $this->argument( 'months' );
public function handle(): int
{
$this->vendor = (string)$this->argument('vendor');
$this->package = (string)$this->argument('package');
$months = (int)$this->argument('months');
$this->info( 'Checking: ' . sprintf( '%s/%s', $this->vendor, $this->package ) );
$this->info( 'Months: ' . $months );
$this->info('Checking: ' . sprintf('%s/%s', $this->vendor, $this->package));
$this->info('Months: ' . $months);
$payload = Http::get( sprintf(
'https://packagist.org/packages/%s/%s.json',
$this->vendor,
$this->package
) );
$payload = Http::get(
sprintf(
'https://packagist.org/packages/%s/%s.json',
$this->vendor,
$this->package
)
);
$this->filter = now()->subMonths( $months )->day( 1 )->toDateString();
$this->filter = now()->subMonths($months)->day(1)->toDateString();
try {
$pkg = new PackagistApiPackagePayload( $payload->json() );
$this->info( 'Found the package. Type: ' . $pkg->type );
$pkg = new PackagistApiPackagePayload($payload->json());
$this->info('Found the package. Type: ' . $pkg->type);
$versions = collect( $pkg->versions ?? [] )
$versions = collect($pkg->versions ?? [])
->keys()
// Filter actual versions out.
->filter( fn( $version ) => \str_starts_with( $version, 'dev-' ) )
->filter(fn($version) => \str_starts_with($version, 'dev-'))
->sort();
$this->total_branches = $versions->count();
$this->totalBranches = $versions->count();
$this->info( sprintf(
'Package has %d branches. Starting to download statistics.',
$this->total_branches
) );
$this->info(
sprintf(
'Package has %d branches. Starting to download statistics.',
$this->totalBranches
)
);
$statistics = collect( $versions )
->mapWithKeys( fn( $branch ) => $this->get_statistics( $branch ) )
$statistics = collect($versions)
->mapWithKeys(fn($branch) => $this->getStatistics($branch))
->toArray();
$this->info( 'Downloaded statistics...' );
$this->info('Downloaded statistics...');
$this->output_table( $statistics );
$this->output_suggestions( $statistics );
}
catch ( \Exception $e ) {
$this->error( $e->getMessage(), $e );
$this->outputTable($statistics);
$this->outputSuggestions($statistics);
} catch (\Exception $e) {
$this->error($e->getMessage(), $e);
}
return 0;
}
private function get_statistics( $branch ) : array {
$payload = Http::get( sprintf(
'https://packagist.org/packages/%s/%s/stats/%s.json?average=monthly&from=%s',
$this->vendor,
$this->package,
$branch,
$this->filter
) );
private function getStatistics($branch): array
{
$payload = Http::get(
sprintf(
'https://packagist.org/packages/%s/%s/stats/%s.json?average=monthly&from=%s',
$this->vendor,
$this->package,
$branch,
$this->filter
)
);
$data = collect( $payload->json() );
$labels = collect( $data->get( 'labels', [] ) )->toArray();
$values = collect( $data->get( 'values', [] ) )->flatten()->toArray();
$data = collect($payload->json());
$labels = collect($data->get('labels', []))->toArray();
$values = collect($data->get('values', []))->flatten()->toArray();
$labels[] = 'Total';
$values[] = array_sum( $values );
$values[] = array_sum($values);
return [ $branch => \array_combine( $labels, $values ) ];
return [$branch => \array_combine($labels, $values)];
}
private function output_table( array $statistics ) : void {
if ( empty( $statistics ) ) {
$this->info( 'No statistics found... Stopping.' );
exit( 0 );
private function outputTable(array $statistics): void
{
if (empty($statistics)) {
$this->info('No statistics found... Stopping.');
exit(0);
}
$tableHeaders = [ '' => 'Branch' ];
$tableHeaders = ['' => 'Branch'];
$tableBranches = [];
foreach ( $statistics as $branch => $stats ) {
foreach ( $stats as $m => $v ) {
$tableHeaders[ $m ] = (string) $m;
$tableBranches[ $branch ][ $branch ] = $branch;
$tableBranches[ $branch ][ $m ] = (string) $v;
foreach ($statistics as $branch => $stats) {
foreach ($stats as $m => $v) {
$tableHeaders[$m] = (string)$m;
$tableBranches[$branch][$branch] = $branch;
$tableBranches[$branch][$m] = (string)$v;
}
}
$this->line('');
$this->table( $tableHeaders, $tableBranches );
$this->table($tableHeaders, $tableBranches);
}
private function output_suggestions( array $statistics = [] ) : void {
private function outputSuggestions(array $statistics = []): void
{
$deletable = [];
if ( empty( $statistics ) ) {
$this->info( 'No statistics to give suggestions for. Quitting...' );
exit( 0 );
if (empty($statistics)) {
$this->info('No statistics to give suggestions for. Quitting...');
exit(0);
}
foreach ( $statistics as $k => $values ) {
if ( ! empty( $values['Total'] ) ) {
foreach ($statistics as $k => $values) {
if (!empty($values['Total'])) {
continue;
}
$deletable[ $k ] = $values['Total'];
$deletable[$k] = $values['Total'];
}
if ( empty( $deletable ) ) {
$this->info( 'No suggestions available. Good job!' );
exit( 0 );
if (empty($deletable)) {
$this->info('No suggestions available. Good job!');
exit(0);
}
$keys = array_keys( $deletable );
$keys = array_keys($deletable);
$branches = collect( $keys )->mapWithKeys( function ( $branch ) {
$branches = collect($keys)->mapWithKeys(function ($branch) {
return [
$branch => [
$branch,
@@ -141,15 +151,17 @@ class CheckCommand extends Command {
),
],
];
} );
});
$this->line('');
$this->info( sprintf(
'Found %d branches (out of %d total) with no downloads since %s',
$branches->count(),
$this->total_branches,
$this->filter
) );
$this->table( [ 'Branch', 'URL' ], $branches );
$this->info(
sprintf(
'Found %d branches (out of %d total) with no downloads since %s',
$branches->count(),
$this->totalBranches,
$this->filter
)
);
$this->table(['Branch', 'URL'], $branches);
}
}

View File

@@ -4,6 +4,7 @@ namespace App\Commands;
use Illuminate\Console\Scheduling\Schedule;
use LaravelZero\Framework\Commands\Command;
use function Termwind\{render};
class InspireCommand extends Command
@@ -29,20 +30,23 @@ class InspireCommand extends Command
*/
public function handle()
{
render(<<<'HTML'
render(
<<<'HTML'
<div class="py-1 ml-2">
<div class="px-1 bg-blue-300 text-black">Laravel Zero</div>
<em class="ml-1">
Simplicity is the ultimate sophistication.
</em>
</div>
HTML);
HTML
);
}
/**
* Define the command's schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @param \Illuminate\Console\Scheduling\Schedule $schedule
*
* @return void
*/
public function schedule(Schedule $schedule)

View File

@@ -4,7 +4,8 @@ namespace App\Dto;
use Spatie\DataTransferObject\DataTransferObject;
class GitHubApiBranch extends DataTransferObject {
class GitHubApiBranch extends DataTransferObject
{
public string $name;
public bool $protected;
}

View File

@@ -4,7 +4,8 @@ namespace App\Dto;
use Spatie\DataTransferObject\Attributes\MapFrom;
class PackagistApiPackagePayload extends \Spatie\DataTransferObject\DataTransferObject {
class PackagistApiPackagePayload extends \Spatie\DataTransferObject\DataTransferObject
{
#[MapFrom('package.name')]
public string $name = '';
#[MapFrom('package.description')]

View File

@@ -4,7 +4,8 @@ namespace App\Dto;
use Spatie\DataTransferObject\Attributes\MapFrom;
class PackagistApiStatsPayload extends \Spatie\DataTransferObject\DataTransferObject {
class PackagistApiStatsPayload extends \Spatie\DataTransferObject\DataTransferObject
{
public array $labels;
#[MapFrom('values.[0]')]
public string $version;

View File

@@ -4,9 +4,11 @@ namespace App\Fetchers;
use Illuminate\Support\Facades\Http;
class GitHubRestApi {
public static function getBranches( string $vendor, string $package ) : array {
$pages = self::downloader( $vendor, $package );
class GitHubRestApi
{
public static function getBranches(string $vendor, string $package): array
{
$pages = self::downloader($vendor, $package);
$pages = \collect($pages)
->flatten(1)
->toArray();
@@ -14,7 +16,8 @@ class GitHubRestApi {
return $pages;
}
public static function downloader( $vendor, $package ) : array {
public static function downloader($vendor, $package): array
{
$responses = [];
$continue = true;
@@ -25,15 +28,15 @@ class GitHubRestApi {
$package
);
while ( $continue ) {
$response = Http::get( $gh_api . '&page=' . $page );
while ($continue) {
$response = Http::get($gh_api . '&page=' . $page);
if ( empty( $response ) ) {
if (empty($response)) {
$continue = false;
}
$responses[ $page ] = $response;
$page ++;
$responses[$page] = $response;
$page++;
}
return $responses;