Skip to content

Commit 339d65f

Browse files
Merge pull request #66 from matomo-org/fix-warnings-m5
Fixes Uncaught TypeError on getDevice due to default being empty
2 parents 3f3acac + 45c4dfa commit 339d65f

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

CachedEntry.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ public function __construct(string $userAgent, $clientHints, array $values)
2828
$clientHints = $clientHints ? ClientHints::factory($clientHints) : null;
2929
parent::__construct($userAgent, $clientHints);
3030

31-
$this->bot = $values['bot'] ?? '';
31+
$this->bot = $values['bot'] ?? null;
3232
$this->brand = $values['brand'] ?? '';
33-
$this->client = $values['client'] ?? '';
34-
$this->device = $values['device'] ?? '';
33+
$this->client = $values['client'] ?? null;
34+
$this->device = $values['device'] ?? null;
3535
$this->model = $values['model'] ?? '';
36-
$this->os = $values['os'] ?? '';
36+
$this->os = $values['os'] ?? null;
3737

3838
// Or cached entries only use the useragents, so if we have some client hints provided,
3939
// We use some special parsers, which use the cached user agent result and parses it again using client hints

tests/Integration/CachedEntryTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,49 @@ public function testConstruct()
115115
$instance = new CachedEntry($userAgent, $clientHints, $values);
116116
$this->assertIsObject($instance);
117117
$this->assertInstanceOf(CachedEntry::class, $instance);
118-
$this->assertSame('', $instance->getBot());
118+
$this->assertSame(null, $instance->getBot());
119119
$this->assertSame($values['brand'], $instance->getBrandName());
120120
$this->assertSame($expectedClient, $instance->getClient());
121121
$this->assertSame($values['device'], $instance->getDevice());
122122
$this->assertSame($values['model'], $instance->getModel());
123123
$this->assertSame($expectedOs, $instance->getOs());
124124
}
125125

126+
public function testConstructDefault()
127+
{
128+
$userAgent = "unknown";
129+
130+
$clientHints = [
131+
'HTTP_SEC_CH_UA_PLATFORM' => '"Windows"',
132+
'HTTP_SEC_CH_UA' => '" Not A;Brand";v="99", "Chromium";v="95", "Microsoft Edge";v="95"',
133+
'HTTP_SEC_CH_UA_MOBILE' => "?0",
134+
'HTTP_SEC_CH_UA_FULL_VERSION' => '"98.0.0.1"',
135+
'HTTP_SEC_CH_UA_PLATFORM_VERSION' => '"14.0.0"',
136+
'HTTP_SEC_CH_UA_ARCH' => "x86",
137+
'HTTP_SEC_CH_UA_BITNESS' => "64",
138+
'HTTP_SEC_CH_UA_MODEL' => ""
139+
];
140+
141+
$expectedOs = [
142+
'name' => 'Windows',
143+
'short_name' => 'WIN',
144+
'version' => '11',
145+
'platform' => 'x64',
146+
'family' => 'Windows',
147+
];
148+
149+
150+
$instance = new CachedEntry($userAgent, $clientHints, []);
151+
$this->assertIsObject($instance);
152+
$this->assertInstanceOf(CachedEntry::class, $instance);
153+
$this->assertSame(null, $instance->getBot());
154+
$this->assertSame('', $instance->getBrandName());
155+
$this->assertSame(null, $instance->getClient());
156+
$this->assertSame(null, $instance->getDevice());
157+
$this->assertSame('', $instance->getModel());
158+
$this->assertSame($expectedOs, $instance->getOs());
159+
}
160+
126161
public function testGetNumCacheFiles_noneCached()
127162
{
128163
$this->assertEquals(0, CachedEntry::getNumEntriesInCacheDir());

0 commit comments

Comments
 (0)