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
|
|
|
class Extractor |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var FileExtractor[] |
23
|
|
|
*/ |
24
|
|
|
private $fileExtractors = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param Finder $finder |
28
|
|
|
* |
29
|
|
|
* @return SourceCollection |
30
|
|
|
*/ |
31
|
1 |
|
public function extract(Finder $finder) |
32
|
|
|
{ |
33
|
1 |
|
return $this->doExtract($finder); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $dir |
38
|
|
|
* |
39
|
|
|
* @return SourceCollection |
40
|
|
|
*/ |
41
|
|
|
public function extractFromDirectory($dir) |
42
|
|
|
{ |
43
|
|
|
$finder = new Finder(); |
44
|
|
|
$finder->files()->in($dir); |
45
|
|
|
|
46
|
|
|
return $this->doExtract($finder); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param SplFileInfo $file |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
1 |
|
private function getType(SplFileInfo $file) |
55
|
|
|
{ |
56
|
1 |
|
$filename = $file->getFilename(); |
57
|
1 |
|
if (preg_match('|.+\.blade\.php$|', $filename)) { |
58
|
1 |
|
$ext = 'blade.php'; |
59
|
1 |
|
} else { |
60
|
1 |
|
$ext = $file->getExtension(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
switch ($ext) { |
64
|
1 |
|
case 'php': |
65
|
1 |
|
case 'php5': |
66
|
1 |
|
return 'php'; |
67
|
1 |
|
case 'twig': |
68
|
1 |
|
return 'twig'; |
69
|
1 |
|
case 'blade.php': |
70
|
1 |
|
return 'blade'; |
71
|
1 |
|
default: |
72
|
1 |
|
return $ext; |
73
|
1 |
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param FileExtractor $fileExtractors |
|
|
|
|
78
|
|
|
*/ |
79
|
1 |
|
public function addFileExtractor(FileExtractor $fileExtractor) |
80
|
|
|
{ |
81
|
1 |
|
$this->fileExtractors[] = $fileExtractor; |
82
|
1 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param Finder $finder |
86
|
|
|
* |
87
|
|
|
* @return SourceCollection |
88
|
|
|
*/ |
89
|
1 |
|
private function doExtract(Finder $finder) |
90
|
|
|
{ |
91
|
1 |
|
$collection = new SourceCollection(); |
92
|
1 |
|
foreach ($finder as $file) { |
93
|
1 |
|
$type = $this->getType($file); |
94
|
1 |
|
foreach ($this->fileExtractors as $extractor) { |
95
|
1 |
|
if ($extractor->getType() !== $type) { |
96
|
1 |
|
continue; |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
$extractor->getSourceLocations($file, $collection); |
100
|
1 |
|
} |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
return $collection; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
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.