Skip to content

Commit 0d49411

Browse files
authored
Add custom rector rules to fix types in parameters automatically mapped to objects (#107)
1 parent 85b0d22 commit 0d49411

File tree

5 files changed

+83
-8
lines changed

5 files changed

+83
-8
lines changed

rector.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,22 @@
22

33
declare(strict_types=1);
44

5+
use AmazonPHP\SellingPartner\Model\CatalogItem\Categories;
6+
use AmazonPHP\SellingPartner\Model\ListingsItems\ListingsItemPutRequest;
7+
use AmazonPHP\SellingPartner\Model\Messaging\GetSchemaResponse;
8+
use AmazonPHP\SellingPartner\Model\Uploads\UploadDestination;
9+
use PHPStan\Type\ArrayType;
10+
use PHPStan\Type\MixedType;
11+
use PHPStan\Type\NullType;
12+
use PHPStan\Type\UnionType;
513
use Rector\Core\Configuration\Option;
6-
use Rector\Core\ValueObject\PhpVersion;
714
use Rector\Set\ValueObject\SetList;
15+
use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector;
16+
use Rector\TypeDeclaration\Rector\ClassMethod\AddReturnTypeDeclarationRector;
17+
use Rector\TypeDeclaration\ValueObject\AddParamTypeDeclaration;
18+
use Rector\TypeDeclaration\ValueObject\AddReturnTypeDeclaration;
819
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
20+
use Symplify\SymfonyPhpConfig\ValueObjectInliner;
921

1022
return static function (ContainerConfigurator $containerConfigurator): void {
1123
// get parameters
@@ -32,4 +44,63 @@
3244
$containerConfigurator->import(SetList::PHP_73);
3345
$containerConfigurator->import(SetList::PHP_74);
3446
$containerConfigurator->import(SetList::TYPE_DECLARATION);
47+
48+
/**
49+
* Explanation here: https://github.com/amazon-php/sp-api-sdk/issues/101#issuecomment-1002159988
50+
*/
51+
$services->set(AddParamTypeDeclarationRector::class)
52+
->call('configure', [[
53+
AddParamTypeDeclarationRector::PARAMETER_TYPEHINTS => ValueObjectInliner::inline([
54+
new AddParamTypeDeclaration(
55+
Categories::class,
56+
'setParent',
57+
0,
58+
new UnionType([new NullType(), new ArrayType(new MixedType(), new MixedType())])
59+
),
60+
new AddParamTypeDeclaration(
61+
ListingsItemPutRequest::class,
62+
'setAttributes',
63+
0,
64+
new UnionType([new NullType(), new ArrayType(new MixedType(), new MixedType())])
65+
),
66+
new AddParamTypeDeclaration(
67+
GetSchemaResponse::class,
68+
'setPayload',
69+
0,
70+
new UnionType([new NullType(), new ArrayType(new MixedType(), new MixedType())])
71+
),
72+
new AddParamTypeDeclaration(
73+
UploadDestination::class,
74+
'setHeaders',
75+
0,
76+
new UnionType([new NullType(), new ArrayType(new MixedType(), new MixedType())])
77+
),
78+
])
79+
]]);
80+
81+
$services->set(AddReturnTypeDeclarationRector::class)
82+
->call('configure', [[
83+
AddReturnTypeDeclarationRector::METHOD_RETURN_TYPES => ValueObjectInliner::inline([
84+
new AddReturnTypeDeclaration(
85+
Categories::class,
86+
'getParent',
87+
new UnionType([new NullType(), new ArrayType(new MixedType(), new MixedType())]),
88+
),
89+
new AddReturnTypeDeclaration(
90+
ListingsItemPutRequest::class,
91+
'getAttributes',
92+
new UnionType([new NullType(), new ArrayType(new MixedType(), new MixedType())]),
93+
),
94+
new AddReturnTypeDeclaration(
95+
GetSchemaResponse::class,
96+
'getPayload',
97+
new UnionType([new NullType(), new ArrayType(new MixedType(), new MixedType())]),
98+
),
99+
new AddReturnTypeDeclaration(
100+
GetSchemaResponse::class,
101+
'getHeaders',
102+
new UnionType([new NullType(), new ArrayType(new MixedType(), new MixedType())]),
103+
),
104+
])
105+
]]);
35106
};

src/AmazonPHP/SellingPartner/Model/CatalogItem/Categories.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,10 @@ public function setProductCategoryName(?string $product_category_name) : self
262262

263263
/**
264264
* Gets parent.
265+
*
266+
* @return null|object
265267
*/
266-
public function getParent() : ?object
268+
public function getParent() : ?array
267269
{
268270
return $this->container['parent'];
269271
}
@@ -273,7 +275,7 @@ public function getParent() : ?object
273275
*
274276
* @param null|object $parent the parent product category
275277
*/
276-
public function setParent(?object $parent) : self
278+
public function setParent(?array $parent) : self
277279
{
278280
$this->container['parent'] = $parent;
279281

src/AmazonPHP/SellingPartner/Model/ListingsItems/ListingsItemPutRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public function setRequirements(?string $requirements) : self
313313
/**
314314
* Gets attributes.
315315
*/
316-
public function getAttributes() : object
316+
public function getAttributes() : ?array
317317
{
318318
return $this->container['attributes'];
319319
}
@@ -323,7 +323,7 @@ public function getAttributes() : object
323323
*
324324
* @param object $attributes JSON object containing structured listings item attribute data keyed by attribute name
325325
*/
326-
public function setAttributes(object $attributes) : self
326+
public function setAttributes(?array $attributes) : self
327327
{
328328
$this->container['attributes'] = $attributes;
329329

src/AmazonPHP/SellingPartner/Model/Messaging/GetSchemaResponse.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,10 @@ public function setLinks(?GetSchemaResponseLinks $_links) : self
242242

243243
/**
244244
* Gets payload.
245+
*
246+
* @return null|object
245247
*/
246-
public function getPayload() : ?object
248+
public function getPayload() : ?array
247249
{
248250
return $this->container['payload'];
249251
}
@@ -253,7 +255,7 @@ public function getPayload() : ?object
253255
*
254256
* @param null|object $payload A JSON schema document describing the expected payload of the action. This object can be validated against <a href=http://json-schema.org/draft-04/schema>http://json-schema.org/draft-04/schema</a>.
255257
*/
256-
public function setPayload(?object $payload) : self
258+
public function setPayload(?array $payload) : self
257259
{
258260
$this->container['payload'] = $payload;
259261

src/AmazonPHP/SellingPartner/Model/Uploads/UploadDestination.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function getHeaders() : ?object
273273
*
274274
* @param null|object $headers the headers to include in the upload request
275275
*/
276-
public function setHeaders(?object $headers) : self
276+
public function setHeaders(?array $headers) : self
277277
{
278278
$this->container['headers'] = $headers;
279279

0 commit comments

Comments
 (0)