Skip to content

Commit a503bf0

Browse files
mhujerpolyfractal
authored andcommitted
ClientBuilder: setLogger() and setTracer() only accept \Psr\Log\LoggerInterface (#673)
This is a more verbose approach than directly adding typehints, but it won't break the projects which may be extending ClientBuilder.
1 parent 19638e5 commit a503bf0

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

phpstan.neon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ parameters:
77
# because of \Elasticsearch\Tests\RegisteredNamespaceTest
88
- '#Call to an undefined method Elasticsearch\\Client::foo\(\)#'
99
- '#Call to an undefined method Elasticsearch\\Client::bar\(\)#'
10+
11+
# because of \Elasticsearch\Tests\ClientBuilderTest
12+
- '#expects Psr\\Log\\LoggerInterface, Elasticsearch\\Tests\\ClientBuilder\\DummyLogger given.$#'

src/Elasticsearch/ClientBuilder.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ public function setHandler($handler)
307307
*/
308308
public function setLogger($logger)
309309
{
310+
if (!$logger instanceof LoggerInterface) {
311+
throw new InvalidArgumentException('$logger must implement \Psr\Log\LoggerInterface!');
312+
}
313+
310314
$this->logger = $logger;
311315

312316
return $this;
@@ -318,6 +322,10 @@ public function setLogger($logger)
318322
*/
319323
public function setTracer($tracer)
320324
{
325+
if (!$tracer instanceof LoggerInterface) {
326+
throw new InvalidArgumentException('$tracer must implement \Psr\Log\LoggerInterface!');
327+
}
328+
321329
$this->tracer = $tracer;
322330

323331
return $this;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
declare(strict_types = 1);
3+
4+
namespace Elasticsearch\Tests\ClientBuilder;
5+
6+
class DummyLogger
7+
{
8+
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Elasticsearch\Tests;
6+
7+
use Elasticsearch\ClientBuilder;
8+
use Elasticsearch\Common\Exceptions\InvalidArgumentException;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class ClientBuilderTest extends TestCase
12+
{
13+
14+
public function testClientBuilderThrowsExceptionForIncorrectLoggerClass()
15+
{
16+
$this->expectException(InvalidArgumentException::class);
17+
$this->expectExceptionMessage('$logger must implement \Psr\Log\LoggerInterface!');
18+
19+
ClientBuilder::create()->setLogger(new \Elasticsearch\Tests\ClientBuilder\DummyLogger());
20+
}
21+
22+
public function testClientBuilderThrowsExceptionForIncorrectTracerClass()
23+
{
24+
$this->expectException(InvalidArgumentException::class);
25+
$this->expectExceptionMessage('$tracer must implement \Psr\Log\LoggerInterface!');
26+
27+
ClientBuilder::create()->setTracer(new \Elasticsearch\Tests\ClientBuilder\DummyLogger());
28+
}
29+
}

0 commit comments

Comments
 (0)