Skip to content

Commit 877f60f

Browse files
committed
Refactor TraitBuilder::attributeInfo()
Reused this attributeInfo() helper instead of recomputing data; renamed some of the information fields to more clearly distinguish types for the getter and setter. Change-Id: I52974abbc8ab32e5be45908665a19f4c2d5d9e95
1 parent 369de96 commit 877f60f

File tree

3 files changed

+49
-35
lines changed

3 files changed

+49
-35
lines changed

tools/InterfaceBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ protected function firstLine( string $type, string $topName, array $def ): void
320320
TraitBuilder::collectAttributes( $this->gen, $topName, [], $attrs );
321321
if ( count( $attrs ) > 0 ) {
322322
foreach ( $attrs as $a ) {
323-
$this->nl( " * @property {$a['docType']} \${$a['name']}" );
323+
$this->nl( " * @property {$a['getterTypeDoc']} \${$a['name']}" );
324324
}
325325
}
326326
$this->nl( ' * @phan-forbid-undeclared-magic-properties' );

tools/StubBuilder.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,25 @@ private function collectMixins( string $topName, array &$mixins ) {
5353
/** @inheritDoc */
5454
protected function emitMemberAttribute( string $topName, string $name, array $m ) {
5555
$typeOpts = [ 'topName' => $topName ];
56-
// Getter
57-
$getter = $this->map( $topName, 'get', $name );
58-
$docType = $this->gen->typeToPHPDoc( $m['idlType'], $typeOpts );
59-
$phpType = $this->gen->typeToPHP( $m['idlType'], [ 'setter' => true ] + $typeOpts );
60-
$retType = $this->gen->typeToPHP( $m['idlType'], [ 'returnType' => true ] + $typeOpts );
56+
$info = TraitBuilder::attributeInfo( $this->gen, $topName, $typeOpts, $m );
6157
$this->use( $m['idlType'], $typeOpts );
6258

59+
// Getter
6360
$this->nl( '/**' );
64-
$this->nl( " * @return $docType" );
61+
$this->nl( " * @return {$info['getterTypeDoc']}" );
6562
$this->nl( ' */' );
66-
$this->nl( "public function $getter()$retType {" );
63+
$this->nl( "public function {$info['getter']}(){$info['getterType']} {" );
6764
$this->nl( 'throw self::_unimplemented();' );
6865
$this->nl( '}' );
69-
if ( $m['readonly'] ?? false ) {
66+
if ( $info['setter'] === null ) {
7067
return;
7168
}
7269
$this->nl();
7370
// Setter
74-
$docType = $this->gen->typeToPHPDoc( $m['idlType'], [ 'setter' => true ] + $typeOpts );
75-
$setter = $this->map( $topName, 'set', $name );
7671
$this->nl( '/**' );
77-
$this->nl( " * @param $docType \$val" );
72+
$this->nl( " * @param {$info['setterTypeDoc']} \$val" );
7873
$this->nl( ' */' );
79-
$this->nl( "public function $setter( $phpType \$val ) : void {" );
74+
$this->nl( "public function {$info['setter']}( {$info['setterType']} \$val ) : void {" );
8075
$this->nl( 'throw self::_unimplemented();' );
8176
$this->nl( '}' );
8277
}

tools/TraitBuilder.php

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)