@@ -22,50 +22,48 @@ class SentryComponent extends CApplicationComponent
22
22
* @var string Sentry DSN.
23
23
* @see https://github.com/getsentry/raven-php#configuration
24
24
*/
25
- public $ dsn ;
25
+ public string $ dsn = '' ;
26
26
27
27
/**
28
- * @var array Raven_Client options.
28
+ * @var mixed[] Raven_Client options.
29
29
* @see https://github.com/getsentry/raven-php#configuration
30
30
*/
31
- public $ options = array () ;
31
+ public array $ options = [] ;
32
32
33
33
/**
34
34
* Publish, register and configure Raven-JS.
35
35
* @see https://raven-js.readthedocs.org/
36
- * @var bool
37
36
*/
38
- public $ useRavenJs = false ;
37
+ public bool $ useRavenJs = false ;
39
38
40
39
/**
41
40
* Raven-JS configuration options.
42
- * @var array
41
+ * @var mixed[]
43
42
* @see https://raven-js.readthedocs.org/en/latest/config/index.html#optional-settings
44
43
*/
45
- public $ ravenJsOptions = array () ;
44
+ public array $ ravenJsOptions = [] ;
46
45
47
46
/**
48
47
* Raven-JS plugins.
49
- * @var array
48
+ * @var mixed[]
50
49
* @see https://raven-js.readthedocs.org/en/latest/plugins/index.html
51
50
*/
52
- public $ ravenJsPlugins = array () ;
51
+ public array $ ravenJsPlugins = [] ;
53
52
54
53
/**
55
54
* Initialize Raven_ErrorHandler.
56
- * @var bool
57
55
*/
58
- public $ useRavenErrorHandler = false ;
56
+ public bool $ useRavenErrorHandler = false ;
59
57
60
58
/**
61
- * @var ClientInterface instance.
59
+ * @var ClientInterface|null instance.
62
60
*/
63
- protected $ raven ;
61
+ protected ? ClientInterface $ raven = null ;
64
62
65
63
/**
66
64
* @inheritdoc
67
65
*/
68
- public function init ()
66
+ public function init (): void
69
67
{
70
68
parent ::init ();
71
69
if ($ this ->useRavenJs ) {
@@ -78,39 +76,38 @@ public function init()
78
76
79
77
/**
80
78
* Get Raven_Client instance.
81
- * @return ClientInterface
82
79
*/
83
- public function getRaven ()
80
+ public function getRaven (): ClientInterface
84
81
{
85
82
if (!isset ($ this ->raven )) {
86
83
$ this ->registerRaven ();
87
84
}
88
85
86
+ /** @var ClientInterface */
89
87
return $ this ->raven ;
90
88
}
91
89
92
90
/**
93
91
* Register and configure Raven js.
94
- * @param array|null $options If null, then will be used $this->ravenJsOptions.
95
- * @param array|null $plugins If null, then will be used $this->ravenJsPlugins.
96
- * @param array|null $context If null, then will be used $this->getUserContext().
97
- * @return bool
92
+ * @param mixed[]|null $options If null, then will be used $this->ravenJsOptions.
93
+ * @param mixed[]|null $plugins If null, then will be used $this->ravenJsPlugins.
94
+ * @param mixed[]|null $context If null, then will be used $this->getUserContext().
98
95
* @throws CException
99
96
*/
100
- public function registerRavenJs (array $ options = null , array $ plugins = null , array $ context = null )
97
+ public function registerRavenJs (? array $ options = null , ? array $ plugins = null , ? array $ context = null ): bool
101
98
{
102
- /** @var CAssetManager $assetManager */
99
+ /** @var CAssetManager|null $assetManager */
103
100
$ assetManager = $ this ->getComponent ('assetManager ' );
104
- /** @var CClientScript $clientScript */
101
+ /** @var CClientScript|null $clientScript */
105
102
$ clientScript = $ this ->getComponent ('clientScript ' );
106
103
107
104
if (!$ assetManager || !$ clientScript ) {
108
105
return false ;
109
106
}
110
107
111
- $ jsOptions = $ options !== null ? $ options : $ this ->ravenJsOptions ;
112
- $ jsPlugins = $ plugins !== null ? $ plugins : $ this ->ravenJsPlugins ;
113
- $ jsContext = $ context !== null ? $ context : $ this ->getUserContext ();
108
+ $ jsOptions = $ options ?? $ this ->ravenJsOptions ;
109
+ $ jsPlugins = $ plugins ?? $ this ->ravenJsPlugins ;
110
+ $ jsContext = $ context ?? $ this ->getUserContext ();
114
111
115
112
$ assetUrl = $ assetManager ->publish (__DIR__ . '/assets ' );
116
113
$ clientScript
@@ -132,42 +129,43 @@ public function registerRavenJs(array $options = null, array $plugins = null, ar
132
129
foreach ($ jsPlugins as $ plugin ) {
133
130
$ clientScript ->registerScriptFile ($ assetUrl . '/plugins/ ' . $ plugin . '.js ' , CClientScript::POS_HEAD );
134
131
}
132
+
133
+ return true ;
135
134
}
136
135
137
136
/**
138
137
* Initialize raven client.
139
138
*/
140
- protected function registerRaven ()
139
+ protected function registerRaven (): void
141
140
{
142
141
$ this ->raven = ClientBuilder::create ($ this ->options )->getClient ();
143
142
SentrySdk::getCurrentHub ()->bindClient ($ this ->raven );
144
143
145
144
$ userContext = $ this ->getUserContext ();
146
145
SentrySdk::getCurrentHub ()->configureScope (function (Scope $ scope ) use ($ userContext ) {
147
146
if ($ userContext ) {
148
- $ scope ->setUser ($ userContext, true );
147
+ $ scope ->setUser ($ userContext );
149
148
}
150
149
});
151
150
}
152
151
153
152
/**
154
153
* Return Dsn without security token.
155
- * @return string
156
154
*/
157
- protected function getJsDsn ()
155
+ protected function getJsDsn (): string
158
156
{
159
- return preg_replace ('#:\w+@# ' , '@ ' , $ this ->dsn );
157
+ return ( string ) preg_replace ('#:\w+@# ' , '@ ' , $ this ->dsn );
160
158
}
161
159
162
160
/**
163
161
* Get get context (id, name).
164
- * @return array|null
162
+ * @return array{id: mixed, name: string} |null
165
163
*/
166
- protected function getUserContext ()
164
+ protected function getUserContext (): ? array
167
165
{
168
- /** @var CWebUser $user */
166
+ /** @var CWebUser|null $user */
169
167
$ user = $ this ->getComponent ('user ' );
170
- if ($ user && ! $ user ->isGuest ) {
168
+ if ($ user && (! property_exists ( $ user, ' isGuest ' ) || ! $ user ->isGuest ) ) {
171
169
return array (
172
170
'id ' => $ user ->getId (),
173
171
'name ' => strtoupper ($ user ->getName ()),
@@ -178,16 +176,19 @@ protected function getUserContext()
178
176
179
177
/**
180
178
* Get Yii component if exists and available.
181
- * @param string $component
182
- * @return IApplicationComponent|null
183
179
*/
184
- protected function getComponent ($ component )
180
+ protected function getComponent (string $ component ): ? IApplicationComponent
185
181
{
186
- if (!Yii::app () instanceof CWebApplication) {
182
+ $ app = Yii::app ();
183
+
184
+ if (!$ app instanceof CWebApplication) {
187
185
return null ;
188
186
}
189
187
190
- if ($ instance = Yii::app ()->getComponent ($ component )) {
188
+ /** @var IApplicationComponent|null $instance */
189
+ $ instance = $ app ->getComponent ($ component );
190
+
191
+ if ($ instance !== null ) {
191
192
return $ instance ;
192
193
}
193
194
@@ -196,19 +197,17 @@ protected function getComponent($component)
196
197
197
198
/**
198
199
* Register Raven Error Handlers for exceptions and errors.
199
- * @return bool
200
200
*/
201
- protected function registerRavenErrorHandler ()
201
+ protected function registerRavenErrorHandler (): bool
202
202
{
203
- $ raven = $ this ->getRaven ();
204
- if ($ raven ) {
205
- ErrorHandler::registerOnceExceptionHandler ();
206
- ErrorHandler::registerOnceErrorHandler ();
207
- ErrorHandler::registerOnceFatalErrorHandler ();
208
-
209
- return true ;
203
+ if (!isset ($ this ->raven )) {
204
+ $ this ->registerRaven ();
210
205
}
211
206
212
- return false ;
207
+ ErrorHandler::registerOnceExceptionHandler ();
208
+ ErrorHandler::registerOnceErrorHandler ();
209
+ ErrorHandler::registerOnceFatalErrorHandler ();
210
+
211
+ return true ;
213
212
}
214
213
}
0 commit comments