Skip to content

Commit 54f67c5

Browse files
committed
ConstantStringType - catch Error in setOffsetValueType
1 parent 639a055 commit 54f67c5

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Type/Constant/ConstantStringType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Type\Constant;
44

5+
use Error;
56
use Nette\Utils\RegexpException;
67
use Nette\Utils\Strings;
78
use PhpParser\Node\Name;
@@ -290,7 +291,12 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
290291
&& $valueStringType instanceof ConstantStringType
291292
) {
292293
$value = $this->value;
293-
$value[$offsetType->getValue()] = $valueStringType->getValue();
294+
295+
try {
296+
$value[$offsetType->getValue()] = $valueStringType->getValue();
297+
} catch (Error) {
298+
return new ErrorType();
299+
}
294300

295301
return new self($value);
296302
}

tests/PHPStan/Type/Constant/ConstantStringTypeTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
use InvalidArgumentException;
77
use PHPStan\Testing\PHPStanTestCase;
88
use PHPStan\TrinaryLogic;
9+
use PHPStan\Type\ErrorType;
910
use PHPStan\Type\GeneralizePrecision;
1011
use PHPStan\Type\Generic\GenericClassStringType;
1112
use PHPStan\Type\Generic\TemplateTypeFactory;
1213
use PHPStan\Type\Generic\TemplateTypeScope;
1314
use PHPStan\Type\Generic\TemplateTypeVariance;
15+
use PHPStan\Type\NullType;
1416
use PHPStan\Type\ObjectType;
1517
use PHPStan\Type\StaticType;
1618
use PHPStan\Type\Type;
@@ -169,4 +171,11 @@ public function testShortTextInvalidEncoding(): void
169171
$this->assertSame("'\xc3Lorem ipsum dolor'", (new ConstantStringType("\xc3Lorem ipsum dolor"))->describe(VerbosityLevel::value()));
170172
}
171173

174+
public function testSetInvalidValue(): void
175+
{
176+
$string = new ConstantStringType('internal:/node/add');
177+
$result = $string->setOffsetValueType(new ConstantIntegerType(0), new NullType());
178+
$this->assertInstanceOf(ErrorType::class, $result);
179+
}
180+
172181
}

0 commit comments

Comments
 (0)