99
1010class FileVisitor extends NodeVisitorAbstract
1111{
12- /** @var ? ClassDescriptionBuilder */
12+ /** @var ClassDescriptionBuilder */
1313 private $ classDescriptionBuilder ;
1414
1515 /** @var array */
1616 private $ classDescriptions = [];
1717
18+ public function __construct (ClassDescriptionBuilder $ classDescriptionBuilder )
19+ {
20+ $ this ->classDescriptionBuilder = $ classDescriptionBuilder ;
21+ }
22+
1823 public function enterNode (Node $ node ): void
1924 {
2025 if ($ node instanceof Node \Stmt \Class_) {
2126 if (!$ node ->isAnonymous () && null !== $ node ->namespacedName ) {
22- /** @psalm-suppress UndefinedPropertyFetch */
23- $ this ->classDescriptionBuilder = ClassDescriptionBuilder::create (
24- $ node ->namespacedName ->toCodeString ()
25- );
26- }
27-
28- if (null === $ this ->classDescriptionBuilder ) {
29- return ;
27+ $ this ->classDescriptionBuilder ->setClassName ($ node ->namespacedName ->toCodeString ());
3028 }
3129
3230 foreach ($ node ->implements as $ interface ) {
@@ -55,15 +53,25 @@ public function enterNode(Node $node): void
5553 }
5654 }
5755
56+ if ($ node instanceof Node \Stmt \Enum_ && null !== $ node ->namespacedName ) {
57+ $ this ->classDescriptionBuilder ->setClassName ($ node ->namespacedName ->toCodeString ());
58+
59+ foreach ($ node ->attrGroups as $ attributeGroup ) {
60+ foreach ($ attributeGroup ->attrs as $ attribute ) {
61+ $ this ->classDescriptionBuilder
62+ ->addAttribute ($ attribute ->name ->toString (), $ attribute ->getLine ());
63+ }
64+ }
65+ }
66+
5867 /**
5968 * adding static classes as dependencies
6069 * $constantValue = StaticClass::constant;.
6170 *
6271 * @see FileVisitorTest::test_it_should_return_errors_for_const_outside_namespace
6372 */
6473 if ($ node instanceof Node \Expr \ClassConstFetch &&
65- method_exists ($ node ->class , 'toString ' ) &&
66- null !== $ this ->classDescriptionBuilder
74+ method_exists ($ node ->class , 'toString ' )
6775 ) {
6876 if ($ this ->isSelfOrStaticOrParent ($ node ->class ->toString ())) {
6977 return ;
@@ -80,8 +88,7 @@ public function enterNode(Node $node): void
8088 * @see FileVisitorTest::test_should_returns_all_dependencies
8189 */
8290 if ($ node instanceof Node \Expr \StaticCall &&
83- method_exists ($ node ->class , 'toString ' ) &&
84- null !== $ this ->classDescriptionBuilder
91+ method_exists ($ node ->class , 'toString ' )
8592 ) {
8693 if ($ this ->isSelfOrStaticOrParent ($ node ->class ->toString ())) {
8794 return ;
@@ -92,8 +99,7 @@ public function enterNode(Node $node): void
9299 }
93100
94101 if ($ node instanceof Node \Expr \Instanceof_ &&
95- method_exists ($ node ->class , 'toString ' ) &&
96- null !== $ this ->classDescriptionBuilder
102+ method_exists ($ node ->class , 'toString ' )
97103 ) {
98104 if ($ this ->isSelfOrStaticOrParent ($ node ->class ->toString ())) {
99105 return ;
@@ -104,8 +110,7 @@ public function enterNode(Node $node): void
104110 }
105111
106112 if ($ node instanceof Node \Expr \New_ &&
107- !($ node ->class instanceof Node \Expr \Variable) &&
108- null !== $ this ->classDescriptionBuilder
113+ !($ node ->class instanceof Node \Expr \Variable)
109114 ) {
110115 if ((method_exists ($ node ->class , 'isAnonymous ' ) && $ node ->class ->isAnonymous ()) ||
111116 !method_exists ($ node ->class , 'toString ' )) {
@@ -120,27 +125,13 @@ public function enterNode(Node $node): void
120125 ->addDependency (new ClassDependency ($ node ->class ->toString (), $ node ->getLine ()));
121126 }
122127
123- if ($ node instanceof Node \Stmt \Enum_ && null !== $ node ->namespacedName ) {
124- /** @psalm-suppress UndefinedPropertyFetch */
125- $ this ->classDescriptionBuilder = ClassDescriptionBuilder::create (
126- $ node ->namespacedName ->toCodeString ()
127- );
128-
129- foreach ($ node ->attrGroups as $ attributeGroup ) {
130- foreach ($ attributeGroup ->attrs as $ attribute ) {
131- $ this ->classDescriptionBuilder
132- ->addAttribute ($ attribute ->name ->toString (), $ attribute ->getLine ());
133- }
134- }
135- }
136-
137128 /**
138129 * matches parameters dependency in property definitions like
139130 * public NotBlank $foo;.
140131 *
141132 * @see FileVisitorTest::test_it_parse_typed_property
142133 */
143- if ($ node instanceof Node \Stmt \Property && null !== $ this -> classDescriptionBuilder ) {
134+ if ($ node instanceof Node \Stmt \Property) {
144135 if (null === $ node ->type ) {
145136 return ;
146137 }
@@ -159,7 +150,7 @@ public function enterNode(Node $node): void
159150 }
160151 }
161152
162- if (null !== $ this -> classDescriptionBuilder && null !== $ node ->getDocComment ()) {
153+ if (null !== $ node ->getDocComment ()) {
163154 /** @var Doc $docComment */
164155 $ docComment = $ node ->getDocComment ();
165156
@@ -172,7 +163,7 @@ public function enterNode(Node $node): void
172163 *
173164 * @see FileVisitorTest::test_should_returns_all_dependencies
174165 */
175- if ($ node instanceof Node \Param && null !== $ this -> classDescriptionBuilder ) {
166+ if ($ node instanceof Node \Param) {
176167 $ this ->addParamDependency ($ node );
177168 }
178169 }
@@ -189,16 +180,14 @@ public function clearParsedClassDescriptions(): void
189180
190181 public function leaveNode (Node $ node ): void
191182 {
192- if ($ node instanceof Node \Stmt \Class_ && null !== $ this ->classDescriptionBuilder ) {
193- $ classDescription = $ this ->classDescriptionBuilder ->get ();
194-
195- $ this ->classDescriptions [] = $ classDescription ;
183+ if ($ node instanceof Node \Stmt \Class_ && !$ node ->isAnonymous ()) {
184+ $ this ->classDescriptions [] = $ this ->classDescriptionBuilder ->get ();
185+ $ this ->classDescriptionBuilder ->clear ();
196186 }
197187
198- if ($ node instanceof Node \Stmt \Enum_ && null !== $ this ->classDescriptionBuilder ) {
199- $ classDescription = $ this ->classDescriptionBuilder ->get ();
200-
201- $ this ->classDescriptions [] = $ classDescription ;
188+ if ($ node instanceof Node \Stmt \Enum_) {
189+ $ this ->classDescriptions [] = $ this ->classDescriptionBuilder ->get ();
190+ $ this ->classDescriptionBuilder ->clear ();
202191 }
203192 }
204193
@@ -221,10 +210,6 @@ private function addParamDependency(Node\Param $node): void
221210 return ;
222211 }
223212
224- if (null === $ this ->classDescriptionBuilder ) {
225- return ;
226- }
227-
228213 $ this ->classDescriptionBuilder
229214 ->addDependency (new ClassDependency ($ node ->type ->toString (), $ node ->getLine ()));
230215 }
0 commit comments