Skip to content

Commit 77b81ee

Browse files
committed
Narrow array types
1 parent 097f008 commit 77b81ee

35 files changed

Lines changed: 71 additions & 109 deletions

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"require-dev": {
3838
"friendsofphp/php-cs-fixer": "^3.0",
3939
"fzaninotto/faker": "^1.6",
40-
"phpstan/phpstan": "^1.8",
40+
"phpstan/phpstan": "^2.1",
4141
"phpstan/phpstan-phpunit": "*",
4242
"phpstan/phpstan-strict-rules": "*",
4343
"phpunit/phpunit": "^7.0 || ^8.0 || ^9.0"

src/AbstractApi.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ abstract protected function configureOptions(OptionsResolver $resolver);
128128
* @param Request $request
129129
* @param ResponseInterface $response
130130
*
131-
* @return AbstractResponse|string|array<Transaction>
131+
* @return AbstractResponse|string|list<Transaction>
132132
*/
133133
abstract protected function handleResponse(Request $request, ResponseInterface $response);
134134

@@ -157,7 +157,7 @@ public function __construct(Authentication $authentication)
157157
/**
158158
* Generate the response
159159
*
160-
* @return AbstractResponse|string|array<Transaction>
160+
* @return AbstractResponse|string|list<Transaction>
161161
* @throws GuzzleException
162162
* @throws ResponseHeaderException
163163
* @throws ResponseMessageException
@@ -233,7 +233,9 @@ protected function doConfigureOptions()
233233
$this->setCustomerInfoResolver($resolver);
234234
$this->setValidationUrlResolver($resolver);
235235
$this->setAppleDomainResolver($resolver);
236-
$this->options = $resolver->resolve($this->unresolvedOptions);
236+
/** @var array<string, mixed> */
237+
$options = $resolver->resolve($this->unresolvedOptions);
238+
$this->options = $options;
237239
}
238240

239241
/**
@@ -253,13 +255,13 @@ protected function validateResponse($response)
253255
}
254256

255257
if (property_exists($response, 'MerchantErrorMessage')) {
256-
if ($response->MerchantErrorMessage) {
258+
if ($response->MerchantErrorMessage && is_string($response->MerchantErrorMessage)) {
257259
throw new Exceptions\ResponseMessageException($response->MerchantErrorMessage);
258260
}
259261
}
260262

261263
if (property_exists($response, 'CardHolderErrorMessage') && property_exists($response, 'CardHolderMessageMustBeShown')) {
262-
if ($response->CardHolderMessageMustBeShown) {
264+
if ($response->CardHolderMessageMustBeShown && is_string($response->CardHolderErrorMessage)) {
263265
throw new Exceptions\ResponseMessageException($response->CardHolderErrorMessage);
264266
}
265267
}
@@ -268,7 +270,7 @@ protected function validateResponse($response)
268270
/**
269271
* Generate the response
270272
*
271-
* @return AbstractResponse|string|array<Transaction>
273+
* @return AbstractResponse|string|list<Transaction>
272274
* @throws GuzzleException
273275
* @throws ClientException
274276
* @throws ResponseHeaderException
@@ -318,7 +320,7 @@ protected function parseUrl()
318320
/**
319321
* Get User Agent details
320322
*
321-
* @return string
323+
* @return ?string
322324
*/
323325
protected function getUserAgent()
324326
{
@@ -328,14 +330,14 @@ protected function getUserAgent()
328330
$userAgent = 'api-php/' . self::PHP_API_VERSION;
329331
if (extension_loaded('curl') && function_exists('curl_version')) {
330332
$curlInfo = \curl_version();
331-
if (is_array($curlInfo) && array_key_exists("version", $curlInfo)) {
333+
if (is_array($curlInfo) && array_key_exists("version", $curlInfo) && is_string($curlInfo["version"])) {
332334
$userAgent .= ' curl/' . $curlInfo["version"];
333335
}
334336
}
335337
$userAgent .= ' PHP/' . PHP_VERSION;
336338
}
337339

338-
return $userAgent;
340+
return is_string($userAgent) ? $userAgent : null;
339341
}
340342

