Skip to content

Commit 6e1f604

Browse files
Jean85ste93cry
authored andcommitted
Add return types to the ClientBuilder methods and make the Options class final (#728)
1 parent efbac6e commit 6e1f604

7 files changed

+95
-175
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ parameters:
44
- src
55
- tests
66
ignoreErrors:
7-
- '/Call to an undefined method Sentry\\ClientBuilder::methodThatDoesNotExists\(\)/'
7+
- '/Method Sentry\\ClientBuilder::\w+\(\) should return \$this\(Sentry\\ClientBuilderInterface\) but returns \$this\(Sentry\\ClientBuilder\)/'
88
- '/Argument of an invalid type object supplied for foreach, only iterables are supported/'
99
- '/Binary operation "\*" between array and 2 results in an error\./'
1010
- '/Method Sentry\\Serializer\\RepresentationSerializer::(representationSerialize|serializeValue)\(\) should return [\w|]+ but returns [\w|]+/'

src/ClientBuilder.php

Lines changed: 24 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -34,40 +34,6 @@
3434
* The default implementation of {@link ClientBuilderInterface}.
3535
*
3636
* @author Stefano Arlandini <[email protected]>
37-
*
38-
* @method int getSendAttempts()
39-
* @method setSendAttempts(int $attemptsCount)
40-
* @method string[] getPrefixes()
41-
* @method setPrefixes(array $prefixes)
42-
* @method float getSampleRate()
43-
* @method setSampleRate(float $sampleRate)
44-
* @method bool shouldAttachStacktrace()
45-
* @method setAttachStacktrace(bool $enable)
46-
* @method int getContextLines()
47-
* @method setContextLines(int $contextLines)
48-
* @method null|string getEnvironment()
49-
* @method setEnvironment(null|string $environment)
50-
* @method string[] getExcludedProjectPaths()
51-
* @method setExcludedProjectPaths(string[] $paths)
52-
* @method setExcludedLoggers(string[] $loggers)
53-
* @method string[] getExcludedExceptions()
54-
* @method string getProjectRoot()
55-
* @method setProjectRoot(string $path)
56-
* @method string getLogger()
57-
* @method setLogger(string $logger)
58-
* @method string getRelease()
59-
* @method setRelease(string $release)
60-
* @method string getDsn()
61-
* @method string getServerName()
62-
* @method setServerName(string $serverName)
63-
* @method string[] getTags()
64-
* @method setTags(string[] $tags)
65-
* @method bool shouldSendDefaultPii()
66-
* @method setSendDefaultPii(bool $enable)
67-
* @method bool hasDefaultIntegrations()
68-
* @method setDefaultIntegrations(bool $enable)
69-
* @method callable getBeforeSendCallback()
70-
* @method setBeforeSendCallback(callable $beforeSend)
7137
*/
7238
final class ClientBuilder implements ClientBuilderInterface
7339
{
@@ -124,11 +90,11 @@ final class ClientBuilder implements ClientBuilderInterface
12490
/**
12591
* Class constructor.
12692
*
127-
* @param array $options The client options
93+
* @param Options|null $options The client options
12894
*/
129-
public function __construct(array $options = [])
95+
public function __construct(Options $options = null)
13096
{
131-
$this->options = new Options($options);
97+
$this->options = $options ?? new Options();
13298

13399
if ($this->options->hasDefaultIntegrations()) {
134100
$this->options->setIntegrations(\array_merge([
@@ -141,15 +107,23 @@ public function __construct(array $options = [])
141107
/**
142108
* {@inheritdoc}
143109
*/
144-
public static function create(array $options = []): self
110+
public static function create(array $options = []): ClientBuilderInterface
145111
{
146-
return new static($options);
112+
return new static(new Options($options));
147113
}
148114

149115
/**
150116
* {@inheritdoc}
151117
*/
152-
public function setUriFactory(UriFactory $uriFactory): self
118+
public function getOptions(): Options
119+
{
120+
return $this->options;
121+
}
122+
123+
/**
124+
* {@inheritdoc}
125+
*/
126+
public function setUriFactory(UriFactory $uriFactory): ClientBuilderInterface
153127
{
154128
$this->uriFactory = $uriFactory;
155129

@@ -159,7 +133,7 @@ public function setUriFactory(UriFactory $uriFactory): self
159133
/**
160134
* {@inheritdoc}
161135
*/
162-
public function setMessageFactory(MessageFactory $messageFactory): self
136+
public function setMessageFactory(MessageFactory $messageFactory): ClientBuilderInterface
163137
{
164138
$this->messageFactory = $messageFactory;
165139

@@ -169,7 +143,7 @@ public function setMessageFactory(MessageFactory $messageFactory): self
169143
/**
170144
* {@inheritdoc}
171145
*/
172-
public function setTransport(TransportInterface $transport): self
146+
public function setTransport(TransportInterface $transport): ClientBuilderInterface
173147
{
174148
$this->transport = $transport;
175149

@@ -179,7 +153,7 @@ public function setTransport(TransportInterface $transport): self
179153
/**
180154
* {@inheritdoc}
181155
*/
182-
public function setHttpClient(HttpAsyncClient $httpClient): self
156+
public function setHttpClient(HttpAsyncClient $httpClient): ClientBuilderInterface
183157
{
184158
$this->httpClient = $httpClient;
185159

@@ -189,7 +163,7 @@ public function setHttpClient(HttpAsyncClient $httpClient): self
189163
/**
190164
* {@inheritdoc}
191165
*/
192-
public function addHttpClientPlugin(Plugin $plugin): self
166+
public function addHttpClientPlugin(Plugin $plugin): ClientBuilderInterface
193167
{
194168
$this->httpClientPlugins[] = $plugin;
195169

@@ -199,7 +173,7 @@ public function addHttpClientPlugin(Plugin $plugin): self
199173
/**
200174
* {@inheritdoc}
201175
*/
202-
public function removeHttpClientPlugin(string $className): self
176+
public function removeHttpClientPlugin(string $className): ClientBuilderInterface
203177
{
204178
foreach ($this->httpClientPlugins as $index => $httpClientPlugin) {
205179
if (!$httpClientPlugin instanceof $className) {
@@ -215,7 +189,7 @@ public function removeHttpClientPlugin(string $className): self
215189
/**
216190
* {@inheritdoc}
217191
*/
218-
public function setSerializer(SerializerInterface $serializer): self
192+
public function setSerializer(SerializerInterface $serializer): ClientBuilderInterface
219193
{
220194
$this->serializer = $serializer;
221195

@@ -225,7 +199,7 @@ public function setSerializer(SerializerInterface $serializer): self
225199
/**
226200
* {@inheritdoc}
227201
*/
228-
public function setRepresentationSerializer(RepresentationSerializerInterface $representationSerializer): self
202+
public function setRepresentationSerializer(RepresentationSerializerInterface $representationSerializer): ClientBuilderInterface
229203
{
230204
$this->representationSerializer = $representationSerializer;
231205

@@ -235,7 +209,7 @@ public function setRepresentationSerializer(RepresentationSerializerInterface $r
235209
/**
236210
* {@inheritdoc}
237211
*/
238-
public function setSdkIdentifier(string $sdkIdentifier): self
212+
public function setSdkIdentifier(string $sdkIdentifier): ClientBuilderInterface
239213
{
240214
$this->sdkIdentifier = $sdkIdentifier;
241215

@@ -259,7 +233,7 @@ private function getSdkVersion(): string
259233
/**
260234
* {@inheritdoc}
261235
*/
262-
public function setSdkVersion(string $sdkVersion): self
236+
public function setSdkVersion(string $sdkVersion): ClientBuilderInterface
263237
{
264238
$this->sdkVersion = $sdkVersion;
265239

@@ -273,7 +247,7 @@ public function setSdkVersion(string $sdkVersion): self
273247
*
274248
* @return $this
275249
*/
276-
public function setSdkVersionByPackageName(string $packageName): self
250+
public function setSdkVersionByPackageName(string $packageName): ClientBuilderInterface
277251
{
278252
$this->sdkVersion = PrettyVersions::getVersion($packageName)->getPrettyVersion();
279253

@@ -293,25 +267,6 @@ public function getClient(): ClientInterface
293267
return new Client($this->options, $this->transport, $this->createEventFactory());
294268
}
295269

296-
/**
297-
* This method forwards all methods calls to the options object.
298-
*
299-
* @param string $name The name of the method being called
300-
* @param array $arguments Parameters passed to the $name'ed method
301-
*
302-
* @return $this
303-
*
304-
* @throws \BadMethodCallException If the called method does not exists
305-
*/
306-
public function __call($name, $arguments)
307-
{
308-
if (!method_exists($this->options, $name)) {
309-
throw new \BadMethodCallException(sprintf('The method named "%s" does not exists.', $name));
310-
}
311-
312-
return $this->options->$name(...$arguments);
313-
}
314-
315270
/**
316271
* Creates a new instance of the HTTP client.
317272
*

src/ClientBuilderInterface.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@ interface ClientBuilderInterface
2222
/**
2323
* Creates a new instance of this builder.
2424
*
25-
* @param array $options The client options
25+
* @param array $options The client options, in naked array form
2626
*
2727
* @return static
2828
*/
29-
public static function create(array $options = []);
29+
public static function create(array $options = []): self;
30+
31+
/**
32+
* The options that will be used to create the {@see Client}.
33+
*
34+
* @return Options
35+
*/
36+
public function getOptions(): Options;
3037

3138
/**
3239
* Sets the factory to use to create URIs.
@@ -35,7 +42,7 @@ public static function create(array $options = []);
3542
*
3643
* @return $this
3744
*/
38-
public function setUriFactory(UriFactory $uriFactory);
45+
public function setUriFactory(UriFactory $uriFactory): self;
3946

4047
/**
4148
* Sets the factory to use to create PSR-7 messages.
@@ -44,7 +51,7 @@ public function setUriFactory(UriFactory $uriFactory);
4451
*
4552
* @return $this
4653
*/
47-
public function setMessageFactory(MessageFactory $messageFactory);
54+
public function setMessageFactory(MessageFactory $messageFactory): self;
4855

4956
/**
5057
* Sets the transport that will be used to send events.
@@ -53,7 +60,7 @@ public function setMessageFactory(MessageFactory $messageFactory);
5360
*
5461
* @return $this
5562
*/
56-
public function setTransport(TransportInterface $transport);
63+
public function setTransport(TransportInterface $transport): self;
5764

5865
/**
5966
* Sets the HTTP client.
@@ -62,7 +69,7 @@ public function setTransport(TransportInterface $transport);
6269
*
6370
* @return $this
6471
*/
65-
public function setHttpClient(HttpAsyncClient $httpClient);
72+
public function setHttpClient(HttpAsyncClient $httpClient): self;
6673

6774
/**
6875
* Adds a new HTTP client plugin to the end of the plugins chain.
@@ -71,7 +78,7 @@ public function setHttpClient(HttpAsyncClient $httpClient);
7178
*
7279
* @return $this
7380
*/
74-
public function addHttpClientPlugin(Plugin $plugin);
81+
public function addHttpClientPlugin(Plugin $plugin): self;
7582

7683
/**
7784
* Removes a HTTP client plugin by its fully qualified class name (FQCN).
@@ -80,7 +87,7 @@ public function addHttpClientPlugin(Plugin $plugin);
8087
*
8188
* @return $this
8289
*/
83-
public function removeHttpClientPlugin(string $className);
90+
public function removeHttpClientPlugin(string $className): self;
8491

8592
/**
8693
* Gets the instance of the client built using the configured options.
@@ -96,7 +103,7 @@ public function getClient(): ClientInterface;
96103
*
97104
* @return $this
98105
*/
99-
public function setSerializer(SerializerInterface $serializer);
106+
public function setSerializer(SerializerInterface $serializer): self;
100107

101108
/**
102109
* Sets a representation serializer instance to be injected as a dependency of the client.
@@ -107,7 +114,7 @@ public function setSerializer(SerializerInterface $serializer);
107114
*
108115
* @return $this
109116
*/
110-
public function setRepresentationSerializer(RepresentationSerializerInterface $representationSerializer);
117+
public function setRepresentationSerializer(RepresentationSerializerInterface $representationSerializer): self;
111118

112119
/**
113120
* Sets the SDK identifier to be passed onto {@see Event} and HTTP User-Agent header.
@@ -118,7 +125,7 @@ public function setRepresentationSerializer(RepresentationSerializerInterface $r
118125
*
119126
* @internal
120127
*/
121-
public function setSdkIdentifier(string $sdkIdentifier);
128+
public function setSdkIdentifier(string $sdkIdentifier): self;
122129

123130
/**
124131
* Sets the SDK version to be passed onto {@see Event} and HTTP User-Agent header.
@@ -129,5 +136,5 @@ public function setSdkIdentifier(string $sdkIdentifier);
129136
*
130137
* @internal
131138
*/
132-
public function setSdkVersion(string $sdkVersion);
139+
public function setSdkVersion(string $sdkVersion): self;
133140
}

src/Options.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*
1414
* @author Stefano Arlandini <[email protected]>
1515
*/
16-
class Options
16+
final class Options
1717
{
1818
/**
1919
* The default maximum number of breadcrumbs that will be sent with an event.

src/Sdk.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
*/
1414
function init(array $options = []): void
1515
{
16-
Hub::setCurrent(new Hub(ClientBuilder::create($options)->getClient()));
16+
$client = ClientBuilder::create($options)->getClient();
17+
Hub::setCurrent(new Hub($client));
1718
}
1819

1920
/**

0 commit comments

Comments
 (0)