Skip to content

Commit 9a1a39c

Browse files
committed
fix: resolve all PHPStan level 8 errors
- Add PHPDoc annotations for array types in BookStackClient - Add array type annotations to BookStackClientInterface - Fix exception classes: use self instead of static in final classes - Add proper array type annotations to all DTOs - Fix SyncService return type with explicit array shape - Fix BookStackExportCommand parameter types - Update Facade PHPDoc with precise return types
1 parent 79caa12 commit 9a1a39c

15 files changed

Lines changed: 150 additions & 54 deletions

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ includes:
22
- phpstan-baseline.neon
33

44
parameters:
5-
level: 5
5+
level: 8
66
paths:
77
- src
88
- config

src/Api/BookStackClient.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public function getShelf(int $id): ShelfDTO
5858
return ShelfDTO::fromArray($response);
5959
}
6060

61+
/**
62+
* @param array<int> $bookIds
63+
*/
6164
public function createShelf(string $name, ?string $description = null, array $bookIds = []): ShelfDTO
6265
{
6366
$data = ['name' => $name];
@@ -73,6 +76,9 @@ public function createShelf(string $name, ?string $description = null, array $bo
7376
return ShelfDTO::fromArray($response);
7477
}
7578

79+
/**
80+
* @param array<int>|null $bookIds
81+
*/
7682
public function updateShelf(int $id, ?string $name = null, ?string $description = null, ?array $bookIds = null): ShelfDTO
7783
{
7884
$data = array_filter([
@@ -294,6 +300,10 @@ public function search(string $query, int $count = 100, int $offset = 0): array
294300
}
295301

296302
// HTTP methods
303+
/**
304+
* @param array<string, mixed> $query
305+
* @return array<string, mixed>
306+
*/
297307
private function get(string $endpoint, array $query = []): array
298308
{
299309
try {
@@ -320,6 +330,10 @@ private function getRaw(string $endpoint): string
320330
}
321331
}
322332

333+
/**
334+
* @param array<string, mixed> $data
335+
* @return array<string, mixed>
336+
*/
323337
private function post(string $endpoint, array $data = []): array
324338
{
325339
try {
@@ -333,6 +347,10 @@ private function post(string $endpoint, array $data = []): array
333347
}
334348
}
335349

350+
/**
351+
* @param array<string, mixed> $data
352+
* @return array<string, mixed>
353+
*/
336354
private function put(string $endpoint, array $data = []): array
337355
{
338356
try {
@@ -357,6 +375,9 @@ private function delete(string $endpoint): void
357375
}
358376
}
359377

378+
/**
379+
* @return array<string, mixed>
380+
*/
360381
private function parseResponse(ResponseInterface $response): array
361382
{
362383
$body = (string) $response->getBody();

src/BookStackSync.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ public function shelf(int $id): ShelfDTO
8686
return $this->client->getShelf($id);
8787
}
8888

89+
/**
90+
* @param array<int> $bookIds
91+
*/
8992
public function createShelf(string $name, ?string $description = null, array $bookIds = []): ShelfDTO
9093
{
9194
return $this->client->createShelf($name, $description, $bookIds);

src/Commands/BookStackExportCommand.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class BookStackExportCommand extends Command
2222

2323
public function handle(BookStackSync $bookstack): int
2424
{
25-
$type = $this->argument('type');
25+
$type = (string) $this->argument('type');
2626
$id = (int) $this->argument('id');
27-
$formatStr = $this->option('format');
27+
$formatStr = (string) $this->option('format');
2828
$output = $this->option('output');
2929

3030
// Validate type
31-
if (! in_array($type, ['book', 'chapter', 'page'])) {
31+
if (! in_array($type, ['book', 'chapter', 'page'], true)) {
3232
$this->error("Invalid type '{$type}'. Must be: book, chapter, or page");
3333

3434
return self::FAILURE;
@@ -48,8 +48,7 @@ public function handle(BookStackSync $bookstack): int
4848
$content = match ($type) {
4949
'book' => $bookstack->exportBook($id, $format),
5050
'chapter' => $bookstack->client()->exportChapter($id, $format),
51-
'page' => $bookstack->exportPage($id, $format),
52-
default => throw new \InvalidArgumentException("Invalid type: {$type}"),
51+
default => $bookstack->exportPage($id, $format),
5352
};
5453

5554
if ($output) {

src/Contracts/BookStackClientInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ public function listShelves(int $count = 100, int $offset = 0): array;
2121

2222
public function getShelf(int $id): ShelfDTO;
2323

24+
/**
25+
* @param array<int> $bookIds
26+
*/
2427
public function createShelf(string $name, ?string $description = null, array $bookIds = []): ShelfDTO;
2528

29+
/**
30+
* @param array<int>|null $bookIds
31+
*/
2632
public function updateShelf(int $id, ?string $name = null, ?string $description = null, ?array $bookIds = null): ShelfDTO;
2733

2834
public function deleteShelf(int $id): bool;

src/DTOs/BaseDTO.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public function __construct(
1616
public readonly ?string $updatedAt = null,
1717
) {}
1818

19+
/**
20+
* @return array<string, mixed>
21+
*/
1922
public function toArray(): array
2023
{
2124
return array_filter(
@@ -24,6 +27,9 @@ public function toArray(): array
2427
);
2528
}
2629

30+
/**
31+
* @return array<string, mixed>
32+
*/
2733
public function jsonSerialize(): array
2834
{
2935
return $this->toArray();
@@ -50,5 +56,8 @@ protected static function extractUserId(mixed $value): ?int
5056
return null;
5157
}
5258

59+
/**
60+
* @param array<string, mixed> $data
61+
*/
5362
abstract public static function fromArray(array $data): static;
5463
}

src/DTOs/BookDTO.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public function __construct(
2626
parent::__construct($id, $name, $slug, $createdAt, $updatedAt);
2727
}
2828

29+
/**
30+
* @param array<string, mixed> $data
31+
*/
2932
public static function fromArray(array $data): static
3033
{
3134
$contents = [];

src/DTOs/ChapterDTO.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public function __construct(
2828
parent::__construct($id, $name, $slug, $createdAt, $updatedAt);
2929
}
3030

31+
/**
32+
* @param array<string, mixed> $data
33+
*/
3134
public static function fromArray(array $data): static
3235
{
3336
$pages = [];

src/DTOs/PageDTO.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public function __construct(
3131
parent::__construct($id, $name, $slug, $createdAt, $updatedAt);
3232
}
3333

34+
/**
35+
* @param array<string, mixed> $data
36+
*/
3437
public static function fromArray(array $data): static
3538
{
3639
return new self(

src/DTOs/SearchResultDTO.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
final class SearchResultDTO extends BaseDTO
1010
{
11+
/**
12+
* @param array<string, mixed>|null $tags
13+
*/
1114
public function __construct(
1215
?int $id = null,
1316
?string $name = null,
@@ -24,6 +27,9 @@ public function __construct(
2427
parent::__construct($id, $name, $slug, $createdAt, $updatedAt);
2528
}
2629

30+
/**
31+
* @param array<string, mixed> $data
32+
*/
2733
public static function fromArray(array $data): static
2834
{
2935
$type = null;

0 commit comments

Comments
 (0)