Skip to content

Commit 5d7c0ca

Browse files
committed
Implement NonFalseyNonEmptyStringMutator
1 parent 6f3df78 commit 5d7c0ca

File tree

3 files changed

+148
-2
lines changed

3 files changed

+148
-2
lines changed

src/Infection/IsSuperTypeOfCalleeAndArgumentMutator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
use Infection\Mutator\Definition;
66
use Infection\Mutator\Mutator;
77
use Infection\Mutator\MutatorCategory;
8+
use LogicException;
89
use PhpParser\Node;
9-
use RuntimeException;
1010
use function count;
1111
use function in_array;
1212

@@ -83,7 +83,7 @@ public function mutate(Node $node): iterable
8383
{
8484
$args = $node->getArgs();
8585
if (count($args) !== 1) {
86-
throw new RuntimeException();
86+
throw new LogicException();
8787
}
8888

8989
yield new Node\Expr\MethodCall(
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Infection;
4+
5+
use Infection\Mutator\Definition;
6+
use Infection\Mutator\Mutator;
7+
use Infection\Mutator\MutatorCategory;
8+
use LogicException;
9+
use PhpParser\Node;
10+
use function count;
11+
use function in_array;
12+
13+
/**
14+
* @implements Mutator<Node\Expr\MethodCall>
15+
*/
16+
final class NonFalseyNonEmptyStringMutator implements Mutator
17+
{
18+
19+
public static function getDefinition(): Definition
20+
{
21+
return new Definition(
22+
<<<'TXT'
23+
Replaces boolean Type->isNonFalseyString() with Type->isNonEmptyString().
24+
TXT
25+
,
26+
MutatorCategory::ORTHOGONAL_REPLACEMENT,
27+
null,
28+
<<<'DIFF'
29+
- $type->isNonFalseyString()->yes();
30+
+ $type->isNonEmptyString()->yes();
31+
DIFF,
32+
);
33+
}
34+
35+
public function getName(): string
36+
{
37+
return self::class;
38+
}
39+
40+
public function canMutate(Node $node): bool
41+
{
42+
if (!$node instanceof Node\Expr\MethodCall) {
43+
return false;
44+
}
45+
46+
if (!$node->name instanceof Node\Identifier) {
47+
return false;
48+
}
49+
50+
if (!in_array($node->name->name, ['isNonEmptyString', 'isNonFalseyString'], true)) {
51+
return false;
52+
}
53+
54+
if (count($node->getArgs()) !== 0) {
55+
return false;
56+
}
57+
58+
return true;
59+
}
60+
61+
public function mutate(Node $node): iterable
62+
{
63+
if (!$node->name instanceof Node\Identifier) {
64+
throw new LogicException();
65+
}
66+
67+
if ($node->name->name === 'isNonEmptyString') {
68+
yield new Node\Expr\MethodCall($node->var, 'isNonFalseyString');
69+
} else {
70+
yield new Node\Expr\MethodCall($node->var, 'isNonEmptyString');
71+
}
72+
}
73+
74+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Infection;
4+
5+
use Infection\Testing\BaseMutatorTestCase;
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
9+
#[CoversClass(NonFalseyNonEmptyStringMutator::class)]
10+
final class NonFalseyNonEmptyStringMutatorTest extends BaseMutatorTestCase
11+
{
12+
13+
/**
14+
* @param string|string[] $expected
15+
*/
16+
#[DataProvider('mutationsProvider')]
17+
public function testMutator(string $input, $expected = []): void
18+
{
19+
$this->assertMutatesInput($input, $expected);
20+
}
21+
22+
/**
23+
* @return iterable<string, array{0: string, 1?: string}>
24+
*/
25+
public static function mutationsProvider(): iterable
26+
{
27+
yield 'It mutates isNonEmptyString()' => [
28+
<<<'PHP'
29+
<?php
30+
31+
$type->isNonEmptyString()->yes();
32+
PHP
33+
,
34+
<<<'PHP'
35+
<?php
36+
37+
$type->isNonFalseyString()->yes();
38+
PHP
39+
,
40+
];
41+
42+
yield 'It mutates isNonFalseyString()' => [
43+
<<<'PHP'
44+
<?php
45+
46+
$type->isNonFalseyString()->yes();
47+
PHP
48+
,
49+
<<<'PHP'
50+
<?php
51+
52+
$type->isNonEmptyString()->yes();
53+
PHP
54+
,
55+
];
56+
57+
yield 'skip with arguments' => [
58+
<<<'PHP'
59+
<?php
60+
61+
$a->isNonFalseyString($b);
62+
PHP
63+
,
64+
];
65+
}
66+
67+
protected function getTestedMutatorClassName(): string
68+
{
69+
return NonFalseyNonEmptyStringMutator::class;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)