Skip to content

Commit 3d2cb5d

Browse files
committed
test: fix tests/phpstan when laravel-debugbar not installed
1 parent 4828727 commit 3d2cb5d

File tree

5 files changed

+79
-54
lines changed

5 files changed

+79
-54
lines changed

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ parameters:
55
- src/
66
level: 9
77
treatPhpDocTypesAsCertain: false
8+
excludePaths:
9+
- src/Bag/DebugBar

src/Bag/BagServiceProvider.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,12 @@ public function register(): void
8686
]);
8787
}
8888

89-
if ($this->app->bound(LaravelDebugbar::class)) {
90-
BagCollector::init();
91-
$this->app->make(LaravelDebugbar::class)->addCollector(new BagCollector());
89+
if (class_exists(LaravelDebugbar::class)) {
90+
if ($this->app->bound(LaravelDebugbar::class)) {
91+
BagCollector::init();
92+
// @phpstan-ignore argument.type
93+
$this->app->make(LaravelDebugbar::class)->addCollector(new BagCollector());
94+
}
9295
}
9396
}
9497
}

src/Bag/DebugBar/Collectors/BagCollector.php

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,68 +5,82 @@
55
namespace Bag\DebugBar\Collectors;
66

77
use Bag\Bag;
8+
use DebugBar\DataCollector\DataCollectorInterface;
89
use DebugBar\DataCollector\MessagesCollector;
910

