Skip to content

Commit a03dee8

Browse files
committed
style: #33 follow PHPMD
1 parent b90495d commit a03dee8

File tree

12 files changed

+604
-67
lines changed

12 files changed

+604
-67
lines changed

.git-pre-commit

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/bin/sh
22

3-
FILES=$(git diff --diff-filter=d --name-only HEAD)
4-
if [ -n "$FILES" ]; then
5-
echo "$FILES" | xargs ./vendor/bin/phpcs --extensions=php --standard=PSR12
6-
fi
3+
FILES=$(git diff --diff-filter=d --name-only HEAD | grep '.php$')
4+
for file in $FILES; do
5+
./vendor/bin/phpcs --extensions=php --standard=PSR12 "$file"
6+
./vendor/bin/phpmd "$file" text phpmd.xml --exclude vendor
7+
done

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ jobs:
2727
with:
2828
args: ./vendor/bin/phpcs --extensions=php --standard=PSR12 app/ tests/
2929

30+
- name: PHPMD
31+
uses: docker://ecoding/php:8.0
32+
with:
33+
args: ./vendor/bin/phpmd . text phpmd.xml --exclude vendor
34+
3035
- name: test
3136
uses: docker://ecoding/php:8.0
3237
env:

app/Coding.php

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
namespace App;
44

5+
use Exception;
56
use GuzzleHttp\Client;
67
use Illuminate\Support\Facades\File;
78
use Illuminate\Support\Facades\Log;
89
use Illuminate\Support\Facades\Storage;
910
use Illuminate\Support\Str;
11+
use ZipArchive;
1012

