Skip to content

Commit a5be056

Browse files
authored
Optimize format script code (#327)
* Optimize format script code
1 parent 6bbce32 commit a5be056

23 files changed

+115
-68
lines changed

.github/workflows/format.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
php-version: ['8.0']
1515
steps:
1616
- name: Checkout
17-
uses: actions/checkout@v3
17+
uses: actions/checkout@v4
1818

1919
- name: Setup PHP
2020
uses: shivammathur/setup-php@v2

.github/workflows/install.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
fail-fast: false
1717
steps:
1818
- name: Checkout
19-
uses: actions/checkout@v3
19+
uses: actions/checkout@v4
2020

2121
- name: Setup PHP
2222
uses: shivammathur/setup-php@v2

bin/format

Lines changed: 91 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,102 @@
66
* Usage: php bin/format
77
*/
88

9-
function scan_dir(string $dir, callable $filter = null): array
10-
{
11-
$files = array_filter(scandir($dir), function (string $file) {
12-
return $file[0] !== '.';
13-
});
14-
array_walk($files, function (&$file) use ($dir) {
15-
$file = "{$dir}/{$file}";
16-
});
17-
return array_values($filter ? array_filter($files, $filter) : $files);
9+
$directories = ['src', 'tests', 'sample'];
10+
$rootDirectoryPath = realpath(dirname(__DIR__));
11+
12+
foreach ($directories as $directory) {
13+
$directoryPath = $rootDirectoryPath . '/' . $directory;
14+
15+
$directoryIterator = new RecursiveDirectoryIterator($directoryPath, RecursiveDirectoryIterator::SKIP_DOTS);
16+
$iterator = new RecursiveIteratorIterator($directoryIterator, RecursiveIteratorIterator::SELF_FIRST);
17+
18+
$phpFiles = findPhpFiles($iterator);
19+
20+
foreach ($phpFiles as $file) {
21+
$formattedContent = formatFile($file);
22+
writeToFile($file, $formattedContent, $directory);
23+
}
1824
}
1925

20-
function fix_tests_in_this_dir(string $dir, string $root = '')
26+
echo 'Formatting completed', PHP_EOL;
27+
28+
/**
29+
* Returns an array of PHP file paths from the directory iterator.
30+
*
31+
* @param RecursiveIteratorIterator $iterator The iterator of the directory to search through.
32+
* @return array An array of PHP file paths.
33+
*/
34+
function findPhpFiles($iterator)
2135
{
22-
$files = scan_dir($dir);
23-
foreach ($files as $file) {
24-
if (pathinfo($file, PATHINFO_EXTENSION) === 'php') {
25-
$requirement_level = (function () use ($root, $file) {
26-
for ($l = 0; $l < 8; $l++) {
27-
$file = dirname($file);
28-
if ($file === $root) {
29-
return $l;
30-
}
31-
}
32-
return -1;
33-
})();
34-
if ($requirement_level < 0) {
35-
echo("Failed to get requirement level of file {$file}");
36-
}
37-
$content = file_get_contents($file);
38-
$changed = false;
39-
// empty lines
40-
$_content = trim($content) . "\n";
41-
if ($content !== $_content) {
42-
echo "Format empty lines in {$file}", PHP_EOL;
43-
$content = $_content;
44-
$changed = true;
45-
}
46-
if ($changed) {
47-
file_put_contents($file, $content);
48-
}
49-
} elseif (is_dir($file)) {
50-
fix_tests_in_this_dir($file, $root);
36+
$phpFiles = [];
37+
foreach ($iterator as $file) {
38+
if ($file->getExtension() !== 'php' || $file->isDir()) {
39+
continue;
5140
}
41+
$phpFiles[] = $file->getPathname();
5242
}
43+
return $phpFiles;
44+
}
45+
46+
/**
47+
* Returns the formatted content of a file.
48+
*
49+
* @param string $file The path to the file to format.
50+
* @return string The formatted content.
51+
*/
52+
function formatFile($file)
53+
{
54+
$content = file_get_contents($file);
55+
return formatLineEndings($content);
5356
}
5457

55-
$root = realpath(dirname(__DIR__));
56-
$dirs = ['src', 'tests', 'sample'];
57-
foreach ($dirs as $dir) {
58-
fix_tests_in_this_dir($root . '/' . $dir, $root);
58+
/**
59+
* Returns the content with formatted line endings.
60+
*
61+
* @param string $content The content to format.
62+
* @return string The content with formatted line endings.
63+
*/
64+
function formatLineEndings($content)
65+
{
66+
return trim($content) . "\n";
67+
}
68+
69+
/**
70+
* Returns the content with all occurrences of 'schema' replaced with 'scheme'.
71+
*
72+
* @param string $content The content to perform replacements on.
73+
* @return string The content with all occurrences of 'schema' replaced with 'scheme'.
74+
*/
75+
function replaceSchemaWithScheme($content)
76+
{
77+
return str_replace(['schema', 'Schema'], ['scheme', 'Scheme'], $content);
78+
}
79+
80+
/**
81+
* Writes the provided content to a file if it differs from the original content of the file.
82+
* Also checks the directory and if it's not 'src', replaces 'schema' with 'scheme' in the content.
83+
*
84+
* @param string $file The path to the file to write.
85+
* @param string $content The content to write to the file.
86+
* @param string $directory The directory the file resides in.
87+
*/
88+
function writeToFile($file, $content, $directory)
89+
{
90+
$originalContent = file_get_contents($file);
91+
$changed = false;
92+
if ($originalContent !== $content) {
93+
echo "Formatted empty lines in {$file}", PHP_EOL;
94+
$changed = true;
95+
}
96+
if ($directory != 'src') {
97+
$_content = replaceSchemaWithScheme($content);
98+
if ($content !== $_content) {
99+
echo "Use scheme instead of schema in {$file}", PHP_EOL;
100+
$content = $_content;
101+
$changed = true;
102+
}
103+
}
104+
if ($changed) {
105+
file_put_contents($file, $content);
106+
}
59107
}
60-
echo 'Format done', PHP_EOL;

sample/aIBodyRecognitionProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$cosClient = new Qcloud\Cos\Client(
99
array(
1010
'region' => $region,
11-
'schema' => 'https', //协议头部,默认为http
11+
'scheme' => 'https', //协议头部,默认为http
1212
'credentials'=> array(
1313
'secretId' => $secretId,
1414
'secretKey' => $secretKey)));

sample/aIGameRecProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$cosClient = new Qcloud\Cos\Client(
99
array(
1010
'region' => $region,
11-
'schema' => 'https', //协议头部,默认为http
11+
'scheme' => 'https', //协议头部,默认为http
1212
'credentials'=> array(
1313
'secretId' => $secretId,
1414
'secretKey' => $secretKey)));

sample/aIImageColoringProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$cosClient = new Qcloud\Cos\Client(
99
array(
1010
'region' => $region,
11-
'schema' => 'https', //协议头部,默认为http
11+
'scheme' => 'https', //协议头部,默认为http
1212
'credentials'=> array(
1313
'secretId' => $secretId,
1414
'secretKey' => $secretKey)));

sample/aIImageCropProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$cosClient = new Qcloud\Cos\Client(
99
array(
1010
'region' => $region,
11-
'schema' => 'https', //协议头部,默认为http
11+
'scheme' => 'https', //协议头部,默认为http
1212
'credentials'=> array(
1313
'secretId' => $secretId,
1414
'secretKey' => $secretKey)));

sample/aIImageEnhanceProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$cosClient = new Qcloud\Cos\Client(
99
array(
1010
'region' => $region,
11-
'schema' => 'https', //协议头部,默认为http
11+
'scheme' => 'https', //协议头部,默认为http
1212
'credentials'=> array(
1313
'secretId' => $secretId,
1414
'secretKey' => $secretKey)));

sample/aIImageSuperResolutionProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$cosClient = new Qcloud\Cos\Client(
99
array(
1010
'region' => $region,
11-
'schema' => 'https', //协议头部,默认为http
11+
'scheme' => 'https', //协议头部,默认为http
1212
'credentials'=> array(
1313
'secretId' => $secretId,
1414
'secretKey' => $secretKey)));

sample/aILicenseRecProcess.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
$cosClient = new Qcloud\Cos\Client(
99
array(
1010
'region' => $region,
11-
'schema' => 'https', //协议头部,默认为http
11+
'scheme' => 'https', //协议头部,默认为http
1212
'credentials'=> array(
1313
'secretId' => $secretId,
1414
'secretKey' => $secretKey)));

0 commit comments

Comments
 (0)