Completed
Push — master ( 216072...caf6f7 )
by Team eFabrica
02:21
created

addFalsePositivePattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\TokenModifier;
4
5
use Efabrica\TranslationsAutomatization\Tokenizer\TokenCollection;
6
7
class FalsePositiveRemoverTokenModifier implements TokenModifierInterface
8
{
9
    private $falsePositivePatterns = [];
10
11 2
    public function addFalsePositivePattern(string $blockPattern, string $textPattern)
12
    {
13 2
        $this->falsePositivePatterns[] = [
14 2
            'block_pattern' => $blockPattern,
15 2
            'text_pattern' => $textPattern,
16
        ];
17 2
        return $this;
18
    }
19
20 2
    public function modifyAll(TokenCollection $tokenCollection): TokenCollection
21
    {
22 2
        $newTokenCollection = new TokenCollection($tokenCollection->getFilePath());
23 2
        foreach ($tokenCollection->getTokens() as $token) {
24 2
            $originalBlock = $token->getOriginalBlock();
25 2
            $originalText = $token->getOriginalText();
26
27 2
            foreach ($this->falsePositivePatterns as $falsePositivePattern) {
28 2
                if (preg_match($falsePositivePattern['block_pattern'], $originalBlock) && preg_match($falsePositivePattern['text_pattern'], $originalText)) {
29 2
                    continue;
30
                }
31 2
                $newTokenCollection->addToken($token);
32
            }
33
        }
34 2
        return $newTokenCollection;
35
    }
36
}
37