Skip to content

Commit d893a0d

Browse files
committed
Fix array-shape descriptions with special chars
1 parent 5e3a364 commit d893a0d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ public function describe(VerbosityLevel $level): string
14761476
if (is_string($keyDescription)) {
14771477
if (str_contains($keyDescription, '"')) {
14781478
$keyDescription = sprintf('\'%s\'', $keyDescription);
1479-
} elseif (str_contains($keyDescription, '\'')) {
1479+
} else {
14801480
$keyDescription = sprintf('"%s"', $keyDescription);
14811481
}
14821482
}

tests/PHPStan/Type/Constant/ConstantArrayTypeBuilderTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Type\BooleanType;
66
use PHPStan\Type\NullType;
77
use PHPStan\Type\StringType;
8+
use PHPStan\Type\Type;
89
use PHPStan\Type\TypeCombinator;
910
use PHPStan\Type\VerbosityLevel;
1011
use PHPUnit\Framework\TestCase;
@@ -162,4 +163,38 @@ public function testIsListWithUnion(): void
162163
$this->assertFalse($builder->isList());
163164
}
164165

166+
public function dataQuotedOffsetNames(): iterable
167+
{
168+
yield [
169+
'array{"count(*)": 1}',
170+
new ConstantStringType('count(*)'),
171+
new ConstantIntegerType(1),
172+
];
173+
174+
yield [
175+
"array{'cou\"nt(*)': 1}",
176+
new ConstantStringType('cou"nt(*)'),
177+
new ConstantIntegerType(1),
178+
];
179+
180+
yield [
181+
'array{"count\'ed": 1}',
182+
new ConstantStringType("count'ed"),
183+
new ConstantIntegerType(1),
184+
];
185+
}
186+
187+
/**
188+
* @dataProvider dataQuotedOffsetNames
189+
*/
190+
public function testQuotedOffsetNames(string $expectedDescription, Type $key, Type $value): void
191+
{
192+
$builder = ConstantArrayTypeBuilder::createEmpty();
193+
$builder->setOffsetValueType($key, $value);
194+
195+
$array1 = $builder->getArray();
196+
$this->assertInstanceOf(ConstantArrayType::class, $array1);
197+
$this->assertSame($expectedDescription, $array1->describe(VerbosityLevel::precise()));
198+
}
199+
165200
}

0 commit comments

Comments
 (0)