Completed
Push — master ( 04f40e...f278b5 )
by Tobias
05:43
created

Extractor   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 72.72%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 88
c 1
b 0
f 0
ccs 24
cts 33
cp 0.7272
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A extract() 0 4 1
A extractFromDirectory() 0 7 1
A addFileExtractor() 0 4 1
C getType() 0 22 7
A doExtract() 0 16 4
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;
13
14
use Symfony\Component\Finder\Finder;
15
use Symfony\Component\Finder\SplFileInfo;
16
use Translation\Extractor\FileExtractor\FileExtractor;
17
use Translation\Extractor\Model\SourceCollection;
18
19
/**
20
 * Main class for all extractors. This is the service that will be loaded with file
21
 * extractors.
22
 *
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
final class Extractor
26
{
27
    /**
28
     * @var FileExtractor[]
29
     */
30
    private $fileExtractors = [];
31
32
    /**
33
     * @param Finder $finder
34
     *
35
     * @return SourceCollection
36
     */
37 1
    public function extract(Finder $finder)
38
    {
39 1
        return $this->doExtract($finder);
40
    }
41
42
    /**
43
     * @param string $dir
44
     *
45
     * @return SourceCollection
46
     */
47
    public function extractFromDirectory($dir)
48
    {
49
        $finder = new Finder();
50
        $finder->files()->in($dir);
51
52
        return $this->doExtract($finder);
53
    }
54
55
    /**
56
     * @param SplFileInfo $file
57
     *
58
     * @return string
59
     */
60 1
    private function getType(SplFileInfo $file)
61
    {
62 1
        $filename = $file->getFilename();
63 1
        if (preg_match('|.+\.blade\.php$|', $filename)) {
64 1
            $ext = 'blade.php';
65
        } else {
66 1
            $ext = $file->getExtension();
67
        }
68
69 1
        switch ($ext) {
70
            case 'php':
71
            case 'php5':
72
            case 'phtml':
73 1
                return 'php';
74
            case 'twig':
75 1
                return 'twig';
76
            case 'blade.php':
77 1
                return 'blade';
78
            default:
79 1
                return $ext;
80
        }
81
    }
82
83
    /**
84
     * @param FileExtractor $fileExtractors
0 ignored issues
show
Documentation introduced by
There is no parameter named $fileExtractors. Did you maybe mean $fileExtractor?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
85
     */
86 1
    public function addFileExtractor(FileExtractor $fileExtractor)
87
    {
88 1
        $this->fileExtractors[] = $fileExtractor;
89 1
    }
90
91
    /**
92
     * @param Finder $finder
93
     *
94
     * @return SourceCollection
95
     */
96 1
    private function doExtract(Finder $finder)
97
    {
98 1
        $collection = new SourceCollection();
99 1
        foreach ($finder as $file) {
100 1
            $type = $this->getType($file);
101 1
            foreach ($this->fileExtractors as $extractor) {
102 1
                if ($extractor->getType() !== $type) {
103 1
                    continue;
104
                }
105
106 1
                $extractor->getSourceLocations($file, $collection);
107
            }
108
        }
109
110 1
        return $collection;
111
    }
112
}
113