Passed
Branch fuck-54 (18c095)
by Kacper
03:37
created

Matcher/CommentMatcher.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2015, 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
19
use Kadet\Highlighter\Parser\TokenFactoryInterface;
20
21
class CommentMatcher implements MatcherInterface
22
{
23
    private $singleLine = [];
24
    private $multiLine  = [];
25
26
    /**
27
     * CommentMatcher constructor.
28
     *
29
     * @param array $singleLine
30
     * @param array $multiLine
31
     */
32 4
    public function __construct(array $singleLine, array $multiLine)
33
    {
34 4
        $this->singleLine = $singleLine;
35 4
        $this->multiLine = $multiLine;
36 4
    }
37
38
39
    /**
40
     * Matches all occurrences and returns token list
41
     *
42
     * @param string                $source Source to match tokens
43
     *
44
     * @param TokenFactoryInterface $factory
45
     *
46
     * @return \Iterator
47
     */
48 4
    public function match($source, TokenFactoryInterface $factory)
49
    {
50 4
        $all = [];
51
52 4
        foreach ($this->multiLine as $name => $comment) {
53
            $comment = array_map(function ($e) { return preg_quote($e, '/'); }, $comment);
54
55 2
            $all[] = [$name, "/{$comment[0]}(.*?){$comment[1]}/ms"];
56 4
        }
57
58 4
        foreach ($this->singleLine as $name => $comment) {
59 2
            $comment = preg_quote($comment, '/');
60 2
            $all[] = [$name, "/{$comment}(.*)/"];
61 4
        }
62
63 4
        foreach ($all as $i => $comment) {
64 4
            $matches = [];
65
66 4
            $name = $comment[0];
67 4
            $name = is_string($name) ? $name : null;
68 4
            $regex = $comment[1];
69
70 4
            if (preg_match_all($regex, $source, $matches, PREG_OFFSET_CAPTURE)) {
71 4 View Code Duplication
                foreach ($matches[0] as $match) {
1 ignored issue
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72 4
                    yield $factory->create(['pos' => $match[1], 'length' => strlen($match[0]), 'index' => $i, $name]);
73 4
                }
74 4
            }
75 4
        }
76
    }
77
}