addTokensToNewCollection()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

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