Skip to content

Commit b37c018

Browse files
authored
Add strict comparison null !== instead of ! (#1794)
1 parent b9d4e57 commit b37c018

File tree

109 files changed

+1447
-889
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+1447
-889
lines changed

src/CodeGenerator/src/Generator/PaginationGenerator.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ private function generateOutputPaginationLoader(string $iterator, string $common
207207
if (!$moreResult) {
208208
$moreCondition = '';
209209
foreach ($outputToken as $property) {
210-
$moreCondition .= $this->generateGetter('$page', $property, (bool) $common);
210+
$moreCondition .= 'null !== ' . $this->generateGetter('$page', $property, (bool) $common);
211+
212+
break;
211213
}
212214
} else {
213215
$moreCondition = $this->generateGetter('$page', $moreResult, (bool) $common);

src/CodeGenerator/src/Generator/ResponseParser/RestXmlParser.php

Lines changed: 100 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class RestXmlParser implements Parser
5050
*/
5151
private $imports = [];
5252

53+
/**
54+
* @var array<string, true>
55+
*/
56+
private $generatedFunctions = [];
57+
5358
public function __construct(NamespaceRegistry $namespaceRegistry, RequirementsRegistry $requirementsRegistry, TypeGenerator $typeGenerator)
5459
{
5560
$this->namespaceRegistry = $namespaceRegistry;
@@ -61,6 +66,7 @@ public function generate(StructureShape $shape, bool $throwOnError = true): Pars
6166
{
6267
$properties = [];
6368
$this->functions = [];
69+
$this->generatedFunctions = [];
6470
$this->imports = [];
6571
if (null !== $payload = $shape->getPayload()) {
6672
$member = $shape->getMember($payload);
@@ -80,19 +86,10 @@ public function generate(StructureShape $shape, bool $throwOnError = true): Pars
8086
continue;
8187
}
8288

83-
if (!$member->isNullable() && !$member->isRequired()) {
84-
$properties[] = strtr('if (null !== $v = (PROPERTY_ACCESSOR)) {
85-
$this->PROPERTY_NAME = $v;
86-
}', [
87-
'PROPERTY_NAME' => GeneratorHelper::normalizeName($member->getName()),
88-
'PROPERTY_ACCESSOR' => $this->parseXmlElement($this->getInputAccessor('$data', $member), $member->getShape(), $member->isRequired(), false),
89-
]);
90-
} else {
91-
$properties[] = strtr('$this->PROPERTY_NAME = PROPERTY_ACCESSOR;', [
92-
'PROPERTY_NAME' => GeneratorHelper::normalizeName($member->getName()),
93-
'PROPERTY_ACCESSOR' => $this->parseXmlElement($this->getInputAccessor('$data', $member), $member->getShape(), $member->isRequired(), false),
94-
]);
95-
}
89+
$properties[] = strtr('$this->PROPERTY_NAME = PROPERTY_ACCESSOR;', [
90+
'PROPERTY_NAME' => GeneratorHelper::normalizeName($member->getName()),
91+
'PROPERTY_ACCESSOR' => $this->parseXmlElement($this->getInputAccessor('$data', $member), $member->getShape(), $member->isRequired(), false),
92+
]);
9693
}
9794
}
9895

@@ -204,20 +201,35 @@ private function parseXmlElement(string $input, Shape $shape, bool $required, bo
204201

205202
private function parseXmlResponseStructure(StructureShape $shape, string $input, bool $required): string
206203
{
207-
$properties = [];
208-
foreach ($shape->getMembers() as $member) {
209-
$properties[] = strtr('PROPERTY_NAME => PROPERTY_ACCESSOR,', [
210-
'PROPERTY_NAME' => var_export($member->getName(), true),
211-
'PROPERTY_ACCESSOR' => $this->parseXmlElement($this->getInputAccessor($input, $member), $member->getShape(), $member->isRequired(), true),
212-
]);
204+
$functionName = 'populateResult' . ucfirst($shape->getName());
205+
if (!isset($this->generatedFunctions[$functionName])) {
206+
// prevent recursion
207+
$this->generatedFunctions[$functionName] = true;
208+
209+
$properties = [];
210+
foreach ($shape->getMembers() as $member) {
211+
$properties[] = strtr('PROPERTY_NAME => PROPERTY_ACCESSOR,', [
212+
'PROPERTY_NAME' => var_export($member->getName(), true),
213+
'PROPERTY_ACCESSOR' => $this->parseXmlElement($this->getInputAccessor('$xml', $member), $member->getShape(), $member->isRequired(), true),
214+
]);
215+
}
216+
217+
$body = 'return new CLASS_NAME([
218+
PROPERTIES
219+
]);';
220+
221+
$className = $this->namespaceRegistry->getObject($shape);
222+
$this->imports[] = $className;
223+
224+
$this->functions[$functionName] = $this->createPopulateMethod($functionName, strtr($body, [
225+
'CLASS_NAME' => $className->getName(),
226+
'PROPERTIES' => implode("\n", $properties),
227+
]), $shape);
213228
}
214229

215-
return strtr('REQUIRED new CLASS_NAME([
216-
PROPERTIES
217-
])', [
218-
'REQUIRED' => $required ? '' : '!' . $input . ' ? null : ',
219-
'CLASS_NAME' => $this->namespaceRegistry->getObject($shape)->getName(),
220-
'PROPERTIES' => implode("\n", $properties),
230+
return strtr($required ? '$this->FUNCTION_NAME(INPUT)' : '0 === INPUT->count() ? null : $this->FUNCTION_NAME(INPUT)', [
231+
'INPUT' => $input,
232+
'FUNCTION_NAME' => $functionName,
221233
]);
222234
}
223235

