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 |
|
|
|
|
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
|
|
|
|
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 methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.