@@ -79,6 +79,36 @@ public function extractMethodBodies(string $className): array
7979 }
8080
8181
82+ /** @return array<string, array<string, array{string, bool}>> */
83+ public function extractPropertyHookBodies (string $ className ): array
84+ {
85+ if (!class_exists (Node \PropertyHook::class)) {
86+ return [];
87+ }
88+
89+ $ nodeFinder = new NodeFinder ;
90+ $ classNode = $ nodeFinder ->findFirst (
91+ $ this ->statements ,
92+ fn (Node $ node ) => $ node instanceof Node \Stmt \ClassLike && $ node ->namespacedName ->toString () === $ className ,
93+ );
94+
95+ $ res = [];
96+ foreach ($ nodeFinder ->findInstanceOf ($ classNode , Node \Stmt \Property::class) as $ propertyNode ) {
97+ foreach ($ propertyNode ->props as $ propNode ) {
98+ $ propName = $ propNode ->name ->toString ();
99+ foreach ($ propertyNode ->hooks as $ hookNode ) {
100+ $ body = $ hookNode ->body ;
101+ if ($ body !== null ) {
102+ $ contents = $ this ->getReformattedContents (is_array ($ body ) ? $ body : [$ body ], 3 );
103+ $ res [$ propName ][$ hookNode ->name ->toString ()] = [$ contents , !is_array ($ body )];
104+ }
105+ }
106+ }
107+ }
108+ return $ res ;
109+ }
110+
111+
82112 public function extractFunctionBody (string $ name ): ?string
83113 {
84114 $ functionNode = (new NodeFinder )->findFirst (
@@ -94,6 +124,9 @@ public function extractFunctionBody(string $name): ?string
94124 /** @param Node[] $nodes */
95125 private function getReformattedContents (array $ nodes , int $ level ): string
96126 {
127+ if (!$ nodes ) {
128+ return '' ;
129+ }
97130 $ body = $ this ->getNodeContents (...$ nodes );
98131 $ body = $ this ->performReplacements ($ body , $ this ->prepareReplacements ($ nodes , $ level ));
99132 return Helpers::unindent ($ body , $ level );
0 commit comments