Skip to content

Commit 9ef7ec4

Browse files
committed
fix: resolve PHPStan level 8 errors for command argument/option types
- Replace (string) casts with is_string() checks in command classes - Use is_numeric() for integer options like limit and id - Update tests to pass string values for --book option (matches CLI behavior) - Fixes GitHub CI PHPStan failures for array|bool|string|null types
1 parent 2e92a8c commit 9ef7ec4

File tree

5 files changed

+27
-22
lines changed

5 files changed

+27
-22
lines changed

src/Commands/BookStackExportCommand.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ class BookStackExportCommand extends Command
2222

2323
public function handle(BookStackSync $bookstack): int
2424
{
25-
$type = (string) $this->argument('type');
26-
$id = (int) $this->argument('id');
27-
$formatStr = (string) $this->option('format');
25+
$typeArg = $this->argument('type');
26+
$type = is_string($typeArg) ? $typeArg : '';
27+
$idArg = $this->argument('id');
28+
$id = is_numeric($idArg) ? (int) $idArg : 0;
29+
$formatOption = $this->option('format');
30+
$formatStr = is_string($formatOption) ? $formatOption : 'markdown';
2831
$outputOption = $this->option('output');
2932
$output = is_string($outputOption) ? $outputOption : null;
3033

src/Commands/BookStackPullCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class BookStackPullCommand extends Command
2222
public function handle(BookStackSync $bookstack): int
2323
{
2424
$bookOption = $this->option('book');
25-
$bookId = $bookOption !== null && $bookOption !== false ? (string) $bookOption : config('bookstack-sync.defaults.book_id');
25+
$bookId = is_string($bookOption) ? $bookOption : config('bookstack-sync.defaults.book_id');
2626
$pathOption = $this->option('path');
27-
$path = $pathOption !== null && $pathOption !== false && ! is_array($pathOption) ? (string) $pathOption : (string) config('bookstack-sync.markdown.source_path', '');
28-
$dryRun = (bool) $this->option('dry-run');
27+
$path = is_string($pathOption) ? $pathOption : (is_string(config('bookstack-sync.markdown.source_path')) ? config('bookstack-sync.markdown.source_path') : '');
28+
$dryRun = $this->option('dry-run') === true;
2929

3030
if (empty($bookId)) {
3131
$this->error('No book ID specified. Use --book option or set BOOKSTACK_BOOK_ID.');

src/Commands/BookStackPushCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ class BookStackPushCommand extends Command
2222
public function handle(BookStackSync $bookstack): int
2323
{
2424
$pathArg = $this->argument('path');
25-
$path = $pathArg !== null && $pathArg !== false && ! is_array($pathArg) ? (string) $pathArg : (string) config('bookstack-sync.markdown.source_path', '');
25+
$path = is_string($pathArg) ? $pathArg : (is_string(config('bookstack-sync.markdown.source_path')) ? config('bookstack-sync.markdown.source_path') : '');
2626
$bookOption = $this->option('book');
27-
$bookId = $bookOption !== null && $bookOption !== false ? (string) $bookOption : config('bookstack-sync.defaults.book_id');
28-
$dryRun = (bool) $this->option('dry-run');
27+
$bookId = is_string($bookOption) ? $bookOption : config('bookstack-sync.defaults.book_id');
28+
$dryRun = $this->option('dry-run') === true;
2929

3030
if (empty($path)) {
3131
$this->error('No path specified. Provide a path argument or set BOOKSTACK_MARKDOWN_PATH.');

src/Commands/BookStackSearchCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ class BookStackSearchCommand extends Command
1818

1919
public function handle(BookStackSync $bookstack): int
2020
{
21-
$query = (string) $this->argument('query');
22-
$limit = (int) $this->option('limit');
21+
$queryArg = $this->argument('query');
22+
$query = is_string($queryArg) ? $queryArg : '';
23+
$limitOption = $this->option('limit');
24+
$limit = is_numeric($limitOption) ? (int) $limitOption : 20;
2325

2426
$this->info("Searching for: \"{$query}\"");
2527
$this->newLine();

tests/Feature/CommandsTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
->with($this->tempDir, 1)
290290
->andReturn(['created' => 1, 'updated' => 0, 'deleted' => 0, 'skipped' => 0, 'errors' => []]);
291291

292-
$this->artisan('bookstack:push', ['path' => $this->tempDir, '--book' => 1, '--force' => true])
292+
$this->artisan('bookstack:push', ['path' => $this->tempDir, '--book' => '1', '--force' => true])
293293
->expectsOutputToContain('Sync completed')
294294
->assertSuccessful();
295295
});
@@ -309,7 +309,7 @@
309309
$this->bookstack->shouldReceive('pushToBook')
310310
->andReturn(['created' => 1, 'updated' => 0, 'deleted' => 0, 'skipped' => 0, 'errors' => []]);
311311

312-
$this->artisan('bookstack:push', ['path' => $this->tempDir, '--book' => 1, '--force' => true, '--dry-run' => true])
312+
$this->artisan('bookstack:push', ['path' => $this->tempDir, '--book' => '1', '--force' => true, '--dry-run' => true])
313313
->expectsOutputToContain('DRY RUN')
314314
->assertSuccessful();
315315
});
@@ -325,7 +325,7 @@
325325
$this->bookstack->shouldReceive('pushToBook')
326326
->andReturn(['created' => 0, 'updated' => 0, 'deleted' => 0, 'skipped' => 0, 'errors' => ['Error 1', 'Error 2']]);
327327

328-
$this->artisan('bookstack:push', ['path' => $this->tempDir, '--book' => 1, '--force' => true])
328+
$this->artisan('bookstack:push', ['path' => $this->tempDir, '--book' => '1', '--force' => true])
329329
->expectsOutputToContain('Errors')
330330
->assertFailed();
331331
});
@@ -334,7 +334,7 @@
334334
$this->bookstack->shouldReceive('book')
335335
->andThrow(BookStackException::notFound('book', 999));
336336

