Skip to content

Commit 336a99e

Browse files
committed
Add type hints to extract() methods
Add ?string parameter and array return type hints to all extract() methods. This fixes PHP 8.1+ deprecation warnings about passing null to string functions.
1 parent f1bed21 commit 336a99e

13 files changed

Lines changed: 31 additions & 16 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ project adheres to [Semantic Versioning](http://semver.org/).
66
### Changed
77
- Minimum PHP version is now 7.2 (previously 5.4)
88
- Migrate CI from Travis to GitHub Actions
9+
- Add type hints to all `extract()` methods (`?string` parameter, `array` return)
910

1011
### Security
1112
- Upgrade PHPUnit to 8.5.52 to fix unsafe deserialization vulnerability

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<phpunit colors="true" bootstrap="vendor/autoload.php">
22
<testsuites>
3-
<testsuite>
3+
<testsuite name="Identifiers">
44
<directory>tests</directory>
55
</testsuite>
66
</testsuites>

src/AdsBibcode.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
class AdsBibcode
55
{
6-
public static function extract($str)
6+
public static function extract(?string $str): array
77
{
8+
$str = $str ?? '';
89
preg_match_all('/\b\d{4}[a-z][0-9a-z&.]{14}\b/ui', $str, $matches);
910

1011
return $matches[0];

src/ArxivId.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ class ArxivId
2929
}xiu
3030
EOT;
3131

32-
public static function extract($str)
32+
public static function extract(?string $str): array
3333
{
34+
$str = $str ?? '';
35+
3436
return array_merge(self::extractPre2007ArxivIds($str), self::extractPost2007ArxivIds($str));
3537
}
3638

src/Doi.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ class Doi
3232
}xu
3333
EOT;
3434

35-
public static function extract($str)
35+
public static function extract(?string $str): array
3636
{
37+
$str = $str ?? '';
3738
preg_match_all(self::REGEXP, mb_strtolower($str, 'UTF-8'), $matches);
3839

3940
return $matches[0];

src/Handle.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
class Handle
55
{
6-
public static function extract($str)
6+
public static function extract(?string $str): array
77
{
8+
$str = $str ?? '';
89
preg_match_all('#\b[\d.]+/\S+\b#u', $str, $matches);
910

1011
return $matches[0];

src/Isbn.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ class Isbn
4848
}xu
4949
EOT;
5050

51-
public static function extract($str)
51+
public static function extract(?string $str): array
5252
{
53+
$str = $str ?? '';
54+
5355
return array_merge(self::extractIsbnAs($str), self::extractIsbn13s($str), self::extractIsbn10s($str));
5456
}
5557

@@ -123,9 +125,9 @@ private static function stripHyphenation($match, $limit)
123125
return $isbn;
124126
}
125127

126-
private static function isValidIsbn13($str)
128+
private static function isValidIsbn13(?string $str): bool
127129
{
128-
if (strlen($str) !== 13) {
130+
if ($str === null || strlen($str) !== 13) {
129131
return false;
130132
}
131133

@@ -134,9 +136,9 @@ private static function isValidIsbn13($str)
134136
return $checkDigit === (int) $str[12];
135137
}
136138

137-
private static function isValidIsbn10($str)
139+
private static function isValidIsbn10(?string $str): bool
138140
{
139-
if (strlen($str) !== 10) {
141+
if ($str === null || strlen($str) !== 10) {
140142
return false;
141143
}
142144

src/NationalClinicalTrialId.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
class NationalClinicalTrialId
55
{
6-
public static function extract($str)
6+
public static function extract(?string $str): array
77
{
8+
$str = $str ?? '';
89
preg_match_all('/\bNCT\d+\b/ui', $str, $matches);
910

1011
return array_map('strtoupper', $matches[0]);

src/OrcidId.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
class OrcidId
66
{
7-
public static function extract($str)
7+
public static function extract(?string $str): array
88
{
9+
$str = $str ?? '';
910
preg_match_all('/\d{4}-\d{4}-\d{4}-\d{3}[\dX]/i', $str, $matches);
1011

1112
return array_filter(array_map('strtoupper', $matches[0]), [__CLASS__, 'isValid']);

src/PubmedId.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ class PubmedId
3838
}xu
3939
EOT;
4040

41-
public static function extract($str)
41+
public static function extract(?string $str): array
4242
{
43+
$str = $str ?? '';
44+
4345
return array_merge(self::extractPubmedIds($str), self::extractPubmedUris($str));
4446
}
4547

0 commit comments

Comments
 (0)