Skip to content

Commit f0a43ed

Browse files
committed
refactor: share string normalization helpers
1 parent 85d6691 commit f0a43ed

4 files changed

Lines changed: 27 additions & 28 deletions

File tree

lib/Client.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ public function __construct(
104104
bool $loadFeatureFlags = true,
105105
) {
106106
$this->apiKey = trim($apiKey);
107-
$this->personalAPIKey = self::normalizeOptionalString($personalAPIKey);
107+
$this->personalAPIKey = StringNormalizer::normalizeOptional($personalAPIKey);
108108
$this->options = $options;
109109
$this->debug = $options["debug"] ?? false;
110-
$this->options['host'] = HostNormalizer::normalize($options['host'] ?? null);
110+
$this->options['host'] = StringNormalizer::normalizeHost($options['host'] ?? null);
111111
if ($this->apiKey === '') {
112112
error_log('[PostHog][Client] apiKey is empty after trimming whitespace; check your project API key');
113113
}
@@ -147,16 +147,6 @@ public function __destruct()
147147
$this->consumer->__destruct();
148148
}
149149

150-
private static function normalizeOptionalString(?string $value): ?string
151-
{
152-
if ($value === null) {
153-
return null;
154-
}
155-
156-
$normalized = trim($value);
157-
return $normalized === '' ? null : $normalized;
158-
}
159-
160150
/**
161151
* Captures a user action
162152
*

lib/HostNormalizer.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

lib/PostHog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function init(
4242

4343
// Infer ssl from the host protocol if the user hasn't explicitly set it
4444
if ($rawHost !== null && !array_key_exists("ssl", $options)) {
45-
$normalizedHost = HostNormalizer::normalize($rawHost);
45+
$normalizedHost = StringNormalizer::normalizeHost($rawHost);
4646
if (str_starts_with($normalizedHost, "http://")) {
4747
$options["ssl"] = false;
4848
} elseif (str_starts_with($normalizedHost, "https://")) {
@@ -374,7 +374,7 @@ public static function getClient(): Client
374374

375375
private static function cleanHost(?string $host): string
376376
{
377-
$host = HostNormalizer::normalize($host);
377+
$host = StringNormalizer::normalizeHost($host);
378378

379379
// remove protocol
380380
if (substr($host, 0, 8) === "https://") {

lib/StringNormalizer.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace PostHog;
4+
5+
class StringNormalizer
6+
{
7+
public const DEFAULT_HOST = 'us.i.posthog.com';
8+
9+
public static function normalizeOptional(?string $value): ?string
10+
{
11+
if ($value === null) {
12+
return null;
13+
}
14+
15+
$normalized = trim($value);
16+
return $normalized === '' ? null : $normalized;
17+
}
18+
19+
public static function normalizeHost(?string $host): string
20+
{
21+
return self::normalizeOptional($host) ?? self::DEFAULT_HOST;
22+
}
23+
}

0 commit comments

Comments
 (0)