Skip to content

Commit

Permalink
Reduce use of substr for readability/performance. (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz authored Nov 13, 2023
1 parent 062386c commit ffdf126
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 11 deletions.
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

0 comments on commit ffdf126

Please sign in to comment.