Skip to content

Commit 703f4bc

Browse files
authored
Fix StripslashesFromStringsOnlyDynamicFunctionReturnTypeExtension (#295)
1 parent 9edf798 commit 703f4bc

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

phpcs.xml.dist

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
<exclude-pattern>tests/DynamicConstantTypeTest.php</exclude-pattern>
2424
</rule>
2525

26+
<!-- TypeTraverser uses callable $traverse -->
27+
<rule ref="NeutronStandard.Functions.VariableFunctions.VariableFunction">
28+
<exclude-pattern>src/StripslashesFromStringsOnlyDynamicFunctionReturnTypeExtension.php</exclude-pattern>
29+
</rule>
30+
2631
<!-- Allow string concatenation in tests -->
2732
<rule ref="PSR12NeutronRuleset.Strings.ConcatenationUsage.NotAllowed">
2833
<exclude-pattern>tests/*</exclude-pattern>

src/StripslashesFromStringsOnlyDynamicFunctionReturnTypeExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace SzepeViktor\PHPStan\WordPress;
66

77
use PhpParser\Node\Expr\FuncCall;
8+
use PhpParser\Node\UnionType;
89
use PHPStan\Analyser\Scope;
910
use PHPStan\Reflection\FunctionReflection;
1011
use PHPStan\Type\Constant\ConstantStringType;
@@ -31,10 +32,15 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
3132

3233
return TypeTraverser::map(
3334
$argType,
34-
static function (Type $type): Type {
35+
static function (Type $type, callable $traverse): Type {
36+
if ($type instanceof UnionType) {
37+
return $traverse($type);
38+
}
39+
3540
if ($type instanceof ConstantStringType) {
3641
return new ConstantStringType(stripslashes($type->getValue()));
3742
}
43+
3844
return $type;
3945
}
4046
);

tests/data/stripslashes-from-strings-only.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@
1616
assertType('array{}', stripslashes_from_strings_only([]));
1717
assertType("array{'foo'}", stripslashes_from_strings_only(['foo']));
1818
assertType("array{'foo\\'s bar'}", stripslashes_from_strings_only(['foo\'s bar']));
19+
20+
assertType("'foo\'s bar'|array{'foo\\'s bar'}", stripslashes_from_strings_only(rand(0, 1) === 1 ? ['foo\'s bar'] : 'foo\'s bar'));

0 commit comments

Comments
 (0)