1113
class Coding
1214
{
1315
private Client $client;
14-
private \ZipArchive $zipArchive;
16+
private ZipArchive $zipArchive;
1517

16-
public function __construct(Client $client = null, \ZipArchive $zipArchive = null)
18+
public function __construct(Client $client = null, ZipArchive $zipArchive = null)
1719
{
1820
$this->client = $client ?? new Client();
19-
$this->zipArchive = $zipArchive ?? new \ZipArchive();
21+
$this->zipArchive = $zipArchive ?? new ZipArchive();
2022
}
2123

2224
public function createWiki($token, $projectName, $data)
@@ -64,7 +66,7 @@ public function createUploadToken($token, $projectName, $fileName)
6466
public function createMarkdownZip($markdown, $path, $markdownFilename): bool|string
6567
{
6668
$zipFileFullPath = sys_get_temp_dir() . '/' . $markdownFilename . '-' . Str::uuid() . '.zip';
67-
if ($this->zipArchive->open($zipFileFullPath, \ZipArchive::CREATE) !== true) {
69+
if ($this->zipArchive->open($zipFileFullPath, ZipArchive::CREATE) !== true) {
6870
Log::error("cannot open <$zipFileFullPath>");
6971
return false;
7072
}
@@ -114,11 +116,10 @@ public function createWikiByZip(string $token, string $projectName, array $uploa
114116
],
115117
]);
116118
$result = json_decode($response->getBody(), true);
117-
if (isset($result['Response']['JobId'])) {
118-
return $result['Response'];
119-
} else {
120-
return new \Exception('createWikiByZip failed');
119+
if (!isset($result['Response']['JobId'])) {
120+
return new Exception('failed');
121121
}
122+
return $result['Response'];
122123
}
123124

124125
/**
@@ -145,12 +146,7 @@ public function getImportJobStatus(string $token, string $projectName, string $j
145146
],
146147
]);
147148
$result = json_decode($response->getBody(), true);
148-
if (isset($result['Response']['Data'])) {
149-
return $result['Response']['Data'];
150-
} else {
151-
// TODO exception message
152-
return new \Exception('failed');
153-
}
149+
return $result['Response']['Data'];
154150
}
155151

156152
public function createWikiByUploadZip(string $token, string $projectName, string $zipFileFullPath, int $parentId)
@@ -187,7 +183,7 @@ public function getWiki(string $token, string $projectName, int $id, int $versio
187183
return $result['Response']['Data'];
188184
}
189185

190-
public function updateWikiTitle(string $token, string $projectName, int $id, string $title)
186+
public function updateWikiTitle(string $token, string $projectName, int $id, string $title): bool
191187
{
192188
$response = $this->client->request('POST', 'https://e.coding.net/open-api', [
193189
'headers' => [

app/Commands/WikiImportCommand.php

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Coding;
66
use Confluence\Content;
7+
use DOMDocument;
78
use Illuminate\Support\Str;
89
use LaravelFans\Confluence\Facades\Confluence;
910
use LaravelZero\Framework\Commands\Command;
@@ -37,22 +38,21 @@ class WikiImportCommand extends Command
3738
protected $description = '从 Confluence 导入 Wiki';
3839

3940
private \App\Confluence $confluence;
40-
private \DOMDocument $document;
41+
private DOMDocument $document;
4142

4243
/**
4344
* Execute the console command.
4445
*
4546
*/
46-
public function handle(Coding $coding, \App\Confluence $confluence, \DOMDocument $document): int
47+
public function handle(Coding $coding, \App\Confluence $confluence, DOMDocument $document): int
4748
{
4849
$this->coding = $coding;
4950
$this->confluence = $confluence;
5051
$this->document = $document;
5152
$this->setCodingApi();
5253

53-
if ($this->option('coding_import_provider')) {
54-
$provider = $this->option('coding_import_provider');
55-
} else {
54+
$provider = $this->option('coding_import_provider');
55+
if (is_null($provider)) {
5656
$provider = config('coding.import.provider') ?? $this->choice(
5757
'数据来源?',
5858
['Confluence', 'MediaWiki'],
@@ -64,9 +64,8 @@ public function handle(Coding $coding, \App\Confluence $confluence, \DOMDocument
6464
return 1;
6565
}
6666

67-
if ($this->option('coding_import_data_type')) {
68-
$dataType = $this->option('coding_import_data_type');
69-
} else {
67+
$dataType = $this->option('coding_import_data_type');
68+
if (is_null($dataType)) {
7069
$dataType = config('coding.import.data_type') ?? $this->choice(
7170
'数据类型?',
7271
['HTML', 'API'],
@@ -92,24 +91,21 @@ private function createWiki($data)
9291

9392
private function handleConfluenceApi(): int
9493
{
95-
if ($this->option('confluence_base_uri')) {
96-
$baseUri = $this->option('confluence_base_uri');
97-
} else {
94+
$baseUri = $this->option('confluence_base_uri');
95+
if (is_null($baseUri)) {
9896
$baseUri = config('confluence.base_uri') ?? $this->ask(
9997
'Confluence API 链接:',
10098
'http://localhost:8090/rest/api/'
10199
);
102100
}
103101
config(['confluence.base_uri' => $baseUri]);
104102

105-
if ($this->option('confluence_username')) {
106-
$username = $this->option('confluence_username');
107-
} else {
103+
$username = $this->option('confluence_username');
104+
if (is_null($username)) {
108105
$username = config('confluence.username') ?? $this->ask('Confluence 账号:', 'admin');
109106
}
110-
if ($this->option('confluence_password')) {
111-
$password = $this->option('confluence_password');
112-
} else {
107+
$password = $this->option('confluence_password');
108+
if (is_null($password)) {
113109
$password = config('confluence.password') ?? $this->ask('Confluence 密码:', '123456');
114110
}
115111
config(['confluence.auth' => [$username, $password]]);
@@ -133,9 +129,8 @@ private function handleConfluenceApi(): int
133129

134130
private function handleConfluenceHtml(): int
135131
{
136-
if ($this->option('coding_import_data_path')) {
137-
$dataPath = $this->option('coding_import_data_path');
138-
} else {
132+
$dataPath = $this->option('coding_import_data_path');
133+
if (is_null($dataPath)) {
139134
$dataPath = config('coding.import.data_path') ?? trim($this->ask(
140135
'空间导出的 HTML 目录',
141136
'./confluence/space1/'
@@ -167,9 +162,8 @@ private function handleConfluenceHtml(): int
167162
if (empty($pages['tree'])) {
168163
$this->info("未发现有效数据");
169164
return 0;
170-
} else {
171-
$this->info('发现 ' . count($pages['tree']) . ' 个一级页面');
172165
}
166+
$this->info('发现 ' . count($pages['tree']) . ' 个一级页面');
173167
$this->info("开始导入 CODING:");
174168
$this->uploadConfluencePages($dataPath, $pages['tree'], $pages['titles']);
175169
} catch (\ErrorException $e) {
@@ -196,7 +190,7 @@ private function uploadConfluencePages(string $dataPath, array $tree, array $tit
196190
);
197191
$this->info('上传成功,正在处理,任务 ID:' . $result['JobId']);
198192
$wikiId = null;
199-
$waiting_times = 0;
193+
$waitingTimes = 0;
200194
while (true) {
201195
// HACK 如果上传成功立即查询,会报错:invoke function
202196
sleep(1);
@@ -205,8 +199,8 @@ private function uploadConfluencePages(string $dataPath, array $tree, array $tit
205199
$this->codingProjectUri,
206200
$result['JobId']
207201
);
208-
if (in_array($jobStatus['Status'], ['wait_process', 'processing']) && $waiting_times < 10) {
209-
$waiting_times++;
202+
if (in_array($jobStatus['Status'], ['wait_process', 'processing']) && $waitingTimes < 10) {
203+
$waitingTimes++;
210204
continue;
211205
}
212206
if ($jobStatus['Status'] == 'success') {

app/Commands/WithCoding.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,19 @@ trait WithCoding
1313

1414
private function setCodingApi(): void
1515
{
16-
if ($this->option('coding_team_domain')) {
17-
$codingTeamDomain = $this->option('coding_team_domain');
18-
} else {
16+
$codingTeamDomain = $this->option('coding_team_domain');
17+
if (is_null($codingTeamDomain)) {
1918
$codingTeamDomain = config('coding.team_domain') ?? $this->ask('CODING 团队域名:');
2019
}
2120
$this->codingTeamDomain = str_replace('.coding.net', '', $codingTeamDomain);
2221

23-
if ($this->option('coding_project_uri')) {
24-
$this->codingProjectUri = $this->option('coding_project_uri');
25-
} else {
22+
$this->codingProjectUri = $this->option('coding_project_uri');
23+
if (is_null($this->codingProjectUri)) {
2624
$this->codingProjectUri = config('coding.project_uri') ?? $this->ask('CODING 项目标识:');
2725
}
2826

29-
if ($this->option('coding_token')) {
30-
$this->codingToken = $this->option('coding_token');
31-
} else {
27+
$this->codingToken = $this->option('coding_token');
28+
if (is_null($this->codingToken)) {
3229
$this->codingToken = config('coding.token') ?? $this->ask('CODING Token:');
3330
}
3431
}

app/Confluence.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
namespace App;
44

5-
use JetBrains\PhpStorm\ArrayShape;
5+
use DOMDocument;
6+
use DOMXPath;
67
use League\HTMLToMarkdown\HtmlConverter;
7-
use phpDocumentor\Reflection\Types\Array_;
88

99
class Confluence
1010
{
11-
private \DOMDocument $document;
11+
private DOMDocument $document;
1212
private HtmlConverter $htmlConverter;
1313
private array $pageTitles;
1414

15-
public function __construct(\DOMDocument $document = null, HtmlConverter $htmlConverter = null)
15+
public function __construct(DOMDocument $document = null, HtmlConverter $htmlConverter = null)
1616
{
17-
$this->document = $document ?? new \DOMDocument();
17+
$this->document = $document ?? new DOMDocument();
1818
$this->htmlConverter = $htmlConverter ?? new HtmlConverter();
1919
$this->htmlConverter->getConfig()->setOption('strip_tags', true);
2020
}
@@ -65,14 +65,14 @@ public function parseAvailablePages(string $filename): array
6565
'titles' => [],
6666
];
6767
}
68-
$xpath = new \DOMXPath($this->document);
68+
$xpath = new DOMXPath($this->document);
6969
return [
7070
'tree' => $this->parsePagesTree($xpath, $divElement),
7171
'titles' => $this->pageTitles,
7272
];
7373
}
7474

75-
public function parsePagesTree(\DOMXPath $xpath, \DOMElement $parentElement)
75+
public function parsePagesTree(DOMXPath $xpath, \DOMElement $parentElement)
7676
{
7777
$liElements = $xpath->query('ul/li', $parentElement);
7878
if ($liElements->count() == 0) {

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"require-dev": {
3030
"fakerphp/faker": "^1.14",
3131
"mockery/mockery": "^1.4.3",
32+
"phpmd/phpmd": "^2.10",
3233
"phpunit/phpunit": "^9.5",
3334
"squizlabs/php_codesniffer": "^3.6"
3435
},

0 commit comments

Comments
 (0)