Skip to content

Commit

Permalink
refactor & regenerate
Browse files Browse the repository at this point in the history
  • Loading branch information
fetus-hina committed Dec 31, 2024
1 parent 06a2a6a commit c0113a6
Show file tree
Hide file tree
Showing 66 changed files with 360 additions and 360 deletions.
116 changes: 17 additions & 99 deletions commands/DocCommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,34 @@

namespace app\commands;

use Collator;
use DirectoryIterator;
use Exception;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
use Yii;
use app\components\helpers\GitAuthorHelper;
use app\components\helpers\TypeHelper;
use yii\console\Controller;
use yii\console\ExitCode;
use yii\helpers\Console;

use function array_key_exists;
use function array_keys;
use function array_map;
use function array_reduce;
use function dirname;
use function escapeshellarg;
use function exec;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function implode;
use function in_array;
use function max;
use function ltrim;
use function min;
use function preg_quote;
use function preg_replace_callback;
use function rtrim;
use function str_starts_with;
use function substr;
use function time;
use function trim;
use function uksort;
use function vsprintf;

use const PHP_INT_MAX;

final class DocCommentController extends Controller
{
public $defaultAction = 'rewrite-php';
Expand All @@ -60,13 +51,13 @@ public function init()

public function actionRewritePhp(): int
{
$this->rewritePhpRecursive(dirname(__DIR__));
$this->rewritePhpRecursive(dirname(__DIR__) . '/messages/ja');
$this->rewritePhpRecursive(dirname(__DIR__), '');
$this->rewritePhpRecursive(dirname(__DIR__) . '/messages/ja', 'messages/ja');

return ExitCode::OK;
}

private function rewritePhpRecursive(string $dir): void
private function rewritePhpRecursive(string $dir, string $relPath): void
{
$it = new DirectoryIterator($dir);
foreach ($it as $entry) {
Expand Down Expand Up @@ -102,18 +93,22 @@ private function rewritePhpRecursive(string $dir): void
continue;
}

$this->rewritePhpRecursive($entry->getPathname());
$this->rewritePhpRecursive(
$entry->getPathname(),
ltrim($relPath . '/' . $entry->getFilename(), '/'),
);
} elseif ($entry->isFile() && substr($entry->getFilename(), -4) === '.php') {
if ($entry->getFilename() === 'requirements.php') {
$fileRelPath = ltrim($relPath . '/' . $entry->getFilename(), '/');
if ($fileRelPath === 'requirements.php') {
continue;
}

$this->rewritePhpFile($entry->getPathname());
$this->rewritePhpFile($entry->getPathname(), $fileRelPath);
}
}
}

private function rewritePhpFile(string $path): void
private function rewritePhpFile(string $path, string $relPath): void
{
$file = TypeHelper::string(file_get_contents($path));
if (!str_starts_with($file, '<?php')) {
Expand All @@ -134,13 +129,13 @@ private function rewritePhpFile(string $path): void
1,
);
if ($file === $newFile) {
echo Console::ansiFormat($path, [Console::FG_GREY]) . "\n";
echo Console::ansiFormat($relPath, [Console::FG_GREY]) . "\n";
return;
}

echo Console::ansiFormat($path, [Console::FG_PURPLE]) . "\n";
echo Console::ansiFormat($relPath, [Console::FG_PURPLE]) . "\n";
$differ = new Differ(new UnifiedDiffOutputBuilder());
print $differ->diff($file, $newFile) . "\n";
echo $differ->diff($file, $newFile) . "\n";

file_put_contents($path, rtrim($newFile) . "\n");
}
Expand All @@ -149,8 +144,7 @@ private function makeDocComment(string $path): ?string
{
$f = Yii::$app->formatter;

$authors = $this->getAuthors($path);

$authors = GitAuthorHelper::getAuthors($path);
$minCommitDate = array_reduce(
$authors,
fn (int $carry, array $item): int => min($carry, $item[0]),
Expand All @@ -177,80 +171,4 @@ private function makeDocComment(string $path): ?string
implode("\n", array_map(fn (string $line): string => " * {$line}", $lines)) .
"\n */";
}

/**
* @return array<string, array{int, int}>
*/
private function getAuthors(string $path): array
{
$cmdline = vsprintf('/usr/bin/env git log --pretty=%s -- %s | sort | uniq', [
escapeshellarg('%at/%an <%ae>%n%ct/%cn <%ce>'),
escapeshellarg($path),
]);
$status = null;
$lines = [];
@exec($cmdline, $lines, $status);
if ($status !== ExitCode::OK) {
throw new Exception('Could not get contributors');
}

$results = [
'AIZAWA Hina <hina@fetus.jp>' => [time(), time()],
];
foreach ($lines as $line) {
if (!$line = trim($line)) {
continue;
}

[$timestamp, $author] = explode('/', $line, 2);
if (!$author = $this->fixAuthor($author)) {
continue;
}

$results[$author] ??= [PHP_INT_MAX, 0];
$results[$author][0] = min($results[$author][0], (int)$timestamp);
$results[$author][1] = max($results[$author][1], (int)$timestamp);
}

$locale = TypeHelper::instanceOf(Collator::create('en_US'), Collator::class);
$locale->setAttribute(Collator::NUMERIC_COLLATION, Collator::ON);

uksort(
$results,
function (string $a, string $b) use ($locale): int {
if ($a === $b) {
return 0;
}

if (str_starts_with($a, 'AIZAWA Hina')) {
return -1;
}

if (str_starts_with($b, 'AIZAWA Hina')) {
return 1;
}

return $locale->compare($a, $b);
},
);

return $results;
}

private function fixAuthor(string $author): ?string
{
$authorMap = [
'AIZAWA Hina <hina@bouhime.com>' => 'AIZAWA Hina <hina@fetus.jp>',
'AIZAWA, Hina <hina@bouhime.com>' => 'AIZAWA Hina <hina@fetus.jp>',
'GitHub <noreply@github.com>' => null,
'Lukas <github@muffl0n.de>' => 'Lukas Böttcher <github@muffl0n.de>',
'StyleCI Bot <bot@styleci.io>' => null,
'Unknown <wkoichi@gmail.com>' => 'Koichi Watanabe <wkoichi@gmail.com>',
'Yifan <44556003+liuyifan-eric@users.noreply.github.com>' => 'Yifan Liu <yifanliu00@gmail.com>',
'li <170cm.kurekure@gmail.com>' => 'li <nvblstr@gmail.com>',
'spacemeowx2 <spacemeowx2@gmail.com>' => 'imspace <spacemeowx2@gmail.com>',
];

return array_key_exists($author, $authorMap) ? $authorMap[$author] : $author;
}
}
Loading

0 comments on commit c0113a6

Please sign in to comment.