Skip to content

Commit 1d3928b

Browse files
committed
Code: add types
1 parent f300969 commit 1d3928b

File tree

3 files changed

+58
-76
lines changed

3 files changed

+58
-76
lines changed

src/Client.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,27 @@
22

33
namespace Contributte\Elastica;
44

5+
use Elastica\Client as ElasticaClient;
56
use Elastica\Request;
67
use Elastica\Response;
78
use Nette\SmartObject;
89
use Throwable;
910

10-
class Client extends \Elastica\Client
11+
class Client extends ElasticaClient
1112
{
1213

1314
use SmartObject;
1415

1516
/** @var callable[] */
16-
public $onSuccess = [];
17+
public array $onSuccess = [];
1718

1819
/** @var callable[] */
19-
public $onFailure = [];
20+
public array $onFailure = [];
2021

2122
/**
22-
* @param mixed[]|mixed $data
23-
* @param mixed[] $query
24-
* @throws Throwable
23+
* {@inheritdoc}
2524
*/
26-
public function request(string $path, string $method = Request::GET, $data = [], array $query = [], string $contentType = Request::DEFAULT_CONTENT_TYPE): Response
25+
public function request(string $path, string $method = Request::GET, $data = [], array $query = [], string $contentType = Request::DEFAULT_CONTENT_TYPE): Response
2726
{
2827
$start = microtime(true);
2928

@@ -34,6 +33,7 @@ public function request(string $path, string $method = Request::GET, $data = [],
3433
return $response;
3534
} catch (Throwable $e) {
3635
$this->onFailure($this, $this->_lastRequest, $e, microtime(true) - $start);
36+
3737
throw $e;
3838
}
3939
}

src/DI/ElasticaExtension.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Contributte\Elastica\Diagnostics\Panel;
77
use Nette\DI\CompilerExtension;
88
use Nette\PhpGenerator\ClassType;
9-
use Nette\PhpGenerator\PhpLiteral;
9+
use Nette\PhpGenerator\Literal;
1010
use Nette\Schema\Expect;
1111
use Nette\Schema\Schema;
1212
use stdClass;
@@ -54,7 +54,6 @@ public function getConfigSchema(): Schema
5454
]);
5555
}
5656

57-
5857
public function loadConfiguration(): void
5958
{
6059
$builder = $this->getContainerBuilder();
@@ -73,7 +72,7 @@ public function loadConfiguration(): void
7372
public function afterCompile(ClassType $class): void
7473
{
7574
$initialize = $class->getMethod('initialize');
76-
$initialize->addBody('?::getBlueScreen()->addPanel(?);', [new PhpLiteral(Debugger::class), Panel::class . '::renderException']);
75+
$initialize->addBody('?::getBlueScreen()->addPanel(?);', [new Literal(Debugger::class), Panel::class . '::renderException']);
7776
}
7877

7978
}

src/Diagnostics/Panel.php

Lines changed: 49 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,56 @@
77
use Elastica\Exception\ResponseException;
88
use Elastica\Request;
99
use Elastica\Response;
10-
use Exception;
11-
use Nette;
10+
use Nette\Http\Url;
1211
use Nette\Utils\Html;
1312
use Nette\Utils\Json;
13+
use Nette\Utils\JsonException;
1414
use Throwable;
1515
use Tracy\Debugger;
1616
use Tracy\Dumper;
1717
use Tracy\IBarPanel;
1818

