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 PhpParser\Error; |
15
|
|
|
use PhpParser\NodeTraverser; |
16
|
|
|
use PhpParser\NodeVisitor; |
17
|
|
|
use PhpParser\ParserFactory; |
18
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
19
|
|
|
use Translation\Extractor\Model\SourceCollection; |
20
|
|
|
use Translation\Extractor\Visitor\Visitor; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author Tobias Nyholm <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class PHPFileExtractor implements FileExtractor |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var Visitor[]|NodeVisitor[] |
29
|
|
|
*/ |
30
|
|
|
private $visitors = []; |
31
|
|
|
|
32
|
9 |
|
public function getSourceLocations(SplFileInfo $file, SourceCollection $collection) |
33
|
|
|
{ |
34
|
9 |
|
$path = $file->getRelativePath(); |
35
|
9 |
|
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); |
36
|
9 |
|
$traverser = new NodeTraverser(); |
37
|
9 |
|
foreach ($this->visitors as $v) { |
38
|
9 |
|
$v->init($collection, $file); |
|
|
|
|
39
|
9 |
|
$traverser->addVisitor($v); |
|
|
|
|
40
|
9 |
|
} |
41
|
|
|
|
42
|
|
|
try { |
43
|
9 |
|
$tokens = $parser->parse($file->getContents()); |
44
|
9 |
|
$traverser->traverse($tokens); |
45
|
9 |
|
} catch (Error $e) { |
46
|
|
|
trigger_error(sprintf('Skipping file "%s" because of parse Error: %s. ', $path, $e->getMessage())); |
47
|
|
|
} |
48
|
9 |
|
} |
49
|
|
|
|
50
|
1 |
|
public function getType() |
51
|
|
|
{ |
52
|
1 |
|
return 'php'; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param NodeVisitor $visitor |
57
|
|
|
*/ |
58
|
9 |
|
public function addVisitor(NodeVisitor $visitor) |
59
|
|
|
{ |
60
|
9 |
|
$this->visitors[] = $visitor; |
61
|
9 |
|
} |
62
|
|
|
} |
63
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: