Skip to content

Commit 7c7fb87

Browse files
committed
Code style update
1 parent 5f4111e commit 7c7fb87

2 files changed

Lines changed: 172 additions & 164 deletions

File tree

src/KeywordEnhancer.php

Lines changed: 73 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,77 @@
33
namespace PHPWatch\PhpKeywordEnhancer;
44

55
class KeywordEnhancer {
6-
protected const CODIFY_PATTERNS = [
7-
'/\b(?<!`)(?:zend|php)_[a-z_]+\(\)(?!`)/i', // zend_foo_bar()
8-
'/\b(?<!`)(?:zend|php)_[a-z_*]+\b(?![`.(])\*?/i', // zend_foo_bar()
9-
'/\b(?<![`\/])[a-z][a-z\d_-]+(.stubs?)?\.(phpt?|c|h)(?![`.?])/', // run-tests.php / foo.stub.php foo.stubs.php / test-foo-bar.phpt,
10-
'/\b(?<!`)ext\/[a-z_]+\b(?![`\/])/', // ext-names
11-
'/\b(?<!`)[A-Z][A-Za-z]+::[a-z][A-Za-z_]+\(\)(?![`\/])/', // Class::methods()
12-
'/\b(?<!`)[A-Z][A-Za-z]+::[A-Z_]+\b(?![`\/])/', // Class::CONSTANTS
13-
'/\b(?<!`)[A-Z][A-Za-z]+::[a-z][A-Za-z_\d]+\b(?![`\/])/', // Class::constants
14-
'/\b(?<!`)[a-z]+_[a-z]+(?:_[a-z_]+)?\(\)(?![`\/])/', // Functions with underscores and ()
15-
'/\b(?<!`)(?:ldap|ftp|array|mb|stream|open|hash)_[a-z_]+\d?\b(?![`\/])/', // Functions with underscores and no ()
16-
'/\b(?<!`)xleak\b(?![`\/])/i', // xleak
17-
];
18-
public static function enhance(string $inputText): string {
19-
return static::format($inputText);
20-
}
21-
22-
private static function format(string $inputText): string {
23-
24-
$inputText = static::linkToBug($inputText);
25-
$inputText = static::linkToGitHub($inputText);
26-
$inputText = static::codifyText($inputText);
27-
$inputText = static::linkToSecurityAnnouncements($inputText);
28-
29-
return $inputText;
30-
}
31-
32-
private static function linkToBug(string $subject): string {
33-
if (preg_match('/#[5-9]\d{4}/', $subject)) {
34-
return preg_replace('/#(\d{5})/', '[#$1](https://bugs.php.net/bug.php?id=$1)', $subject);
35-
}
36-
37-
return $subject;
38-
}
39-
40-
private static function linkToGitHub(string $subject): string {
41-
$subject = preg_replace('/\bGH-(\d{3,6})\b/', "[GH-$1](https://github.com/php/php-src/issues/$1)", $subject);
42-
$subject = preg_replace('/\b(([a-fA-F\d]){8})([a-fA-F\d]){4,32}\b/', "[$1](https://github.com/php/php-src/commit/$0)", $subject);
43-
44-
if (preg_match('/\(#(\d{3,6})\)$/', $subject)) {
45-
return preg_replace('/\(#(\d{3,6})\)$/', "in [GH-$1](https://github.com/php/php-src/pull/$1)", $subject);
46-
}
47-
48-
if (preg_match('/Closes GH-(\d{3,6})\D?/', $subject, $matches)) {
49-
$subject .= sprintf(" in [GH-%d](https://github.com/php/php-src/pull/%d)", $matches[1], $matches[1]);
50-
return $subject;
51-
}
52-
53-
return $subject;
54-
}
55-
56-
private static function linkToSecurityAnnouncements(string $inputText): string {
57-
if (preg_match('/(CVE-20\d\d-\d{1,5})\D/', $inputText)) {
58-
$inputText = preg_replace('/(CVE-20\d\d-\d{1,5})(\D)/', "[$1](https://nvd.nist.gov/vuln/detail/$1)$2", $inputText);
59-
}
60-
61-
return $inputText;
62-
}
63-
64-
private static function codifyText(string $input): string {
65-
66-
foreach (static::CODIFY_PATTERNS as $pattern) {
67-
$input = preg_replace($pattern, '`$0`', $input);
68-
}
69-
70-
return $input;
71-
}
6+
protected const CODIFY_PATTERNS = [
7+
'/\b(?<!`)(?:zend|php)_[a-z_]+\(\)(?!`)/i', // zend_foo_bar()
8+
'/\b(?<!`)(?:zend|php)_[a-z_*]+\b(?![`.(])\*?/i', // zend_foo_bar()
9+
'/\b(?<![`\/])[a-z][a-z\d_-]+(.stubs?)?\.(phpt?|c|h)(?![`.?])/', // run-tests.php / foo.stub.php foo.stubs.php / test-foo-bar.phpt,
10+
'/\b(?<!`)ext\/[a-z_]+\b(?![`\/])/', // ext-names
11+
'/\b(?<!`)[A-Z][A-Za-z]+::[a-z][A-Za-z_]+\(\)(?![`\/])/', // Class::methods()
12+
'/\b(?<!`)[A-Z][A-Za-z]+::[A-Z_]+\b(?![`\/])/', // Class::CONSTANTS
13+
'/\b(?<!`)[A-Z][A-Za-z]+::[a-z][A-Za-z_\d]+\b(?![`\/])/', // Class::constants
14+
'/\b(?<!`)[a-z]+_[a-z]+(?:_[a-z_]+)?\(\)(?![`\/])/', // Functions with underscores and ()
15+
'/\b(?<!`)(?:ldap|ftp|array|mb|stream|open|hash)_[a-z_]+\d?\b(?![`\/])/', // Functions with underscores and no ()
16+
'/\b(?<!`)xleak\b(?![`\/])/i', // xleak
17+
];
18+
19+
public static function enhance(string $inputText): string {
20+
return static::format($inputText);
21+
}
22+
23+
private static function format(string $inputText): string {
24+
$inputText = static::linkToBug($inputText);
25+
$inputText = static::linkToGitHub($inputText);
26+
$inputText = static::codifyText($inputText);
27+
$inputText = static::linkToSecurityAnnouncements($inputText);
28+
29+
return $inputText;
30+
}
31+
32+
private static function linkToBug(string $subject): string {
33+
if (preg_match('/#[5-9]\d{4}/', $subject)) {
34+
return preg_replace('/#(\d{5})/', '[#$1](https://bugs.php.net/bug.php?id=$1)', $subject);
35+
}
36+
37+
return $subject;
38+
}
39+
40+
private static function linkToGitHub(string $subject): string {
41+
$subject = preg_replace('/\bGH-(\d{3,6})\b/', "[GH-$1](https://github.com/php/php-src/issues/$1)", $subject);
42+
$subject = preg_replace(
43+
'/\b(([a-fA-F\d]){8})([a-fA-F\d]){4,32}\b/',
44+
"[$1](https://github.com/php/php-src/commit/$0)",
45+
$subject
46+
);
47+
48+
if (preg_match('/\(#(\d{3,6})\)$/', $subject)) {
49+
return preg_replace('/\(#(\d{3,6})\)$/', "in [GH-$1](https://github.com/php/php-src/pull/$1)", $subject);
50+
}
51+
52+
if (preg_match('/Closes GH-(\d{3,6})\D?/', $subject, $matches)) {
53+
$subject .= sprintf(" in [GH-%d](https://github.com/php/php-src/pull/%d)", $matches[1], $matches[1]);
54+
return $subject;
55+
}
56+
57+
return $subject;
58+
}
59+
60+
private static function linkToSecurityAnnouncements(string $inputText): string {
61+
if (preg_match('/(CVE-20\d\d-\d{1,5})\D/', $inputText)) {
62+
$inputText = preg_replace(
63+
'/(CVE-20\d\d-\d{1,5})(\D)/',
64+
"[$1](https://nvd.nist.gov/vuln/detail/$1)$2",
65+
$inputText
66+
);
67+
}
68+
69+
return $inputText;
70+
}
71+
72+
private static function codifyText(string $input): string {
73+
foreach (static::CODIFY_PATTERNS as $pattern) {
74+
$input = preg_replace($pattern, '`$0`', $input);
75+
}
76+
77+
return $input;
78+
}
7279
}

