Skip to content

Commit 9a72dc7

Browse files
authored
Improve the parsing of values coming from headers (#1483)
* Replace usage of FILTER_VALIDATE_INT with a cast * Enable the fixer turning isset ternaries into null coalescing
1 parent 699e20c commit 9a72dc7

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

src/Generator/ClientGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ public function getVersion() {
9898
return false;
9999
}
100100

101-
$configRegion = isset($config['signRegion']) ? $config['signRegion'] : $region;
102-
$defaultConfigRegion = isset($defaultConfig['signRegion']) ? $defaultConfig['signRegion'] : $region;
101+
$configRegion = $config['signRegion'] ?? $region;
102+
$defaultConfigRegion = $defaultConfig['signRegion'] ?? $region;
103103
if ($configRegion !== $defaultConfigRegion) {
104104
return false;
105105
}

src/Generator/CodeGenerator/PopulatorGenerator.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -182,26 +182,40 @@ private function generatePopulator(Operation $operation, StructureShape $shape,
182182
$locationName = strtolower($member->getLocationName() ?? $member->getName());
183183
$propertyName = GeneratorHelper::normalizeName($member->getName());
184184
$memberShape = $member->getShape();
185-
if ('timestamp' === $memberShape->getType()) {
186-
$body .= strtr('$this->PROPERTY_NAME = isset($headers["LOCATION_NAME"][0]) ? new \DateTimeImmutable($headers["LOCATION_NAME"][0]) : null;' . "\n", [
187-
'PROPERTY_NAME' => $propertyName,
188-
'LOCATION_NAME' => $locationName,
189-
]);
190-
} else {
191-
if (null !== $constant = $this->typeGenerator->getFilterConstant($memberShape)) {
185+
switch ($memberShape->getType()) {
186+
case 'timestamp':
187+
$body .= strtr('$this->PROPERTY_NAME = isset($headers["LOCATION_NAME"][0]) ? new \DateTimeImmutable($headers["LOCATION_NAME"][0]) : null;' . "\n", [
188+
'PROPERTY_NAME' => $propertyName,
189+
'LOCATION_NAME' => $locationName,
190+
]);
191+
192+
break;
193+
case 'integer':
194+
case 'long':
195+
$body .= strtr('$this->PROPERTY_NAME = isset($headers["LOCATION_NAME"][0]) ? (int) $headers["LOCATION_NAME"][0] : null;' . "\n", [
196+
'PROPERTY_NAME' => $propertyName,
197+
'LOCATION_NAME' => $locationName,
198+
]);
199+
200+
break;
201+
case 'boolean':
192202
$this->requirementsRegistry->addRequirement('ext-filter');
193203

194-
$body .= strtr('$this->PROPERTY_NAME = isset($headers["LOCATION_NAME"][0]) ? filter_var($headers["LOCATION_NAME"][0], FILTER) : null;' . "\n", [
204+
$body .= strtr('$this->PROPERTY_NAME = isset($headers["LOCATION_NAME"][0]) ? filter_var($headers["LOCATION_NAME"][0], FILTER_VALIDATE_BOOLEAN) : null;' . "\n", [
195205
'PROPERTY_NAME' => $propertyName,
196206
'LOCATION_NAME' => $locationName,
197-
'FILTER' => $constant,
198207
]);
199-
} else {
208+
209+
break;
210+
case 'string':
200211
$body .= strtr('$this->PROPERTY_NAME = $headers["LOCATION_NAME"][0] ?? null;' . "\n", [
201212
'PROPERTY_NAME' => $propertyName,
202213
'LOCATION_NAME' => $locationName,
203214
]);
204-
}
215+
216+
break;
217+
default:
218+
throw new \RuntimeException(sprintf('Type %s is not yet implemented', $memberShape->getType()));
205219
}
206220
}
207221

src/Generator/CodeGenerator/TypeGenerator.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,6 @@ public function getPhpType(Shape $shape): array
169169
return [$type, $doc, $memberClassNames];
170170
}
171171

172-
public function getFilterConstant(Shape $shape): ?string
173-
{
174-
switch ($shape->getType()) {
175-
case 'integer':
176-
case 'long':
177-
return 'FILTER_VALIDATE_INT';
178-
case 'boolean':
179-
return 'FILTER_VALIDATE_BOOLEAN';
180-
case 'string':
181-
default:
182-
return null;
183-
}
184-
}
185-
186172
private function getNativePhpType(string $parameterType): string
187173
{
188174
switch ($parameterType) {

0 commit comments

Comments
 (0)