Skip to content

Commit

Permalink
[+]: "str_contains_all" -> fix the loop
Browse files Browse the repository at this point in the history
-> thanks @ phpstan (v0.12) :)
  • Loading branch information
voku committed Dec 4, 2019
1 parent acc2404 commit 589f93e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 56 deletions.
2 changes: 1 addition & 1 deletion phpcs.php_cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ return PhpCsFixer\Config::create()
'phpdoc_inline_tag' => true,
'phpdoc_no_access' => true,
'phpdoc_no_alias_tag' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_no_empty_return' => fase,
'phpdoc_no_package' => true,
'phpdoc_no_useless_inheritdoc' => true,
'phpdoc_order' => true,
Expand Down
6 changes: 2 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ parameters:
autoload_files:
- %currentWorkingDirectory%/vendor/autoload.php
ignoreErrors:
# phpstan errors?
- '#between \*NEVER\* and#'
- '#Function mb_encode_numericentity invoked with.*#'
# ignore
- '#int<68472, 68479> and 68479 is always true#'
- '#no value type specified in iterable type#'
- '#voku\\helper\\UTF8::html_entity_decode\(\) expects string, array<string>|string given#'
- '#voku\\helper\\UTF8::substr_replace\(\) should return array<string>\|string but returns array<array<string>\|string>#'
- '#voku\\helper\\UTF8::encode\(\) should return string but returns array<string>\|string#'
Expand Down
97 changes: 50 additions & 47 deletions src/voku/helper/UTF8.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class UTF8
*
* INFO: https://en.wikipedia.org/wiki/Byte_order_mark
*
* @var array
* @var array<string, int>
*/
private static $BOM = [
"\xef\xbb\xbf" => 3, // UTF-8 BOM
Expand All @@ -37,7 +37,7 @@ final class UTF8
*
* url: http://www.w3schools.com/charsets/ref_utf_punctuation.asp
*
* @var array
* @var array<int, string>
*/
private static $WHITESPACE = [
// NUL Byte
Expand Down Expand Up @@ -95,7 +95,7 @@ final class UTF8
];

/**
* @var array
* @var array<string, string>
*/
private static $WHITESPACE_TABLE = [
'SPACE' => "\x20",
Expand Down Expand Up @@ -160,57 +160,57 @@ final class UTF8
];

/**
* @var array
* @var array<string, mixed>
*/
private static $SUPPORT = [];

/**
* @var array|null
* @var array<string, string>|null
*/
private static $BROKEN_UTF8_FIX;

/**
* @var array|null
* @var array<int, string>|null
*/
private static $WIN1252_TO_UTF8;

/**
* @var array|null
* @var array<int ,string>|null
*/
private static $INTL_TRANSLITERATOR_LIST;

/**
* @var array|null
* @var array<string>|null
*/
private static $ENCODINGS;

/**
* @var array|null
* @var array<string ,int>|null
*/
private static $ORD;

/**
* @var array|null
* @var array<string, string>|null
*/
private static $EMOJI;

/**
* @var array|null
* @var array<string>|null
*/
private static $EMOJI_VALUES_CACHE;

/**
* @var array|null
* @var array<string>|null
*/
private static $EMOJI_KEYS_CACHE;

/**
* @var array|null
* @var array<string>|null
*/
private static $EMOJI_KEYS_REVERSIBLE_CACHE;

/**
* @var array|null
* @var array<int, string>|null
*/
private static $CHR;

Expand Down Expand Up @@ -264,10 +264,10 @@ public static function add_bom_to_string(string $str): string
/**
* Changes all keys in an array.
*
* @param array $array <p>The array to work on</p>
* @param int $case [optional] <p> Either <strong>CASE_UPPER</strong><br>
* or <strong>CASE_LOWER</strong> (default)</p>
* @param string $encoding [optional] <p>Set the charset for e.g. "mb_" function</p>
* @param array<string, mixed> $array <p>The array to work on</p>
* @param int $case [optional] <p> Either <strong>CASE_UPPER</strong><br>
* or <strong>CASE_LOWER</strong> (default)</p>
* @param string $encoding [optional] <p>Set the charset for e.g. "mb_" function</p>
*
* @return string[]
* <p>An array with its keys lower- or uppercased.</p>
Expand Down Expand Up @@ -1833,27 +1833,27 @@ public static function filter_var(
*
* @see http://php.net/manual/en/function.filter-var-array.php
*
* @param array $data <p>
* An array with string keys containing the data to filter.
* </p>
* @param mixed $definition [optional] <p>
* An array defining the arguments. A valid key is a string
* containing a variable name and a valid value is either a
* filter type, or an
* array optionally specifying the filter, flags and options.
* If the value is an array, valid keys are filter
* which specifies the filter type,
* flags which specifies any flags that apply to the
* filter, and options which specifies any options that
* apply to the filter. See the example below for a better understanding.
* </p>
* <p>
* This parameter can be also an integer holding a filter constant. Then all values in the
* input array are filtered by this filter.
* </p>
* @param bool $add_empty [optional] <p>
* Add missing keys as <b>NULL</b> to the return value.
* </p>
* @param array<mixed> $data <p>
* An array with string keys containing the data to filter.
* </p>
* @param mixed $definition [optional] <p>
* An array defining the arguments. A valid key is a string
* containing a variable name and a valid value is either a
* filter type, or an
* array optionally specifying the filter, flags and options.
* If the value is an array, valid keys are filter
* which specifies the filter type,
* flags which specifies any flags that apply to the
* filter, and options which specifies any options that
* apply to the filter. See the example below for a better understanding.
* </p>
* <p>
* This parameter can be also an integer holding a filter constant. Then all values in the
* input array are filtered by this filter.
* </p>
* @param bool $add_empty [optional] <p>
* Add missing keys as <b>NULL</b> to the return value.
* </p>
*
* @return mixed an array containing the values of the requested variables on success, or <b>FALSE</b> on failure.
* An array value will be <b>FALSE</b> if the filter fails, or <b>NULL</b> if the variable is not
Expand Down Expand Up @@ -2147,6 +2147,8 @@ public static function getSupportInfo(string $key = null)
*
* @return array<string, string|null>
* <p>with this keys: 'ext', 'mime', 'type'</p>
*
* @phpstan-param array{ext: null|string, mime: null|string, type: null|string} $fallback
*/
public static function get_file_type(
string $str,
Expand All @@ -2169,8 +2171,8 @@ public static function get_file_type(
// DEBUG
//var_dump($str_info);

/** @var array|false $str_info - needed for PhpStan (stubs error) */
$str_info = \unpack('C2chars', $str_info);
/** @var array|false $str_info - needed for PhpStan (stubs error) */
if ($str_info === false) {
return $fallback;
}
Expand Down Expand Up @@ -4945,7 +4947,7 @@ public static function rtrim(string $str = '', string $chars = null): string
/**
* WARNING: Print native UTF-8 support (libs), e.g. for debugging.
*
* @psalm-suppress MissingReturnType
* @return void
*/
public static function showSupport()
{
Expand Down Expand Up @@ -5193,15 +5195,16 @@ public static function str_contains_all(

/** @noinspection LoopWhichDoesNotLoopInspection */
foreach ($needles as &$needle) {
if (!$needle) {
return false;
}

if ($case_sensitive) {
return \strpos($haystack, $needle) !== false;
/** @noinspection NestedPositiveIfStatementsInspection */
if (\strpos($haystack, $needle) === false) {
return false;
}
}

return \mb_stripos($haystack, $needle) !== false;
if (\mb_stripos($haystack, $needle) === false) {
return false;
}
}

return true;
Expand Down
9 changes: 5 additions & 4 deletions tests/Utf8TestsFromStringyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ static function (array $array) {
[false, 'Str contains foo bar', ['Foo', 'bar']],
[false, 'Str contains foo bar', ['foobar', 'bar']],
[false, 'Str contains foo bar', ['foo bar ', 'bar']],
[false, 'Str contains foo bar', ['Str', 'foo bar ', 'bar']],
[false, 'Ο συγγραφέας είπε', [' συγγραφέας ', ' συγγραφ '], true],
[false, 'å´¥©¨ˆßå˚ ∆∂˙©å∑¥øœ¬', [' ßå˚', ' ß '], true],
[true, 'Str contains foo bar', ['Foo bar', 'bar'], false],
Expand Down Expand Up @@ -1999,28 +2000,28 @@ public function testHasWhitespace($str1, $str2)
$result = UTF8::has_whitespace($str1);

static::assertInternalType('boolean', $result);
static::assertSame(true, $result, 'tested: ' . $str1);
static::assertTrue($result, 'tested: ' . $str1);

// ---

$result = UTF8::has_whitespace($str2);

static::assertInternalType('boolean', $result);
static::assertSame(true, $result, 'tested: ' . $str2);
static::assertTrue($result, 'tested: ' . $str2);

// ---

$result = UTF8::has_whitespace('');

static::assertInternalType('boolean', $result);
static::assertSame(false, $result);
static::assertFalse($result);

// ---

$result = UTF8::has_whitespace('abc-öäü');

static::assertInternalType('boolean', $result);
static::assertSame(false, $result);
static::assertFalse($result);
}

/**
Expand Down

0 comments on commit 589f93e

Please sign in to comment.