HummingBirdAnimeClient/build/update_header_comments.php

90 lines
2.2 KiB
PHP
Raw Normal View History

2022-03-04 15:50:35 -05:00
<?php declare(strict_types=1);
/**
* Hummingbird Anime List Client
*
* An API client for Kitsu to manage anime and manga watch lists
*
* PHP version 8
*
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @version 5.2
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
*/
namespace Aviat\Ion\Etc;
2015-11-16 11:40:01 -05:00
if ( ! function_exists('glob_recursive'))
{
2015-12-09 14:54:11 -05:00
// Does not support flag GLOB_BRACE
2015-11-16 11:40:01 -05:00
2021-02-23 15:38:29 -05:00
function glob_recursive(string $pattern, int $flags = 0): array
2015-12-09 14:54:11 -05:00
{
$files = glob($pattern, $flags);
2015-11-16 11:40:01 -05:00
foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir)
2015-12-09 14:54:11 -05:00
{
$files = array_merge($files, glob_recursive($dir . '/' . basename($pattern), $flags));
2015-12-09 14:54:11 -05:00
}
2015-11-16 11:40:01 -05:00
2015-12-09 14:54:11 -05:00
return $files;
}
2015-11-16 11:40:01 -05:00
}
2019-12-09 14:41:04 -05:00
function get_text_to_replace(array $tokens): string
2015-11-16 11:40:01 -05:00
{
2016-10-20 22:09:36 -04:00
$output = '';
2015-11-16 11:40:01 -05:00
2016-10-20 22:09:36 -04:00
// Tokens have the follow structure if arrays:
// [0] => token type constant
2019-12-06 09:15:49 -05:00
// [1] => raw syntax parsed to that token
2016-10-20 22:09:36 -04:00
// [2] => line number
foreach($tokens as $token)
2015-11-16 11:40:01 -05:00
{
2016-10-20 22:09:36 -04:00
// Since we only care about opening docblocks,
// bail out when we get to the namespace token
if (is_array($token) && $token[0] === T_NAMESPACE)
{
break;
}
if (is_array($token))
{
$token = $token[1];
}
$output .= $token;
2015-11-16 11:40:01 -05:00
}
2016-10-20 22:09:36 -04:00
return $output;
2015-11-16 11:40:01 -05:00
}
2022-03-04 15:50:35 -05:00
function replace_file(string $file, string $template): void
2015-11-16 11:40:01 -05:00
{
2022-03-04 15:50:35 -05:00
$source = file_get_contents($file);
if ($source === FALSE || stripos($source, 'namespace') === FALSE)
2015-11-16 11:40:01 -05:00
{
2022-03-04 15:50:35 -05:00
return;
}
2016-10-20 22:09:36 -04:00
2022-03-04 15:50:35 -05:00
$tokens = token_get_all($source);
$text_to_replace = get_text_to_replace($tokens);
2015-11-16 11:40:01 -05:00
2022-03-04 15:50:35 -05:00
$header = file_get_contents(__DIR__ . $template);
$new_text = "<?php declare(strict_types=1);\n{$header}";
2015-11-16 11:40:01 -05:00
2022-03-04 15:50:35 -05:00
$new_source = str_replace($text_to_replace, $new_text, $source);
file_put_contents($file, $new_source);
2015-11-16 11:40:01 -05:00
}
2022-03-04 15:50:35 -05:00
// ----------------------------------------------------------------------------
$files = array_filter(
glob_recursive('*.php'),
fn (string $file) => ! (str_contains($file, '/vendor/') || str_contains($file, '/tmp/'))
);
array_walk($files, fn (string $file) => replace_file($file, '/header_comment.txt'));
2015-11-16 11:40:01 -05:00
2022-03-04 15:50:35 -05:00
echo json_encode(array_values($files), JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR) . "\n";
printf("Successfully updated header comments in %d files\n", count($files));