Skip to content

Commit 1be74dc

Browse files
[Imported] AC-1718 return types for arrayaccess methods (#138)
* AC-1717: Passing null to non-nullable internal function parameters is deprecated
1 parent 8bc679b commit 1be74dc

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* Copyright 2021 Adobe
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Rector\Src;
9+
10+
use PhpParser\Node;
11+
use PhpParser\Node\Stmt\Class_;
12+
use Rector\Core\Rector\AbstractRector;
13+
use Symplify\RuleDocGenerator\Exception\PoorDocumentationException;
14+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
15+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
16+
17+
final class AddArrayAccessInterfaceReturnTypes extends AbstractRector
18+
{
19+
/**
20+
* @return array<class-string<Node>>
21+
*/
22+
public function getNodeTypes(): array
23+
{
24+
return [Class_::class];
25+
}
26+
27+
/**
28+
* @param Class_ $node
29+
*/
30+
public function refactor(Node $node): ?Node
31+
{
32+
foreach ($node->implements as $implement) {
33+
if ($this->isName($implement, 'ArrayAccess')) {
34+
break;
35+
}
36+
return null;
37+
}
38+
39+
foreach ($node->getMethods() as $method) {
40+
if ($this->isName($method, 'offsetExists') && empty($method->getReturnType())) {
41+
$method->returnType = new \PhpParser\Node\Name('bool');
42+
}
43+
if ($this->isName($method, 'offsetSet') && empty($method->getReturnType())) {
44+
$method->returnType = new \PhpParser\Node\Name('void');
45+
}
46+
if ($this->isName($method, 'offsetUnset') && empty($method->getReturnType())) {
47+
$method->returnType = new \PhpParser\Node\Name('void');
48+
}
49+
}
50+
51+
return null;
52+
}
53+
54+
/**
55+
* @return RuleDefinition
56+
* @throws PoorDocumentationException
57+
*/
58+
public function getRuleDefinition(): RuleDefinition
59+
{
60+
return new RuleDefinition(
61+
'Add return types specified by ArrayAccess interface', [
62+
new CodeSample(
63+
'public function offsetSet($offset, $value)',
64+
'public function offsetSet($offset, $value): void'
65+
),
66+
]
67+
);
68+
}
69+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright 2021 Adobe
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento2\Rector\Tests\AddArrayAccessInterfaceReturnTypes;
9+
10+
use Iterator;
11+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
12+
use Symplify\SmartFileSystem\SmartFileInfo;
13+
14+
final class AddArrayAccessInterfaceReturnTypesTest extends AbstractRectorTestCase
15+
{
16+
/**
17+
* @dataProvider provideData()
18+
*/
19+
public function test(SmartFileInfo $fileInfo): void
20+
{
21+
$this->doTestFileInfo($fileInfo);
22+
}
23+
24+
/**
25+
* @return Iterator<SmartFileInfo>
26+
*/
27+
public function provideData(): Iterator
28+
{
29+
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
30+
}
31+
32+
public function provideConfigFilePath(): string
33+
{
34+
return __DIR__ . '/config/configured_rule.php';
35+
}
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
class MyExampleClass implements \ArrayAccess
4+
{
5+
public function offsetExists($offset)
6+
{
7+
}
8+
public function offsetGet($offset)
9+
{
10+
}
11+
public function offsetSet($offset, $value)
12+
{
13+
}
14+
public function offsetUnset($offset)
15+
{
16+
}
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
class MyExampleClass implements \ArrayAccess
24+
{
25+
public function offsetExists($offset): bool
26+
{
27+
}
28+
public function offsetGet($offset)
29+
{
30+
}
31+
public function offsetSet($offset, $value): void
32+
{
33+
}
34+
public function offsetUnset($offset): void
35+
{
36+
}
37+
}
38+
39+
?>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Copyright 2021 Adobe
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento2\Rector\Src\AddArrayAccessInterfaceReturnTypes;
9+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
10+
11+
return static function (ContainerConfigurator $containerConfigurator): void {
12+
$services = $containerConfigurator->services();
13+
$services->set(AddArrayAccessInterfaceReturnTypes::class);
14+
};

0 commit comments

Comments
 (0)