Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce use of substr for readability/performance. #14

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file, in reverse
### Changed

- The minimum PHP version requirement has been raised to 8.0.
- The str_starts_with and str_ends_with functions have been used instead of substr where possible.

### Deprecated

Expand Down
8 changes: 3 additions & 5 deletions src/VuFindCode/EAN.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,8 @@ public static function getEAN13CheckDigit($ean)
public static function isValidEAN13($ean)
{
$ean = static::normalizeEAN($ean);
if (strlen($ean) != 13) {
return false;
}
return
substr($ean, 12) == self::getEAN13CheckDigit(substr($ean, 0, 12));
return (strlen($ean) != 13)
? false
: str_ends_with($ean, self::getEAN13CheckDigit(substr($ean, 0, 12)));
}
}
9 changes: 4 additions & 5 deletions src/VuFindCode/ISBN.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function get10()
return $this->raw;
} elseif (
strlen($this->raw) == 13
&& substr($this->raw, 0, 3) == '978'
&& str_starts_with($this->raw, '978')
) {
// Is it a Bookland EAN? If so, we can convert to ISBN-10.
$start = substr($this->raw, 3, 9);
Expand Down Expand Up @@ -176,10 +176,9 @@ public static function getISBN10CheckDigit($isbn)
public static function isValidISBN10($isbn)
{
$isbn = self::normalizeISBN($isbn);
if (strlen($isbn) != 10) {
return false;
}
return substr($isbn, 9) == self::getISBN10CheckDigit(substr($isbn, 0, 9));
return (strlen($isbn) != 10)
? false
: str_ends_with($isbn, self::getISBN10CheckDigit(substr($isbn, 0, 9)));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/VuFindCode/ISMN.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function get10()
return $this->raw;
} elseif (
strlen($this->raw) == 13
&& substr($this->raw, 0, 3) == '979'
&& str_starts_with($this->raw, '979')
) {
// Is it a music EAN? If so, we can convert to ISMN-10.
$start = 'M' . substr($this->raw, 4, 8);
Expand Down