Skip to content

Commit 9d09873

Browse files
authored
Add more precise types in the Laravel integrations (#1496)
1 parent e7d9c3c commit 9d09873

File tree

9 files changed

+139
-44
lines changed

9 files changed

+139
-44
lines changed

psalm.baseline.xml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
</RedundantCast>
8787
</file>
8888
<file src="src/Integration/Laravel/Cache/src/ServiceProvider.php">
89+
<InvalidArgument>
90+
<code>$config</code>
91+
</InvalidArgument>
8992
<UndefinedInterfaceMethod>
9093
<code><![CDATA[$this->app]]></code>
9194
</UndefinedInterfaceMethod>
@@ -104,11 +107,6 @@
104107
<code><![CDATA[$this->app]]></code>
105108
</UndefinedInterfaceMethod>
106109
</file>
107-
<file src="src/Integration/Laravel/Mail/src/Transport/AsyncAwsSesTransport.php">
108-
<InvalidArgument>
109-
<code><![CDATA[array_merge($this->options, $input)]]></code>
110-
</InvalidArgument>
111-
</file>
112110
<file src="src/Integration/Laravel/Queue/src/ServiceProvider.php">
113111
<UndefinedInterfaceMethod>
114112
<code><![CDATA[$this->app]]></code>

src/Integration/Laravel/Cache/src/AsyncAwsDynamoDbStore.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use AsyncAws\DynamoDb\DynamoDbClient;
88
use AsyncAws\DynamoDb\Enum\KeyType;
99
use AsyncAws\DynamoDb\Exception\ConditionalCheckFailedException;
10+
use AsyncAws\DynamoDb\ValueObject\AttributeValue;
1011
use AsyncAws\DynamoDb\ValueObject\KeySchemaElement;
1112
use Illuminate\Contracts\Cache\LockProvider;
1213
use Illuminate\Contracts\Cache\Store;
@@ -64,22 +65,14 @@ class AsyncAwsDynamoDbStore implements LockProvider, Store
6465

6566
/**
6667
* Create a new store instance.
67-
*
68-
* @param string $table
69-
* @param string $keyAttribute
70-
* @param string $valueAttribute
71-
* @param string $expirationAttribute
72-
* @param string $prefix
73-
*
74-
* @return void
7568
*/
7669
public function __construct(
7770
DynamoDbClient $dynamo,
78-
$table,
79-
$keyAttribute = 'key',
80-
$valueAttribute = 'value',
81-
$expirationAttribute = 'expires_at',
82-
$prefix = ''
71+
string $table,
72+
string $keyAttribute = 'key',
73+
string $valueAttribute = 'value',
74+
string $expirationAttribute = 'expires_at',
75+
string $prefix = ''
8376
) {
8477
$this->table = $table;
8578
$this->dynamoDb = $dynamo;
@@ -125,11 +118,11 @@ public function getRaw(string $key, bool $consistent)
125118

126119
$item = $response->getItem();
127120
if (empty($item)) {
128-
return;
121+
return null;
129122
}
130123

131124
if ($this->isExpired($item)) {
132-
return;
125+
return null;
133126
}
134127

135128
if (isset($item[$this->valueAttribute])) {
@@ -139,6 +132,8 @@ public function getRaw(string $key, bool $consistent)
139132
null
140133
);
141134
}
135+
136+
return null;
142137
}
143138

