Passed
Pull Request — master (#41)
by Def
02:21 queued 18s
created

TranslationExtractor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 9
eloc 37
c 2
b 1
f 0
dl 0
loc 93
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 4
A hasSkippedLines() 0 3 1
A extract() 0 20 3
A getSkippedLines() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Translator\Extractor;
6
7
use Yiisoft\Files\FileHelper;
8
use Yiisoft\Files\PathMatcher\PathMatcher;
9
10
/**
11
 * Extractor messages
12
 */
13
final class TranslationExtractor
14
{
15
    private string $path;
16
17
    /** @var string[] */
18
    private array $only = ['**.php'];
19
20
    /** @psalm-var array<string, array<array<string|array{0: int, 1: string, 2: int}>>> */
21
    private array $skippedLines = [];
22
23
    /** @psalm-suppress PropertyNotSetInConstructor */
24
    private ?ContentParser $parser;
25
26
    /** @var string[] */
27
    private array $except = [
28
        '.svn',
29
        '.git',
30
        '.gitignore',
31
        '.gitkeep',
32
        '.hgignore',
33
        '.hgkeep',
34
        '/messages',
35
    ];
36
37
    private static array $commaSpare = [
0 ignored issues
show
introduced by
The private property $commaSpare is not used, and could be removed.
Loading history...
38
        ')' => '(',
39
        ']' => '[',
40
        '}' => '{',
41
    ];
42
43
    /**
44
     * TranslationExtractor constructor.
45
     *
46
     * @param string $path
47
     * @param string[]|null $only
48
     * @param string[]|null $except
49
     */
50 7
    public function __construct(string $path, ?array $only = null, ?array $except = null)
51
    {
52 7
        if (!is_dir($path)) {
53 1
            throw new \RuntimeException(sprintf('Directory "%s" does not exist.', $path));
54
        }
55
56 6
        $this->path = $path;
57
58 6
        if (isset($only)) {
59 2
            $this->only = $only;
60
        }
61
62 6
        if (isset($except)) {
63 2
            $this->except = $except;
64
        }
65 6
    }
66
67
    /**
68
     * @param string|null $defaultCategory
69
     * @param string|null $translator
70
     *
71
     * @return array
72
     */
73 6
    public function extract(?string $defaultCategory = null, ?string $translator = null): array
74
    {
75 6
        $messages = [];
76 6
        $this->parser = new ContentParser($defaultCategory, $translator);
77
78 6
        $files = FileHelper::findFiles($this->path, [
79 6
            'filter' => (new pathMatcher())->only(...$this->only)->except(...$this->except),
80
            'recursive' => true,
81
        ]);
82
83
        /** @var string[] $files */
84 6
        foreach ($files as $file) {
85 6
            $fileContent = file_get_contents($file);
86 6
            $messages = array_merge_recursive($messages, $this->parser->extract($fileContent));
87 6
            if ($this->parser->hasSkippedLines()) {
88 4
                $this->skippedLines[$file] = $this->parser->getSkippedLines();
89
            }
90
        }
91
92 6
        return $messages;
93
    }
94
95 6
    public function hasSkippedLines(): bool
96
    {
97 6
        return !empty($this->skippedLines);
98
    }
99
100
    /**
101
     * @psalm-return array<string, array<array<string|array{0: int, 1: string, 2: int}>>>
102
     */
103 4
    public function getSkippedLines(): array
104
    {
105 4
        return $this->skippedLines;
106
    }
107
}
108