Completed
Push — master ( ba40ee...261a9c )
by Tobias
02:47
created

BladeFileExtractor::findTranslations()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 16
cts 16
cp 1
rs 9.504
c 0
b 0
f 0
cc 3
nc 2
nop 1
crap 3
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Extractor\FileExtractor;
13
14
use Symfony\Component\Finder\SplFileInfo;
15
use Translation\Extractor\Model\SourceCollection;
16
use Translation\Extractor\Model\SourceLocation;
17
18
/**
19
 * @author Tobias Nyholm <[email protected]>
20
 */
21
final class BladeFileExtractor implements FileExtractor
22
{
23 3
    public function getSourceLocations(SplFileInfo $file, SourceCollection $collection)
24
    {
25 3
        $realPath = $file->getRealPath();
26 3
        $messages = $this->findTranslations($file);
27 3
        foreach ($messages as $message) {
28 3
            $collection->addLocation(new SourceLocation($message, $realPath, 0));
29
        }
30 3
    }
31
32
    /**
33
     * @author uusa35 and contributors to {@link https://github.com/barryvdh/laravel-translation-manager}
34
     *
35
     * @param SplFileInfo $file
36
     *
37
     * @return string[]
38
     */
39 3
    public function findTranslations(SplFileInfo $file)
40
    {
41 3
        $keys = [];
42 3
        $functions = ['trans', 'trans_choice', 'Lang::get', 'Lang::choice', 'Lang::trans', 'Lang::transChoice', '@lang', '@choice'];
43
        $pattern = // See http://regexr.com/392hu
44
            "[^\w|>]".// Must not have an alphanum or _ or > before real method
45 3
            '('.implode('|', $functions).')'.// Must start with one of the functions
46 3
            "\(".// Match opening parenthese
47 3
            "[\'\"]".// Match " or '
48 3
            '('.// Start a new group to match:
49 3
            '[a-zA-Z0-9_-]+'.// Must start with group
50 3
            '([.][^\\1)]+)+'.// Be followed by one or more items/keys
51 3
            ')'.// Close group
52 3
            "[\'\"]".// Closing quote
53 3
            "[\),]";                            // Close parentheses or new parameter
54
55
        // Search the current file for the pattern
56 3
        if (preg_match_all("/$pattern/siU", $file->getContents(), $matches)) {
57
            // Get all matches
58 3
            foreach ($matches[2] as $key) {
59 3
                $keys[] = $key;
60
            }
61
        }
62
63 3
        return $keys;
64
    }
65
66
    public function getType()
67
    {
68
        return 'blade';
69
    }
70
}
71