@@ -227,7 +239,7 @@ private function parseXmlResponseString(string $input, bool $required): string
227239
return strtr('(string) INPUT', ['INPUT' => $input]);
228240
}
229241

230-
return strtr('($v = INPUT) ? (string) $v : null', ['INPUT' => $input]);
242+
return strtr('(null !== $v = INPUT[0]) ? (string) $v : null', ['INPUT' => $input]);
231243
}
232244

233245
private function parseXmlResponseInteger(string $input, bool $required): string
@@ -236,7 +248,7 @@ private function parseXmlResponseInteger(string $input, bool $required): string
236248
return strtr('(int) (string) INPUT', ['INPUT' => $input]);
237249
}
238250

239-
return strtr('($v = INPUT) ? (int) (string) $v : null', ['INPUT' => $input]);
251+
return strtr('(null !== $v = INPUT[0]) ? (int) (string) $v : null', ['INPUT' => $input]);
240252
}
241253

242254
private function parseXmlResponseFloat(string $input, bool $required): string
@@ -245,7 +257,7 @@ private function parseXmlResponseFloat(string $input, bool $required): string
245257
return strtr('(float) (string) INPUT', ['INPUT' => $input]);
246258
}
247259

248-
return strtr('($v = INPUT) ? (float) (string) $v : null', ['INPUT' => $input]);
260+
return strtr('(null !== $v = INPUT[0]) ? (float) (string) $v : null', ['INPUT' => $input]);
249261
}
250262

251263
private function parseXmlResponseBool(string $input, bool $required): string
@@ -256,7 +268,7 @@ private function parseXmlResponseBool(string $input, bool $required): string
256268
return strtr('filter_var((string) INPUT, FILTER_VALIDATE_BOOLEAN)', ['INPUT' => $input]);
257269
}
258270

259-
return strtr('($v = INPUT) ? filter_var((string) $v, FILTER_VALIDATE_BOOLEAN) : null', ['INPUT' => $input]);
271+
return strtr('(null !== $v = INPUT[0]) ? filter_var((string) $v, FILTER_VALIDATE_BOOLEAN) : null', ['INPUT' => $input]);
260272
}
261273

262274
private function parseXmlResponseBlob(string $input, bool $required): string
@@ -265,7 +277,7 @@ private function parseXmlResponseBlob(string $input, bool $required): string
265277
return strtr('base64_decode((string) INPUT)', ['INPUT' => $input]);
266278
}
267279

268-
return strtr('($v = INPUT) ? base64_decode((string) $v) : null', ['INPUT' => $input]);
280+
return strtr('(null !== $v = INPUT[0]) ? base64_decode((string) $v) : null', ['INPUT' => $input]);
269281
}
270282

271283
private function parseXmlResponseTimestamp(Shape $shape, string $input, bool $required): string
@@ -281,47 +293,52 @@ private function parseXmlResponseTimestamp(Shape $shape, string $input, bool $re
281293
}
282294

283295
if (!$required) {
284-
$body = '($v = INPUT) ? ' . strtr($body, ['INPUT' => '$v']) . ' : null';
296+
$body = '(null !== $v = INPUT[0]) ? ' . strtr($body, ['INPUT' => '$v']) . ' : null';
285297
}
286298

287299
return strtr($body, ['INPUT' => $input]);
288300
}
289301

