22
33namespace PHPStan \Analyser ;
44
5+ use PhpParser \Token ;
56use PHPStan \Reflection \ClassReflection ;
67use PHPStan \ShouldNotHappenException ;
78
89final class ScopeContext
910{
1011
12+ /**
13+ * @param string $file
14+ * @param \PHPStan\Reflection\ClassReflection|null $classReflection
15+ * @param \PHPStan\Reflection\ClassReflection|null $traitReflection
16+ * @param Token[] $tokens
17+ */
1118 private function __construct (
1219 private string $ file ,
1320 private ?ClassReflection $ classReflection ,
1421 private ?ClassReflection $ traitReflection ,
22+ private array $ tokens ,
1523 )
1624 {
1725 }
1826
19- /** @api */
20- public static function create (string $ file ): self
27+ /**
28+ * @param string $file
29+ * @param Token[] $tokens
30+ *
31+ * @return self
32+ *
33+ * @api
34+ */
35+ public static function create (string $ file , array $ tokens = []): self
2136 {
22- return new self ($ file , classReflection: null , traitReflection: null );
37+ return new self ($ file , classReflection: null , traitReflection: null , tokens: $ tokens );
2338 }
2439
2540 public function beginFile (): self
2641 {
27- return new self ($ this ->file , classReflection: null , traitReflection: null );
42+ return new self ($ this ->file , classReflection: null , traitReflection: null , tokens: $ this -> tokens );
2843 }
2944
3045 public function enterClass (ClassReflection $ classReflection ): self
@@ -35,7 +50,7 @@ public function enterClass(ClassReflection $classReflection): self
3550 if ($ classReflection ->isTrait ()) {
3651 throw new ShouldNotHappenException ();
3752 }
38- return new self ($ this ->file , $ classReflection , traitReflection: null );
53+ return new self ($ this ->file , $ classReflection , traitReflection: null , tokens: $ this -> tokens );
3954 }
4055
4156 public function enterTrait (ClassReflection $ traitReflection ): self
@@ -47,7 +62,7 @@ public function enterTrait(ClassReflection $traitReflection): self
4762 throw new ShouldNotHappenException ();
4863 }
4964
50- return new self ($ this ->file , $ this ->classReflection , $ traitReflection );
65+ return new self ($ this ->file , $ this ->classReflection , $ traitReflection, $ this -> tokens );
5166 }
5267
5368 public function equals (self $ otherContext ): bool
@@ -90,4 +105,12 @@ public function getTraitReflection(): ?ClassReflection
90105 return $ this ->traitReflection ;
91106 }
92107
108+ /**
109+ * @return Token[]
110+ */
111+ public function getTokens (): array
112+ {
113+ return $ this ->tokens ;
114+ }
115+
93116}
0 commit comments