Skip to content

Commit

Permalink
Optimize format script code
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Oct 24, 2023
1 parent a430665 commit 4e2d35f
Show file tree
Hide file tree
Showing 21 changed files with 113 additions and 66 deletions.
135 changes: 91 additions & 44 deletions bin/format
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,102 @@
* Usage: php bin/format
*/

function scan_dir(string $dir, callable $filter = null): array
{
$files = array_filter(scandir($dir), function (string $file) {
return $file[0] !== '.';
});
array_walk($files, function (&$file) use ($dir) {
$file = "{$dir}/{$file}";
});
return array_values($filter ? array_filter($files, $filter) : $files);
$directories = ['src', 'tests', 'sample'];
$rootDirectoryPath = realpath(dirname(__DIR__));

foreach ($directories as $directory) {
$directoryPath = $rootDirectoryPath . '/' . $directory;

$directoryIterator = new RecursiveDirectoryIterator($directoryPath, RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST);

$phpFiles = findPhpFiles($iterator);

foreach ($phpFiles as $file) {
$formattedContent = formatFile($file);
writeToFile($file, $formattedContent, $directory);
}
}

function fix_tests_in_this_dir(string $dir, string $root = '')
echo 'Formatting completed', PHP_EOL;

/**
* Returns an array of PHP file paths from the directory iterator.
*
* @param RecursiveIteratorIterator $iterator The iterator of the directory to search through.
* @return array An array of PHP file paths.
*/
function findPhpFiles($iterator)
{
$files = scan_dir($dir);
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
$requirement_level = (function () use ($root, $file) {
for ($l = 0; $l < 8; $l++) {
$file = dirname($file);
if ($file === $root) {
return $l;
}
}
return -1;
})();
if ($requirement_level < 0) {
echo("Failed to get requirement level of file {$file}");
}
$content = file_get_contents($file);
$changed = false;
// empty lines
$_content = trim($content) . "\n";
if ($content !== $_content) {
echo "Format empty lines in {$file}", PHP_EOL;
$content = $_content;
$changed = true;
}
if ($changed) {
file_put_contents($file, $content);
}
} elseif (is_dir($file)) {
fix_tests_in_this_dir($file, $root);
$phpFiles = [];
foreach ($iterator as $file) {
if ($file->getExtension() !== 'php' || $file->isDir()) {
continue;
}
$phpFiles[] = $file->getPathname();
}
return $phpFiles;
}

/**
* Returns the formatted content of a file.
*
* @param string $file The path to the file to format.
* @return string The formatted content.
*/
function formatFile($file)
{
$content = file_get_contents($file);
return formatLineEndings($content);
}

$root = realpath(dirname(__DIR__));
$dirs = ['src', 'tests', 'sample'];
foreach ($dirs as $dir) {
fix_tests_in_this_dir($root . '/' . $dir, $root);
/**
* Returns the content with formatted line endings.
*
* @param string $content The content to format.
* @return string The content with formatted line endings.
*/
function formatLineEndings($content)
{
return trim($content) . "\n";
}

/**
* Returns the content with all occurrences of 'schema' replaced with 'scheme'.
*
* @param string $content The content to perform replacements on.
* @return string The content with all occurrences of 'schema' replaced with 'scheme'.
*/
function replaceSchemaWithScheme($content)
{
return str_replace('schema', 'scheme', $content);
}

/**
* Writes the provided content to a file if it differs from the original content of the file.
* Also checks the directory and if it's not 'src', replaces 'schema' with 'scheme' in the content.
*
* @param string $file The path to the file to write.
* @param string $content The content to write to the file.
* @param string $directory The directory the file resides in.
*/
function writeToFile($file, $content, $directory)
{
$originalContent = file_get_contents($file);
$changed = false;
if ($originalContent !== $content) {
echo "Formatted empty lines in {$file}", PHP_EOL;
$changed = true;
}
if ($directory != 'src') {
$_content = replaceSchemaWithScheme($content);
if ($content !== $_content) {
echo "Use scheme instead of schema in {$file}", PHP_EOL;
$content = $_content;
$changed = true;
}
}
if ($changed) {
file_put_contents($file, $content);
}
}
echo 'Format done', PHP_EOL;
2 changes: 1 addition & 1 deletion sample/aIBodyRecognitionProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/aIGameRecProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/aIImageColoringProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/aIImageCropProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/aIImageEnhanceProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/aIImageSuperResolutionProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/aILicenseRecProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/autoTranslationBlockProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials' => array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/closeAiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', // 万象接口必须用https
'scheme' => 'https', // 万象接口必须用https
'credentials' => array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/cosClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
'anonymous' => true, //匿名模式
),
'timezone' => 'PRC', //时区
'locationWithSchema' => true //Location中是否包含schema
'locationWithScheme' => true //Location中是否包含scheme
)
);
2 changes: 1 addition & 1 deletion sample/createMediaSegmentVideoBodyJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/createMediaTargetRecJobs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/createMediaTargetRecTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/detectLabelProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/detectPetProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/goodsMattingProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/livenessRecognitionProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/recognizeLogoProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
2 changes: 1 addition & 1 deletion sample/updateMediaTargetRecTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
$cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'scheme' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId,
'secretKey' => $secretKey)));
Expand Down
6 changes: 3 additions & 3 deletions tests/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function getCosClient()
$cosClient = new Client(
array(
'region' => self::getRegion(),
'schema' => 'https',
'scheme' => 'https',
'credentials' => array(
'secretId' => getenv('COS_KEY'),
'secretKey' => getenv('COS_SECRET')
Expand All @@ -73,7 +73,7 @@ public static function getCiClient() {
$cosClient = new Client(
array(
'region' => self::getRegion(),
'schema' => 'https',
'scheme' => 'https',
'credentials' => array(
'secretId' => getenv('CI_KEY'),
'secretKey' => getenv('CI_SECRET')
Expand All @@ -91,7 +91,7 @@ public static function getCertainRegionClient($region) {
$cosClient = new Client(
array(
'region' => $region,
'schema' => 'https',
'scheme' => 'https',
'credentials' => array(
'secretId' => getenv('CI_KEY'),
'secretKey' => getenv('CI_SECRET')
Expand Down

0 comments on commit 4e2d35f

Please sign in to comment.