Skip to content

Generate the phpdoc for the static create method of objects #1464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 8 additions & 3 deletions src/CodeGenerator/src/Generator/InputGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,16 @@ public function generate(Operation $operation): ClassName
}

// Add named constructor
$classBuilder->addMethod('create')
$createMethod = $classBuilder->addMethod('create')
->setStatic(true)
->setReturnType('self')
->setBody('return $input instanceof self ? $input : new self($input);')
->addParameter('input');
->setBody('return $input instanceof self ? $input : new self($input);');
$createMethod->addParameter('input');
[$doc, $memberClassNames] = $this->typeGenerator->generateDocblock($shape, $className, true, true, false, [' \'@region\'?: string|null,']);
$createMethod->addComment($doc);
foreach ($memberClassNames as $memberClassName) {
$classBuilder->addUse($memberClassName->getFqdn());
}

$constructorBody .= 'parent::__construct($input);';
$constructor = $classBuilder->addMethod('__construct');
Expand Down
22 changes: 16 additions & 6 deletions src/CodeGenerator/src/Generator/ObjectGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,30 @@ private function isShapeUsedInput(StructureShape $shape): bool
private function namedConstructor(StructureShape $shape, ClassBuilder $classBuilder): void
{
if (empty($shape->getMembers())) {
$classBuilder->addMethod('create')
$createMethod = $classBuilder->addMethod('create')
->setStatic(true)
->setReturnType('self')
->setBody('return $input instanceof self ? $input : new self();')
->addParameter('input');
->setBody('return $input instanceof self ? $input : new self();');
$createMethod->addParameter('input');
[$doc, $memberClassNames] = $this->typeGenerator->generateDocblock($shape, $this->generated[$shape->getName()], true, false, true);
$createMethod->addComment($doc);
foreach ($memberClassNames as $memberClassName) {
$classBuilder->addUse($memberClassName->getFqdn());
}

return;
}

$classBuilder->addMethod('create')
$createMethod = $classBuilder->addMethod('create')
->setStatic(true)
->setReturnType('self')
->setBody('return $input instanceof self ? $input : new self($input);')
->addParameter('input');
->setBody('return $input instanceof self ? $input : new self($input);');
$createMethod->addParameter('input');
[$doc, $memberClassNames] = $this->typeGenerator->generateDocblock($shape, $this->generated[$shape->getName()], true, false, true);
$createMethod->addComment($doc);
foreach ($memberClassNames as $memberClassName) {
$classBuilder->addUse($memberClassName->getFqdn());
}

// We need a constructor
$constructor = $classBuilder->addMethod('__construct');
Expand Down
16 changes: 16 additions & 0 deletions src/Core/src/Sts/Input/AssumeRoleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,22 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* RoleArn?: string,
* RoleSessionName?: string,
* PolicyArns?: PolicyDescriptorType[],
* Policy?: string,
* DurationSeconds?: int,
* Tags?: Tag[],
* TransitiveTagKeys?: string[],
* ExternalId?: string,
* SerialNumber?: string,
* TokenCode?: string,
* SourceIdentity?: string,
* '@region'?: string|null,
* }|AssumeRoleRequest $input
*/
public static function create($input): self
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is just a proxy for --construct.
I'm not sure duplicating this docbloc provides any value.

If the user enters invalid content, the STA should report the issue when calling new self($input); right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. STA won't report anything because they will think that the method accept mixed. All SA tools are using functions as boundaries for the analysis. They don't rely on the implementation of methods to decide whether callers are using them properly.

Btw, this is one of the main blocker to increase the phpstan level (and probably also to lower the Psalm level or to re-enable the disabled Psalm rules). At level 6, phpstan complains about functions or properties that don't declare their type precisely because of this.

{
return $input instanceof self ? $input : new self($input);
Expand Down
12 changes: 12 additions & 0 deletions src/Core/src/Sts/Input/AssumeRoleWithWebIdentityRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* RoleArn?: string,
* RoleSessionName?: string,
* WebIdentityToken?: string,
* ProviderId?: string,
* PolicyArns?: PolicyDescriptorType[],
* Policy?: string,
* DurationSeconds?: int,
* '@region'?: string|null,
* }|AssumeRoleWithWebIdentityRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Sts/Input/GetCallerIdentityRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* '@region'?: string|null,
* }|GetCallerIdentityRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
6 changes: 6 additions & 0 deletions src/Core/src/Sts/ValueObject/AssumedRoleUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public function __construct(array $input)
$this->arn = $input['Arn'] ?? null;
}

