@@ -144,30 +144,49 @@ public static function collectAttributes( Generator $gen, string $topName, array
144144 '@phan-var array $def ' ; // @var array $def
145145 foreach ( $ def ['members ' ] ?? [] as $ m ) {
146146 if ( $ m ['type ' ] === 'attribute ' || $ m ['type ' ] === 'field ' ) {
147- $ readonly = ( $ m ['readonly ' ] ?? false ) || ( $ m ['type ' ] === 'field ' );
148- $ default = '' ;
149- if ( ( $ m ['required ' ] ?? false ) === false && ( $ m ['default ' ] ?? null ) !== null ) {
150- $ val = $ gen ->valueToPHP ( $ m ['default ' ] );
151- $ default = " ?? $ val " ;
152- }
153- $ attrs [] = [
154- 'topName ' => $ topName ,
155- 'type ' => $ m ['type ' ],
156- 'name ' => $ m ['name ' ],
157- 'idlType ' => $ m ['idlType ' ],
158- 'readonly ' => $ readonly ,
159- 'docType ' => $ gen ->typeToPHPDoc ( $ m ['idlType ' ], $ typeOpts ),
160- 'phpType ' => $ gen ->typeToPHP ( $ m ['idlType ' ], $ typeOpts ),
161- 'retType ' => $ gen ->typeToPHP ( $ m ['idlType ' ], [ 'returnType ' => true ] + $ typeOpts ),
162- 'getter ' => $ gen ->map ( $ topName , 'get ' , $ m ['name ' ] ),
163- 'setter ' => $ readonly ? null :
164- $ gen ->map ( $ topName , 'set ' , $ m ['name ' ] ),
165- 'default ' => $ default ,
166- ];
147+ $ info = self ::attributeInfo ( $ gen , $ topName , $ typeOpts , $ m );
148+ $ attrs [] = $ info ;
167149 }
168150 }
169151 }
170152
153+ /**
154+ * Extract attribute information from a member array.
155+ * @param Generator $gen
156+ * @param string $topName
157+ * @param array $typeOpts
158+ * @param array $m Member information
159+ * @return array
160+ */
161+ public static function attributeInfo ( Generator $ gen , string $ topName , array $ typeOpts , array $ m ): array {
162+ Assert::invariant (
163+ $ m ['type ' ] === 'attribute ' || $ m ['type ' ] === 'field ' ,
164+ "Member is not an attribute "
165+ );
166+ $ readonly = ( $ m ['readonly ' ] ?? false ) || ( $ m ['type ' ] === 'field ' );
167+ $ default = '' ;
168+ if ( ( $ m ['required ' ] ?? false ) === false && ( $ m ['default ' ] ?? null ) !== null ) {
169+ $ val = $ gen ->valueToPHP ( $ m ['default ' ] );
170+ $ default = " ?? $ val " ;
171+ }
172+ $ info = [
173+ 'topName ' => $ topName ,
174+ 'type ' => $ m ['type ' ],
175+ 'name ' => $ m ['name ' ],
176+ 'idlType ' => $ m ['idlType ' ],
177+ 'readonly ' => $ readonly ,
178+ 'getterType ' => $ gen ->typeToPHP ( $ m ['idlType ' ], [ 'returnType ' => true ] + $ typeOpts ),
179+ 'getterTypeDoc ' => $ gen ->typeToPHPDoc ( $ m ['idlType ' ], $ typeOpts ),
180+ 'setterType ' => $ gen ->typeToPHP ( $ m ['idlType ' ], [ 'setter ' => true ] + $ typeOpts ),
181+ 'setterTypeDoc ' => $ gen ->typeToPHPDoc ( $ m ['idlType ' ], [ 'setter ' => true ] + $ typeOpts ),
182+ 'getter ' => $ gen ->map ( $ topName , 'get ' , $ m ['name ' ] ),
183+ 'setter ' => $ readonly ? null :
184+ $ gen ->map ( $ topName , 'set ' , $ m ['name ' ] ),
185+ 'default ' => $ default ,
186+ ];
187+ return $ info ;
188+ }
189+
171190 /** @inheritDoc */
172191 protected function emitDictionary ( string $ topName , array $ def ): void {
173192 $ typeOpts = [ 'topName ' => $ topName ];
@@ -252,9 +271,9 @@ protected function emitDictionary( string $topName, array $def ): void {
252271 $ this ->nl ();
253272 foreach ( $ attrs as $ a ) {
254273 $ this ->nl ( '/** ' );
255- $ this ->nl ( " * @return {$ a ['docType ' ]}" );
274+ $ this ->nl ( " * @return {$ a ['getterTypeDoc ' ]}" );
256275 $ this ->nl ( ' */ ' );
257- $ this ->nl ( "public function {$ a ['getter ' ]}() {$ a ['retType ' ]} { " );
276+ $ this ->nl ( "public function {$ a ['getter ' ]}() {$ a ['getterType ' ]} { " );
258277 $ this ->nl ( 'return $this->a[ ' . json_encode ( $ a ['name ' ] ) . '] ' . $ a ['default ' ] . '; ' );
259278 $ this ->nl ( '} ' );
260279 $ this ->nl ();
0 commit comments