341343
/**
@@ -380,7 +382,10 @@ protected function getBasicHeaders()
380382
);
381383
}
382384

383-
$headers['User-Agent'] = $this->getUserAgent();
385+
$userAgen = $this->getUserAgent();
386+
if ($userAgen) {
387+
$headers['User-Agent'] = $userAgen;
388+
}
384389

385390
return $headers;
386391
}

src/Api/Ecommerce/PaymentRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,10 @@ protected function configureOptions(OptionsResolver $resolver)
306306
$resolver->setAllowedValues('language', Types\LanguageTypes::getAllowed());
307307
$resolver->setDefault('type', 'payment');
308308
$resolver->setAllowedValues('type', Types\PaymentTypes::getAllowed());
309-
$resolver->setAllowedValues('sale_reconciliation_identifier', function ($value) {
309+
$resolver->setAllowedValues('sale_reconciliation_identifier', function (string $value) {
310310
return mb_strlen($value) <= 100;
311311
});
312-
$resolver->setAllowedValues('sale_invoice_number', function ($value) {
312+
$resolver->setAllowedValues('sale_invoice_number', function (string $value) {
313313
return mb_strlen($value) <= 100;
314314
});
315315
$resolver->setAllowedTypes('sales_tax', ['int', 'float']);
@@ -320,7 +320,7 @@ protected function configureOptions(OptionsResolver $resolver)
320320
$resolver->setNormalizer('config', function (Options $options, Config $value) {
321321
return $value->serialize();
322322
});
323-
$resolver->setAllowedValues('organisation_number', function ($value) {
323+
$resolver->setAllowedValues('organisation_number', function (string $value) {
324324
return mb_strlen($value) <= 20;
325325
});
326326
$resolver->setAllowedTypes('account_offer', 'bool');

src/Api/Others/Payments.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ protected function configureOptions(OptionsResolver $resolver)
9797
* @param Request $request
9898
* @param ResponseInterface $response
9999
*
100-
* @return Transaction[]
100+
* @return list<Transaction>
101101
* @throws \Exception
102102
*/
103103
protected function handleResponse(Request $request, ResponseInterface $response)

src/Api/Payments/CardWalletAuthorize.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,10 @@ protected function configureOptions(OptionsResolver $resolver)
291291
$resolver->setAllowedValues('language', Types\LanguageTypes::getAllowed());
292292
$resolver->setDefault('type', 'payment');
293293
$resolver->setAllowedValues('type', Types\PaymentTypes::getAllowed());
294-
$resolver->setAllowedValues('sale_reconciliation_identifier', function ($value) {
294+
$resolver->setAllowedValues('sale_reconciliation_identifier', function (string $value) {
295295
return mb_strlen($value) <= 100;
296296
});
297-
$resolver->setAllowedValues('sale_invoice_number', function ($value) {
297+
$resolver->setAllowedValues('sale_invoice_number', function (string $value) {
298298
return mb_strlen($value) <= 100;
299299
});
300300
$resolver->setAllowedTypes('sales_tax', ['int', 'float']);
@@ -305,7 +305,7 @@ protected function configureOptions(OptionsResolver $resolver)
305305
$resolver->setNormalizer('config', function (Options $options, Config $value) {
306306
return $value->serialize();
307307
});
308-
$resolver->setAllowedValues('organisation_number', function ($value) {
308+
$resolver->setAllowedValues('organisation_number', function (string $value) {
309309
return mb_strlen($value) <= 20;
310310
});
311311
$resolver->setAllowedTypes('account_offer', 'bool');

src/Api/Payments/ReservationOfFixedAmount.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ protected function configureOptions(OptionsResolver $resolver)
303303
return $value->serialize();
304304
});
305305
$resolver->setAllowedTypes('surcharge', ['int', 'float']);
306-
$resolver->setAllowedValues('sale_invoice_number', function ($value) {
306+
$resolver->setAllowedValues('sale_invoice_number', function (string $value) {
307307
return mb_strlen($value) <= 100;
308308
});
309309
$resolver->setNormalizer('cardnum', function (Options $options, $value) {

src/Request/Customer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,11 +841,11 @@ public function serialize()
841841
}
842842

843843
if ($this->clientJavascriptEnabled !== null) {
844-
$output['client_javascript_enabled'] = $this->clientJavascriptEnabled;
844+
$output['client_javascript_enabled'] = (string)$this->clientJavascriptEnabled;
845845
}
846846

847847
if ($this->clientJavaEnabled !== null) {
848-
$output['client_java_enabled'] = $this->clientJavaEnabled;
848+
$output['client_java_enabled'] = (string)$this->clientJavaEnabled;
849849
}
850850

851851
if ($this->clientColorDepth) {

src/Request/OrderLine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
class OrderLine extends AbstractSerializer
2727
{
28-
/** @var array<int, string> */
28+
/** @var list<string> */
2929
private static $goodsTypes = [
3030
'shipment',
3131
'handling',

src/Serializer/ResponseSerializer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static function serialize(
6262
* @param \SimpleXMLElement $data
6363
* @param string $childKey
6464
*
65-
* @return array<int, T>
65+
* @return list<T>
6666
* @throws \InvalidArgumentException
6767
*/
6868
public static function serializeChildren(
@@ -72,10 +72,10 @@ public static function serializeChildren(
7272
) {
7373
$documents = [];
7474

75-
if (! empty($data) && ! empty($data->{$childKey})) {
75+
if (! empty($data) && ! empty($data->{$childKey}) && is_array($data->{$childKey})) {
7676
foreach ($data->{$childKey} as $d) {
7777
$object = new $objectName();
78-
$documents[] = $object->deserialize($d);
78+
$documents[] = $object->deserialize($d instanceof \SimpleXMLElement ? $d : null);
7979
}
8080
}
8181

src/Traits/CsvToArrayTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ trait CsvToArrayTrait
3535
*
3636
* @param bool $includeHeader
3737
*
38-
* @return array<int, array<int, string|null>>
38+
* @return list<list<string|null>>
3939
*/
4040
public function __toArray($includeHeader = false)
4141
{

0 commit comments

Comments
 (0)