@@ -54,7 +54,7 @@ class Entity extends AbstractHydrator implements OptionsAwareInterface
5454 *
5555 * @return array
5656 */
57- protected function load (ModelInterface $ model ): array
57+ protected function loadStandard (ModelInterface $ model ): array
5858 {
5959 $ ref = new ReflectionClass ($ model );
6060
@@ -86,6 +86,68 @@ protected function load(ModelInterface $model): array
8686 return $ propertyInfo ;
8787 }
8888
89+ /**
90+ * @param ModelInterface $model
91+ * @return array
92+ * @throws ReflectionException
93+ */
94+ protected function load (ModelInterface $ model ): array
95+ {
96+ $ saveComments = (string )ini_get ('opcache.save_comments ' );
97+ $ useOpcacheFallback = ($ saveComments !== '1 ' );
98+
99+ if ($ useOpcacheFallback === true ) {
100+ return $ this ->loadWithFallback ($ model );
101+ }
102+
103+ return $ this ->loadStandard ($ model );
104+ }
105+
106+
107+ /**
108+ * @param ModelInterface $model
109+ * @return array
110+ */
111+ protected function loadWithFallback (ModelInterface $ model ): array
112+ {
113+ $ ref = new ReflectionClass ($ model );
114+ $ properties = $ ref ->getProperties ();
115+
116+ $ propertyInfo = [];
117+
118+ foreach ($ properties as $ property ) {
119+ $ docComment = $ property ->getDocComment ();
120+
121+ // If docblock exists, try to parse @var
122+ if ($ docComment !== false &&
123+ preg_match ("/@var(?: \n|\s(?P<type>.*)) \n/s " , $ docComment , $ annotations )
124+ ) {
125+ $ propertyInfo [$ property ->getName ()] = $ annotations ['type ' ];
126+ continue ;
127+ }
128+
129+ // Try to determine type via setter reflection
130+ $ setter = 'set ' . ucfirst ($ property ->getName ());
131+ if ($ ref ->hasMethod ($ setter )) {
132+ $ method = $ ref ->getMethod ($ setter );
133+ $ params = $ method ->getParameters ();
134+
135+ if (!empty ($ params )) {
136+ $ param = $ params [0 ];
137+ $ paramType = $ param ->getType ();
138+
139+ if ($ paramType && !$ paramType ->isBuiltin ()) {
140+ // Example: PayOrder::setAmount(Amount $amount)
141+ $ propertyInfo [$ property ->getName ()] = $ paramType ->getName ();
142+ }
143+ }
144+ }
145+ }
146+
147+ return $ propertyInfo ;
148+ }
149+
150+
89151 /**
90152 * @param array $data
91153 * @param object $object
0 commit comments