Completed
Branch 0.8-dev (5d9532)
by Kacper
02:49
created

DelegateRegexMatcher::match()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 6
nc 3
nop 2
crap 12
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Matcher;
17
18
use Kadet\Highlighter\Parser\TokenFactoryInterface;
19
20
class DelegateRegexMatcher implements MatcherInterface
21
{
22
    private $regex;
23
    private $callable;
24
25
    /**
26
     * RegexMatcher constructor.
27
     *
28
     * @param          $regex
29
     * @param callable $callable
30
     */
31
    public function __construct($regex, callable $callable)
32
    {
33
        $this->regex    = $regex;
34
        $this->callable = $callable;
35
    }
36
37
    /**
38
     * Matches all occurrences and returns token list
39
     *
40
     * @param string                $source Source to match tokens
41
     *
42
     * @param TokenFactoryInterface $factory
43
     *
44
     * @return \Generator
45
     */
46
    public function match($source, TokenFactoryInterface $factory)
47
    {
48
        preg_match_all($this->regex, $source, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
49
50
        $callable = $this->callable;
51
        foreach ($matches as $match) {
1 ignored issue
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> 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...
52
            foreach($callable($match, $factory) as $token) {
53
                yield $token;
54
            }
55
        }
56
    }
57
}
58