tests/KeywordEnhancerTest.php

Lines changed: 99 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -6,112 +6,113 @@
66
use PHPUnit\Framework\TestCase;
77

88
class KeywordEnhancerTest extends TestCase {
9-
public static function dataLinkToSecurityAnnouncements(): array {
10-
return [
11-
[
12-
'eloadfont(). (CVE-2022-31630) (cmb)',
13-
'eloadfont(). ([CVE-2022-31630](https://nvd.nist.gov/vuln/detail/CVE-2022-31630)) (cmb)'
14-
],
15-
[
16-
'eloadfont(). (CVE-1987-31630) (cmb)',
17-
'eloadfont(). (CVE-1987-31630) (cmb)'
18-
],
19-
[
20-
'Fixed bug #81726: phar wrapper: DOS when using quine gzip file. (CVE-2022-31628). (cmb)',
21-
'Fixed bug [#81726](https://bugs.php.net/bug.php?id=81726): phar wrapper: DOS when using quine gzip file. ([CVE-2022-31628](https://nvd.nist.gov/vuln/detail/CVE-2022-31628)). (cmb)'
22-
],
23-
];
24-
}
9+
public static function dataLinkToSecurityAnnouncements(): array {
10+
return [
11+
[
12+
'eloadfont(). (CVE-2022-31630) (cmb)',
13+
'eloadfont(). ([CVE-2022-31630](https://nvd.nist.gov/vuln/detail/CVE-2022-31630)) (cmb)',
14+
],
15+
[
16+
'eloadfont(). (CVE-1987-31630) (cmb)',
17+
'eloadfont(). (CVE-1987-31630) (cmb)',
18+
],
19+
[
20+
'Fixed bug #81726: phar wrapper: DOS when using quine gzip file. (CVE-2022-31628). (cmb)',
21+
'Fixed bug [#81726](https://bugs.php.net/bug.php?id=81726): phar wrapper: DOS when using quine gzip file. ([CVE-2022-31628](https://nvd.nist.gov/vuln/detail/CVE-2022-31628)). (cmb)',
22+
],
23+
];
24+
}
2525

26-
public static function dataLinksToBugsPhp(): array {
27-
return [
28-
[
29-
'Fixed bug #81739: OOB read due to',
30-
'Fixed bug [#81739](https://bugs.php.net/bug.php?id=81739): OOB read due to'
31-
],
32-
[
33-
'Fixed bug #45: OOB read due to',
34-
'Fixed bug #45: OOB read due to'
35-
],
36-
[
37-
'Fixed bug #5943: OOB read due to',
38-
'Fixed bug #5943: OOB read due to'
39-
],
40-
[
41-
'Fixed bug #11453: OOB read due to',
42-
'Fixed bug #11453: OOB read due to'
43-
],
44-
[
45-
'Fixed bug #123456: OOB read due to',
46-
'Fixed bug #123456: OOB read due to'
47-
],
48-
];
49-
}
26+
public static function dataLinksToBugsPhp(): array {
27+
return [
28+
[
29+
'Fixed bug #81739: OOB read due to',
30+
'Fixed bug [#81739](https://bugs.php.net/bug.php?id=81739): OOB read due to',
31+
],
32+
[
33+
'Fixed bug #45: OOB read due to',
34+
'Fixed bug #45: OOB read due to',
35+
],
36+
[
37+
'Fixed bug #5943: OOB read due to',
38+
'Fixed bug #5943: OOB read due to',
39+
],
40+
[
41+
'Fixed bug #11453: OOB read due to',
42+
'Fixed bug #11453: OOB read due to',
43+
],
44+
[
45+
'Fixed bug #123456: OOB read due to',
46+
'Fixed bug #123456: OOB read due to',
47+
],
48+
];
49+
}
5050

51-
public static function dataLinkToGitHub(): array {
52-
return [
53-
[
54-
'Fixed bug GH-1: OOB read due to',
55-
'Fixed bug GH-1: OOB read due to'
56-
],
57-
[
58-
'Fixed bug GH-123: OOB read due to',
59-
'Fixed bug [GH-123](https://github.com/php/php-src/issues/123): OOB read due to'
60-
],
61-
];
62-
}
51+
public static function dataLinkToGitHub(): array {
52+
return [
53+
[
54+
'Fixed bug GH-1: OOB read due to',
55+
'Fixed bug GH-1: OOB read due to',
56+
],
57+
[
58+
'Fixed bug GH-123: OOB read due to',
59+
'Fixed bug [GH-123](https://github.com/php/php-src/issues/123): OOB read due to',
60+
],
61+
];
62+
}
6363

64-
public static function dataCodifyText(): array {
65-
return [
66-
[ 'password_hash()', '`password_hash()`' ],
67-
[ '`password_hash()`', '`password_hash()`' ],
68-
[
69-
'`password_hash()` and password_hash()',
70-
'`password_hash()` and `password_hash()`'
71-
],
72-
];
73-
}
64+
public static function dataCodifyText(): array {
65+
return [
66+
['password_hash()', '`password_hash()`'],
67+
['`password_hash()`', '`password_hash()`'],
68+
[
69+
'`password_hash()` and password_hash()',
70+
'`password_hash()` and `password_hash()`',
71+
],
72+
['password_hash()', '`password_hash()`'],
73+
];
74+
}
7475

75-
/**
76-
* @covers \PHPWatch\PhpKeywordEnhancer\KeywordEnhancer::enhance
77-
*/
78-
public function testReturnsVerbatimOnEmptyStrings(): void {
79-
self::assertSame( '', KeywordEnhancer::enhance( '' ) );
80-
self::assertSame( ' ', KeywordEnhancer::enhance( ' ' ) );
81-
self::assertSame( 'test', KeywordEnhancer::enhance( 'test' ) );
82-
}
76+
/**
77+
* @covers \PHPWatch\PhpKeywordEnhancer\KeywordEnhancer::enhance
78+
*/
79+
public function testReturnsVerbatimOnEmptyStrings(): void {
80+
self::assertSame('', KeywordEnhancer::enhance(''));
81+
self::assertSame(' ', KeywordEnhancer::enhance(' '));
82+
self::assertSame('test', KeywordEnhancer::enhance('test'));
83+
}
8384

84-
/**
85-
* @covers \PHPWatch\PhpKeywordEnhancer\KeywordEnhancer::linkToBug
86-
* @dataProvider dataLinksToBugsPhp
87-
*/
88-
public function testLinksToBugsPhp( string $input, string $expected ): void {
89-
self::assertSame( $expected, KeywordEnhancer::enhance( $input ) );
90-
}
85+
/**
86+
* @covers \PHPWatch\PhpKeywordEnhancer\KeywordEnhancer::linkToBug
87+
* @dataProvider dataLinksToBugsPhp
88+
*/
89+
public function testLinksToBugsPhp(string $input, string $expected): void {
90+
self::assertSame($expected, KeywordEnhancer::enhance($input));
91+
}
9192

92-
/**
93-
* @covers \PHPWatch\PhpKeywordEnhancer\KeywordEnhancer::linkToGitHub
94-
* @dataProvider dataLinkToGitHub
95-
*/
96-
public function testLinkToGitHub( string $input, string $expected ): void {
97-
self::assertSame( $expected, KeywordEnhancer::enhance( $input ) );
98-
}
93+
/**
94+
* @covers \PHPWatch\PhpKeywordEnhancer\KeywordEnhancer::linkToGitHub
95+
* @dataProvider dataLinkToGitHub
96+
*/
97+
public function testLinkToGitHub(string $input, string $expected): void {
98+
self::assertSame($expected, KeywordEnhancer::enhance($input));
99+
}
99100

100-
/**
101-
* @covers \PHPWatch\PhpKeywordEnhancer\KeywordEnhancer::linkToSecurityAnnouncements
102-
* @dataProvider dataLinkToSecurityAnnouncements
103-
*/
104-
public function testLinkToSecurityAnnouncements( string $input, string $expected ): void {
105-
self::assertSame( $expected, KeywordEnhancer::enhance( $input ) );
106-
}
101+
/**
102+
* @covers \PHPWatch\PhpKeywordEnhancer\KeywordEnhancer::linkToSecurityAnnouncements
103+
* @dataProvider dataLinkToSecurityAnnouncements
104+
*/
105+
public function testLinkToSecurityAnnouncements(string $input, string $expected): void {
106+
self::assertSame($expected, KeywordEnhancer::enhance($input));
107+
}
107108

108-
/**
109-
* @covers \PHPWatch\PhpKeywordEnhancer\KeywordEnhancer::codifyText
110-
* @dataProvider dataCodifyText
111-
*/
112-
public function testCodifyText( string $input, string $expected ): void {
113-
self::assertSame( $expected, KeywordEnhancer::enhance( $input ) );
114-
}
109+
/**
110+
* @covers \PHPWatch\PhpKeywordEnhancer\KeywordEnhancer::codifyText
111+
* @dataProvider dataCodifyText
112+
*/
113+
public function testCodifyText(string $input, string $expected): void {
114+
self::assertSame($expected, KeywordEnhancer::enhance($input));
115+
}
115116

116117

117118
}

0 commit comments

Comments
 (0)