Skip to content

Fix typehint on enums to reflect that AWS might add new values #1923

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
  •  
  •  
  •  
232 changes: 0 additions & 232 deletions psalm.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,186 +88,6 @@
<code>getRows</code>
</PossiblyNullReference>
</file>
<file src="src/Service/CloudFormation/src/Result/DescribeStacksOutput.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<Capability::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/CodeBuild/src/Result/BatchGetBuildsOutput.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<CacheMode::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/CodeBuild/src/Result/StartBuildOutput.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<CacheMode::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/CodeBuild/src/Result/StopBuildOutput.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<CacheMode::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/CodeDeploy/src/Result/GetDeploymentOutput.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<AutoRollbackEvent::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Kinesis/src/Result/DescribeStreamOutput.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<MetricsName::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Kinesis/src/Result/DescribeStreamSummaryOutput.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<MetricsName::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Kinesis/src/Result/EnhancedMonitoringOutput.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<MetricsName::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Kms/src/Result/CreateKeyResponse.php">
<LessSpecificReturnStatement>
<code>$items</code>
<code>$items</code>
<code>$items</code>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<EncryptionAlgorithmSpec::*>]]></code>
<code><![CDATA[list<MacAlgorithmSpec::*>]]></code>
<code><![CDATA[list<SigningAlgorithmSpec::*>]]></code>
<code><![CDATA[list<KeyAgreementAlgorithmSpec::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Lambda/src/Result/ListFunctionsResponse.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<Architecture::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Lambda/src/Result/ListLayerVersionsResponse.php">
<LessSpecificReturnStatement>
<code>$items</code>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<Architecture::*>]]></code>
<code><![CDATA[list<Runtime::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Lambda/src/Result/ListVersionsByFunctionResponse.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<Architecture::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Lambda/src/Result/PublishLayerVersionResponse.php">
<LessSpecificReturnStatement>
<code>$items</code>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<Architecture::*>]]></code>
<code><![CDATA[list<Runtime::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/MediaConvert/src/Result/CreateJobResponse.php">
<LessSpecificReturnStatement>
<code>$items</code>
<code>$items</code>
<code>$items</code>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<AudioChannelTag::*>]]></code>
<code><![CDATA[list<HlsAdMarkers::*>]]></code>
<code><![CDATA[list<TeletextPageType::*>]]></code>
<code><![CDATA[list<FrameMetricType::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/MediaConvert/src/Result/GetJobResponse.php">
<LessSpecificReturnStatement>
<code>$items</code>
<code>$items</code>
<code>$items</code>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<AudioChannelTag::*>]]></code>
<code><![CDATA[list<HlsAdMarkers::*>]]></code>
<code><![CDATA[list<TeletextPageType::*>]]></code>
<code><![CDATA[list<FrameMetricType::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/MediaConvert/src/Result/ListJobsResponse.php">
<LessSpecificReturnStatement>
<code>$items</code>
<code>$items</code>
<code>$items</code>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<AudioChannelTag::*>]]></code>
<code><![CDATA[list<HlsAdMarkers::*>]]></code>
<code><![CDATA[list<TeletextPageType::*>]]></code>
<code><![CDATA[list<FrameMetricType::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Rekognition/src/Result/IndexFacesResponse.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<Reason::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/S3/src/Result/ListObjectsV2Output.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<ChecksumAlgorithm::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/S3/src/Result/ListObjectVersionsOutput.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<ChecksumAlgorithm::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/S3/src/Signer/SignerV4ForS3.php">
<InvalidArgument>
<code>array_keys($s3SignerOptions)</code>
Expand All @@ -289,56 +109,4 @@
<code><![CDATA[$d = \DateTimeImmutable::createFromFormat('U.u', \sprintf('%.6F', $data['stopDate']))]]></code>
</PossiblyFalsePropertyAssignmentValue>
</file>
<file src="src/Service/Sqs/src/Result/GetQueueAttributesResult.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[array<QueueAttributeName::*, string>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Sqs/src/Result/ReceiveMessageResult.php">
<LessSpecificReturnStatement>
<code>$items</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[array<MessageSystemAttributeName::*, string>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Lambda/src/Result/FunctionConfiguration.php">
<LessSpecificReturnStatement>
<code><![CDATA[$items]]></code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<Architecture::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/CognitoIdentityProvider/src/Result/InitiateAuthResponse.php">
<LessSpecificReturnStatement>
<code><![CDATA[$items]]></code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<ChallengeNameType::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/CognitoIdentityProvider/src/Result/AdminInitiateAuthResponse.php">
<LessSpecificReturnStatement>
<code><![CDATA[$items]]></code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<ChallengeNameType::*>]]></code>
</MoreSpecificReturnType>
</file>
<file src="src/Service/Kms/src/Result/GetPublicKeyResponse.php">
<LessSpecificReturnStatement>
<code><![CDATA[$items]]></code>
<code><![CDATA[$items]]></code>
<code><![CDATA[$items]]></code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType>
<code><![CDATA[list<EncryptionAlgorithmSpec::*>]]></code>
<code><![CDATA[list<KeyAgreementAlgorithmSpec::*>]]></code>
<code><![CDATA[list<SigningAlgorithmSpec::*>]]></code>
</MoreSpecificReturnType>
</file>
</files>
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private function generateProperties(StructureShape $shape, ClassBuilder $classBu
if (null !== $propertyDocumentation = $memberShape->getDocumentationMember()) {
$property->setComment(GeneratorHelper::parseDocumentation($propertyDocumentation));
}
[$returnType, $parameterType, $memberClassNames] = $this->typeGenerator->getPhpType($memberShape);
[$returnType, $parameterType, $memberClassNames] = $this->typeGenerator->getPhpType($memberShape, false);
foreach ($memberClassNames as $memberClassName) {
$classBuilder->addUse($memberClassName->getFqdn());
}
Expand All @@ -100,7 +100,7 @@ private function generateProperties(StructureShape $shape, ClassBuilder $classBu
}

if ($memberShape instanceof StructureShape) {
$this->objectGenerator->generate($memberShape);
$this->objectGenerator->generate($memberShape, false, false);
} elseif ($memberShape instanceof MapShape) {
$mapKeyShape = $memberShape->getKey()->getShape();
if ('string' !== $mapKeyShape->getType()) {
Expand All @@ -111,7 +111,7 @@ private function generateProperties(StructureShape $shape, ClassBuilder $classBu
}

if (($valueShape = $memberShape->getValue()->getShape()) instanceof StructureShape) {
$this->objectGenerator->generate($valueShape);
$this->objectGenerator->generate($valueShape, false, false);
}
if (!empty($valueShape->getEnum())) {
$this->enumGenerator->generate($valueShape);
Expand Down Expand Up @@ -299,7 +299,7 @@ private function generatePopulator(Operation $operation, StructureShape $shape,
private function generateListShapeMemberShape(Shape $memberShape, bool $forEndpoint): void
{
if ($memberShape instanceof StructureShape) {
$this->objectGenerator->generate($memberShape, $forEndpoint);
$this->objectGenerator->generate($memberShape, $forEndpoint, false);
} elseif ($memberShape instanceof ListShape) {
$this->generateListShapeMemberShape($memberShape->getMember()->getShape(), $forEndpoint);
}
Expand Down
37 changes: 29 additions & 8 deletions src/CodeGenerator/src/Generator/CodeGenerator/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(NamespaceRegistry $namespaceRegistry)
*
* @return array{string, ClassName[]} [docblock representation, ClassName related]
*/
public function generateDocblock(StructureShape $shape, ClassName $shapeClassName, bool $alternateClass = true, bool $allNullable = false, bool $isObject = false, array $extra = []): array
public function generateDocblock(StructureShape $shape, ClassName $shapeClassName, bool $alternateClass = true, bool $allNullable = false, bool $isObject = false, array $extra = [], bool $strictEnum = true): array
{
$classNames = [];
if ($alternateClass) {
Expand Down Expand Up @@ -67,7 +67,11 @@ public function generateDocblock(StructureShape $shape, ClassName $shapeClassNam
$param = 'array<' . $className->getName() . '|array>';
} elseif (!empty($listMemberShape->getEnum())) {
$classNames[] = $className = $this->namespaceRegistry->getEnum($listMemberShape);
$param = 'array<' . $className->getName() . '::*>';
if ($strictEnum) {
$param = 'array<' . $className->getName() . '::*>';
} else {
$param = 'array<' . $className->getName() . '::*|string>';
}
} else {
$param = $this->getNativePhpType($listMemberShape->getType()) . '[]';
}
Expand All @@ -81,13 +85,20 @@ public function generateDocblock(StructureShape $shape, ClassName $shapeClassNam
} elseif (!empty($mapValueShape->getEnum())) {
$classNames[] = $className = $this->namespaceRegistry->getEnum($mapValueShape);
$param = $className->getName() . '::*';
if (!$strictEnum) {
$param .= '|string';
}
} else {
$param = $this->getNativePhpType($mapValueShape->getType());
}
$mapKeyShape = $memberShape->getKey()->getShape();
if (!empty($mapKeyShape->getEnum())) {
$classNames[] = $className = $this->namespaceRegistry->getEnum($mapKeyShape);
$param = 'array<' . $className->getName() . '::*, ' . $param . '>';
if ($strictEnum) {
$param = 'array<' . $className->getName() . '::*, ' . $param . '>';
} else {
$param = 'array<' . $className->getName() . '::*|string, ' . $param . '>';
}
} else {
$param = 'array<string, ' . $param . '>';
}
Expand All @@ -101,6 +112,9 @@ public function generateDocblock(StructureShape $shape, ClassName $shapeClassNam
if (!empty($memberShape->getEnum())) {
$classNames[] = $className = $this->namespaceRegistry->getEnum($memberShape);
$param = $className->getName() . '::*';
if (!$strictEnum) {
$param .= '|string';
}
} else {
$param = $this->getNativePhpType($memberShape->getType());
}
Expand Down Expand Up @@ -136,7 +150,7 @@ public function generateDocblock(StructureShape $shape, ClassName $shapeClassNam
*
* @return array{string, string, ClassName[]} [typeHint value, docblock representation, ClassName related]
*/
public function getPhpType(Shape $shape): array
public function getPhpType(Shape $shape, bool $strictEnum = true): array
{
$memberClassNames = [];
if ($shape instanceof StructureShape) {
Expand All @@ -147,8 +161,8 @@ public function getPhpType(Shape $shape): array

if ($shape instanceof ListShape) {
$listMemberShape = $shape->getMember()->getShape();
[$type, $doc, $memberClassNames] = $this->getPhpType($listMemberShape);
if ('::*' === substr($doc, -3)) {
[$type, $doc, $memberClassNames] = $this->getPhpType($listMemberShape, $strictEnum);
if (!empty($listMemberShape->getEnum())) {
$doc = "list<$doc>";
} else {
$doc .= '[]';
Expand All @@ -160,10 +174,14 @@ public function getPhpType(Shape $shape): array
if ($shape instanceof MapShape) {
$mapKeyShape = $shape->getKey()->getShape();
$mapValueShape = $shape->getValue()->getShape();
[$type, $doc, $memberClassNames] = $this->getPhpType($mapValueShape);
[$type, $doc, $memberClassNames] = $this->getPhpType($mapValueShape, $strictEnum);
if (!empty($mapKeyShape->getEnum())) {
$memberClassNames[] = $memberClassName = $this->namespaceRegistry->getEnum($mapKeyShape);
$doc = "array<{$memberClassName->getName()}::*, $doc>";
if ($strictEnum) {
$doc = "array<{$memberClassName->getName()}::*, $doc>";
} else {
$doc = "array<{$memberClassName->getName()}::*|string, $doc>";
}
} else {
$doc = "array<string, $doc>";
}
Expand All @@ -180,6 +198,9 @@ public function getPhpType(Shape $shape): array
$memberClassNames[] = $memberClassName = $this->namespaceRegistry->getEnum($shape);

$doc = $memberClassName->getName() . '::*';
if (!$strictEnum) {
$doc .= '|string';
}
}

return [$type, $doc, $memberClassNames];
Expand Down
Loading