144139
/**
@@ -463,11 +458,11 @@ public function setPrefix($prefix)
463458
/**
464459
* Determine if the given item is expired.
465460
*
466-
* @param \DateTimeInterface|null $expiration
461+
* @param array<string, AttributeValue> $item
467462
*
468463
* @return bool
469464
*/
470-
private function isExpired(array $item, $expiration = null)
465+
private function isExpired(array $item, ?\DateTimeInterface $expiration = null)
471466
{
472467
$expiration = $expiration ?: Carbon::now();
473468

@@ -477,12 +472,8 @@ private function isExpired(array $item, $expiration = null)
477472

478473
/**
479474
* Get the UNIX timestamp for the given number of seconds.
480-
*
481-
* @param int $seconds
482-
*
483-
* @return int
484475
*/
485-
private function toTimestamp($seconds)
476+
private function toTimestamp(int $seconds): int
486477
{
487478
return $seconds > 0
488479
? $this->availableAt($seconds)
@@ -493,10 +484,8 @@ private function toTimestamp($seconds)
493484
* Serialize the value.
494485
*
495486
* @param mixed $value
496-
*
497-
* @return mixed
498487
*/
499-
private function serialize($value)
488+
private function serialize($value): string
500489
{
501490
return is_numeric($value) ? (string) $value : serialize($value);
502491
}

src/Integration/Laravel/Cache/src/ServiceProvider.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
class ServiceProvider extends AbstractServiceProvider
1212
{
13+
/**
14+
* @return void
15+
*/
1316
public function boot()
1417
{
1518
/** @var CacheManager $manager */
@@ -25,13 +28,38 @@ public function boot()
2528
});
2629
}
2730

31+
/**
32+
* @param mixed $app
33+
* @param array{
34+
* key?: string|null,
35+
* secret?: string|null,
36+
* token?: string|null,
37+
* endpoint?: string|null,
38+
* region?: string|null,
39+
* table: string,
40+
* attributes?: array{key?: string|null, value?: string|null, expiration?: string|null},
41+
* prefix?: string|null
42+
* } $config
43+
*/
2844
public function createStore($app, array $config): AsyncAwsDynamoDbStore
2945
{
3046
$closure = $this->getClosure();
3147

3248
return $closure($app, $config);
3349
}
3450

51+
/**
52+
* @return \Closure(mixed, array{
53+
* key?: string|null,
54+
* secret?: string|null,
55+
* token?: string|null,
56+
* endpoint?: string|null,
57+
* region?: string|null,
58+
* table: string,
59+
* attributes?: array{key?: string|null, value?: string|null, expiration?: string|null},
60+
* prefix?: string|null
61+
* }): AsyncAwsDynamoDbStore
62+
*/
3563
private function getClosure(): \Closure
3664
{
3765
return \Closure::fromCallable(function ($app, array $config) {

src/Integration/Laravel/Filesystem/src/AsyncAwsFilesystemManager.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ class AsyncAwsFilesystemManager extends FilesystemManager
1212
/**
1313
* Create an instance of the Amazon S3 driver.
1414
*
15+
* @param array{
16+
* key?: string|null,
17+
* secret?: string|null,
18+
* token?: string|null,
19+
* endpoint?: string|null,
20+
* region?: string|null,
21+
* bucket: string,
22+
* root?: string|null,
23+
* options?: array<string, mixed>,
24+
* ...
25+
* } $config
26+
*
1527
* @return FilesystemAdapter
1628
*/
1729
public function createAsyncAwsS3Driver(array $config)

src/Integration/Laravel/Filesystem/src/ServiceProvider.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace AsyncAws\Illuminate\Filesystem;
66

7+
use Illuminate\Contracts\Foundation\Application;
78
use Illuminate\Filesystem\FilesystemAdapter;
89
use Illuminate\Filesystem\FilesystemManager;
910
use Illuminate\Support\ServiceProvider as AbstractServiceProvider;
@@ -15,6 +16,9 @@ class ServiceProvider extends AbstractServiceProvider
1516
*/
1617
private $manager;
1718

19+
/**
20+
* @return void
21+
*/
1822
public function boot()
1923
{
2024
/** @var FilesystemManager $manager */
@@ -23,11 +27,28 @@ public function boot()
2327
$manager->extend('async-aws-s3', \Closure::fromCallable([$this, 'createFilesystem']));
2428
}
2529

30+
/**
31+
* @param Application $app
32+
* @param array{
33+
* key?: string|null,
34+
* secret?: string|null,
35+
* token?: string|null,
36+
* endpoint?: string|null,
37+
* region?: string|null,
38+
* bucket: string,
39+
* root?: string|null,
40+
* options?: array<string, mixed>,
41+
* ...
42+
* } $config
43+
*/
2644
public function createFilesystem($app, array $config): FilesystemAdapter
2745
{
2846
return $this->getManager($app)->createAsyncAwsS3Driver($config);
2947
}
3048

49+
/**
50+
* @param Application $app
51+
*/
3152
private function getManager($app): AsyncAwsFilesystemManager
3253
{
3354
if (null === $this->manager) {

src/Integration/Laravel/Mail/src/ServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
class ServiceProvider extends AbstractServiceProvider
1313
{
14+
/**
15+
* @return void
16+
*/
1417
public function boot()
1518
{
1619
/** @var MailManager $manager */

src/Integration/Laravel/Mail/src/Transport/AsyncAwsSesTransport.php

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use AsyncAws\Ses\SesClient;
66
use AsyncAws\Ses\ValueObject\Destination;
77
use AsyncAws\Ses\ValueObject\EmailContent;
8+
use AsyncAws\Ses\ValueObject\ListManagementOptions;
9+
use AsyncAws\Ses\ValueObject\MessageTag;
810
use AsyncAws\Ses\ValueObject\RawMessage;
911
use Illuminate\Mail\Transport\Transport;
1012

@@ -23,18 +25,34 @@ class AsyncAwsSesTransport extends Transport
2325
/**
2426
* The Amazon SES transmission options.
2527
*
26-
* @var array
28+
* @var array{
29+
* FromEmailAddress?: string,
30+
* FromEmailAddressIdentityArn?: string,
31+
* ReplyToAddresses?: string[],
32+
* FeedbackForwardingEmailAddress?: string,
33+
* FeedbackForwardingEmailAddressIdentityArn?: string,
34+
* EmailTags?: array<MessageTag|array>,
35+
* ConfigurationSetName?: string,
36+
* ListManagementOptions?: ListManagementOptions|array,
37+
* }
2738
*/
2839
protected $options = [];
2940

3041
/**
3142
* Create a new SES transport instance.
3243
*
33-
* @param array $options
34-
*
35-
* @return void
44+
* @param array{
45+
* FromEmailAddress?: string,
46+
* FromEmailAddressIdentityArn?: string,
47+
* ReplyToAddresses?: string[],
48+
* FeedbackForwardingEmailAddress?: string,
49+
* FeedbackForwardingEmailAddressIdentityArn?: string,
50+
* EmailTags?: array<MessageTag|array>,
51+
* ConfigurationSetName?: string,
52+
* ListManagementOptions?: ListManagementOptions|array,
53+
* } $options
3654
*/
37-
public function __construct(SesClient $ses, $options = [])
55+
public function __construct(SesClient $ses, array $options = [])
3856
{
3957
$this->ses = $ses;
4058
$this->options = $options;
@@ -87,7 +105,16 @@ public function ses()
87105
/**
88106
* Get the transmission options being used by the transport.
89107
*
90-
* @return array
108+
* @return array{
109+
* FromEmailAddress?: string,
110+
* FromEmailAddressIdentityArn?: string,
111+
* ReplyToAddresses?: string[],
112+
* FeedbackForwardingEmailAddress?: string,
113+
* FeedbackForwardingEmailAddressIdentityArn?: string,
114+
* EmailTags?: array<MessageTag|array>,
115+
* ConfigurationSetName?: string,
116+
* ListManagementOptions?: ListManagementOptions|array,
117+
* }
91118
*/
92119
public function getOptions()
93120
{
@@ -97,7 +124,27 @@ public function getOptions()
97124
/**
98125
* Set the transmission options being used by the transport.
99126
*
100-
* @return array
127+
* @param array{
128+
* FromEmailAddress?: string,
129+
* FromEmailAddressIdentityArn?: string,
130+
* ReplyToAddresses?: string[],
131+
* FeedbackForwardingEmailAddress?: string,
132+
* FeedbackForwardingEmailAddressIdentityArn?: string,
133+
* EmailTags?: array<MessageTag|array>,
134+
* ConfigurationSetName?: string,
135+
* ListManagementOptions?: ListManagementOptions|array,
136+
* } $options
137+
*
138+
* @return array{
139+
* FromEmailAddress?: string,
140+
* FromEmailAddressIdentityArn?: string,
141+
* ReplyToAddresses?: string[],
142+
* FeedbackForwardingEmailAddress?: string,
143+
* FeedbackForwardingEmailAddressIdentityArn?: string,
144+
* EmailTags?: array<MessageTag|array>,
145+
* ConfigurationSetName?: string,
146+
* ListManagementOptions?: ListManagementOptions|array,
147+
* }
101148
*/
102149
public function setOptions(array $options)
103150
{

src/Integration/Laravel/Queue/src/AsyncAwsSqsQueue.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,8 @@ class AsyncAwsSqsQueue extends Queue implements QueueContract
4545

4646
/**
4747
* Create a new Amazon SQS queue instance.
48-
*
49-
* @param string $default
50-
* @param string $prefix
51-
* @param string $suffix
52-
*
53-
* @return void
5448
*/
55-
public function __construct(SqsClient $sqs, $default, $prefix = '', $suffix = '')
49+
public function __construct(SqsClient $sqs, string $default, string $prefix = '', string $suffix = '')
5650
{
5751
$this->sqs = $sqs;
5852
$this->prefix = $prefix;

src/Integration/Laravel/Queue/src/ServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
class ServiceProvider extends AbstractServiceProvider
1212
{
13+
/**
14+
* @return void
15+
*/
1316
public function boot()
1417
{
1518
/** @var QueueManager $manager */

0 commit comments

Comments
 (0)