Passed
Pull Request — master (#41)
by Def
02:10
created

TranslationExtractor::hasSkippedLines()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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
     * @param string $path
46
     * @param string[]|null $only
47
     * @param string[]|null $except
48
     */
49 7
    public function __construct(string $path, ?array $only = null, ?array $except = null)
50
    {
51 7
        if (!is_dir($path)) {
52 1
            throw new \RuntimeException(sprintf('Directory "%s" does not exist.', $path));
53
        }
54
55 6
        $this->path = $path;
56
57 6
        if (isset($only)) {
58 2
            $this->only = $only;
59
        }
60
61 6
        if (isset($except)) {
62 2
            $this->except = $except;
63
        }
64 6
    }
65
66
    /**
67
     * @param string|null $defaultCategory
68
     * @param string|null $translator
69
     *
70
     * @return array
71
     */
72 6
    public function extract(?string $defaultCategory = null, ?string $translator = null): array
73
    {
74 6
        $messages = [];
75 6
        $this->parser = new ContentParser($defaultCategory, $translator);
76
77 6
        $files = FileHelper::findFiles($this->path, [
78 6
            'filter' => (new pathMatcher())->only(...$this->only)->except(...$this->except),
79
            'recursive' => true,
80
        ]);
81
82
        /** @var string[] $files */
83 6
        foreach ($files as $file) {
84 6
            $fileContent = file_get_contents($file);
85 6
            $messages = array_merge_recursive($messages, $this->parser->extract($fileContent));
86 6
            if ($this->parser->hasSkippedLines()) {
87 4
                $this->skippedLines[$file] = $this->parser->getSkippedLines();
88
            }
89
        }
90
91 6
        return $messages;
92
    }
93
94 6
    public function hasSkippedLines(): bool
95
    {
96 6
        return !empty($this->skippedLines);
97
    }
98
99
    /**
100
     * @psalm-return array<string, array<array<string|array{0: int, 1: string, 2: int}>>>
101
     */
102 4
    public function getSkippedLines(): array
103
    {
104 4
        return $this->skippedLines;
105
    }
106
}
107