|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the webmozart/php-scoper package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[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 Humbug\PhpScoper; |
|
13
|
|
|
|
|
14
|
|
|
use Humbug\PhpScoper\NodeVisitor\FullyQualifiedNamespaceUseScoperNodeVisitor; |
|
15
|
|
|
use Humbug\PhpScoper\NodeVisitor\GroupUseNamespaceScoperNodeVisitor; |
|
16
|
|
|
use Humbug\PhpScoper\NodeVisitor\NamespaceScoperNodeVisitor; |
|
17
|
|
|
use Humbug\PhpScoper\NodeVisitor\ParentNodeVisitor; |
|
18
|
|
|
use Humbug\PhpScoper\NodeVisitor\UseNamespaceScoperNodeVisitor; |
|
19
|
|
|
use Humbug\PhpScoper\Throwable\Exception\ParsingException; |
|
20
|
|
|
use PhpParser\Error; |
|
21
|
|
|
use PhpParser\NodeTraverser; |
|
22
|
|
|
use PhpParser\Parser; |
|
23
|
|
|
use PhpParser\PrettyPrinter\Standard; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @final |
|
27
|
|
|
*/ |
|
28
|
|
|
class Scoper |
|
29
|
|
|
{ |
|
30
|
|
|
private $parser; |
|
31
|
|
|
|
|
32
|
22 |
|
public function __construct(Parser $parser) |
|
33
|
|
|
{ |
|
34
|
22 |
|
$this->parser = $parser; |
|
35
|
22 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $content Content of the file to scope |
|
39
|
|
|
* @param string $prefix Prefix to apply to the file |
|
40
|
|
|
* |
|
41
|
|
|
* @throws ParsingException |
|
42
|
|
|
* |
|
43
|
|
|
* @return string Content of the file with the prefix applied |
|
44
|
|
|
*/ |
|
45
|
22 |
|
public function scope(string $content, string $prefix): string |
|
46
|
|
|
{ |
|
47
|
22 |
|
$traverser = new NodeTraverser(); |
|
48
|
22 |
|
$traverser->addVisitor(new ParentNodeVisitor()); |
|
49
|
22 |
|
$traverser->addVisitor(new GroupUseNamespaceScoperNodeVisitor($prefix)); |
|
50
|
22 |
|
$traverser->addVisitor(new NamespaceScoperNodeVisitor($prefix)); |
|
51
|
22 |
|
$traverser->addVisitor(new UseNamespaceScoperNodeVisitor($prefix)); |
|
52
|
22 |
|
$traverser->addVisitor(new FullyQualifiedNamespaceUseScoperNodeVisitor($prefix)); |
|
53
|
|
|
|
|
54
|
|
|
try { |
|
55
|
22 |
|
$statements = $this->parser->parse($content); |
|
56
|
1 |
|
} catch (Error $error) { |
|
57
|
1 |
|
throw new ParsingException($error->getMessage(), 0, $error); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
21 |
|
$statements = $traverser->traverse($statements); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
21 |
|
$prettyPrinter = new Standard(); |
|
63
|
|
|
|
|
64
|
21 |
|
return $prettyPrinter->prettyPrintFile($statements)."\n"; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.