Skip to content

Commit 77d6e0f

Browse files
authored
Merge pull request #1966 from FriendsOfSymfony/phpfeatures
Apply PHP 8 features
2 parents eded996 + 51de203 commit 77d6e0f

File tree

1 file changed

+12
-31
lines changed

1 file changed

+12
-31
lines changed

src/Elastica/Client.php

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -48,29 +48,25 @@ public function __construct(array|string $config = [], array $forbiddenCodes = [
4848
*
4949
* @var array<string, Index>
5050
*/
51-
private $indexCache = [];
51+
private array $indexCache = [];
5252

5353
/**
5454
* Stores created index template to avoid recreation.
5555
*
5656
* @var array<string, IndexTemplate>
5757
*/
58-
private $indexTemplateCache = [];
58+
private array $indexTemplateCache = [];
5959

6060
/**
6161
* Symfony's debugging Stopwatch.
62-
*
63-
* @var Stopwatch|null
6462
*/
65-
private $stopwatch;
63+
private ?Stopwatch $stopwatch = null;
6664

6765
private ?EventDispatcherInterface $dispatcher = null;
6866

6967
public function sendRequest(RequestInterface $request): Elasticsearch
7068
{
71-
if ($this->stopwatch) {
72-
$this->stopwatch->start('es_request', 'fos_elastica');
73-
}
69+
$this->stopwatch?->start('es_request', 'fos_elastica');
7470

7571
$path = \ltrim($request->getUri()->getPath(), '/'); // to have the same result as in the 6.0
7672
$method = $request->getMethod();
@@ -82,26 +78,20 @@ public function sendRequest(RequestInterface $request): Elasticsearch
8278
$query = [];
8379
\parse_str($request->getUri()->getQuery(), $query);
8480

85-
if (null !== $this->dispatcher) {
86-
$this->dispatcher->dispatch(new PreElasticaRequestEvent($path, $method, $data, $query, $request->getHeaderLine('Content-Type')));
87-
}
81+
$this->dispatcher?->dispatch(new PreElasticaRequestEvent($path, $method, $data, $query, $request->getHeaderLine('Content-Type')));
8882

8983
$start = \microtime(true);
9084
try {
9185
$elasticResponse = parent::sendRequest($request);
9286
$response = $this->toElasticaResponse($elasticResponse);
9387

94-
if (null !== $this->dispatcher) {
95-
$this->dispatcher->dispatch(new PostElasticaRequestEvent($request, $response));
96-
}
88+
$this->dispatcher?->dispatch(new PostElasticaRequestEvent($request, $response));
9789
} catch (ClientResponseException $responseException) {
9890
$this->logQuery($path, $method, $data, $query, 0.0, 0, 0);
9991

10092
$response = $responseException->getResponse();
10193

102-
if (null !== $this->dispatcher) {
103-
$this->dispatcher->dispatch(new ElasticaRequestExceptionEvent($request, $responseException));
104-
}
94+
$this->dispatcher?->dispatch(new ElasticaRequestExceptionEvent($request, $responseException));
10595

10696
if (\in_array($response->getStatusCode(), $this->forbiddenCodes, true)) {
10797
$body = (string) $response->getBody();
@@ -113,9 +103,7 @@ public function sendRequest(RequestInterface $request): Elasticsearch
113103
} catch (ElasticsearchException $e) {
114104
$this->logQuery($path, $method, $data, $query, 0.0, 0, 0);
115105

116-
if (null !== $this->dispatcher) {
117-
$this->dispatcher->dispatch(new ElasticaRequestExceptionEvent($request, $e));
118-
}
106+
$this->dispatcher?->dispatch(new ElasticaRequestExceptionEvent($request, $e));
119107

120108
throw $e;
121109
}
@@ -129,26 +117,19 @@ public function sendRequest(RequestInterface $request): Elasticsearch
129117
$this->logQuery($path, $method, $data, $query, $end - $start, 0, 0);
130118
}
131119

132-
if ($this->stopwatch) {
133-
$this->stopwatch->stop('es_request');
134-
}
120+
$this->stopwatch?->stop('es_request');
135121

136122
return $elasticResponse;
137123
}
138124

139125
public function getIndex(string $name): Index
140126
{
141-
// TODO PHP >= 7.4 ??=
142-
return $this->indexCache[$name] ?? ($this->indexCache[$name] = new Index($this, $name));
127+
return $this->indexCache[$name] ??= new Index($this, $name);
143128
}
144129

145-
/**
146-
* @param string $name
147-
*/
148-
public function getIndexTemplate($name): IndexTemplate
130+
public function getIndexTemplate(string $name): IndexTemplate
149131
{
150-
// TODO PHP >= 7.4 ??=
151-
return $this->indexTemplateCache[$name] ?? ($this->indexTemplateCache[$name] = new IndexTemplate($this, $name));
132+
return $this->indexTemplateCache[$name] ??= new IndexTemplate($this, $name);
152133
}
153134

154135
/**

0 commit comments

Comments
 (0)