Skip to content

Commit 1f2c753

Browse files
authored
chore: add PHPStan and Psalm and modernize code (#4)
1 parent d95135d commit 1f2c753

File tree

9 files changed

+166
-95
lines changed

9 files changed

+166
-95
lines changed

SentryComponent.php

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,50 +22,48 @@ class SentryComponent extends CApplicationComponent
2222
* @var string Sentry DSN.
2323
* @see https://github.com/getsentry/raven-php#configuration
2424
*/
25-
public $dsn;
25+
public string $dsn = '';
2626

2727
/**
28-
* @var array Raven_Client options.
28+
* @var mixed[] Raven_Client options.
2929
* @see https://github.com/getsentry/raven-php#configuration
3030
*/
31-
public $options = array();
31+
public array $options = [];
3232

3333
/**
3434
* Publish, register and configure Raven-JS.
3535
* @see https://raven-js.readthedocs.org/
36-
* @var bool
3736
*/
38-
public $useRavenJs = false;
37+
public bool $useRavenJs = false;
3938

4039
/**
4140
* Raven-JS configuration options.
42-
* @var array
41+
* @var mixed[]
4342
* @see https://raven-js.readthedocs.org/en/latest/config/index.html#optional-settings
4443
*/
45-
public $ravenJsOptions = array();
44+
public array $ravenJsOptions = [];
4645

4746
/**
4847
* Raven-JS plugins.
49-
* @var array
48+
* @var mixed[]
5049
* @see https://raven-js.readthedocs.org/en/latest/plugins/index.html
5150
*/
52-
public $ravenJsPlugins = array();
51+
public array $ravenJsPlugins = [];
5352

5453
/**
5554
* Initialize Raven_ErrorHandler.
56-
* @var bool
5755
*/
58-
public $useRavenErrorHandler = false;
56+
public bool $useRavenErrorHandler = false;
5957

6058
/**
61-
* @var ClientInterface instance.
59+
* @var ClientInterface|null instance.
6260
*/
63-
protected $raven;
61+
protected ?ClientInterface $raven = null;
6462

6563
/**
6664
* @inheritdoc
6765
*/
68-
public function init()
66+
public function init(): void
6967
{
7068
parent::init();
7169
if ($this->useRavenJs) {
@@ -78,39 +76,38 @@ public function init()
7876

7977
/**
8078
* Get Raven_Client instance.
81-
* @return ClientInterface
8279
*/
83-
public function getRaven()
80+
public function getRaven(): ClientInterface
8481
{
8582
if (!isset($this->raven)) {
8683
$this->registerRaven();
8784
}
8885

86+
/** @var ClientInterface */
8987
return $this->raven;
9088
}
9189

9290
/**
9391
* 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().
9895
* @throws CException
9996
*/
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
10198
{
102-
/** @var CAssetManager $assetManager */
99+
/** @var CAssetManager|null $assetManager */
103100
$assetManager = $this->getComponent('assetManager');
104-
/** @var CClientScript $clientScript */
101+
/** @var CClientScript|null $clientScript */
105102
$clientScript = $this->getComponent('clientScript');
106103

107104
if (!$assetManager || !$clientScript) {
108105
return false;
109106
}
110107

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();
114111

115112
$assetUrl = $assetManager->publish(__DIR__ . '/assets');
116113
$clientScript
@@ -132,42 +129,43 @@ public function registerRavenJs(array $options = null, array $plugins = null, ar
132129
foreach ($jsPlugins as $plugin) {
133130
$clientScript->registerScriptFile($assetUrl . '/plugins/' . $plugin . '.js', CClientScript::POS_HEAD);
134131
}
132+
133+
return true;
135134
}
136135

137136
/**
138137
* Initialize raven client.
139138
*/
140-
protected function registerRaven()
139+
protected function registerRaven(): void
141140
{
142141
$this->raven = ClientBuilder::create($this->options)->getClient();
143142
SentrySdk::getCurrentHub()->bindClient($this->raven);
144143

145144
$userContext = $this->getUserContext();
146145
SentrySdk::getCurrentHub()->configureScope(function (Scope $scope) use ($userContext) {
147146
if ($userContext) {
148-
$scope->setUser($userContext, true);
147+
$scope->setUser($userContext);
149148
}
150149
});
151150
}
152151

153152
/**
154153
* Return Dsn without security token.
155-
* @return string
156154
*/
157-
protected function getJsDsn()
155+
protected function getJsDsn(): string
158156
{
159-
return preg_replace('#:\w+@#', '@', $this->dsn);
157+
return (string) preg_replace('#:\w+@#', '@', $this->dsn);
160158
}
161159

162160
/**
163161
* Get get context (id, name).
164-
* @return array|null
162+
* @return array{id: mixed, name: string}|null
165163
*/
166-
protected function getUserContext()
164+
protected function getUserContext(): ?array
167165
{
168-
/** @var CWebUser $user */
166+
/** @var CWebUser|null $user */
169167
$user = $this->getComponent('user');
170-
if ($user && !$user->isGuest) {
168+
if ($user && (!property_exists($user, 'isGuest') || !$user->isGuest)) {
171169
return array(
172170
'id' => $user->getId(),
173171
'name' => strtoupper($user->getName()),
@@ -178,16 +176,19 @@ protected function getUserContext()
178176

179177
/**
180178
* Get Yii component if exists and available.
181-
* @param string $component
182-
* @return IApplicationComponent|null
183179
*/
184-
protected function getComponent($component)
180+
protected function getComponent(string $component): ?IApplicationComponent
185181
{
186-
if (!Yii::app() instanceof CWebApplication) {
182+
$app = Yii::app();
183+
184+
if (!$app instanceof CWebApplication) {
187185
return null;
188186
}
189187

190-
if ($instance = Yii::app()->getComponent($component)) {
188+
/** @var IApplicationComponent|null $instance */
189+
$instance = $app->getComponent($component);
190+
191+
if ($instance !== null) {
191192
return $instance;
192193
}
193194

@@ -196,19 +197,17 @@ protected function getComponent($component)
196197

197198
/**
198199
* Register Raven Error Handlers for exceptions and errors.
199-
* @return bool
200200
*/
201-
protected function registerRavenErrorHandler()
201+
protected function registerRavenErrorHandler(): bool
202202
{
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();
210205
}
211206

212-
return false;
207+
ErrorHandler::registerOnceExceptionHandler();
208+
ErrorHandler::registerOnceErrorHandler();
209+
ErrorHandler::registerOnceFatalErrorHandler();
210+
211+
return true;
213212
}
214213
}

SentryLogRoute.php

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@ class SentryLogRoute extends CLogRoute
1414
/**
1515
* @var string Component ID of the sentry client that should be used to send the logs
1616
*/
17-
public $sentryComponent = 'sentry';
17+
public string $sentryComponent = 'sentry';
1818

1919
/**
2020
* @see self::getStackTrace().
21-
* @var string
2221
*/
23-
public $tracePattern = '/#(?<number>\d+) (?<file>[^(]+)\((?<line>\d+)\): (?<cls>[^-]+)(->|::)(?<func>[^\(]+)/m';
22+
public string $tracePattern = '/#(?<number>\d+) (?<file>[^(]+)\((?<line>\d+)\): (?<cls>[^-]+)(->|::)(?<func>[^\(]+)/m';
2423

2524
/**
2625
* Raven_Client instance from SentryComponent->getRaven();
27-
* @var ClientInterface
2826
*/
29-
protected $raven;
27+
protected ?ClientInterface $raven = null;
3028

3129
public static function getSeverityFromLogLevel(string $level): Severity
3230
{
@@ -50,26 +48,32 @@ public static function getSeverityFromLogLevel(string $level): Severity
5048
}
5149

5250
/**
51+
* @param array{string, string, string, float} $logs
52+
*
5353
* @inheritdoc
5454
*/
55-
protected function processLogs($logs)
55+
protected function processLogs($logs): void
5656
{
57+
/** @phpstan-ignore-next-line */
5758
if (count($logs) == 0) {
5859
return;
5960
}
6061

61-
if (!$raven = $this->getRaven()) {
62+
$raven = $this->getRaven();
63+
64+
if ($raven === null) {
6265
return;
6366
}
6467

6568
SentrySdk::getCurrentHub()->withScope(function (Scope $scope) use ($raven, $logs) {
6669
foreach ($logs as $log) {
70+
/** @phpstan-ignore-next-line */
6771
[$message, $level, $category, $timestamp] = $log;
6872

69-
$title = preg_replace('#Stack trace:.+#s', '', $message); // remove stack trace from title
73+
$title = (string) preg_replace('#Stack trace:.+#s', '', $message); // remove stack trace from title
7074
// ensure %'s in messages aren't interpreted as replacement
7175
// characters by the vsprintf inside raven
72-
$title = str_replace('%', '%%', $title);
76+
$title = (string) str_replace('%', '%%', $title);
7377

7478
$scope->setExtras([
7579
'category' => $category,
@@ -85,15 +89,13 @@ protected function processLogs($logs)
8589
*
8690
* Example log string:
8791
* #22 /var/www/example.is74.ru/vendor/yiisoft/yii/framework/web/CWebApplication.php(282): CController->run('index')
88-
* @param string $log
89-
* @return array|string
92+
* @return array<array{file: string, line: string|int, function: string, class: string}>
9093
*/
91-
protected function getStackTrace($log)
94+
protected function getStackTrace(string $log): array
9295
{
9396
$stack = array();
9497
if (strpos($log, 'Stack trace:') !== false) {
9598
if (preg_match_all($this->tracePattern, $log, $m, PREG_SET_ORDER)) {
96-
$stack = array();
9799
foreach ($m as $row) {
98100
$stack[] = array(
99101
'file' => $row['file'],
@@ -110,18 +112,18 @@ protected function getStackTrace($log)
110112
}
111113

112114
/**
113-
* Return Raven_Client instance or false if error.
115+
* Return Raven_Client instance or null if error.
114116
*
115-
* @return ClientInterface|bool
117+
* @return ClientInterface|null
116118
*/
117-
protected function getRaven()
119+
protected function getRaven(): ?ClientInterface
118120
{
119121
if (!isset($this->raven)) {
120-
$this->raven = false;
122+
$this->raven = null;
121123
if (!Yii::app()->hasComponent($this->sentryComponent)) {
122124
Yii::log("'$this->sentryComponent' does not exist", CLogger::LEVEL_TRACE, __CLASS__);
123125
} else {
124-
/** @var SentryComponent $sentry */
126+
/** @var SentryComponent|null $sentry */
125127
$sentry = Yii::app()->{$this->sentryComponent};
126128
if (!$sentry || !$sentry->getIsInitialized()) {
127129
Yii::log("'$this->sentryComponent' not initialised", CLogger::LEVEL_TRACE, __CLASS__);

0 commit comments

Comments
 (0)