Skip to content

Commit 53224bd

Browse files
authored
Merge pull request #147 from rollbar/updated/types-and-comments
Improved missing typing and comments
2 parents f7cc49c + 88b53b2 commit 53224bd

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

src/MonologHandler.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,53 @@
22

33
namespace Rollbar\Laravel;
44

5+
use Illuminate\Contracts\Foundation\Application;
56
use Monolog\Handler\RollbarHandler;
67
use Monolog\LogRecord;
78

89
class MonologHandler extends RollbarHandler
910
{
10-
protected $app;
11+
protected Application $app;
1112

1213
/**
13-
* @param $app
14+
* Sets the Laravel application, so it can be used to get the current user and session data later.
15+
*
16+
* @param Application $app The Laravel application.
17+
*
1418
* @return void
1519
*/
16-
public function setApp($app)
20+
public function setApp(Application $app): void
1721
{
1822
$this->app = $app;
1923
}
2024

2125
/**
22-
* @param LogRecord $record
26+
* Custom write method to add the Laravel context to the log record for Rollbar.
27+
*
28+
* @param LogRecord $record The Monolog log record.
29+
*
2330
* @return void
2431
*/
2532
protected function write(LogRecord $record): void
2633
{
27-
parent::write(new LogRecord($record->datetime,
28-
$record->channel,
29-
$record->level,
30-
$record->message,
31-
$this->addContext($record->context),
32-
$record->extra,
33-
$record->formatted));
34+
parent::write(
35+
new LogRecord(
36+
$record->datetime,
37+
$record->channel,
38+
$record->level,
39+
$record->message,
40+
$this->addContext($record->context),
41+
$record->extra,
42+
$record->formatted
43+
)
44+
);
3445
}
3546

3647
/**
37-
* @param array $context
48+
* Adds the Laravel context to the log record for Rollbar. This includes person and session data.
49+
*
50+
* @param array $context The {@see LogRecord::context} array.
51+
*
3852
* @return array
3953
*/
4054
protected function addContext(array $context = []): array

src/RollbarServiceProvider.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Rollbar\RollbarLogger;
55
use Illuminate\Support\Arr;
66
use InvalidArgumentException;
7-
use Rollbar\Laravel\MonologHandler;
7+
use Illuminate\Contracts\Foundation\Application;
88
use Illuminate\Support\Facades\Config;
99
use Illuminate\Support\ServiceProvider;
1010

@@ -13,14 +13,14 @@ class RollbarServiceProvider extends ServiceProvider
1313
/**
1414
* Register the service provider.
1515
*/
16-
public function register()
16+
public function register(): void
1717
{
1818
// Don't register rollbar if it is not configured.
1919
if ($this->stop() === true) {
2020
return;
2121
}
2222

23-
$this->app->singleton(RollbarLogger::class, function ($app) {
23+
$this->app->singleton(RollbarLogger::class, function (Application $app) {
2424

2525
$defaults = [
2626
'environment' => $app->environment(),
@@ -54,7 +54,7 @@ public function register()
5454
return Rollbar::logger();
5555
});
5656

57-
$this->app->singleton(MonologHandler::class, function ($app) {
57+
$this->app->singleton(MonologHandler::class, function (Application $app) {
5858

5959
$level = static::config('level', 'debug');
6060

@@ -66,7 +66,7 @@ public function register()
6666
}
6767

6868
/**
69-
* Check if we should prevent the service from registering
69+
* Check if we should prevent the service from registering.
7070
*
7171
* @return boolean
7272
*/
@@ -82,15 +82,16 @@ public function stop() : bool
8282
}
8383

8484
/**
85-
* Return a rollbar logging config
85+
* Return a rollbar logging config.
86+
*
87+
* @param string $key The config key to lookup.
88+
* @param mixed $default The default value to return if the config is not found.
8689
*
87-
* @param array|string $key
88-
* @param mixed $default
8990
* @return mixed
9091
*/
91-
protected static function config($key = '', $default = null)
92+
protected static function config(string $key = '', mixed $default = null): mixed
9293
{
93-
$envKey = 'ROLLBAR_'.strtoupper($key);
94+
$envKey = 'ROLLBAR_' . strtoupper($key);
9495

9596
if ($envKey === 'ROLLBAR_ACCESS_TOKEN') {
9697
$envKey = 'ROLLBAR_TOKEN';

tests/RollbarTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@
1111

1212
class RollbarTest extends \Orchestra\Testbench\TestCase
1313
{
14+
private string $access_token = 'B42nHP04s06ov18Dv8X7VI4nVUs6w04X';
15+
1416
protected function setUp(): void
1517
{
16-
$this->access_token = 'B42nHP04s06ov18Dv8X7VI4nVUs6w04X';
1718
putenv('ROLLBAR_TOKEN=' . $this->access_token);
1819

1920
parent::setUp();

0 commit comments

Comments
 (0)