From 41767656526745288948b26b964f015800a19fc9 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Tue, 24 Oct 2023 17:23:48 +0800 Subject: [PATCH 1/2] Optimize format script code --- bin/format | 135 ++++++++++++++------- sample/aIBodyRecognitionProcess.php | 2 +- sample/aIGameRecProcess.php | 2 +- sample/aIImageColoringProcess.php | 2 +- sample/aIImageCropProcess.php | 2 +- sample/aIImageEnhanceProcess.php | 2 +- sample/aIImageSuperResolutionProcess.php | 2 +- sample/aILicenseRecProcess.php | 2 +- sample/autoTranslationBlockProcess.php | 2 +- sample/closeAiService.php | 2 +- sample/cosClient.php | 2 +- sample/createMediaSegmentVideoBodyJobs.php | 2 +- sample/createMediaTargetRecJobs.php | 2 +- sample/createMediaTargetRecTemplate.php | 2 +- sample/detectLabelProcess.php | 2 +- sample/detectPetProcess.php | 2 +- sample/goodsMattingProcess.php | 2 +- sample/livenessRecognitionProcess.php | 2 +- sample/recognizeLogoProcess.php | 2 +- sample/updateMediaTargetRecTemplate.php | 2 +- tests/Common.php | 6 +- 21 files changed, 113 insertions(+), 66 deletions(-) diff --git a/bin/format b/bin/format index 0716502..5b0f0b6 100755 --- a/bin/format +++ b/bin/format @@ -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', 'Schema'], ['scheme', '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; diff --git a/sample/aIBodyRecognitionProcess.php b/sample/aIBodyRecognitionProcess.php index 5b99817..de3c104 100644 --- a/sample/aIBodyRecognitionProcess.php +++ b/sample/aIBodyRecognitionProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/aIGameRecProcess.php b/sample/aIGameRecProcess.php index 7ef625c..9dbed7c 100644 --- a/sample/aIGameRecProcess.php +++ b/sample/aIGameRecProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/aIImageColoringProcess.php b/sample/aIImageColoringProcess.php index 90f894e..684f972 100644 --- a/sample/aIImageColoringProcess.php +++ b/sample/aIImageColoringProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/aIImageCropProcess.php b/sample/aIImageCropProcess.php index 5755b89..c68e2e4 100644 --- a/sample/aIImageCropProcess.php +++ b/sample/aIImageCropProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/aIImageEnhanceProcess.php b/sample/aIImageEnhanceProcess.php index c004710..58bfd04 100644 --- a/sample/aIImageEnhanceProcess.php +++ b/sample/aIImageEnhanceProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/aIImageSuperResolutionProcess.php b/sample/aIImageSuperResolutionProcess.php index 3e157ca..6079187 100644 --- a/sample/aIImageSuperResolutionProcess.php +++ b/sample/aIImageSuperResolutionProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/aILicenseRecProcess.php b/sample/aILicenseRecProcess.php index 31e00f8..142465c 100644 --- a/sample/aILicenseRecProcess.php +++ b/sample/aILicenseRecProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/autoTranslationBlockProcess.php b/sample/autoTranslationBlockProcess.php index 62a8309..27238ac 100644 --- a/sample/autoTranslationBlockProcess.php +++ b/sample/autoTranslationBlockProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials' => array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/closeAiService.php b/sample/closeAiService.php index 91d429d..098b678 100644 --- a/sample/closeAiService.php +++ b/sample/closeAiService.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', // 万象接口必须用https + 'scheme' => 'https', // 万象接口必须用https 'credentials' => array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/cosClient.php b/sample/cosClient.php index d6e3790..db178de 100644 --- a/sample/cosClient.php +++ b/sample/cosClient.php @@ -27,6 +27,6 @@ 'anonymous' => true, //匿名模式 ), 'timezone' => 'PRC', //时区 - 'locationWithSchema' => true //Location中是否包含schema + 'locationWithScheme' => true //Location中是否包含scheme ) ); diff --git a/sample/createMediaSegmentVideoBodyJobs.php b/sample/createMediaSegmentVideoBodyJobs.php index 9dd9b11..bd39ca5 100644 --- a/sample/createMediaSegmentVideoBodyJobs.php +++ b/sample/createMediaSegmentVideoBodyJobs.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/createMediaTargetRecJobs.php b/sample/createMediaTargetRecJobs.php index 9ba067e..d1ac8bb 100644 --- a/sample/createMediaTargetRecJobs.php +++ b/sample/createMediaTargetRecJobs.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/createMediaTargetRecTemplate.php b/sample/createMediaTargetRecTemplate.php index 48e9ee3..ada61db 100644 --- a/sample/createMediaTargetRecTemplate.php +++ b/sample/createMediaTargetRecTemplate.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/detectLabelProcess.php b/sample/detectLabelProcess.php index 0a90cd4..5ba29a8 100644 --- a/sample/detectLabelProcess.php +++ b/sample/detectLabelProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/detectPetProcess.php b/sample/detectPetProcess.php index f645417..eb63a97 100644 --- a/sample/detectPetProcess.php +++ b/sample/detectPetProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/goodsMattingProcess.php b/sample/goodsMattingProcess.php index dc393b1..e8202a5 100644 --- a/sample/goodsMattingProcess.php +++ b/sample/goodsMattingProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/livenessRecognitionProcess.php b/sample/livenessRecognitionProcess.php index 7dae7c5..9965cdb 100644 --- a/sample/livenessRecognitionProcess.php +++ b/sample/livenessRecognitionProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/recognizeLogoProcess.php b/sample/recognizeLogoProcess.php index 4596191..aa2c7bb 100644 --- a/sample/recognizeLogoProcess.php +++ b/sample/recognizeLogoProcess.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/sample/updateMediaTargetRecTemplate.php b/sample/updateMediaTargetRecTemplate.php index a9635b2..3460168 100644 --- a/sample/updateMediaTargetRecTemplate.php +++ b/sample/updateMediaTargetRecTemplate.php @@ -8,7 +8,7 @@ $cosClient = new Qcloud\Cos\Client( array( 'region' => $region, - 'schema' => 'https', //协议头部,默认为http + 'scheme' => 'https', //协议头部,默认为http 'credentials'=> array( 'secretId' => $secretId, 'secretKey' => $secretKey))); diff --git a/tests/Common.php b/tests/Common.php index a4601b7..803fa27 100644 --- a/tests/Common.php +++ b/tests/Common.php @@ -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') @@ -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') @@ -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') From 323751e685bf1860daeca10c485e119325528242 Mon Sep 17 00:00:00 2001 From: sy-records <52o@qq52o.cn> Date: Thu, 26 Oct 2023 10:38:56 +0800 Subject: [PATCH 2/2] Use actions/checkout v4 --- .github/workflows/format.yml | 2 +- .github/workflows/install.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml index 48e5b51..be5b838 100644 --- a/.github/workflows/format.yml +++ b/.github/workflows/format.yml @@ -14,7 +14,7 @@ jobs: php-version: ['8.0'] steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 diff --git a/.github/workflows/install.yml b/.github/workflows/install.yml index ada192a..fd9297e 100644 --- a/.github/workflows/install.yml +++ b/.github/workflows/install.yml @@ -16,7 +16,7 @@ jobs: fail-fast: false steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2