Skip to content

Commit

Permalink
Fix the Undefined variable: $isEqual PHPStan error. Change of `Meri…
Browse files Browse the repository at this point in the history
…too\Common\Utilities\Regex::arrayFilter()` method signature.
  • Loading branch information
meritoo committed Nov 14, 2024
1 parent 9f71420 commit 8e43dcf
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 109 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ Common and useful classes, methods, exceptions etc.
}
```

5. Change of `Meritoo\Common\Utilities\Regex::arrayFilter()` method signature:
1. 3rd argument, the filter, is now a `\Closure|string`. Previously it was `string`.
2. 4th argument has been removed (not necessary).

Before:

```php
public static function arrayFilter(array $array, string $arrayColumnKey, string $filterExpression, bool $itsRegularExpression = false): array
```

After:

```php
public static function arrayFilter(array $array, string $arrayColumnKey, \Closure|string $filter): array
```

# 1.2.2

1. Refactor the `Reflection` class for PHP `8.0`+
Expand Down
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

### Fix PHPStan errors

- [ ] `Undefined variable: $isEqual` - replace `eval()` with callable
- [x] `Undefined variable: $isEqual` - replace `eval()` with callable
in [src/Utilities/Regex.php:151](./src/Utilities/Regex.php)
- [x] `Unsafe usage of new static()` - chose one
of [possible solutions](https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static) -
in [src/Type/Base/BaseType.php:37](./src/Type/Base/BaseType.php)
- [x] `Unsafe usage of new static()` - chose one
of [possible solutions](https://phpstan.org/blog/solving-phpstan-error-unsafe-usage-of-new-static) -
in [src/Exception/Base/UnknownTypeException.php:40](./src/Exception/Base/UnknownTypeException.php)
- [ ] Clean and remove the [phpstan-baseline.neon](phpstan-baseline.neon) file finally
- [x] Clean and remove the [phpstan-baseline.neon](phpstan-baseline.neon) file finally

### Refactoring

Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
includes:
- phpstan-baseline.neon

parameters:
level: 1
paths:
Expand Down
30 changes: 7 additions & 23 deletions src/Utilities/Regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,15 @@ public static function areValidHtmlAttributes(string $htmlAttributes): bool
* Expression can be simple compare expression, like " == 2", or regular expression.
* Returns filtered array.
*
* @param array $array The 2-dimensional array that should be filtered
* @param string $arrayColumnKey Column name
* @param string $filterExpression Simple filter expression (e.g. "== 2" or "!= \'home\'") or regular
* expression (e.g. "/\d+/" or "/[a-z]+[,;]{2,}/")
* @param bool $itsRegularExpression (optional) If is set to true, means that filter expression is a regular
* expression. Otherwise - not (default behaviour).
* @param array $array The 2-dimensional array that should be filtered
* @param string $arrayColumnKey Column name which value is verified by the filter
* @param \Closure|string $filter A filter that might be regular expression (string) or anonymous function (closure)
* @return array
*/
public static function arrayFilter(
array $array,
string $arrayColumnKey,
string $filterExpression,
bool $itsRegularExpression = false
\Closure|string $filter,
): array {
/*
* No elements?
Expand All @@ -132,25 +128,13 @@ public static function arrayFilter(
}

$value = $item[$arrayColumnKey];
$itsRegularExpression = \is_string($filter);

if ($itsRegularExpression) {
$matchesCount = preg_match($filterExpression, $value);
$matchesCount = preg_match($filter, $value);
$remove = 0 === $matchesCount;
} else {
if (is_string($value)) {
$value = sprintf('\'%s\'', $value);
} elseif (is_bool($value)) {
if (true === $value) {
$value = 'true';
} else {
$value = 'false';
}
}

eval(sprintf('$isEqual = %s%s;', $value, $filterExpression));

/** @var bool $isEqual */
$remove = !$isEqual;
$remove = !$filter($value);
}

if ($remove) {
Expand Down
Loading

0 comments on commit 8e43dcf

Please sign in to comment.