44
55namespace Sentry \State ;
66
7- use Psr \Log \NullLogger ;
87use Sentry \Attachment \Attachment ;
98use Sentry \Breadcrumb ;
109use Sentry \CheckIn ;
1514use Sentry \EventId ;
1615use Sentry \Integration \IntegrationInterface ;
1716use Sentry \MonitorConfig ;
17+ use Sentry \NoOpClient ;
1818use Sentry \Severity ;
1919use Sentry \Tracing \SamplingContext ;
2020use Sentry \Tracing \Span ;
@@ -39,18 +39,18 @@ class Hub implements HubInterface
3939 /**
4040 * Hub constructor.
4141 *
42- * @param ClientInterface|null $client The client bound to the hub
43- * @param Scope|null $scope The scope bound to the hub
42+ * @param ClientInterface $client The client bound to the hub
43+ * @param Scope|null $scope The scope bound to the hub
4444 */
45- public function __construct (? ClientInterface $ client = null , ?Scope $ scope = null )
45+ public function __construct (ClientInterface $ client , ?Scope $ scope = null )
4646 {
4747 $ this ->stack [] = new Layer ($ client , $ scope ?? new Scope ());
4848 }
4949
5050 /**
5151 * {@inheritdoc}
5252 */
53- public function getClient (): ? ClientInterface
53+ public function getClient (): ClientInterface
5454 {
5555 return $ this ->getStackTop ()->getClient ();
5656 }
@@ -123,55 +123,31 @@ public function bindClient(ClientInterface $client): void
123123 */
124124 public function captureMessage (string $ message , ?Severity $ level = null , ?EventHint $ hint = null ): ?EventId
125125 {
126- $ client = $ this ->getClient ();
127-
128- if ($ client !== null ) {
129- return $ this ->lastEventId = $ client ->captureMessage ($ message , $ level , $ this ->getScope (), $ hint );
130- }
131-
132- return null ;
126+ return $ this ->lastEventId = $ this ->getClient ()->captureMessage ($ message , $ level , $ this ->getScope (), $ hint );
133127 }
134128
135129 /**
136130 * {@inheritdoc}
137131 */
138132 public function captureException (\Throwable $ exception , ?EventHint $ hint = null ): ?EventId
139133 {
140- $ client = $ this ->getClient ();
141-
142- if ($ client !== null ) {
143- return $ this ->lastEventId = $ client ->captureException ($ exception , $ this ->getScope (), $ hint );
144- }
145-
146- return null ;
134+ return $ this ->lastEventId = $ this ->getClient ()->captureException ($ exception , $ this ->getScope (), $ hint );
147135 }
148136
149137 /**
150138 * {@inheritdoc}
151139 */
152140 public function captureEvent (Event $ event , ?EventHint $ hint = null ): ?EventId
153141 {
154- $ client = $ this ->getClient ();
155-
156- if ($ client !== null ) {
157- return $ this ->lastEventId = $ client ->captureEvent ($ event , $ hint , $ this ->getScope ());
158- }
159-
160- return null ;
142+ return $ this ->lastEventId = $ this ->getClient ()->captureEvent ($ event , $ hint , $ this ->getScope ());
161143 }
162144
163145 /**
164146 * {@inheritdoc}
165147 */
166148 public function captureLastError (?EventHint $ hint = null ): ?EventId
167149 {
168- $ client = $ this ->getClient ();
169-
170- if ($ client !== null ) {
171- return $ this ->lastEventId = $ client ->captureLastError ($ this ->getScope (), $ hint );
172- }
173-
174- return null ;
150+ return $ this ->lastEventId = $ this ->getClient ()->captureLastError ($ this ->getScope (), $ hint );
175151 }
176152
177153 /**
@@ -183,7 +159,7 @@ public function captureCheckIn(string $slug, CheckInStatus $status, $duration =
183159 {
184160 $ client = $ this ->getClient ();
185161
186- if ($ client === null ) {
162+ if ($ client instanceof NoOpClient ) {
187163 return null ;
188164 }
189165
@@ -211,7 +187,8 @@ public function addBreadcrumb(Breadcrumb $breadcrumb): bool
211187 {
212188 $ client = $ this ->getClient ();
213189
214- if ($ client === null ) {
190+ // No point in storing breadcrumbs if the client will never send them
191+ if ($ client instanceof NoOpClient) {
215192 return false ;
216193 }
217194
@@ -234,9 +211,8 @@ public function addBreadcrumb(Breadcrumb $breadcrumb): bool
234211
235212 public function addAttachment (Attachment $ attachment ): bool
236213 {
237- $ client = $ this ->getClient ();
238-
239- if ($ client === null ) {
214+ // No point in storing attachments if the client will never send them
215+ if ($ this ->getClient () instanceof NoOpClient) {
240216 return false ;
241217 }
242218
@@ -250,13 +226,7 @@ public function addAttachment(Attachment $attachment): bool
250226 */
251227 public function getIntegration (string $ className ): ?IntegrationInterface
252228 {
253- $ client = $ this ->getClient ();
254-
255- if ($ client !== null ) {
256- return $ client ->getIntegration ($ className );
257- }
258-
259- return null ;
229+ return $ this ->getClient ()->getIntegration ($ className );
260230 }
261231
262232 /**
@@ -267,11 +237,10 @@ public function getIntegration(string $className): ?IntegrationInterface
267237 public function startTransaction (TransactionContext $ context , array $ customSamplingContext = []): Transaction
268238 {
269239 $ transaction = new Transaction ($ context , $ this );
270- $ client = $ this ->getClient ();
271- $ options = $ client !== null ? $ client ->getOptions () : null ;
272- $ logger = $ options !== null ? $ options ->getLoggerOrNullLogger () : new NullLogger ();
240+ $ options = $ this ->getClient ()->getOptions ();
241+ $ logger = $ options ->getLoggerOrNullLogger ();
273242
274- if ($ options === null || !$ options ->isTracingEnabled ()) {
243+ if (!$ options ->isTracingEnabled ()) {
275244 $ transaction ->setSampled (false );
276245
277246 $ logger ->warning (\sprintf ('Transaction [%s] was started but tracing is not enabled. ' , (string ) $ transaction ->getTraceId ()), ['context ' => $ context ]);
0 commit comments