|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the humbug/php-scoper package. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2017 Théo FIDRY <[email protected]>, |
|
9
|
|
|
* Pádraic Brady <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Humbug\PhpScoper; |
|
16
|
|
|
|
|
17
|
|
|
use Humbug\PhpScoper\NodeVisitor\FullyQualifiedNamespaceUseScoperNodeVisitor; |
|
18
|
|
|
use Humbug\PhpScoper\NodeVisitor\GroupUseNamespaceScoperNodeVisitor; |
|
19
|
|
|
use Humbug\PhpScoper\NodeVisitor\IgnoreNamespaceScoperNodeVisitor; |
|
20
|
|
|
use Humbug\PhpScoper\NodeVisitor\NamespaceScoperNodeVisitor; |
|
21
|
|
|
use Humbug\PhpScoper\NodeVisitor\ParentNodeVisitor; |
|
22
|
|
|
use Humbug\PhpScoper\NodeVisitor\UseNamespaceScoperNodeVisitor; |
|
23
|
|
|
use PhpParser\Error; |
|
24
|
|
|
use PhpParser\NodeTraverser; |
|
25
|
|
|
use PhpParser\NodeTraverserInterface; |
|
26
|
|
|
use PhpParser\Parser; |
|
27
|
|
|
use PhpParser\PrettyPrinter\Standard; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @final |
|
31
|
|
|
*/ |
|
32
|
|
|
class Scoper |
|
33
|
|
|
{ |
|
34
|
|
|
private $parser; |
|
35
|
22 |
|
|
|
36
|
|
|
public function __construct(Parser $parser) |
|
37
|
22 |
|
{ |
|
38
|
22 |
|
$this->parser = $parser; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $content Content of the file to scope |
|
43
|
|
|
* @param string $prefix Prefix to apply to the file |
|
44
|
|
|
* |
|
45
|
|
|
* @throws Error |
|
46
|
|
|
* |
|
47
|
|
|
* @return string Content of the file with the prefix applied |
|
48
|
22 |
|
*/ |
|
49
|
|
|
public function scope(string $content, string $prefix): string |
|
50
|
22 |
|
{ |
|
51
|
22 |
|
$traverser = $this->createNodeTraverser($prefix); |
|
52
|
22 |
|
|
|
53
|
22 |
|
$statements = $this->parser->parse($content); |
|
54
|
22 |
|
$statements = $traverser->traverse($statements); |
|
|
|
|
|
|
55
|
22 |
|
|
|
56
|
|
|
$prettyPrinter = new Standard(); |
|
57
|
|
|
|
|
58
|
22 |
|
return $prettyPrinter->prettyPrintFile($statements)."\n"; |
|
59
|
1 |
|
} |
|
60
|
1 |
|
|
|
61
|
|
|
private function createNodeTraverser(string $prefix): NodeTraverserInterface |
|
62
|
|
|
{ |
|
63
|
21 |
|
$traverser = new NodeTraverser(); |
|
64
|
|
|
|
|
65
|
21 |
|
$traverser->addVisitor(new ParentNodeVisitor()); |
|
66
|
|
|
$traverser->addVisitor(new IgnoreNamespaceScoperNodeVisitor()); |
|
67
|
21 |
|
$traverser->addVisitor(new GroupUseNamespaceScoperNodeVisitor($prefix)); |
|
68
|
|
|
$traverser->addVisitor(new NamespaceScoperNodeVisitor($prefix)); |
|
69
|
|
|
$traverser->addVisitor(new UseNamespaceScoperNodeVisitor($prefix)); |
|
70
|
|
|
$traverser->addVisitor(new FullyQualifiedNamespaceUseScoperNodeVisitor($prefix)); |
|
71
|
|
|
|
|
72
|
|
|
return $traverser; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
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.