19-
/**
20-
* @codeCoverageIgnore
21-
*/
2219
class Panel implements IBarPanel
2320
{
2421

25-
use Nette\SmartObject;
22+
public float $totalTime = 0;
2623

27-
/** @var float */
28-
public $totalTime = 0;
29-
30-
/** @var int */
31-
public $queriesCount = 0;
24+
public int $queriesCount = 0;
3225

3326
/** @var mixed[] */
34-
public $queries = [];
27+
public array $queries = [];
28+
29+
private Client $client;
30+
31+
/**
32+
* @return array<string, string>|NULL
33+
*/
34+
public static function renderException(Throwable|null $e = null): ?array
35+
{
36+
if (!$e instanceof ExceptionInterface) {
37+
return null;
38+
}
39+
40+
$panel = null;
41+
42+
if ($e instanceof ResponseException) {
43+
$panel .= '<h3>Request</h3>';
44+
$panel .= Dumper::toHtml($e->getRequest());
45+
46+
$panel .= '<h3>Response</h3>';
47+
$panel .= Dumper::toHtml($e->getResponse());
48+
49+
} elseif ($e instanceof \Elastica\Exception\Bulk\ResponseException) {
50+
$panel .= '<h3>Failures</h3>';
51+
$panel .= Dumper::toHtml($e->getFailures());
52+
53+
}
3554

36-
/** @var Client */
37-
private $client;
55+
return $panel ? [
56+
'tab' => 'ElasticSearch',
57+
'panel' => $panel,
58+
] : null;
59+
}
3860

3961
public function getTab(): string
4062
{
@@ -52,7 +74,6 @@ public function getTab(): string
5274
return $tab->addHtml($title)->toHtml();
5375
}
5476

55-
5677
public function getPanel(): ?string
5778
{
5879
if (!$this->queries) {
@@ -72,21 +93,19 @@ public function getPanel(): ?string
7293

7394
try {
7495
return !is_array($data) ? Json::decode($data, Json::FORCE_ARRAY) : $data;
75-
} catch (Nette\Utils\JsonException $e) {
96+
} catch (JsonException $e) {
7697
try {
7798
/** @phpstan-var mixed $data */
78-
return array_map(function ($row) {
79-
return Json::decode((string) $row, Json::FORCE_ARRAY);
80-
}, is_string($data) ? explode("\n", trim($data)) : []);
81-
} catch (Nette\Utils\JsonException $e) {
99+
return array_map(fn ($row) => Json::decode((string) $row, Json::FORCE_ARRAY), is_string($data) ? explode("\n", trim($data)) : []);
100+
} catch (JsonException $e) {
82101
return $data;
83102
}
84103
}
85104
};
86105

87106
$processedQueries = [];
88107
$allQueries = $this->queries;
89-
$totalTime = $this->totalTime;
108+
$totalTime = $this->totalTime; // @phpcs:ignore
90109

91110
foreach ($allQueries as $authority => $requests) {
92111
/** @var Request[] $item */
@@ -131,15 +150,13 @@ public function getPanel(): ?string
131150
return $result === false ? null : $result;
132151
}
133152

134-
135153
public function success(Client $client, Request $request, Response $response, float $time): void
136154
{
137155
$this->queries[$this->requestAuthority($response)][] = [$request, $response, $time];
138156
$this->totalTime += $time;
139157
$this->queriesCount++;
140158
}
141159

142-
143160
public function failure(Client $client, Request $request, Throwable $e, float $time): void
144161
{
145162
/** @var Response $response */
@@ -150,62 +167,28 @@ public function failure(Client $client, Request $request, Throwable $e, float $t
150167
$this->queriesCount++;
151168
}
152169

170+
public function register(Client $client): void
171+
{
172+
$this->client = $client;
173+
$client->onSuccess[] = [$this, 'success'];
174+
$client->onFailure[] = [$this, 'failure'];
153175

176+
Debugger::getBar()->addPanel($this);
177+
}
154178

155179
protected function requestAuthority(?Response $response = null): string
156180
{
157181
if ($response) {
158182
$info = $response->getTransferInfo();
159-
$url = new Nette\Http\Url($info['url']);
183+
$url = new Url($info['url']);
160184

161185
} else {
162186
/** @var string $current */
163187
$current = key($this->queries);
164-
$url = new Nette\Http\Url($current ?: 'http://localhost:9200/');
188+
$url = new Url($current ?: 'http://localhost:9200/');
165189
}
166190

167191
return $url->hostUrl;
168192
}
169193

170-
/**
171-
* @param Exception|Throwable $e
172-
* @return array<string, string>|NULL
173-
*/
174-
public static function renderException($e = null): ?array
175-
{
176-
if (!$e instanceof ExceptionInterface) {
177-
return null;
178-
}
179-
180-
$panel = null;
181-
182-
if ($e instanceof ResponseException) {
183-
$panel .= '<h3>Request</h3>';
184-
$panel .= Dumper::toHtml($e->getRequest());
185-
186-
$panel .= '<h3>Response</h3>';
187-
$panel .= Dumper::toHtml($e->getResponse());
188-
189-
} elseif ($e instanceof \Elastica\Exception\Bulk\ResponseException) {
190-
$panel .= '<h3>Failures</h3>';
191-
$panel .= Dumper::toHtml($e->getFailures());
192-
193-
}
194-
195-
return $panel ? [
196-
'tab' => 'ElasticSearch',
197-
'panel' => $panel,
198-
] : null;
199-
}
200-
201-
202-
public function register(Client $client): void
203-
{
204-
$this->client = $client;
205-
$client->onSuccess[] = [$this, 'success'];
206-
$client->onFailure[] = [$this, 'failure'];
207-
208-
Debugger::getBar()->addPanel($this);
209-
}
210-
211194
}

0 commit comments

Comments
 (0)