337-
$this->artisan('bookstack:push', ['path' => $this->tempDir, '--book' => 999, '--force' => true])
337+
$this->artisan('bookstack:push', ['path' => $this->tempDir, '--book' => '999', '--force' => true])
338338
->expectsOutputToContain('Push failed')
339339
->assertFailed();
340340
});
@@ -343,7 +343,7 @@
343343
$this->bookstack->shouldReceive('book')
344344
->andReturn(new BookDTO(id: 1, name: 'Test Book', slug: 'test-book'));
345345

346-
$this->artisan('bookstack:push', ['path' => $this->tempDir, '--book' => 1])
346+
$this->artisan('bookstack:push', ['path' => $this->tempDir, '--book' => '1'])
347347
->expectsConfirmation('Do you want to proceed?', 'no')
348348
->expectsOutputToContain('cancelled')
349349
->assertSuccessful();
@@ -368,7 +368,7 @@
368368
it('fails without path', function () {
369369
config()->set('bookstack-sync.markdown.source_path', null);
370370

371-
$this->artisan('bookstack:pull', ['--book' => 1])
371+
$this->artisan('bookstack:pull', ['--book' => '1'])
372372
->expectsOutputToContain('No path specified')
373373
->assertFailed();
374374
});
@@ -386,7 +386,7 @@
386386
->with(1, $this->tempDir)
387387
->andReturn(['created' => 2, 'updated' => 0, 'skipped' => 0, 'errors' => []]);
388388

389-
$this->artisan('bookstack:pull', ['--book' => 1, '--path' => $this->tempDir, '--force' => true])
389+
$this->artisan('bookstack:pull', ['--book' => '1', '--path' => $this->tempDir, '--force' => true])
390390
->expectsOutputToContain('Pull completed')
391391
->assertSuccessful();
392392
});
@@ -404,7 +404,7 @@
404404
$this->bookstack->shouldReceive('pullFromBook')
405405
->andReturn(['created' => 1, 'updated' => 0, 'skipped' => 0, 'errors' => []]);
406406

407-
$this->artisan('bookstack:pull', ['--book' => 1, '--path' => $this->tempDir, '--force' => true, '--dry-run' => true])
407+
$this->artisan('bookstack:pull', ['--book' => '1', '--path' => $this->tempDir, '--force' => true, '--dry-run' => true])
408408
->expectsOutputToContain('DRY RUN')
409409
->assertSuccessful();
410410
});
@@ -420,7 +420,7 @@
420420
$this->bookstack->shouldReceive('pullFromBook')
421421
->andReturn(['created' => 0, 'updated' => 0, 'skipped' => 0, 'errors' => ['Pull error']]);
422422

423-
$this->artisan('bookstack:pull', ['--book' => 1, '--path' => $this->tempDir, '--force' => true])
423+
$this->artisan('bookstack:pull', ['--book' => '1', '--path' => $this->tempDir, '--force' => true])
424424
->expectsOutputToContain('Errors')
425425
->assertFailed();
426426
});
@@ -429,7 +429,7 @@
429429
$this->bookstack->shouldReceive('book')
430430
->andThrow(BookStackException::notFound('book', 999));
431431

432-
$this->artisan('bookstack:pull', ['--book' => 999, '--path' => $this->tempDir, '--force' => true])
432+
$this->artisan('bookstack:pull', ['--book' => '999', '--path' => $this->tempDir, '--force' => true])
433433
->expectsOutputToContain('Pull failed')
434434
->assertFailed();
435435
});
@@ -438,7 +438,7 @@
438438
$this->bookstack->shouldReceive('book')
439439
->andReturn(new BookDTO(id: 1, name: 'Test Book', slug: 'test-book'));
440440

441-
$this->artisan('bookstack:pull', ['--book' => 1, '--path' => $this->tempDir])
441+
$this->artisan('bookstack:pull', ['--book' => '1', '--path' => $this->tempDir])
442442
->expectsConfirmation('Do you want to proceed?', 'no')
443443
->expectsOutputToContain('cancelled')
444444
->assertSuccessful();

0 commit comments

Comments
 (0)