@@ -79,6 +79,36 @@ public function extractMethodBodies(string $className): array
79
79
}
80
80
81
81
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
+
82
112
public function extractFunctionBody (string $ name ): ?string
83
113
{
84
114
$ functionNode = (new NodeFinder )->findFirst (
@@ -94,6 +124,9 @@ public function extractFunctionBody(string $name): ?string
94
124
/** @param Node[] $nodes */
95
125
private function getReformattedContents (array $ nodes , int $ level ): string
96
126
{
127
+ if (!$ nodes ) {
128
+ return '' ;
129
+ }
97
130
$ body = $ this ->getNodeContents (...$ nodes );
98
131
$ body = $ this ->performReplacements ($ body , $ this ->prepareReplacements ($ nodes , $ level ));
99
132
return Helpers::unindent ($ body , $ level );
0 commit comments