Skip to content

Commit 1ef6ce3

Browse files
committed
Code cleanup
1 parent 3fda829 commit 1ef6ce3

File tree

5 files changed

+59
-43
lines changed

5 files changed

+59
-43
lines changed

src/CommitFetcher.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
namespace PHPWatch\PHPCommitBuilder;
4-
use Ayesh\CurlFetcher\CurlFetcher;
54

5+
use Ayesh\CurlFetcher\CurlFetcher;
66
class CommitFetcher {
77
private const API_ENDPOINT_COMPARE = 'https://api.github.com/repos/php/php-src/compare/';
88
private const API_ENDPOINT_COMMIT_LIST = 'https://api.github.com/repos/php/php-src/commits';
@@ -31,11 +31,11 @@ private function getListDateRange(string $since, string $until): array {
3131

3232
do {
3333
$paramsUrl = http_build_query($params);
34-
$url = static::API_ENDPOINT_COMMIT_LIST . '?' .$paramsUrl;
34+
$url = static::API_ENDPOINT_COMMIT_LIST . '?' . $paramsUrl;
3535

3636
$headers = [];
3737
if ($this->apiKey) {
38-
$headers[] = 'Authorization: Bearer '. $this->apiKey;
38+
$headers[] = 'Authorization: Bearer ' . $this->apiKey;
3939
}
4040

4141
$data = $this->curlFetcher->getJson($url, $headers);
@@ -44,7 +44,6 @@ private function getListDateRange(string $since, string $until): array {
4444

4545
$params['page']++;
4646
$hardLimits--;
47-
4847
} while (count($data) >= 100 && $hardLimits > 0);
4948

5049
return $return;
@@ -63,11 +62,11 @@ public function getCommitListBetweenTags(string $startTag, string $untilTag): ar
6362

6463
do {
6564
$paramsUrl = http_build_query($params);
66-
$url = $baseUrl . '?' .$paramsUrl;
65+
$url = $baseUrl . '?' . $paramsUrl;
6766

6867
$headers = [];
6968
if ($this->apiKey) {
70-
$headers[] = 'Authorization: Bearer '. $this->apiKey;
69+
$headers[] = 'Authorization: Bearer ' . $this->apiKey;
7170
}
7271

7372
$data = $this->curlFetcher->getJson($url, $headers);
@@ -76,7 +75,6 @@ public function getCommitListBetweenTags(string $startTag, string $untilTag): ar
7675

7776
$params['page']++;
7877
$hardLimits--;
79-
8078
} while (count($data->commits) >= 250 && $hardLimits > 0);
8179

8280
return $return;

src/CommitFormatter.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
namespace PHPWatch\PHPCommitBuilder;
44

55
class CommitFormatter {
6-
private array $commitsList = [];
76

7+
use FormatterHelpers;
8+
private array $commitsList = [];
89
private array $commitsGroupedByAuthor = [];
910

1011
private array $nameReplacements;
1112

12-
use FormatterHelpers;
13-
1413
public function __construct(array $inputCommits, array $nameReplacements = []) {
1514
$this->process($inputCommits);
1615
$this->nameReplacements = $nameReplacements;
@@ -26,7 +25,10 @@ private function process(array $inputCommits): void {
2625
continue;
2726
}
2827

29-
$commitArray['formatted'] = KeywordEnhancer::enhanceCommit($commitArray['subject'], substr($commitArray['hash'], 0, 10));
28+
$commitArray['formatted'] = KeywordEnhancer::enhanceCommit(
29+
$commitArray['subject'],
30+
substr($commitArray['hash'], 0, 10)
31+
);
3032
$formattedCommits[$i] = $commitArray;
3133

3234
if (!isset($this->commitsGroupedByAuthor[$commitArray['author']])) {
@@ -58,7 +60,7 @@ private function splitCommit(\stdClass $commit): array {
5860
'subject' => trim(trim($commitMessageParts[0]), '.'),
5961
'author' => trim($commit->commit->author->name),
6062
'hash' => trim($commit->sha),
61-
'message' => trim($commitMessage)
63+
'message' => trim($commitMessage),
6264
];
6365
}
6466

@@ -104,7 +106,9 @@ public function getFormattedCommitList(): array {
104106
public function getFormattedCommitListMarkup(): string {
105107
$output = '';
106108
foreach ($this->getFormattedCommitList() as $commit) {
107-
$output .= self::markdownListItem($commit['formatted'] . ' by ' . ($this->nameReplacements[$commit['author']] ?? $commit['author']));
109+
$output .= self::markdownListItem(
110+
$commit['formatted'] . ' by ' . ($this->nameReplacements[$commit['author']] ?? $commit['author'])
111+
);
108112
}
109113

110114
return $output;

src/NewsFetcher.php

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
namespace PHPWatch\PHPCommitBuilder;
4+
45
use Ayesh\CurlFetcher\CurlFetcher;
56

67
class NewsFetcher {
@@ -10,34 +11,32 @@ class NewsFetcher {
1011
private const REGEX_RELEASE_HEADER = '/^(?<date>(?<day>\d\d?|\?\?) (?<month>Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec|\?\?\?) (?<year>\?\?\?\?|20\d\d)), (PHP|php) (?<release_id>\d\.\d\.(?:\d\d?|0(?:alpha\d|beta\d|rc\d|RC\d)?))$/';
1112
private const REGEX_EXT_HEADER = '/^- ?(?<ext_name>[A-Za-z][A-Za-z _\/\d]+):? ?$/';
1213

13-
private const REGEX_CHANGE_RECORD_START = '/^ ? ?(\.|-) (?<change_record>.*)$/';
14+
private const REGEX_CHANGE_RECORD_START = '/^ ? ?(\.|-) (?<change_record>.*)$/';
1415
private const REGEX_CHANGE_RECORD_CONTINUATION = '/^( ?|\t)(?<change_record_cont>.*)$/';
1516

1617
private ?string $apiKey = null;
1718
private CurlFetcher $curlFetcher;
1819

19-
private const STATIC_REPLACEMENTS = [
20-
' (Anatol)' => ' (Anatol)',
21-
' (Kalle)' => ' (Kalle)',
22-
];
20+
private const STATIC_REPLACEMENTS = [
21+
' (Anatol)' => ' (Anatol)',
22+
' (Kalle)' => ' (Kalle)',
23+
];
2324

2425
public function __construct(string $apiKey = null) {
2526
$this->apiKey = $apiKey;
2627
$this->curlFetcher = new CurlFetcher();
2728
}
2829

2930
public function fetchAllForVersion(int|string $version): array {
30-
3131
if (is_string($version)) {
3232
if ($version !== 'master') {
3333
throw new \InvalidArgumentException('String arguments must be "master"');
3434
}
35-
$baseUrl = strtr(static::RAW_CONTENT_URL, [
35+
$baseUrl = strtr(static::RAW_CONTENT_URL, [
3636
'%tag' => 'master',
3737
]);
38-
}
39-
else {
40-
preg_match('/^(?<major>^\d)0(?<minor>\d)\d\d?$/', (string) $version, $matches);
38+
} else {
39+
preg_match('/^(?<major>^\d)0(?<minor>\d)\d\d?$/', (string)$version, $matches);
4140

4241
if (empty($matches)) {
4342
throw new \InvalidArgumentException('Invalid $version: Must be in integer "XYYZZ" format');
@@ -50,7 +49,7 @@ public function fetchAllForVersion(int|string $version): array {
5049

5150
$headers = [];
5251
if ($this->apiKey) {
53-
$headers[] = 'Authorization: Bearer '. $this->apiKey;
52+
$headers[] = 'Authorization: Bearer ' . $this->apiKey;
5453
}
5554

5655
$contents = $this->curlFetcher->get($baseUrl, $headers);
@@ -68,12 +67,12 @@ private function parseNewsPage(string $contents): array {
6867
$lastLine = null;
6968

7069
foreach ($contents as $lineNo => $line) {
71-
$lineNo = (int) $lineNo;
70+
$lineNo = (int)$lineNo;
7271
++$lineNo; // Line numbers start from 1, although the array index starts at 0
7372

74-
if (isset(self::STATIC_REPLACEMENTS[$line])) {
75-
$line = self::STATIC_REPLACEMENTS[$line];
76-
}
73+
if (isset(self::STATIC_REPLACEMENTS[$line])) {
74+
$line = self::STATIC_REPLACEMENTS[$line];
75+
}
7776

7877
// Should skip line?
7978
if ($this->skipLine($line, $lineNo)) {
@@ -95,7 +94,9 @@ private function parseNewsPage(string $contents): array {
9594
}
9695

9796
if (empty($cursorVersion)) {
98-
throw new \RuntimeException('Cursor version should not be empty when detecting a new extension change set');
97+
throw new \RuntimeException(
98+
'Cursor version should not be empty when detecting a new extension change set'
99+
);
99100
}
100101

101102
// Is this a new ext changeset header?
@@ -117,7 +118,13 @@ private function parseNewsPage(string $contents): array {
117118
}
118119

119120
if ($lastLine === null) {
120-
throw new \RuntimeException(\sprintf("Cursor last line number should not be empty when detecting a continuation of a change record on line %d:\r\n%s", $lineNo, $line));
121+
throw new \RuntimeException(
122+
\sprintf(
123+
"Cursor last line number should not be empty when detecting a continuation of a change record on line %d:\r\n%s",
124+
$lineNo,
125+
$line
126+
)
127+
);
121128
}
122129

123130
// is this continuation of a line?
@@ -156,7 +163,10 @@ private function releaseHeader(string $line): array|false {
156163
if (preg_match(self::REGEX_RELEASE_HEADER, $line, $matches)) {
157164
return [
158165
'version' => $matches['release_id'],
159-
'date' => str_contains($matches['date'], '?') ? null : "{$matches['year']} {$matches['month']} {$matches['day']}",
166+
'date' => str_contains(
167+
$matches['date'],
168+
'?'
169+
) ? null : "{$matches['year']} {$matches['month']} {$matches['day']}",
160170
'changes' => [],
161171
];
162172
}

src/NewsFormatter.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
<?php
22

33
namespace PHPWatch\PHPCommitBuilder;
4+
45
class NewsFormatter {
56
private array $releases;
67

78
use FormatterHelpers;
9+
810
public function __construct(array $releases) {
911
$this->releases = $releases;
1012
}
1113

1214
public function getNewsListForRelease(string $version): array {
1315
if (!isset($this->releases[$version])) {
14-
return [];
16+
return [];
1517
}
1618

1719
if (!isset($this->releases[$version]['version'], $this->releases[$version]['changes'])) {
18-
throw new \RuntimeException('Parsed array structure is invalid. Does not contain both version and changes fields');
20+
throw new \RuntimeException(
21+
'Parsed array structure is invalid. Does not contain both version and changes fields'
22+
);
1923
}
2024

2125
if (empty($this->releases[$version]['changes'])) {
@@ -38,9 +42,9 @@ public function getNewsListForRelease(string $version): array {
3842
public function getNewsListForReleaseMarkup(string $version): string {
3943
$release = $this->getNewsListForRelease($version);
4044

41-
if (empty($release)) {
42-
return '';
43-
}
45+
if (empty($release)) {
46+
return '';
47+
}
4448

4549
$output = '';
4650

@@ -58,7 +62,7 @@ public function getNewsListForReleaseMarkup(string $version): string {
5862
}
5963

6064
private function removeAuthorInBraces(string $change): string {
61-
$change = preg_replace('/(^(.*))( \([\w\p{L} ,-]+\).?$)/u', '$1', $change, 1, $count);
65+
$change = preg_replace('/(^(.*))( \([\w\p{L} ,-]+\).?$)/u', '$1', $change, 1, $count);
6266

6367
return $change;
6468
}

src/TagListFetcher.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace PHPWatch\PHPCommitBuilder;
44

55
use Ayesh\CurlFetcher\CurlFetcher;
6+
use stdClass;
67

78
class TagListFetcher {
89
private const API_ENDPOINT_TAG_LIST = 'https://api.github.com/repos/php/php-src/tags';
910

1011
private const REGEX_TAG_PATTERN = '/^php-\d\.\d\.(?:\d\d?|0(?:alpha\d|beta\d|rc\d|RC\d)?)$/i';
11-
private ?string $apiKey = null;
12+
private ?string $apiKey;
1213
private CurlFetcher $curlFetcher;
1314

1415
public function __construct(string $apiKey = null) {
@@ -18,8 +19,8 @@ public function __construct(string $apiKey = null) {
1819

1920
public function getReleaseTags(): array {
2021
$tags = $this->getAllTags();
21-
return array_filter($tags, static function(\stdClass $tag): bool {
22-
return (bool) preg_match(self::REGEX_TAG_PATTERN, $tag->name);
22+
return array_filter($tags, static function (stdClass $tag): bool {
23+
return (bool)preg_match(self::REGEX_TAG_PATTERN, $tag->name);
2324
});
2425
}
2526

@@ -36,11 +37,11 @@ public function getAllTags(): array {
3637

3738
do {
3839
$paramsUrl = http_build_query($params);
39-
$url = $baseUrl . '?' .$paramsUrl;
40+
$url = $baseUrl . '?' . $paramsUrl;
4041

4142
$headers = [];
4243
if ($this->apiKey) {
43-
$headers[] = 'Authorization: Bearer '. $this->apiKey;
44+
$headers[] = 'Authorization: Bearer ' . $this->apiKey;
4445
}
4546

4647
$data = $this->curlFetcher->getJson($url, $headers);
@@ -49,7 +50,6 @@ public function getAllTags(): array {
4950

5051
$params['page']++;
5152
$hardLimits--;
52-
5353
} while (count($data) >= 100 && $hardLimits > 0);
5454

5555
return $return;

0 commit comments

Comments
 (0)