Skip to content

Commit

Permalink
Merge pull request #9 from phpfui/PHP71
Browse files Browse the repository at this point in the history
Modernize to PHP 7.1
  • Loading branch information
zbateson authored Jan 11, 2023
2 parents 34a9930 + 39adeb4 commit faf35dd
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 105 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1, 7.0, 5.6, 5.5, 5.4]
php: [8.2, 8.1, 8.0, 7.4, 7.3, 7.2, 7.1]
stability: [prefer-stable]

name: P${{ matrix.php }}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ composer require zbateson/mb-wrapper

## Requirements

mb-wrapper requires PHP 5.4 or newer. Tested on PHP 5.4, 5.5, 5.6, 7, 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, and 8.2 on GitHub Actions.
mb-wrapper requires PHP 7.1 or newer. Tested on PHP 7.1, 7.2, 7.3, 7.4, 8.0, 8.1, and 8.2 on GitHub Actions.

## Description

Expand Down
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
}
],
"require": {
"php": ">=5.4",
"php": ">=7.1",
"symfony/polyfill-mbstring": "^1.9",
"symfony/polyfill-iconv": "^1.9"
},
"require-dev": {
"sanmai/phpunit-legacy-adapter": "^6.3 || ^8"
"phpunit/phpunit": "<=9.0",
"friendsofphp/php-cs-fixer": "*",
"phpstan/phpstan": "*"
},
"suggest": {
"ext-mbstring": "For best support/performance",
Expand Down
8 changes: 8 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
level: 6
errorFormat: raw
editorUrl: '%%file%% %%line%% %%column%%: %%error%%'
paths:
- src
- tests

95 changes: 38 additions & 57 deletions src/MbWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*/

namespace ZBateson\MbWrapper;

/**
Expand All @@ -24,7 +25,7 @@
class MbWrapper
{
/**
* @var array aliased charsets supported by mb_convert_encoding.
* @var array<string, string> aliased charsets supported by mb_convert_encoding.
* The alias is stripped of any non-alphanumeric characters (so CP367
* is equal to CP-367) when comparing.
* Some of these translations are already supported by
Expand Down Expand Up @@ -186,7 +187,7 @@ class MbWrapper
];

/**
* @var array aliased charsets supported by iconv.
* @var array<string, string> aliased charsets supported by iconv.
*/
public static $iconvAliases = [
// iconv aliases -- a lot of these may already be supported
Expand Down Expand Up @@ -278,13 +279,6 @@ class MbWrapper
'1258' => 'CP1258',
];

/**
* @var string[] An array of encodings supported by the mb_* extension, as
* returned by mb_list_encodings(), with the key set to the charset's
* name afte
*/
private static $mbListedEncodings;

/**
* @var string[] cached lookups for quicker retrieval
*/
Expand All @@ -294,15 +288,22 @@ class MbWrapper
'ISO88591' => 'ISO-8859-1',
];

/**
* @var string[] An array of encodings supported by the mb_* extension, as
* returned by mb_list_encodings(), with the key set to the charset's
* name afte
*/
private static $mbListedEncodings;

/**
* Initializes the static mb_* encoding array.
*/
public function __construct()
{
if (self::$mbListedEncodings === null) {
$cs = mb_list_encodings();
$cs = \mb_list_encodings();
$keys = $this->getNormalizedCharset($cs);
self::$mbListedEncodings = array_combine($keys, $cs);
self::$mbListedEncodings = \array_combine($keys, $cs);
}
}

