1+ <?php
2+
3+ namespace Vormkracht10 \Fields \Services ;
4+
5+ use ReflectionClass ;
6+ use Vormkracht10 \Fields \Contracts \FieldInspector ;
7+ use Illuminate \Support \Str ;
8+ use ReflectionMethod ;
9+ use ReflectionProperty ;
10+ use Vormkracht10 \Fields \Facades \Fields ;
11+
12+ class FieldInspectionService implements FieldInspector
13+ {
14+ public function initializeDefaultField (string $ fieldType ): array
15+ {
16+ $ className = 'Vormkracht10 \\Fields \\Fields \\' . Str::studly ($ fieldType );
17+
18+ return $ this ->getClassDetails ($ className );
19+ }
20+
21+ public function initializeCustomField (string $ fieldType ): array
22+ {
23+ $ className = Fields::getFields ()[$ fieldType ] ?? null ;
24+
25+ return $ this ->getClassDetails ($ className );
26+ }
27+
28+ private function getClassDetails (?string $ className ): array
29+ {
30+ if (! $ className || ! class_exists ($ className )) {
31+ return [
32+ 'exists ' => false ,
33+ 'class ' => $ className ,
34+ 'methods ' => [],
35+ 'properties ' => [],
36+ 'constants ' => [],
37+ 'interfaces ' => [],
38+ 'instance ' => null ,
39+ ];
40+ }
41+
42+ $ reflection = new ReflectionClass ($ className );
43+ $ instance = app ($ className );
44+
45+ return [
46+ 'exists ' => true ,
47+ 'class ' => $ className ,
48+ 'methods ' => $ this ->getMethodsDetails ($ reflection ),
49+ 'properties ' => $ this ->getPropertiesDetails ($ reflection ),
50+ 'constants ' => $ reflection ->getConstants (),
51+ 'interfaces ' => $ reflection ->getInterfaceNames (),
52+ 'instance ' => $ instance ,
53+ 'parentClass ' => $ reflection ->getParentClass () ? $ reflection ->getParentClass ()->getName () : null ,
54+ 'traits ' => $ reflection ->getTraitNames (),
55+ ];
56+ }
57+
58+ private function getMethodsDetails (ReflectionClass $ reflection ): array
59+ {
60+ $ methods = [];
61+ foreach ($ reflection ->getMethods () as $ method ) {
62+ $ methods [$ method ->getName ()] = [
63+ 'visibility ' => $ this ->getVisibility ($ method ),
64+ 'static ' => $ method ->isStatic (),
65+ 'parameters ' => $ this ->getParametersDetails ($ method ),
66+ 'returnType ' => $ method ->getReturnType () ? $ method ->getReturnType ()->getName () : null ,
67+ 'docComment ' => $ method ->getDocComment () ?: null ,
68+ ];
69+ }
70+
71+ return $ methods ;
72+ }
73+
74+ private function getPropertiesDetails (ReflectionClass $ reflection ): array
75+ {
76+ $ properties = [];
77+ foreach ($ reflection ->getProperties () as $ property ) {
78+ $ properties [$ property ->getName ()] = [
79+ 'visibility ' => $ this ->getVisibility ($ property ),
80+ 'static ' => $ property ->isStatic (),
81+ 'type ' => $ property ->getType () ? $ property ->getType ()->getName () : null ,
82+ 'docComment ' => $ property ->getDocComment () ?: null ,
83+ 'defaultValue ' => $ this ->getPropertyDefaultValue ($ property ),
84+ ];
85+ }
86+
87+ return $ properties ;
88+ }
89+
90+ private function getParametersDetails (ReflectionMethod $ method ): array
91+ {
92+ $ parameters = [];
93+ foreach ($ method ->getParameters () as $ param ) {
94+ $ parameters [$ param ->getName ()] = [
95+ 'type ' => $ param ->getType () ? $ param ->getType ()->getName () : null ,
96+ 'hasDefaultValue ' => $ param ->isDefaultValueAvailable (),
97+ 'defaultValue ' => $ param ->isDefaultValueAvailable () ? $ param ->getDefaultValue () : null ,
98+ 'isVariadic ' => $ param ->isVariadic (),
99+ 'isPassedByReference ' => $ param ->isPassedByReference (),
100+ ];
101+ }
102+
103+ return $ parameters ;
104+ }
105+
106+ private function getVisibility ($ reflection ): string
107+ {
108+ if ($ reflection ->isPrivate ()) {
109+ return 'private ' ;
110+ }
111+ if ($ reflection ->isProtected ()) {
112+ return 'protected ' ;
113+ }
114+
115+ return 'public ' ;
116+ }
117+
118+ private function getPropertyDefaultValue (ReflectionProperty $ property ): mixed
119+ {
120+ try {
121+ return $ property ->getDefaultValue ();
122+ } catch (\ReflectionException $ e ) {
123+ return null ;
124+ }
125+ }
126+ }
0 commit comments