Skip to content

Commit 0063555

Browse files
committed
Merge branch 'develop' of github.com:magento-commerce/magento-coding-standard into fix-phpcs-issues
2 parents 6987325 + 8bc679b commit 0063555

19 files changed

+510
-43
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Expr\FuncCall;
12+
use PhpParser\Node\Scalar\LNumber;
13+
use Rector\Core\Rector\AbstractRector;
14+
use Symplify\RuleDocGenerator\Exception\PoorDocumentationException;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
18+
final class ReplaceMbStrposNullLimit extends AbstractRector
19+
{
20+
/**
21+
* @return array<class-string<Node>>
22+
*/
23+
public function getNodeTypes(): array
24+
{
25+
return [FuncCall::class];
26+
}
27+
28+
/**
29+
* @param FuncCall $node
30+
*/
31+
public function refactor(Node $node): ?Node
32+
{
33+
if (!$this->isName($node->name, 'mb_strpos')) {
34+
return null;
35+
}
36+
37+
if ($node->args[2] !== LNumber::class) {
38+
$node->args[2] = $this->nodeFactory->createArg(0);
39+
return $node;
40+
}
41+
42+
return null;
43+
}
44+
45+
/**
46+
* @return RuleDefinition
47+
* @throws PoorDocumentationException
48+
*/
49+
public function getRuleDefinition(): RuleDefinition
50+
{
51+
return new RuleDefinition(
52+
'Change mb_strpos offset from null to 0', [
53+
new CodeSample(
54+
'mb_strpos("pattern", "subject", null, "encoding");',
55+
'mb_strpos("pattern", "subject", 0, "encoding");'
56+
),
57+
]
58+
);
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Expr\New_;
12+
use PhpParser\Node\Scalar\String_;
13+
use Rector\Core\Rector\AbstractRector;
14+
use Symplify\RuleDocGenerator\Exception\PoorDocumentationException;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
18+
final class ReplaceNewDateTimeNull extends AbstractRector
19+
{
20+
/**
21+
* @return array<class-string<Node>>
22+
*/
23+
public function getNodeTypes(): array
24+
{
25+
return [New_::class];
26+
}
27+
28+
/**
29+
* @param New_ $node
30+
*/
31+
public function refactor(Node $node): ?Node
32+
{
33+
if (!$this->isName($node->class, 'DateTime')) {
34+
return null;
35+
}
36+
37+
if ($node->args[0] !== String_::class) {
38+
$node->args[0] = $this->nodeFactory->createArg('now');
39+
return $node;
40+
}
41+
42+
return null;
43+
}
44+
45+
/**
46+
* @return RuleDefinition
47+
* @throws PoorDocumentationException
48+
*/
49+
public function getRuleDefinition(): RuleDefinition
50+
{
51+
return new RuleDefinition(
52+
'Change DateTime datetime input from null to "now"', [
53+
new CodeSample(
54+
'new DateTime(null, new DateTimeZone("GMT"));',
55+
'new DateTime("now", new DateTimeZone("GMT"));'
56+
),
57+
]
58+
);
59+
}
60+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\Expr\FuncCall;
12+
use PhpParser\Node\Scalar\LNumber;
13+
use Rector\Core\Rector\AbstractRector;
14+
use Symplify\RuleDocGenerator\Exception\PoorDocumentationException;
15+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
16+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
17+
18+
final class ReplacePregSplitNullLimit extends AbstractRector
19+
{
20+
/**
21+
* @return array<class-string<Node>>
22+
*/
23+
public function getNodeTypes(): array
24+
{
25+
return [FuncCall::class];
26+
}
27+
28+
/**
29+
* @param FuncCall $node
30+
*/
31+
public function refactor(Node $node): ?Node
32+
{
33+
if (!$this->isName($node->name, 'preg_split')) {
34+
return null;
35+
}
36+
37+
if ($node->args[2] !== LNumber::class) {
38+
$node->args[2] = $this->nodeFactory->createArg(-1);
39+
return $node;
40+
}
41+
42+
return null;
43+
}
44+
45+
/**
46+
* @return RuleDefinition
47+
* @throws PoorDocumentationException
48+
*/
49+
public function getRuleDefinition(): RuleDefinition
50+
{
51+
return new RuleDefinition(
52+
'Change preg_split limit from null to -1', [
53+
new CodeSample(
54+
'preg_split("pattern", "subject", null, 0);',
55+
'preg_split("pattern", "subject", -1, 0);'
56+
),
57+
]
58+
);
59+
}
60+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
mb_strpos('haystack with needle', 'needle', null, 'utf-8')
4+
5+
?>
6+
-----
7+
<?php
8+
9+
mb_strpos('haystack with needle', 'needle', 0, 'utf-8')
10+
11+
?>
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\ReplaceMbStrposNullLimit;
9+
10+
use Iterator;
11+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
12+
use Symplify\SmartFileSystem\SmartFileInfo;
13+
14+
final class ReplaceMbStrposNullLimitTest 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: 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\ReplaceMbStrposNullLimit;
9+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
10+
11+
return static function (ContainerConfigurator $containerConfigurator): void {
12+
$services = $containerConfigurator->services();
13+
$services->set(ReplaceMbStrposNullLimit::class);
14+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use DateTime;
4+
use DateTimeZone;
5+
6+
new DateTime(null, new DateTimeZone('GMT'));
7+
8+
?>
9+
-----
10+
<?php
11+
12+
use DateTime;
13+
use DateTimeZone;
14+
15+
new DateTime('now', new DateTimeZone('GMT'));
16+
17+
?>
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\ReplaceNewDateTimeNull;
9+
10+
use Iterator;
11+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
12+
use Symplify\SmartFileSystem\SmartFileInfo;
13+
14+
final class ReplaceNewDateTimeNullTest 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: 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\ReplaceNewDateTimeNull;
9+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
10+
11+
return static function (ContainerConfigurator $containerConfigurator): void {
12+
$services = $containerConfigurator->services();
13+
$services->set(ReplaceNewDateTimeNull::class);
14+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
preg_split('pattern', 'subject', null, 0)
4+
5+
?>
6+
-----
7+
<?php
8+
9+
preg_split('pattern', 'subject', -1, 0)
10+
11+
?>
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\ReplacePregSplitNullLimit;
9+
10+
use Iterator;
11+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
12+
use Symplify\SmartFileSystem\SmartFileInfo;
13+
14+
final class ReplacePregSplitNullLimitTest 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: 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\ReplacePregSplitNullLimit;
9+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
10+
11+
return static function (ContainerConfigurator $containerConfigurator): void {
12+
$services = $containerConfigurator->services();
13+
$services->set(ReplacePregSplitNullLimit::class);
14+
};

0 commit comments

Comments
 (0)