|
1 | 1 | <?php
|
2 |
| - |
| 2 | +/** @noinspection PhpPropertyOnlyWrittenInspection */ |
3 | 3 | namespace Okapi\CodeTransformer\Transformer;
|
4 | 4 |
|
| 5 | +use DI\Attribute\Inject; |
| 6 | +use Microsoft\PhpParser\Node; |
5 | 7 | use Microsoft\PhpParser\Node\SourceFileNode;
|
6 | 8 | use Microsoft\PhpParser\Parser;
|
7 | 9 | use Microsoft\PhpParser\Token;
|
8 |
| -use Okapi\CodeTransformer\Core\AutoloadInterceptor; |
9 | 10 | use Okapi\CodeTransformer\Core\DI;
|
10 | 11 | use Okapi\CodeTransformer\Core\Util\CodeChecker;
|
| 12 | +use Okapi\CodeTransformer\Core\Util\ReflectionHelper; |
11 | 13 | use Okapi\CodeTransformer\Core\Util\StringMutator;
|
12 |
| -use Roave\BetterReflection\BetterReflection; |
13 | 14 | use Roave\BetterReflection\Reflection\ReflectionClass as BetterReflectionClass;
|
14 |
| -use Roave\BetterReflection\Reflector\DefaultReflector; |
15 |
| -use Roave\BetterReflection\SourceLocator\Type\ComposerSourceLocator; |
16 | 15 |
|
17 | 16 | /**
|
18 | 17 | * # Code
|
|
22 | 21 | */
|
23 | 22 | class Code
|
24 | 23 | {
|
| 24 | + // region DI |
| 25 | + |
| 26 | + #[Inject] |
| 27 | + private ReflectionHelper $reflectionHelper; |
| 28 | + |
| 29 | + // endregion |
| 30 | + |
25 | 31 | /**
|
26 | 32 | * String mutator.
|
27 | 33 | *
|
@@ -64,13 +70,16 @@ public function __construct(
|
64 | 70 | /**
|
65 | 71 | * Add an edit to the source code.
|
66 | 72 | *
|
67 |
| - * @param Token $token The token to edit. |
68 |
| - * @param string $replacement The replacement string. |
| 73 | + * @param Token|Node $token The token or node to edit. |
| 74 | + * @param string $replacement The replacement string. |
69 | 75 | *
|
70 | 76 | * @return void
|
| 77 | + * |
| 78 | + * @noinspection PhpDocMissingThrowsInspection |
71 | 79 | */
|
72 |
| - public function edit(Token $token, string $replacement): void |
| 80 | + public function edit(Token|Node $token, string $replacement): void |
73 | 81 | {
|
| 82 | + /** @noinspection PhpUnhandledExceptionInspection */ |
74 | 83 | $this->stringMutator->edit(
|
75 | 84 | $token->getStartPosition(),
|
76 | 85 | $token->getWidth(),
|
@@ -146,17 +155,9 @@ public function getSourceFileNode(): SourceFileNode
|
146 | 155 | */
|
147 | 156 | public function getReflectionClass(): BetterReflectionClass
|
148 | 157 | {
|
149 |
| - static $classLoader, $astLocator, $reflector; |
150 |
| - |
151 |
| - if (!isset($classLoader, $astLocator, $reflector)) { |
152 |
| - $classLoader = DI::get(AutoloadInterceptor::DI); |
153 |
| - $astLocator = (new BetterReflection)->astLocator(); |
154 |
| - $reflector = (new DefaultReflector( |
155 |
| - new ComposerSourceLocator($classLoader, $astLocator) |
156 |
| - )); |
157 |
| - } |
158 |
| - |
159 |
| - return $reflector->reflectClass($this->getNamespacedClass()); |
| 158 | + return $this->reflectionHelper->getReflectionClass( |
| 159 | + $this->getNamespacedClass(), |
| 160 | + ); |
160 | 161 | }
|
161 | 162 |
|
162 | 163 | /**
|
|
0 commit comments