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

FalsePositiveRemoverTokenModifier   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A addFalsePositivePattern() 0 7 1
B modifyAll() 0 15 5
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