10-
class BagCollector extends MessagesCollector
11-
{
12-
/**
13-
* @var array<string, array{bag: Bag, location: array{file: string|null, line: int|null}|false, type: string}>
14-
*/
15-
protected static array $bags = [];
16-
17-
public static function add(Bag $bag): void
11+
if (!class_exists(MessagesCollector::class)) {
12+
class BagCollector
1813
{
19-
$location = false;
20-
$trace = collect(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS))
21-
->firstWhere(fn ($v) =>
22-
isset($v['file']) &&
23-
!str_contains($v['file'], '/vendor/') &&
24-
!\str_starts_with($v['function'], '__'));
25-
26-
$type = 'unknown';
27-
if ($trace !== null) {
28-
$location = ['file' => $trace['file'] ?? null, 'line' => $trace['line'] ?? null];
29-
$type = $trace['function'];
14+
public static function add(Bag $bag): void
15+
{
16+
// Do nothing
3017
}
3118

32-
self::$bags[$bag::class . '#' . \spl_object_id($bag)] = ['bag' => $bag, 'location' => $location, 'type' => $type];
19+
public static function init(): void
20+
{
21+
// Do nothing
22+
}
3323
}
34-
35-
/**
36-
* @return array{messages: array<mixed>, count: int}
37-
*/
38-
public function collect(): array
24+
} else {
25+
class BagCollector extends MessagesCollector implements DataCollectorInterface
3926
{
40-
$localPath = config('debugbar.local_sites_path') ?: base_path();
41-
$remoteSitesPath = config('debugbar.remote_sites_path');
42-
$remotePaths = array_filter(explode(',', is_string($remoteSitesPath) ? $remoteSitesPath : '')) ?: [base_path()];
27+
/**
28+
* @var array<string, array{bag: Bag, location: array{file: string|null, line: int|null}|false, type: string}>
29+
*/
30+
protected static array $bags = [];
31+
32+
public static function add(Bag $bag): void
33+
{
34+
$location = false;
35+
$trace = collect(\debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS))
36+
->firstWhere(fn ($v) => isset($v['file']) &&
37+
!str_contains($v['file'], '/vendor/') &&
38+
!\str_starts_with($v['function'], '__'));
4339

44-
$this->setXdebugReplacements(array_fill_keys($remotePaths, $localPath));
45-
$editor = \config('debugbar.editor');
46-
if (is_string($editor)) {
47-
$this->setEditorLinkTemplate($editor);
40+
$type = 'unknown';
41+
if ($trace !== null) {
42+
$location = ['file' => $trace['file'] ?? null, 'line' => $trace['line'] ?? null];
43+
$type = $trace['function'];
44+
}
45+
46+
self::$bags[$bag::class . '#' . \spl_object_id($bag)] = ['bag' => $bag, 'location' => $location, 'type' => $type];
4847
}
4948

5049
/**
51-
* @var array{bag: Bag, location: array{file: string, line: int|null}, type: string} $message
50+
* @return array{messages: array<mixed>, count: int}
5251
*/
53-
foreach (self::$bags as $message) {
54-
$this->addMessage($message['bag'], $message['type']);
55-
$xdebugLink = $this->getXdebugLink($message['location']['file'], $message['location']['line'] ?? null);
56-
$this->messages[\array_key_last($this->messages)]['xdebug_link'] = $xdebugLink;
57-
}
52+
public function collect(): array
53+
{
54+
$localPath = config('debugbar.local_sites_path') ?: base_path();
55+
$remoteSitesPath = config('debugbar.remote_sites_path');
56+
$remotePaths = array_filter(explode(',', is_string($remoteSitesPath) ? $remoteSitesPath : '')) ?: [base_path()];
5857

59-
// @phpstan-ignore return.type
60-
return parent::collect();
61-
}
58+
$this->setXdebugReplacements(array_fill_keys($remotePaths, $localPath));
59+
$editor = \config('debugbar.editor');
60+
if (is_string($editor)) {
61+
$this->setEditorLinkTemplate($editor);
62+
}
6263

63-
public function getName(): string
64-
{
65-
return 'bags';
66-
}
64+
/**
65+
* @var array{bag: Bag, location: array{file: string, line: int|null}, type: string} $message
66+
*/
67+
foreach (self::$bags as $message) {
68+
$this->addMessage($message['bag'], $message['type']);
69+
$xdebugLink = $this->getXdebugLink($message['location']['file'], $message['location']['line'] ?? null);
70+
$this->messages[\array_key_last($this->messages)]['xdebug_link'] = $xdebugLink;
71+
}
6772

68-
public static function init(): void
69-
{
70-
self::$bags = [];
73+
return parent::collect();
74+
}
75+
76+
public function getName(): string
77+
{
78+
return 'bags';
79+
}
80+
81+
public static function init(): void
82+
{
83+
self::$bags = [];
84+
}
7185
}
7286
}

tests/Unit/DebugBar/Collectors/BagCollectorTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
declare(strict_types=1);
44

55
use Bag\DebugBar\Collectors\BagCollector;
6+
use DebugBar\DataCollector\MessagesCollector;
67
use Illuminate\Support\Facades\Config;
78
use Tests\Fixtures\Values\TestBag;
89

9-
covers(BagCollector::class);
10-
1110
beforeEach(function () {
1211
BagCollector::init();
13-
});
12+
})->skip(!class_exists(MessagesCollector::class));
13+
14+
if (class_exists(MessagesCollector::class)) {
15+
covers(BagCollector::class);
16+
}
1417

1518
test('it collects a bag', function () {
1619
$bag = TestBag::from([

tests/Unit/Pipelines/Pipes/DebugCollectionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
use Bag\DebugBar\Collectors\BagCollector;
66
use Bag\Pipelines\Pipes\DebugCollection;
77
use Bag\Pipelines\Values\BagInput;
8+
use DebugBar\DataCollector\MessagesCollector;
89
use Illuminate\Support\Collection;
910
use Tests\Fixtures\Values\TestBag;
1011

1112
covers(DebugCollection::class);
1213

14+
beforeEach()->skip(!class_exists(MessagesCollector::class));
15+
1316
test('it collects bags', function () {
1417
$pipe = new DebugCollection();
1518
$input = new BagInput(TestBag::class, Collection::empty());

0 commit comments

Comments
 (0)