Regex::matchesAny()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 7.9062

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
ccs 3
cts 8
cp 0.375
cc 4
nc 3
nop 2
crap 7.9062
1
<?php
2
/**
3
 * Utility class for some useful directory operations
4
 *
5
 * @author matias
6
 */
7
8
namespace Columnis\Utils;
9
10
class Regex
11
{
12
13 4
    public static function matchesAny($str, Array $expressions = null)
14
    {
15 4
        if (count($expressions) > 0) {
16
            foreach ($expressions as $expression) {
0 ignored issues
show
Bug introduced by
The expression $expressions of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
17
                if (preg_match($expression, $str) > 0) {
18
                    return true;
19
                }
20
            }
21
        }
22 4
        return false;
23
    }
24
}