Skip to content

Commit

Permalink
phpstan fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zozlak committed Oct 13, 2022
1 parent d4597c8 commit 85463d9
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/acdhOeaw/UriNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public function resolve(string $uri): Request {
}
foreach ($this->mappings as $rule) {
$count = 0;
$url = preg_replace("`" . $rule->match . "`", $rule->resolve, $uri, 1, $count);
$url = (string) preg_replace("`" . $rule->match . "`", $rule->resolve, $uri, 1, $count);
if ($count === 0 || empty($rule->resolve)) {
continue;
}
Expand Down Expand Up @@ -272,7 +272,7 @@ public function fetch(string $uri): Resource {
}
foreach ($this->mappings as $rule) {
$count = 0;
$url = preg_replace("`" . $rule->match . "`", $rule->resolve, $uri, 1, $count);
$url = (string) preg_replace("`" . $rule->match . "`", $rule->resolve, $uri, 1, $count);
if ($count === 0 || empty($rule->resolve)) {
continue;
}
Expand Down
17 changes: 11 additions & 6 deletions src/acdhOeaw/UriNormalizerCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ public function delete(string $key): bool {

public function deleteMultiple($keys): bool {
foreach ($keys as $i) {
unset($this->memCache[$i]);
}
if (isset($this->pdo)) {
$bindings = substr(str_repeat(', ?', count($keys)), 2);
$this->pdo->prepare("DELETE FROM cache WHERE key IN ($bindings)")->execute($keys);
$this->delete($i);
}
return true;
}
Expand All @@ -109,7 +105,7 @@ public function get(string $key, mixed $default = null): mixed {

public function getMultiple($keys, mixed $default = null): iterable {
foreach ($keys as $key) {
yield $this->get($key, $default);
yield $key => $this->get($key, $default);
}
}

Expand All @@ -131,6 +127,9 @@ public function set(string $key, mixed $value,
$this->memCache[$key] = $value;

if (isset($this->pdo)) {
if (is_int($ttl)) {
$ttl = new DateInterval("P{$ttl}S");
}
$ttl ??= new DateInterval("P1D");
$expires = (new DateTimeImmutable())->add($ttl)->format('Y-m-d h:i:s');
$query = $this->pdo->prepare("INSERT OR REPLACE INTO cache (key, value, expires) VALUES (?, ?, ?)");
Expand All @@ -140,6 +139,12 @@ public function set(string $key, mixed $value,
return true;
}

/**
*
* @param iterable<string, mixed> $values
* @param null|int|DateInterval $ttl
* @return bool
*/
public function setMultiple(iterable $values,
null | int | DateInterval $ttl = null): bool {
foreach ($values as $key => $value) {
Expand Down
50 changes: 25 additions & 25 deletions tests/UriNormalizerCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public function testMemoryOnly(): void {
$this->assertNull($cache1->get('foo'));
$this->assertEquals('bar', $cache1->get('foo', 'bar'));
$res = iterator_to_array($cache1->getMultiple(['foo', 'bar'], 'baz'));
$this->assertEquals(['baz', 'baz'], $res);
$this->assertEquals(['foo' => 'baz', 'bar' => 'baz'], $res);

$this->assertTrue($cache1->set('foo', 'bar'));
$this->assertTrue($cache1->has('foo'));
$this->assertEquals('bar', $cache1->get('foo'));
$this->assertEquals('bar', $cache1->get('foo', 'baz'));
$res = iterator_to_array($cache1->getMultiple(['foo', 'bar'], 'baz'));
$this->assertEquals(['bar', 'baz'], $res);
$this->assertEquals(['foo' => 'bar', 'bar' => 'baz'], $res);

$this->assertFalse($cache2->has('foo'));
$this->assertNull($cache2->get('foo'));
Expand All @@ -70,19 +70,19 @@ public function testMemoryOnly(): void {
$this->assertFalse($cache1->has('foo'));

$this->assertTrue($cache1->setMultiple(['FOO', 'BAR']));
$this->assertTrue($cache1->has(0));
$this->assertTrue($cache1->has(1));
$this->assertFalse($cache1->has(2));
$this->assertTrue($cache1->has('0'));
$this->assertTrue($cache1->has('1'));
$this->assertFalse($cache1->has('2'));

$this->assertTrue($cache1->deleteMultiple(['FOO', 'BAR']));
$this->assertTrue($cache1->has(0));
$this->assertTrue($cache1->has(1));
$this->assertFalse($cache1->has(2));
$this->assertTrue($cache1->has('0'));
$this->assertTrue($cache1->has('1'));
$this->assertFalse($cache1->has('2'));

$this->assertTrue($cache1->deleteMultiple([0, 1]));
$this->assertFalse($cache1->has(0));
$this->assertFalse($cache1->has(1));
$this->assertFalse($cache1->has(2));
$this->assertTrue($cache1->deleteMultiple(['0', '1']));
$this->assertFalse($cache1->has('0'));
$this->assertFalse($cache1->has('1'));
$this->assertFalse($cache1->has('2'));

$this->assertTrue($cache1->set('foo', 'BAR'));
$this->assertEquals('BAR', $cache1->get('foo'));
Expand All @@ -98,14 +98,14 @@ public function testSqlite(): void {
$this->assertNull($cache1->get('foo'));
$this->assertEquals('bar', $cache1->get('foo', 'bar'));
$res = iterator_to_array($cache1->getMultiple(['foo', 'bar'], 'baz'));
$this->assertEquals(['baz', 'baz'], $res);
$this->assertEquals(['foo' => 'baz', 'bar' => 'baz'], $res);

$this->assertTrue($cache1->set('foo', 'bar'));
$this->assertTrue($cache1->has('foo'));
$this->assertEquals('bar', $cache1->get('foo'));
$this->assertEquals('bar', $cache1->get('foo', 'baz'));
$res = iterator_to_array($cache1->getMultiple(['foo', 'bar'], 'baz'));
$this->assertEquals(['bar', 'baz'], $res);
$this->assertEquals(['foo' => 'bar', 'bar' => 'baz'], $res);

$this->assertTrue($cache2->has('foo'));
$this->assertEquals('bar', $cache2->get('foo'));
Expand All @@ -123,19 +123,19 @@ public function testSqlite(): void {
$this->assertFalse($cache1->has('foo'));

$this->assertTrue($cache1->setMultiple(['FOO', 'BAR']));
$this->assertTrue($cache1->has(0));
$this->assertTrue($cache1->has(1));
$this->assertFalse($cache1->has(2));
$this->assertTrue($cache1->has('0'));
$this->assertTrue($cache1->has('1'));
$this->assertFalse($cache1->has('2'));

$this->assertTrue($cache1->deleteMultiple(['FOO', 'BAR']));
$this->assertTrue($cache1->has(0));
$this->assertTrue($cache1->has(1));
$this->assertFalse($cache1->has(2));

$this->assertTrue($cache1->deleteMultiple([0, 1]));
$this->assertFalse($cache1->has(0));
$this->assertFalse($cache1->has(1));
$this->assertFalse($cache1->has(2));
$this->assertTrue($cache1->has('0'));
$this->assertTrue($cache1->has('1'));
$this->assertFalse($cache1->has('2'));

$this->assertTrue($cache1->deleteMultiple(['0', '1']));
$this->assertFalse($cache1->has('0'));
$this->assertFalse($cache1->has('1'));
$this->assertFalse($cache1->has('2'));

$this->assertTrue($cache1->set('foo', 'BAR'));
$this->assertEquals('BAR', $cache1->get('foo'));
Expand Down
4 changes: 2 additions & 2 deletions tests/UriNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ public function testResolveCache(): void {
$t1 = microtime(true);
$r2 = $n->resolve($url)->withBody($emptyBody);
$t2 = microtime(true);
$r3 = $n->resolve('https://d-nb.info/gnd/4491366-7/about/lds.ttl', true)->withBody($emptyBody);
$r3 = $n->resolve('https://d-nb.info/gnd/4491366-7/about/lds.ttl')->withBody($emptyBody);
$t3 = microtime(true);

$this->assertEquals($r1, $r2);
Expand All @@ -361,7 +361,7 @@ public function testFetchCache(): void {
$t1 = microtime(true);
$r2 = $n->fetch($url);
$t2 = microtime(true);
$r3 = $n->fetch('https://d-nb.info/gnd/4491366-7/about/lds.ttl', true);
$r3 = $n->fetch('https://d-nb.info/gnd/4491366-7/about/lds.ttl');
$t3 = microtime(true);

$this->assertEquals($r1->dump('text'), $r2->dump('text'));
Expand Down

0 comments on commit 85463d9

Please sign in to comment.