290302
private function parseXmlResponseList(ListShape $shape, string $input, bool $required, bool $inObject): string
291303
{
292-
$shapeMember = $shape->getMember();
293-
if ($shapeMember->getShape() instanceof StructureShape) {
294-
$listAccessorRequired = true;
295-
$body = '
296-
$items = [];
297-
foreach (INPUT_PROPERTY as $item) {
298-
$items[] = LIST_ACCESSOR;
299-
}
304+
$functionName = 'populateResult' . ucfirst($shape->getName());
305+
if (!isset($this->generatedFunctions[$functionName])) {
306+
// prevent recursion
307+
$this->generatedFunctions[$functionName] = true;
308+
309+
$shapeMember = $shape->getMember();
310+
if ($shapeMember->getShape() instanceof ListShape || $shapeMember->getShape() instanceof MapShape) {
311+
$listAccessorRequired = false;
312+
$body = '
313+
$items = [];
314+
foreach (INPUT_PROPERTY as $item) {
315+
$a = LIST_ACCESSOR;
316+
if (null !== $a) {
317+
$items[] = $a;
318+
}
319+
}
300320
301-
return $items;
302-
';
303-
} else {
304-
$listAccessorRequired = false;
305-
$body = '
306-
$items = [];
307-
foreach (INPUT_PROPERTY as $item) {
308-
$a = LIST_ACCESSOR;
309-
if (null !== $a) {
310-
$items[] = $a;
321+
return $items;
322+
';
323+
} else {
324+
$listAccessorRequired = true;
325+
$body = '
326+
$items = [];
327+
foreach (INPUT_PROPERTY as $item) {
328+
$items[] = LIST_ACCESSOR;
311329
}
312-
}
313330
314-
return $items;
315-
';
316-
}
331+
return $items;
332+
';
333+
}
317334

318-
$functionName = 'populateResult' . ucfirst($shape->getName());
319-
$this->functions[$functionName] = $this->createPopulateMethod($functionName, strtr($body, [
320-
'LIST_ACCESSOR' => $this->parseXmlElement('$item', $shapeMember->getShape(), $listAccessorRequired, $inObject),
321-
'INPUT_PROPERTY' => $shape->isFlattened() ? '$xml' : ('$xml->' . ($shapeMember->getLocationName() ? $shapeMember->getLocationName() : 'member')),
322-
]), $shape);
335+
$this->functions[$functionName] = $this->createPopulateMethod($functionName, strtr($body, [
336+
'LIST_ACCESSOR' => $this->parseXmlElement('$item', $shapeMember->getShape(), $listAccessorRequired, $inObject),
337+
'INPUT_PROPERTY' => $shape->isFlattened() ? '$xml' : ('$xml->' . ($shapeMember->getLocationName() ? $shapeMember->getLocationName() : 'member')),
338+
]), $shape);
339+
}
323340

324-
return strtr($required ? '$this->FUNCTION_NAME(INPUT)' : '!INPUT ? EMPTY : $this->FUNCTION_NAME(INPUT)', [
341+
return strtr($required ? '$this->FUNCTION_NAME(INPUT)' : '(0 === ($v = INPUT)->count()) ? EMPTY : $this->FUNCTION_NAME($v)', [
325342
'EMPTY' => !$inObject ? '[]' : 'null',
326343
'INPUT' => $input,
327344
'FUNCTION_NAME' => $functionName,
@@ -330,26 +347,31 @@ private function parseXmlResponseList(ListShape $shape, string $input, bool $req
330347

331348
private function parseXmlResponseMap(MapShape $shape, string $input, bool $required, bool $inObject): string
332349
{
333-
$shapeValue = $shape->getValue();
334-
$body = '
335-
$items = [];
336-
foreach (INPUT as $item) {
337-
$a = $item->MAP_VALUE;
338-
$items[$item->MAP_KEY->__toString()] = MAP_ACCESSOR;
339-
}
350+
$functionName = 'populateResult' . ucfirst($shape->getName());
351+
if (!isset($this->generatedFunctions[$functionName])) {
352+
// prevent recursion
353+
$this->generatedFunctions[$functionName] = true;
340354

341-
return $items;
342-
';
355+
$shapeValue = $shape->getValue();
356+
$body = '
357+
$items = [];
358+
foreach (INPUT as $item) {
359+
$a = $item->MAP_VALUE;
360+
$items[$item->MAP_KEY->__toString()] = MAP_ACCESSOR;
361+
}
343362
344-
$functionName = 'populateResult' . ucfirst($shape->getName());
345-
$this->functions[$functionName] = $this->createPopulateMethod($functionName, strtr($body, [
346-
'INPUT' => $shape->isFlattened() ? '$xml' : '$xml->entry',
347-
'MAP_KEY' => $shape->getKey()->getLocationName() ?? 'key',
348-
'MAP_VALUE' => $shape->getValue()->getLocationName() ?? 'value',
349-
'MAP_ACCESSOR' => $this->parseXmlElement('$a', $shapeValue->getShape(), true, $inObject),
350-
]), $shape);
351-
352-
return strtr($required ? '$this->FUNCTION_NAME(INPUT)' : '!INPUT ? EMPTY : $this->FUNCTION_NAME(INPUT)', [
363+
return $items;
364+
';
365+
366+
$this->functions[$functionName] = $this->createPopulateMethod($functionName, strtr($body, [
367+
'INPUT' => $shape->isFlattened() ? '$xml' : '$xml->entry',
368+
'MAP_KEY' => $shape->getKey()->getLocationName() ?? 'key',
369+
'MAP_VALUE' => $shape->getValue()->getLocationName() ?? 'value',
370+
'MAP_ACCESSOR' => $this->parseXmlElement('$a', $shapeValue->getShape(), true, $inObject),
371+
]), $shape);
372+
}
373+
374+
return strtr($required ? '$this->FUNCTION_NAME(INPUT)' : '(0 === ($v = INPUT)->count()) ? EMPTY : $this->FUNCTION_NAME($v)', [
353375
'EMPTY' => !$inObject ? '[]' : 'null',
354376
'INPUT' => $input,
355377
'FUNCTION_NAME' => $functionName,
@@ -368,6 +390,7 @@ private function createPopulateMethod(string $functionName, string $body, Shape
368390

369391
[$returnType, $parameterType, $memberClassNames] = $this->typeGenerator->getPhpType($shape);
370392
$method
393+
->setReturnType($returnType)
371394
->setComment('@return ' . $parameterType);
372395
$this->imports = array_merge($this->imports, $memberClassNames);
373396

src/Core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
### Changed
1010

11+
- use strict comparison `null !==` instead of `!`
1112
- Fix CS
1213

1314
## 1.22.1

src/Core/src/Signer/SignerV4.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ private function buildCanonicalQuery(Request $request): string
317317
$query = $request->getQuery();
318318

319319
unset($query['X-Amz-Signature']);
320-
if (!$query) {
320+
if (empty($query)) {
321321
return '';
322322
}
323323

src/Core/src/Sts/Result/AssumeRoleResponse.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,27 @@ protected function populateResult(Response $response): void
9494
$data = new \SimpleXMLElement($response->getContent());
9595
$data = $data->AssumeRoleResult;
9696

97-
$this->credentials = !$data->Credentials ? null : new Credentials([
98-
'AccessKeyId' => (string) $data->Credentials->AccessKeyId,
99-
'SecretAccessKey' => (string) $data->Credentials->SecretAccessKey,
100-
'SessionToken' => (string) $data->Credentials->SessionToken,
101-
'Expiration' => new \DateTimeImmutable((string) $data->Credentials->Expiration),
97+
$this->credentials = 0 === $data->Credentials->count() ? null : $this->populateResultCredentials($data->Credentials);
98+
$this->assumedRoleUser = 0 === $data->AssumedRoleUser->count() ? null : $this->populateResultAssumedRoleUser($data->AssumedRoleUser);
99+
$this->packedPolicySize = (null !== $v = $data->PackedPolicySize[0]) ? (int) (string) $v : null;
100+
$this->sourceIdentity = (null !== $v = $data->SourceIdentity[0]) ? (string) $v : null;
101+
}
102+
103+
private function populateResultAssumedRoleUser(\SimpleXMLElement $xml): AssumedRoleUser
104+
{
105+
return new AssumedRoleUser([
106+
'AssumedRoleId' => (string) $xml->AssumedRoleId,
107+
'Arn' => (string) $xml->Arn,
102108
]);
103-
$this->assumedRoleUser = !$data->AssumedRoleUser ? null : new AssumedRoleUser([
104-
'AssumedRoleId' => (string) $data->AssumedRoleUser->AssumedRoleId,
105-
'Arn' => (string) $data->AssumedRoleUser->Arn,
109+
}
110+
111+
private function populateResultCredentials(\SimpleXMLElement $xml): Credentials
112+
{
113+
return new Credentials([
114+
'AccessKeyId' => (string) $xml->AccessKeyId,
115+
'SecretAccessKey' => (string) $xml->SecretAccessKey,
116+
'SessionToken' => (string) $xml->SessionToken,
117+
'Expiration' => new \DateTimeImmutable((string) $xml->Expiration),
106118
]);
107-
$this->packedPolicySize = ($v = $data->PackedPolicySize) ? (int) (string) $v : null;
108-
$this->sourceIdentity = ($v = $data->SourceIdentity) ? (string) $v : null;
109119
}
110120
}

src/Core/src/Sts/Result/AssumeRoleWithWebIdentityResponse.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,30 @@ protected function populateResult(Response $response): void
147147
$data = new \SimpleXMLElement($response->getContent());
148148
$data = $data->AssumeRoleWithWebIdentityResult;
149149

150-
$this->credentials = !$data->Credentials ? null : new Credentials([
151-
'AccessKeyId' => (string) $data->Credentials->AccessKeyId,
152-
'SecretAccessKey' => (string) $data->Credentials->SecretAccessKey,
153-
'SessionToken' => (string) $data->Credentials->SessionToken,
154-
'Expiration' => new \DateTimeImmutable((string) $data->Credentials->Expiration),
150+
$this->credentials = 0 === $data->Credentials->count() ? null : $this->populateResultCredentials($data->Credentials);
151+
$this->subjectFromWebIdentityToken = (null !== $v = $data->SubjectFromWebIdentityToken[0]) ? (string) $v : null;
152+
$this->assumedRoleUser = 0 === $data->AssumedRoleUser->count() ? null : $this->populateResultAssumedRoleUser($data->AssumedRoleUser);
153+
$this->packedPolicySize = (null !== $v = $data->PackedPolicySize[0]) ? (int) (string) $v : null;
154+
$this->provider = (null !== $v = $data->Provider[0]) ? (string) $v : null;
155+
$this->audience = (null !== $v = $data->Audience[0]) ? (string) $v : null;
156+
$this->sourceIdentity = (null !== $v = $data->SourceIdentity[0]) ? (string) $v : null;
157+
}
158+
159+
private function populateResultAssumedRoleUser(\SimpleXMLElement $xml): AssumedRoleUser
160+
{
161+
return new AssumedRoleUser([
162+
'AssumedRoleId' => (string) $xml->AssumedRoleId,
163+
'Arn' => (string) $xml->Arn,
155164
]);
156-
$this->subjectFromWebIdentityToken = ($v = $data->SubjectFromWebIdentityToken) ? (string) $v : null;
157-
$this->assumedRoleUser = !$data->AssumedRoleUser ? null : new AssumedRoleUser([
158-
'AssumedRoleId' => (string) $data->AssumedRoleUser->AssumedRoleId,
159-
'Arn' => (string) $data->AssumedRoleUser->Arn,
165+
}
166+
167+
private function populateResultCredentials(\SimpleXMLElement $xml): Credentials
168+
{
169+
return new Credentials([
170+
'AccessKeyId' => (string) $xml->AccessKeyId,
171+
'SecretAccessKey' => (string) $xml->SecretAccessKey,
172+
'SessionToken' => (string) $xml->SessionToken,
173+
'Expiration' => new \DateTimeImmutable((string) $xml->Expiration),
160174
]);
161-
$this->packedPolicySize = ($v = $data->PackedPolicySize) ? (int) (string) $v : null;
162-
$this->provider = ($v = $data->Provider) ? (string) $v : null;
163-
$this->audience = ($v = $data->Audience) ? (string) $v : null;
164-
$this->sourceIdentity = ($v = $data->SourceIdentity) ? (string) $v : null;
165175
}
166176
}

src/Core/src/Sts/Result/GetCallerIdentityResponse.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ protected function populateResult(Response $response): void
6262
$data = new \SimpleXMLElement($response->getContent());
6363
$data = $data->GetCallerIdentityResult;
6464

65-
$this->userId = ($v = $data->UserId) ? (string) $v : null;
66-
$this->account = ($v = $data->Account) ? (string) $v : null;
67-
$this->arn = ($v = $data->Arn) ? (string) $v : null;
65+
$this->userId = (null !== $v = $data->UserId[0]) ? (string) $v : null;
66+
$this->account = (null !== $v = $data->Account[0]) ? (string) $v : null;
67+
$this->arn = (null !== $v = $data->Arn[0]) ? (string) $v : null;
6868
}
6969
}

0 commit comments

Comments
 (0)