Skip to content

Commit 0967f92

Browse files
committed
Remove type checks from AndroidConfig
Either static analysis or the Firebase API will report invalid types
1 parent cab1937 commit 0967f92

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

src/Firebase/Messaging/AndroidConfig.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
use Kreait\Firebase\Exception\Messaging\InvalidArgument;
99

1010
use function array_filter;
11-
use function array_key_exists;
1211
use function is_int;
13-
use function is_string;
1412
use function preg_match;
1513
use function sprintf;
1614

@@ -122,8 +120,9 @@ public static function new(): self
122120
*/
123121
public static function fromArray(array $config): self
124122
{
125-
if (array_key_exists('ttl', $config) && $config['ttl'] !== null) {
126-
$config['ttl'] = self::ensureValidTtl($config['ttl']);
123+
$ttl = $config['ttl'] ?? null;
124+
if ($ttl !== null) {
125+
$config['ttl'] = self::ensureValidTtl($ttl);
127126
}
128127

129128
return new self($config);
@@ -248,22 +247,25 @@ public function jsonSerialize(): array
248247
}
249248

250249
/**
251-
* @param int|string $value
252-
*
253250
* @throws InvalidArgument
254-
*
255251
* @return non-empty-string
256252
*/
257-
private static function ensureValidTtl($value): string
253+
private static function ensureValidTtl(int|string $value): string
258254
{
259255
$expectedPattern = '/^\d+s$/';
260256
$errorMessage = "The TTL of an AndroidConfig must be an positive integer or string matching {$expectedPattern}";
261257

262-
if (is_int($value) && $value >= 0) {
263-
return sprintf('%ds', $value);
258+
if (is_numeric($value)) {
259+
$value = (int) $value;
264260
}
265261

266-
if (!is_string($value) || $value === '') {
262+
if (is_int($value)) {
263+
$value = sprintf('%ds', $value);
264+
}
265+
266+
$value = trim($value);
267+
268+
if ($value === '') {
267269
throw new InvalidArgument($errorMessage);
268270
}
269271

tests/Unit/Messaging/AndroidConfigTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ public static function validTtlValues(): Iterator
112112

113113
public static function invalidTtlValues(): Iterator
114114
{
115-
yield 'float' => [1.2];
116115
yield 'wrong suffix' => ['1m'];
117-
yield 'not numeric' => [true];
118116
yield 'negative int' => [-1];
119117
yield 'negative string' => ['-1'];
120118
yield 'negative string with suffix' => ['-1s'];

0 commit comments

Comments
 (0)