Skip to content

Commit

Permalink
ConstantStringType - catch Error in setOffsetValueType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 20, 2022
1 parent 639a055 commit 54f67c5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Type/Constant/ConstantStringType.php
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type\Constant;

use Error;
use Nette\Utils\RegexpException;
use Nette\Utils\Strings;
use PhpParser\Node\Name;
Expand Down Expand Up @@ -290,7 +291,12 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
&& $valueStringType instanceof ConstantStringType
) {
$value = $this->value;
$value[$offsetType->getValue()] = $valueStringType->getValue();

try {
$value[$offsetType->getValue()] = $valueStringType->getValue();
} catch (Error) {
return new ErrorType();
}

return new self($value);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/PHPStan/Type/Constant/ConstantStringTypeTest.php
Expand Up @@ -6,11 +6,13 @@
use InvalidArgumentException;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ErrorType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeScope;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\NullType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\StaticType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -169,4 +171,11 @@ public function testShortTextInvalidEncoding(): void
$this->assertSame("'\xc3Lorem ipsum dolor'", (new ConstantStringType("\xc3Lorem ipsum dolor"))->describe(VerbosityLevel::value()));
}

public function testSetInvalidValue(): void
{
$string = new ConstantStringType('internal:/node/add');
$result = $string->setOffsetValueType(new ConstantIntegerType(0), new NullType());
$this->assertInstanceOf(ErrorType::class, $result);
}

}

0 comments on commit 54f67c5

Please sign in to comment.