Skip to content

Commit ceba53f

Browse files
authored
Merge pull request #57 from AltaPay/narrow-array-types
Narrow array types
2 parents e9ebe2d + bb0360e commit ceba53f

38 files changed

+95
-120
lines changed

.github/workflows/code-analysis.yml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,4 @@ jobs:
4242
uses: StephaneBour/[email protected]
4343
with:
4444
dir: './src'
45-
46-
- name: Install PHP 5.6
47-
uses: shivammathur/setup-php@v2
48-
with:
49-
php-version: '5.6'
50-
51-
- name: Run Linter for PHP5.6
52-
uses: StephaneBour/[email protected]
53-
with:
54-
dir: './src'
5545

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [3.5.1] - 2025-06-11
8+
### Added
9+
- Improve type hinting.
10+
- Improve return type annotations for better type clarity.
11+
- Improve error handling by validating error message types.
12+
- Add null check before setting the User-Agent header.
13+
- Restrict supported PHP versions to 7.0 and above.
14+
715
## [3.5.0] - 2025-05-12
816
### Added
917
- Make billing and shipping address optional for Customer Info

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ try {
2626

2727
## Requirements
2828

29-
The AltaPay API PHP requires PHP 5.6.0 or greater with the following extensions installed:
29+
The AltaPay API PHP requires PHP 7.0.0 or greater with the following extensions installed:
3030

3131
- date
3232
- filter

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
]
2323
},
2424
"require": {
25-
"php": "^5.6 || ^7.0 || ^8.0",
25+
"php": "^7.0 || ^8.0",
2626
"ext-date": "*",
2727
"ext-filter": "*",
2828
"ext-mbstring": "*",
@@ -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: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ abstract class AbstractApi
5555
/**
5656
* PHP API version
5757
*/
58-
const PHP_API_VERSION = '3.5.0';
58+
const PHP_API_VERSION = '3.5.1';
5959

6060
/**
6161
* Event dispatcher
@@ -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) {

0 commit comments

Comments
 (0)