Skip to content

Commit af7434a

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 826df66 commit af7434a

File tree

7 files changed

+38
-37
lines changed

7 files changed

+38
-37
lines changed

.php-cs-fixer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
'ordered_class_elements' => ['sort_algorithm' => 'none'],
8484
'class_attributes_separation' => ['elements' => ['property' => 'one', 'method' => 'one']],
8585
'array_indentation' => true,
86+
'ternary_to_null_coalescing' => true,
8687
])
8788
->setFinder($finder)
8889
;

src/CodeGenerator/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/CodeGenerator/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/CodeGenerator/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) {

src/Integration/Flysystem/S3/tests/Integration/AsyncAwsS3AdapterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public function adapter(): AdapterInterface
100100

101101
protected function createFilesystemAdapter(): AdapterInterface
102102
{
103-
$bucket = isset($_SERVER['FLYSYSTEM_AWS_S3_BUCKET']) ? $_SERVER['FLYSYSTEM_AWS_S3_BUCKET'] : 'flysystem-bucket-v1';
104-
$prefix = isset($_SERVER['FLYSYSTEM_AWS_S3_PREFIX']) ? $_SERVER['FLYSYSTEM_AWS_S3_PREFIX'] : static::$adapterPrefix;
103+
$bucket = $_SERVER['FLYSYSTEM_AWS_S3_BUCKET'] ?? 'flysystem-bucket-v1';
104+
$prefix = $_SERVER['FLYSYSTEM_AWS_S3_PREFIX'] ?? static::$adapterPrefix;
105105

106106
return new AsyncAwsS3Adapter($this->s3Client(), $bucket, $prefix);
107107
}
@@ -115,7 +115,7 @@ private function s3Client(): S3Client
115115
$key = $_SERVER['FLYSYSTEM_AWS_S3_KEY'] ?? null;
116116
$secret = $_SERVER['FLYSYSTEM_AWS_S3_SECRET'] ?? null;
117117
$bucket = $_SERVER['FLYSYSTEM_AWS_S3_BUCKET'] ?? null;
118-
$region = isset($_SERVER['FLYSYSTEM_AWS_S3_REGION']) ? $_SERVER['FLYSYSTEM_AWS_S3_REGION'] : 'eu-central-1';
118+
$region = $_SERVER['FLYSYSTEM_AWS_S3_REGION'] ?? 'eu-central-1';
119119

120120
if (!$key || !$secret || !$bucket) {
121121
self::$docker = true;

src/Service/S3/src/Result/GetObjectOutput.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,13 @@ protected function populateResult(Response $response): void
506506
$this->expiration = $headers['x-amz-expiration'][0] ?? null;
507507
$this->restore = $headers['x-amz-restore'][0] ?? null;
508508
$this->lastModified = isset($headers['last-modified'][0]) ? new \DateTimeImmutable($headers['last-modified'][0]) : null;
509-
$this->contentLength = isset($headers['content-length'][0]) ? filter_var($headers['content-length'][0], \FILTER_VALIDATE_INT) : null;
509+
$this->contentLength = isset($headers['content-length'][0]) ? (int) $headers['content-length'][0] : null;
510510
$this->etag = $headers['etag'][0] ?? null;
511511
$this->checksumCrc32 = $headers['x-amz-checksum-crc32'][0] ?? null;
512512
$this->checksumCrc32C = $headers['x-amz-checksum-crc32c'][0] ?? null;
513513
$this->checksumSha1 = $headers['x-amz-checksum-sha1'][0] ?? null;
514514
$this->checksumSha256 = $headers['x-amz-checksum-sha256'][0] ?? null;
515-
$this->missingMeta = isset($headers['x-amz-missing-meta'][0]) ? filter_var($headers['x-amz-missing-meta'][0], \FILTER_VALIDATE_INT) : null;
515+
$this->missingMeta = isset($headers['x-amz-missing-meta'][0]) ? (int) $headers['x-amz-missing-meta'][0] : null;
516516
$this->versionId = $headers['x-amz-version-id'][0] ?? null;
517517
$this->cacheControl = $headers['cache-control'][0] ?? null;
518518
$this->contentDisposition = $headers['content-disposition'][0] ?? null;
@@ -530,8 +530,8 @@ protected function populateResult(Response $response): void
530530
$this->storageClass = $headers['x-amz-storage-class'][0] ?? null;
531531
$this->requestCharged = $headers['x-amz-request-charged'][0] ?? null;
532532
$this->replicationStatus = $headers['x-amz-replication-status'][0] ?? null;
533-
$this->partsCount = isset($headers['x-amz-mp-parts-count'][0]) ? filter_var($headers['x-amz-mp-parts-count'][0], \FILTER_VALIDATE_INT) : null;
534-
$this->tagCount = isset($headers['x-amz-tagging-count'][0]) ? filter_var($headers['x-amz-tagging-count'][0], \FILTER_VALIDATE_INT) : null;
533+
$this->partsCount = isset($headers['x-amz-mp-parts-count'][0]) ? (int) $headers['x-amz-mp-parts-count'][0] : null;
534+
$this->tagCount = isset($headers['x-amz-tagging-count'][0]) ? (int) $headers['x-amz-tagging-count'][0] : null;
535535
$this->objectLockMode = $headers['x-amz-object-lock-mode'][0] ?? null;
536536
$this->objectLockRetainUntilDate = isset($headers['x-amz-object-lock-retain-until-date'][0]) ? new \DateTimeImmutable($headers['x-amz-object-lock-retain-until-date'][0]) : null;
537537
$this->objectLockLegalHoldStatus = $headers['x-amz-object-lock-legal-hold'][0] ?? null;

src/Service/S3/src/Result/HeadObjectOutput.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,13 +534,13 @@ protected function populateResult(Response $response): void
534534
$this->restore = $headers['x-amz-restore'][0] ?? null;
535535
$this->archiveStatus = $headers['x-amz-archive-status'][0] ?? null;
536536
$this->lastModified = isset($headers['last-modified'][0]) ? new \DateTimeImmutable($headers['last-modified'][0]) : null;
537-
$this->contentLength = isset($headers['content-length'][0]) ? filter_var($headers['content-length'][0], \FILTER_VALIDATE_INT) : null;
537+
$this->contentLength = isset($headers['content-length'][0]) ? (int) $headers['content-length'][0] : null;
538538
$this->checksumCrc32 = $headers['x-amz-checksum-crc32'][0] ?? null;
539539
$this->checksumCrc32C = $headers['x-amz-checksum-crc32c'][0] ?? null;
540540
$this->checksumSha1 = $headers['x-amz-checksum-sha1'][0] ?? null;
541541
$this->checksumSha256 = $headers['x-amz-checksum-sha256'][0] ?? null;
542542
$this->etag = $headers['etag'][0] ?? null;
543-
$this->missingMeta = isset($headers['x-amz-missing-meta'][0]) ? filter_var($headers['x-amz-missing-meta'][0], \FILTER_VALIDATE_INT) : null;
543+
$this->missingMeta = isset($headers['x-amz-missing-meta'][0]) ? (int) $headers['x-amz-missing-meta'][0] : null;
544544
$this->versionId = $headers['x-amz-version-id'][0] ?? null;
545545
$this->cacheControl = $headers['cache-control'][0] ?? null;
546546
$this->contentDisposition = $headers['content-disposition'][0] ?? null;
@@ -557,7 +557,7 @@ protected function populateResult(Response $response): void
557557
$this->storageClass = $headers['x-amz-storage-class'][0] ?? null;
558558
$this->requestCharged = $headers['x-amz-request-charged'][0] ?? null;
559559
$this->replicationStatus = $headers['x-amz-replication-status'][0] ?? null;
560-
$this->partsCount = isset($headers['x-amz-mp-parts-count'][0]) ? filter_var($headers['x-amz-mp-parts-count'][0], \FILTER_VALIDATE_INT) : null;
560+
$this->partsCount = isset($headers['x-amz-mp-parts-count'][0]) ? (int) $headers['x-amz-mp-parts-count'][0] : null;
561561
$this->objectLockMode = $headers['x-amz-object-lock-mode'][0] ?? null;
562562
$this->objectLockRetainUntilDate = isset($headers['x-amz-object-lock-retain-until-date'][0]) ? new \DateTimeImmutable($headers['x-amz-object-lock-retain-until-date'][0]) : null;
563563
$this->objectLockLegalHoldStatus = $headers['x-amz-object-lock-legal-hold'][0] ?? null;

0 commit comments

Comments
 (0)