Skip to content

Commit 292819b

Browse files
committed
Cleaned up code after PHPStan code analysis
1 parent 43cef14 commit 292819b

6 files changed

Lines changed: 55 additions & 14 deletions

File tree

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"require-dev": {
1616
"php": "^8.3",
1717
"ergebnis/composer-normalize": "^2.52",
18-
"friendsofphp/php-cs-fixer": "^3.95"
18+
"friendsofphp/php-cs-fixer": "^3.95",
19+
"phpstan/phpstan": "^2.2"
1920
},
2021
"autoload": {
2122
"psr-4": {

phpstan.dist.neon

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
parameters:
2+
level: 6
3+
paths:
4+
- src
5+
6+
treatPhpDocTypesAsCertain: false
7+
8+
ignoreErrors:
9+
- message: '#Method ItkDev\\F2ApiClient\\Client\\ApiClient::[^ ]+\(\) has parameter \$[^ ]+ with no value type specified in iterable type array.#'
10+
path: src/*
11+
12+
- message: '#Method ItkDev\\F2ApiClient\\Model\\[^ ]+::jsonSerialize\(\) return type has no value type specified in iterable type array.#'
13+
path: src/*
14+
15+
- message: '#Method ItkDev\\F2ApiClient\\Command\\F2ApiClientCommand::responseToArray\(\) has parameter \$response with no value type specified in iterable type array.#'
16+
path: src/Command/F2ApiClientCommand.php

src/Client/ApiClient.php

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,35 @@
2727
/**
2828
* @phpstan-type AccessToken array{access_token: string, token_type: string, expires_in: int, refresh_token: string}
2929
* @phpstan-type ServiceIndex array<string, array{href: string, title: string}>
30-
*/
30+
* @phpstan-type OptionsInput array{
31+
* api_uri: non-empty-string,
32+
* api_username: non-empty-string,
33+
* api_secret: non-empty-string,
34+
* f2_username: non-empty-string,
35+
* cache_item_pool: CacheItemPoolInterface,
36+
* cache_item_lifetime?: int,
37+
* }
38+
* @phpstan-type Options array{
39+
* api_uri: non-empty-string,
40+
* api_username: non-empty-string,
41+
* api_secret: non-empty-string,
42+
* f2_username: non-empty-string,
43+
* cache_item_pool: CacheItemPoolInterface,
44+
* cache_item_lifetime: int,
45+
* }*/
3146
class ApiClient
3247
{
3348
use LoggerAwareTrait;
3449
use LoggerTrait;
3550

51+
/** @var Options */
3652
private readonly array $options;
53+
3754
private ?HttpClientInterface $client = null;
3855

56+
/**
57+
* @param OptionsInput $options
58+
*/
3959
public function __construct(array $options)
4060
{
4161
$resolver = new OptionsResolver();
@@ -68,7 +88,7 @@ public function getServiceIndex(): array
6888
],
6989
]);
7090

71-
/** @var ServiceIndex */
91+
/* @var ServiceIndex */
7292
return $response->toArray();
7393
});
7494
}
@@ -140,6 +160,9 @@ public function caseCreate(array $caseData): CaseFile
140160
return $this->createItemResult($response, CaseFile::class);
141161
}
142162

163+
/**
164+
* @return Atom[]
165+
*/
143166
public function matterSearch(string $searchTerms, int $count = 10): array
144167
{
145168
$query = [
@@ -366,10 +389,10 @@ protected function configureOptions(OptionsResolver $resolver): void
366389
'api_secret',
367390
'f2_username',
368391
])
369-
->setDefault('cache_item_lifetime', 86_400)
370-
->setAllowedTypes('cache_item_lifetime', 'int')
371392
->setRequired('cache_item_pool')
372-
->setAllowedTypes('cache_item_pool', CacheItemPoolInterface::class);
393+
->setAllowedTypes('cache_item_pool', CacheItemPoolInterface::class)
394+
->setDefault('cache_item_lifetime', 86_400)
395+
->setAllowedTypes('cache_item_lifetime', 'int');
373396
}
374397

375398
protected function getRequestUrl(string $rel, array $values = []): string
@@ -468,7 +491,7 @@ protected function createItemResult(ResponseInterface $response, string $class):
468491
{
469492
$item = new \SimpleXMLElement($response->getContent());
470493

471-
/** @var T */
494+
/* @var T */
472495
return $class::fromSimpleXMLElement($item);
473496
}
474497

src/Command/F2ApiClientCommand.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace ItkDev\F2ApiClient\Command;
66

77
use ItkDev\F2ApiClient\Client\ApiClient;
8+
use ItkDev\F2ApiClient\Model\AbstractItem;
89
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
910
use Symfony\Component\Console\Attribute\Argument;
1011
use Symfony\Component\Console\Attribute\AsCommand;
@@ -80,7 +81,10 @@ private function createClient(?string $cacheDirectory): ApiClient
8081
return new ApiClient($config);
8182
}
8283

83-
private function responseToArray($response): array
84+
/**
85+
* @return array<string, mixed>
86+
*/
87+
private function responseToArray(array|AbstractItem $response): array
8488
{
8589
// @mago-ignore analysis:invalid-type-cast
8690
return (array) json_decode(

src/Model/AbstractF2Item.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ abstract class AbstractF2Item extends AbstractItem implements \Stringable
1515
#[\Override]
1616
public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
1717
{
18-
parent::setFromSimpleXMLElement($sxe);
19-
2018
$this->id = (int) $sxe->Id;
2119
$this->links = Links::fromSimpleXMLElement($sxe);
2220

@@ -34,7 +32,7 @@ public function jsonSerialize(): array
3432
{
3533
return [
3634
'id' => $this->id,
37-
'links' => $this->links?->jsonSerialize(),
35+
'links' => $this->links->jsonSerialize(),
3836
];
3937
}
4038
}

src/Model/AbstractItem.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ abstract class AbstractItem implements \JsonSerializable, \Stringable
99
public static function fromSimpleXMLElement(\SimpleXMLElement $sxe): static
1010
{
1111
// @mago-ignore analysis:unsafe-instantiation
12+
// @phpstan-ignore new.static
1213
return (new static())->setFromSimpleXMLElement($sxe);
1314
}
1415

@@ -27,10 +28,8 @@ public function setFromSimpleXMLElement(\SimpleXMLElement $sxe): static
2728
protected static function listOf(string $class, \SimpleXMLElement $sxe): array
2829
{
2930
$items = [];
31+
/** @var \SimpleXMLElement $child */
3032
foreach ($sxe as $child) {
31-
if (!$child instanceof \SimpleXMLElement) {
32-
continue;
33-
}
3433
/** @var T $item */
3534
$item = $class::fromSimpleXMLElement($child);
3635
$items[] = $item;

0 commit comments

Comments
 (0)