Expand All @@ -316,12 +317,12 @@ public function __construct()
private function getNormalizedCharset($charset)
{
$upper = null;
if (is_array($charset)) {
$upper = array_map('strtoupper', $charset);
if (\is_array($charset)) {
$upper = \array_map('strtoupper', $charset);
} else {
$upper = strtoupper($charset);
$upper = \strtoupper($charset);
}
return preg_replace('/[^A-Z0-9]+/', '', $upper);
return \preg_replace('/[^A-Z0-9]+/', '', $upper);
}

/**
Expand All @@ -332,10 +333,8 @@ private function getNormalizedCharset($charset)
* back to iconv if not. If the source or destination character sets aren't
* supported, a blank string is returned.
*
* @param string $str
* @return string
*/
public function convert($str, $fromCharset, $toCharset)
public function convert(string $str, string $fromCharset, string $toCharset) : string
{
// there may be some mb-supported encodings not supported by iconv (on my libiconv for instance
// HZ isn't supported), and so it may happen that failing an mb_convert_encoding, an iconv
Expand All @@ -347,15 +346,15 @@ public function convert($str, $fromCharset, $toCharset)

if ($str !== '') {
if ($from !== false && $to === false) {
$str = mb_convert_encoding($str, 'UTF-8', $from);
return iconv('UTF-8', $this->getIconvAlias($toCharset) . '//TRANSLIT//IGNORE', $str);
$str = \mb_convert_encoding($str, 'UTF-8', $from);
return \iconv('UTF-8', $this->getIconvAlias($toCharset) . '//TRANSLIT//IGNORE', $str);
} elseif ($from === false && $to !== false) {
$str = iconv($this->getIconvAlias($fromCharset), 'UTF-8//TRANSLIT//IGNORE', $str);
return mb_convert_encoding($str, $to, 'UTF-8');
$str = \iconv($this->getIconvAlias($fromCharset), 'UTF-8//TRANSLIT//IGNORE', $str);
return \mb_convert_encoding($str, $to, 'UTF-8');
} elseif ($from !== false && $to !== false) {
return mb_convert_encoding($str, $to, $from);
return \mb_convert_encoding($str, $to, $from);
}
return iconv(
return \iconv(
$this->getIconvAlias($fromCharset),
$this->getIconvAlias($toCharset) . '//TRANSLIT//IGNORE',
$str
Expand All @@ -369,52 +368,39 @@ public function convert($str, $fromCharset, $toCharset)
*
* Either uses mb_check_encoding, or iconv if it's not a supported mb
* encoding.
*
* @param type $str
* @param type $charset
*/
public function checkEncoding($str, $charset)
public function checkEncoding(string $str, string $charset) : bool
{
$mb = $this->getMbCharset($charset);
if ($mb !== false) {
return mb_check_encoding($str, $mb);
return \mb_check_encoding($str, $mb);
}
$ic = $this->getIconvAlias($charset);
return (@iconv($ic, $ic, $str) !== false);
return (@\iconv($ic, $ic, $str) !== false);
}

/**
* Uses either mb_strlen or iconv_strlen to return the number of characters
* in the passed $str for the given $charset
*
* @param string $str
* @param string $charset
* @return int
*/
public function getLength($str, $charset)
public function getLength(string $str, string $charset) : int
{
$mb = $this->getMbCharset($charset);
if ($mb !== false) {
return mb_strlen($str, $mb);
return \mb_strlen($str, $mb);
}
return iconv_strlen($str, $this->getIconvAlias($charset));
return \iconv_strlen($str, $this->getIconvAlias($charset));
}

/**
* Uses either mb_substr or iconv_substr to create and return a substring of
* the passed $str.
*
* @param string $str
* @param string $charset
* @param int $start
* @param int $length
* @return string
*/
public function getSubstr($str, $charset, $start, $length = null)
public function getSubstr(string $str, string $charset, int $start, ?int $length = null) : string
{
$mb = $this->getMbCharset($charset);
if ($mb !== false) {
return mb_substr($str, $start, $length, $mb);
return \mb_substr($str, $start, $length, $mb);
}
$ic = $this->getIconvAlias($charset);
if ($ic === 'CP1258') {
Expand All @@ -424,13 +410,11 @@ public function getSubstr($str, $charset, $start, $length = null)
return $this->convert($this->getSubstr($str, 'UTF-8', $start, $length), 'UTF-8', $ic);
}
if ($length === null) {
$length = iconv_strlen($str, $ic);
$length = \iconv_strlen($str, $ic);
}
return iconv_substr($str, $start, $length, $ic);
return \iconv_substr($str, $start, $length, $ic);
}



/**
* Looks up a charset from mb_list_encodings and identified aliases,
* checking if the lookup has been cached already first.
Expand All @@ -439,16 +423,14 @@ public function getSubstr($str, $charset, $start, $length = null)
*
* On success, the method will return the charset name as accepted by mb_*.
*
* @param string $cs
* @param bool $mbSupported
* @return string|bool
*/
private function getMbCharset($cs)
private function getMbCharset(string $cs)
{
$normalized = $this->getNormalizedCharset($cs);
if (array_key_exists($normalized, self::$mbListedEncodings)) {
if (\array_key_exists($normalized, self::$mbListedEncodings)) {
return self::$mbListedEncodings[$normalized];
} elseif (array_key_exists($normalized, self::$mbAliases)) {
} elseif (\array_key_exists($normalized, self::$mbAliases)) {
return self::$mbAliases[$normalized];
}
return false;
Expand All @@ -458,13 +440,12 @@ private function getMbCharset($cs)
* Looks up the passed charset in self::$iconvAliases, returning the mapped
* charset if applicable. Otherwise returns charset.
*
* @param string $cs
* @return string the mapped charset (if mapped) or $cs otherwise
*/
private function getIconvAlias($cs)
private function getIconvAlias(string $cs) : string
{
$normalized = $this->getNormalizedCharset($cs);
if (array_key_exists($normalized, self::$iconvAliases)) {
if (\array_key_exists($normalized, self::$iconvAliases)) {
return static::$iconvAliases[$normalized];
}
return $cs;
Expand Down
Loading

0 comments on commit faf35dd

Please sign in to comment.