Skip to content

Commit 2d751b7

Browse files
committed
Implement phpstan generics
1 parent 81d9848 commit 2d751b7

File tree

82 files changed

+360
-291
lines changed

Some content is hidden

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

82 files changed

+360
-291
lines changed

.github/workflows/ci.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: CI
2+
3+
on:
4+
push: ~
5+
pull_request: ~
6+
7+
permissions: read-all
8+
9+
jobs:
10+
ci:
11+
uses: 'terminal42/contao-build-tools/.github/workflows/build-tools.yml@main'

composer-dependency-analyser.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
4+
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;
5+
6+
return (new Configuration())
7+
->ignoreErrorsOnExtensionAndPath('ext-dom', 'src/XmlHelper.php', [ErrorType::SHADOW_DEPENDENCY])
8+
;

phpstan.neon

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
ignoreErrors:
3+
- '#no value type specified in iterable type array#'

src/Api/AbstractCRUDEndpoint.php

+20-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Terminal42\CashctrlApi\Exception\DomainException;
1010
use Terminal42\CashctrlApi\Result;
1111

12+
/**
13+
* @template T of EntityInterface
14+
*/
1215
abstract class AbstractCRUDEndpoint
1316
{
1417
public function __construct(
@@ -17,6 +20,9 @@ public function __construct(
1720
) {
1821
}
1922

23+
/**
24+
* @return EntityInterface<T>|null
25+
*/
2026
public function read(int $id): EntityInterface|null
2127
{
2228
$result = $this->get('read.json', ['id' => $id], false);
@@ -28,6 +34,9 @@ public function read(int $id): EntityInterface|null
2834
return $this->createInstance($result->data());
2935
}
3036

37+
/**
38+
* @param EntityInterface<T> $entity
39+
*/
3140
public function create(EntityInterface $entity): Result
3241
{
3342
if (null !== $entity->getId()) {
@@ -37,6 +46,9 @@ public function create(EntityInterface $entity): Result
3746
return $this->post('create.json', $entity->toArray());
3847
}
3948

49+
/**
50+
* @param EntityInterface<T> $entity
51+
*/
4052
public function update(EntityInterface $entity): Result
4153
{
4254
if (null === $entity->getId()) {
@@ -46,12 +58,15 @@ public function update(EntityInterface $entity): Result
4658
return $this->post('update.json', $entity->toArray());
4759
}
4860

61+
/**
62+
* @param array<int> $ids
63+
*/
4964
public function delete(array $ids): Result
5065
{
51-
return $this->post('delete.json', ['ids' => $ids]);
66+
return $this->post('delete.json', ['ids' => implode(',', $ids)]);
5267
}
5368

54-
protected function get(string $url, array $params = [], bool $throwValidationError = true)
69+
protected function get(string $url, array $params = [], bool $throwValidationError = true): Result|string
5570
{
5671
return $this->client->get($this->urlPrefix.'/'.$url, $params, $throwValidationError);
5772
}
@@ -61,5 +76,8 @@ protected function post(string $url, array $params, bool $throwValidationError =
6176
return $this->client->post($this->urlPrefix.'/'.$url, $params, $throwValidationError);
6277
}
6378

79+
/**
80+
* @return EntityInterface<T>
81+
*/
6482
abstract protected function createInstance(array $data): EntityInterface;
6583
}

src/Api/AbstractEndpoint.php

+9
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,18 @@
55
namespace Terminal42\CashctrlApi\Api;
66

77
use Terminal42\CashctrlApi\Api\Filter\ListFilter;
8+
use Terminal42\CashctrlApi\Entity\EntityInterface;
89

10+
/**
11+
* @template T of EntityInterface
12+
*
13+
* @extends AbstractCRUDEndpoint<T>
14+
*/
915
abstract class AbstractEndpoint extends AbstractCRUDEndpoint
1016
{
17+
/**
18+
* @return ListFilter<T>
19+
*/
1120
public function list(): ListFilter
1221
{
1322
return new ListFilter($this->client, $this->urlPrefix, fn (array $data) => $this->createInstance($data));

src/Api/AccountEndpoint.php

+2-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
use Terminal42\CashctrlApi\Result;
1111

1212
/**
13-
* @method Account|null read(int $id)
14-
* @method Result create(Account $account)
15-
* @method Result update(Account $account)
16-
* @method Result delete(array $ids)
13+
* @extends AbstractEndpoint<Account>
1714
*/
1815
class AccountEndpoint extends AbstractEndpoint
1916
{
@@ -22,15 +19,12 @@ public function __construct(ApiClientInterface $client)
2219
parent::__construct($client, 'account');
2320
}
2421

25-
/**
26-
* @return array<Account>|AccountListFilter
27-
*/
2822
public function list(): AccountListFilter
2923
{
3024
return new AccountListFilter($this->client, 'account', fn (array $data) => $this->createInstance($data));
3125
}
3226

33-
public function balance(int $id, \DateTimeInterface|null $date = null)
27+
public function balance(int $id, \DateTimeInterface|null $date = null): string
3428
{
3529
$params = ['id' => $id];
3630

src/Api/CurrencyEndpoint.php

+1-6
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44

55
namespace Terminal42\CashctrlApi\Api;
66

7-
use Terminal42\CashctrlApi\Api\Filter\ListFilter;
87
use Terminal42\CashctrlApi\ApiClientInterface;
98
use Terminal42\CashctrlApi\Entity\Currency;
109
use Terminal42\CashctrlApi\Result;
1110

1211
/**
13-
* @method Currency|null read(int $id)
14-
* @method array<Currency>|ListFilter list()
15-
* @method Result create(Currency $entity)
16-
* @method Result update(Currency $entity)
17-
* @method Result delete(array $ids)
12+
* @extends AbstractEndpoint<Currency>
1813
*/
1914
class CurrencyEndpoint extends AbstractEndpoint
2015
{

src/Api/CustomfieldEndpoint.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@
1010
use Terminal42\CashctrlApi\Result;
1111

1212
/**
13-
* @method Customfield|null read(int $id)
14-
* @method Result create(Customfield $customfield)
15-
* @method Result update(Customfield $customfield)
16-
* @method Result delete(array $ids)
13+
* @extends AbstractCRUDEndpoint<Customfield>
1714
*/
1815
class CustomfieldEndpoint extends AbstractCRUDEndpoint
1916
{
@@ -36,9 +33,6 @@ public function __construct(ApiClientInterface $client)
3633
parent::__construct($client, 'customfield');
3734
}
3835

39-
/**
40-
* @return array<Customfield>|CustomfieldListFilter
41-
*/
4236
public function list(string $type): CustomfieldListFilter
4337
{
4438
return new CustomfieldListFilter(

src/Api/CustomfieldGroupEndpoint.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44

55
namespace Terminal42\CashctrlApi\Api;
66

7-
use Terminal42\CashctrlApi\Api\Filter\ListFilter;
87
use Terminal42\CashctrlApi\ApiClientInterface;
98
use Terminal42\CashctrlApi\Entity\CustomfieldGroup;
10-
use Terminal42\CashctrlApi\Result;
119

1210
/**
13-
* @method CustomfieldGroup|null read(int $id)
14-
* @method array<CustomfieldGroup>|ListFilter list()
15-
* @method Result create(CustomfieldGroup $entity)
16-
* @method Result update(CustomfieldGroup $entity)
17-
* @method Result delete(array $ids)
11+
* @extends AbstractEndpoint<CustomfieldGroup>
1812
*/
1913
class CustomfieldGroupEndpoint extends AbstractEndpoint
2014
{

src/Api/Filter/AccountListFilter.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Terminal42\CashctrlApi\Entity\Account;
99
use Terminal42\CashctrlApi\Exception\DomainException;
1010

11+
/**
12+
* @extends ListFilter<Account>
13+
*/
1114
class AccountListFilter extends ListFilter
1215
{
1316
protected int|null $categoryId = null;
@@ -49,17 +52,17 @@ public function get(): array
4952
return parent::get();
5053
}
5154

52-
public function getExcel()
55+
public function getExcel(): string
5356
{
5457
return $this->client->get('account/list.xlsx', $this->toArray());
5558
}
5659

57-
public function getCsv()
60+
public function getCsv(): string
5861
{
5962
return $this->client->get('account/list.csv', $this->toArray());
6063
}
6164

62-
public function getPdf()
65+
public function getPdf(): string
6366
{
6467
return $this->client->get('account/list.pdf', $this->toArray());
6568
}

src/Api/Filter/CustomfieldListFilter.php

+4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
namespace Terminal42\CashctrlApi\Api\Filter;
66

77
use Terminal42\CashctrlApi\ApiClientInterface;
8+
use Terminal42\CashctrlApi\Entity\Customfield;
89

10+
/**
11+
* @extends ListFilter<Customfield>
12+
*/
913
class CustomfieldListFilter extends ListFilter
1014
{
1115
public function __construct(

src/Api/Filter/ListFilter.php

+32-9
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,37 @@
55
namespace Terminal42\CashctrlApi\Api\Filter;
66

77
use Terminal42\CashctrlApi\ApiClientInterface;
8-
use Terminal42\CashctrlApi\Entity\EntityInterface;
9-
use Terminal42\CashctrlApi\Entity\PropertiesTrait;
108

9+
/**
10+
* @template T
11+
*
12+
* @implements \IteratorAggregate<T>
13+
*/
1114
class ListFilter implements \IteratorAggregate
1215
{
13-
use PropertiesTrait;
14-
1516
public const EQUALS = 'eq';
1617

1718
public const NOT_EQUALS = 'neq';
1819

1920
protected array|null $filter = null;
2021

2122
public function __construct(
22-
private ApiClientInterface $client,
23-
private string $urlPrefix,
24-
private \Closure $createInstance,
23+
private readonly ApiClientInterface $client,
24+
private readonly string $urlPrefix,
25+
private readonly \Closure $createInstance,
2526
) {
2627
}
2728

29+
/**
30+
* @return \ArrayIterator<int, T>
31+
*/
2832
public function getIterator(): \ArrayIterator
2933
{
3034
return new \ArrayIterator($this->get());
3135
}
3236

3337
/**
34-
* @return array<EntityInterface>
38+
* @return array<T>
3539
*/
3640
public function get(): array
3741
{
@@ -40,7 +44,10 @@ public function get(): array
4044
return array_map($this->createInstance, $result->data());
4145
}
4246

43-
public function filter(string $property, $value, string|null $comparison = null): self
47+
/**
48+
* @return ListFilter<T>
49+
*/
50+
public function filter(string $property, float|string $value, string|null $comparison = null): self
4451
{
4552
if (null === $this->filter) {
4653
$this->filter = [];
@@ -56,4 +63,20 @@ public function filter(string $property, $value, string|null $comparison = null)
5663

5764
return $this;
5865
}
66+
67+
/**
68+
* @return array{
69+
* filter: array<array{
70+
* field: string,
71+
* value: float|string,
72+
* comparison?: string
73+
* }>
74+
* }
75+
*/
76+
public function toArray(): array
77+
{
78+
return [
79+
'filter' => $this->filter,
80+
];
81+
}
5982
}

src/Api/Filter/OrderBookentryListFilter.php

+4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
namespace Terminal42\CashctrlApi\Api\Filter;
66

77
use Terminal42\CashctrlApi\ApiClientInterface;
8+
use Terminal42\CashctrlApi\Entity\OrderBookentry;
89

10+
/**
11+
* @extends ListFilter<OrderBookentry>
12+
*/
913
class OrderBookentryListFilter extends ListFilter
1014
{
1115
public function __construct(

src/Api/Filter/OrderListFilter.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
use Terminal42\CashctrlApi\Entity\Order;
99
use Terminal42\CashctrlApi\Exception\DomainException;
1010

11+
/**
12+
* @extends ListFilter<Order>
13+
*/
1114
class OrderListFilter extends ListFilter
1215
{
1316
public const TYPE_SALES = 'SALES';
@@ -49,9 +52,6 @@ public function __construct(ApiClientInterface $client, string $urlPrefix, \Clos
4952
$this->client = $client;
5053
}
5154

52-
/**
53-
* @return array<Order>
54-
*/
5555
public function get(): array
5656
{
5757
if (null !== $this->columns) {
@@ -61,22 +61,22 @@ public function get(): array
6161
return parent::get();
6262
}
6363

64-
public function getExcel()
64+
public function getExcel(): string
6565
{
6666
return $this->client->get('order/list.xlsx', $this->toArray());
6767
}
6868

69-
public function getCsv()
69+
public function getCsv(): string
7070
{
7171
return $this->client->get('order/list.csv', $this->toArray());
7272
}
7373

74-
public function getPdf()
74+
public function getPdf(): string
7575
{
7676
return $this->client->get('order/list.pdf', $this->toArray());
7777
}
7878

79-
public function getVCard()
79+
public function getVCard(): string
8080
{
8181
return $this->client->get('order/list.vcf', $this->toArray());
8282
}

0 commit comments

Comments
 (0)