/**
* @param array{
* AssumedRoleId: string,
* Arn: string,
* }|AssumedRoleUser $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
8 changes: 8 additions & 0 deletions src/Core/src/Sts/ValueObject/Credentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function __construct(array $input)
$this->expiration = $input['Expiration'] ?? null;
}

/**
* @param array{
* AccessKeyId: string,
* SecretAccessKey: string,
* SessionToken: string,
* Expiration: \DateTimeImmutable,
* }|Credentials $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
5 changes: 5 additions & 0 deletions src/Core/src/Sts/ValueObject/PolicyDescriptorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public function __construct(array $input)
$this->arn = $input['arn'] ?? null;
}

/**
* @param array{
* arn?: null|string,
* }|PolicyDescriptorType $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
6 changes: 6 additions & 0 deletions src/Core/src/Sts/ValueObject/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public function __construct(array $input)
$this->value = $input['Value'] ?? null;
}

/**
* @param array{
* Key: string,
* Value: string,
* }|Tag $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
18 changes: 18 additions & 0 deletions src/Service/AppSync/src/Input/CreateResolverRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* apiId?: string,
* typeName?: string,
* fieldName?: string,
* dataSourceName?: string,
* requestMappingTemplate?: string,
* responseMappingTemplate?: string,
* kind?: ResolverKind::*,
* pipelineConfig?: PipelineConfig|array,
* syncConfig?: SyncConfig|array,
* cachingConfig?: CachingConfig|array,
* maxBatchSize?: int,
* runtime?: AppSyncRuntime|array,
* code?: string,
* '@region'?: string|null,
* }|CreateResolverRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
8 changes: 8 additions & 0 deletions src/Service/AppSync/src/Input/DeleteResolverRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* apiId?: string,
* typeName?: string,
* fieldName?: string,
* '@region'?: string|null,
* }|DeleteResolverRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* apiId?: string,
* '@region'?: string|null,
* }|GetSchemaCreationStatusRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
8 changes: 8 additions & 0 deletions src/Service/AppSync/src/Input/ListApiKeysRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* apiId?: string,
* nextToken?: string,
* maxResults?: int,
* '@region'?: string|null,
* }|ListApiKeysRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
9 changes: 9 additions & 0 deletions src/Service/AppSync/src/Input/ListResolversRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* apiId?: string,
* typeName?: string,
* nextToken?: string,
* maxResults?: int,
* '@region'?: string|null,
* }|ListResolversRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
7 changes: 7 additions & 0 deletions src/Service/AppSync/src/Input/StartSchemaCreationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* apiId?: string,
* definition?: string,
* '@region'?: string|null,
* }|StartSchemaCreationRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
9 changes: 9 additions & 0 deletions src/Service/AppSync/src/Input/UpdateApiKeyRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* apiId?: string,
* id?: string,
* description?: string,
* expires?: string,
* '@region'?: string|null,
* }|UpdateApiKeyRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
17 changes: 17 additions & 0 deletions src/Service/AppSync/src/Input/UpdateDataSourceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,23 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* apiId?: string,
* name?: string,
* description?: string,
* type?: DataSourceType::*,
* serviceRoleArn?: string,
* dynamodbConfig?: DynamodbDataSourceConfig|array,
* lambdaConfig?: LambdaDataSourceConfig|array,
* elasticsearchConfig?: ElasticsearchDataSourceConfig|array,
* openSearchServiceConfig?: OpenSearchServiceDataSourceConfig|array,
* httpConfig?: HttpDataSourceConfig|array,
* relationalDatabaseConfig?: RelationalDatabaseDataSourceConfig|array,
* eventBridgeConfig?: EventBridgeDataSourceConfig|array,
* '@region'?: string|null,
* }|UpdateDataSourceRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
18 changes: 18 additions & 0 deletions src/Service/AppSync/src/Input/UpdateResolverRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ public function __construct(array $input = [])
parent::__construct($input);
}

/**
* @param array{
* apiId?: string,
* typeName?: string,
* fieldName?: string,
* dataSourceName?: string,
* requestMappingTemplate?: string,
* responseMappingTemplate?: string,
* kind?: ResolverKind::*,
* pipelineConfig?: PipelineConfig|array,
* syncConfig?: SyncConfig|array,
* cachingConfig?: CachingConfig|array,
* maxBatchSize?: int,
* runtime?: AppSyncRuntime|array,
* code?: string,
* '@region'?: string|null,
* }|UpdateResolverRequest $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
8 changes: 8 additions & 0 deletions src/Service/AppSync/src/ValueObject/ApiKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ public function __construct(array $input)
$this->deletes = $input['deletes'] ?? null;
}

/**
* @param array{
* id?: null|string,
* description?: null|string,
* expires?: null|string,
* deletes?: null|string,
* }|ApiKey $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
6 changes: 6 additions & 0 deletions src/Service/AppSync/src/ValueObject/AppSyncRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public function __construct(array $input)
$this->runtimeVersion = $input['runtimeVersion'] ?? null;
}

/**
* @param array{
* name: RuntimeName::*,
* runtimeVersion: string,
* }|AppSyncRuntime $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
6 changes: 6 additions & 0 deletions src/Service/AppSync/src/ValueObject/AuthorizationConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public function __construct(array $input)
$this->awsIamConfig = isset($input['awsIamConfig']) ? AwsIamConfig::create($input['awsIamConfig']) : null;
}

/**
* @param array{
* authorizationType: AuthorizationType::*,
* awsIamConfig?: null|AwsIamConfig|array,
* }|AuthorizationConfig $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
6 changes: 6 additions & 0 deletions src/Service/AppSync/src/ValueObject/AwsIamConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public function __construct(array $input)
$this->signingServiceName = $input['signingServiceName'] ?? null;
}

/**
* @param array{
* signingRegion?: null|string,
* signingServiceName?: null|string,
* }|AwsIamConfig $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
5 changes: 5 additions & 0 deletions src/Service/AppSync/src/ValueObject/BadRequestDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public function __construct(array $input)
$this->codeErrors = isset($input['codeErrors']) ? array_map([CodeError::class, 'create'], $input['codeErrors']) : null;
}

/**
* @param array{
* codeErrors?: null|CodeError[],
* }|BadRequestDetail $input
*/
public static function create($input): self
{
return $input instanceof self ? $input : new self($input);
Expand Down
Loading