Skip to content

Commit ea428d6

Browse files
authored
Add types for the Monolog integration (#1493)
1 parent 75ffa2e commit ea428d6

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

psalm.baseline.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@
114114
<code><![CDATA[$this->app]]></code>
115115
</UndefinedInterfaceMethod>
116116
</file>
117+
<file src="src/Integration/Monolog/CloudWatch/src/CloudWatchLogsHandler.php">
118+
<InvalidOperand>
119+
<code><![CDATA[$entry['datetime']->format('U.u')]]></code>
120+
</InvalidOperand>
121+
</file>
117122
<file src="src/Integration/Symfony/Bundle/src/DependencyInjection/Configuration.php">
118123
<PossiblyNullReference>
119124
<code>scalarNode</code>

src/Integration/Monolog/CloudWatch/src/CloudWatchLogsHandler.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Monolog\Handler\AbstractProcessingHandler;
1212
use Monolog\Logger;
1313

14+
/**
15+
* @phpstan-import-type FormattedRecord from AbstractProcessingHandler
16+
*/
1417
class CloudWatchLogsHandler extends AbstractProcessingHandler
1518
{
1619
/**
@@ -38,12 +41,16 @@ class CloudWatchLogsHandler extends AbstractProcessingHandler
3841
private $client;
3942

4043
/**
41-
* @var array
44+
* @var array{
45+
* batchSize: int,
46+
* group: string,
47+
* stream: string,
48+
* }
4249
*/
4350
private $options;
4451

4552
/**
46-
* @var array
53+
* @var array<array{message: string, timestamp: int|float}>
4754
*/
4855
private $buffer = [];
4956

@@ -78,15 +85,14 @@ class CloudWatchLogsHandler extends AbstractProcessingHandler
7885
* stream: string,
7986
* } $options
8087
* @param int|string $level
81-
* @param bool $bubble
8288
*
8389
* @throws \InvalidArgumentException
8490
*/
8591
public function __construct(
8692
CloudWatchLogsClient $client,
87-
$options,
93+
array $options,
8894
$level = Logger::DEBUG,
89-
$bubble = true
95+
bool $bubble = true
9096
) {
9197
$options['batchSize'] = $options['batchSize'] ?? 10000;
9298

@@ -123,8 +129,6 @@ protected function getDefaultFormatter(): FormatterInterface
123129

124130
/**
125131
* {@inheritdoc}
126-
*
127-
* @phpstan-ignore-next-line
128132
*/
129133
protected function write(array $record): void
130134
{
@@ -183,6 +187,10 @@ private function flushBuffer(): void
183187
/**
184188
* Event size in the batch can not be bigger than 256 KB
185189
* https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html.
190+
*
191+
* @phpstan-param FormattedRecord $entry
192+
*
193+
* @return list<array{message: string, timestamp: int|float}>
186194
*/
187195
private function formatRecords(array $entry): array
188196
{
@@ -203,9 +211,9 @@ private function formatRecords(array $entry): array
203211
/**
204212
* http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html.
205213
*
206-
* @param array $record
214+
* @param array{message: string, timestamp: int|float} $record
207215
*/
208-
private function getMessageSize($record): int
216+
private function getMessageSize(array $record): int
209217
{
210218
return \strlen($record['message']) + 26;
211219
}
@@ -220,6 +228,8 @@ private function getMessageSize($record): int
220228
* expressed as the number of milliseconds since Jan 1, 1970 00:00:00 UTC).
221229
* - The maximum number of log events in a batch is 10,000.
222230
* - A batch of log events in a single request cannot span more than 24 hours. Otherwise, the operation fails.
231+
*
232+
* @param array<array{message: string, timestamp: int|float}> $entries
223233
*/
224234
private function send(array $entries): void
225235
{

0 commit comments

Comments
 (0)