Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Type/Php/ArrayChunkFunctionThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use ValueError;
use function count;

/**
Expand All @@ -24,7 +26,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
return $functionReflection->getName() === 'array_chunk';
}

public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type
public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): Type
{
if ($scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no()) {
return new VoidType();
Expand All @@ -33,19 +35,19 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
$args = $funcCall->getArgs();
foreach ($args as $arg) {
if ($arg->unpack || $arg->name !== null) {
return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}
}

if (count($args) < 2) {
return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}

if (IntegerRangeType::fromInterval(1, null)->isSuperTypeOf($scope->getNativeType($args[1]->value))->yes()) {
return new VoidType();
}

return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}

}
10 changes: 8 additions & 2 deletions src/Type/Php/ArrayCombineFunctionThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use ValueError;
use function count;

#[AutowiredService]
Expand All @@ -25,17 +27,21 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo

public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type
{
if ($scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no()) {
return null;
}

$args = $funcCall->getArgs();
if (count($args) < 2) {
return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}

$firstArg = $args[0]->value;
$secondArg = $args[1]->value;

$hasValueError = $this->arrayCombineHelper->getReturnAndThrowType($firstArg, $secondArg, $scope)[1];
if (!$hasValueError->no()) {
return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}

return null;
Expand Down
8 changes: 5 additions & 3 deletions src/Type/Php/MinMaxFunctionThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use ValueError;
use function count;
use function in_array;

Expand All @@ -33,7 +35,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
return $functionReflection->getThrowType();
}

if (!$scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->yes()) {
if ($scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no()) {
return new VoidType();
}

Expand All @@ -49,7 +51,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
$argTypes = [];
foreach ($args as $arg) {
if ($arg->unpack || $arg->name !== null) {
return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}

$argTypes[] = $scope->getNativeType($arg->value);
Expand All @@ -58,7 +60,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
$parametersAcceptor = ParametersAcceptorSelector::selectFromTypes($argTypes, $functionReflection->getVariants(), false);
$parameters = $parametersAcceptor->getParameters();
if (isset($parameters[0]) && $parameters[0]->getType()->isArray()->yes()) {
return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}

return new VoidType();
Expand Down
31 changes: 26 additions & 5 deletions src/Type/Php/PrintfFunctionThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,29 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
foreach ($args as $i => $arg) {
$originalArg = $arg->getAttribute(ArgumentsNormalizer::ORIGINAL_ARG_ATTRIBUTE);
if ($arg->name !== null || ($originalArg instanceof Arg && $originalArg->name !== null)) {
return $functionReflection->getThrowType();
return $this->getDefaultThrowType($functionReflection, $scope);
}
if ($i <= $formatPosition && $arg->unpack) {
return $functionReflection->getThrowType();
return $this->getDefaultThrowType($functionReflection, $scope);
}
}

if (!isset($args[$formatPosition])) {
return $functionReflection->getThrowType();
return $this->getDefaultThrowType($functionReflection, $scope);
}

$formatType = $scope->getNativeType($args[$formatPosition]->value);
$formats = $formatType->getConstantStrings();
if (!$formatType->isString()->yes() || count($formats) === 0) {
return $functionReflection->getThrowType();
return $this->getDefaultThrowType($functionReflection, $scope);
}

$uses = [];
$requiredArgumentsCount = 0;
foreach ($formats as $format) {
$formatUses = $this->formatParser->parse($format->getValue(), $scope->getPhpVersion());
if ($formatUses === null) {
return $functionReflection->getThrowType();
return $this->getDefaultThrowType($functionReflection, $scope);
}

foreach ($formatUses as $use) {
Expand Down Expand Up @@ -135,6 +135,27 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
return TypeCombinator::union(...$throwTypes);
}

/**
* The stubs of a PHP version range below PHP 8 leave out the errors these functions throw as of PHP 8.
*/
private function getDefaultThrowType(FunctionReflection $functionReflection, Scope $scope): ?Type
{
$throwType = $functionReflection->getThrowType();
if (!$scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->maybe()) {
return $throwType;
}

$php8ThrowTypes = [new ObjectType(ValueError::class)];
if (!in_array($functionReflection->getName(), self::ARRAY_FUNCTIONS, true)) {
$php8ThrowTypes[] = new ObjectType(ArgumentCountError::class);
}
if ($throwType !== null) {
$php8ThrowTypes[] = $throwType;
}

return TypeCombinator::union(...$php8ThrowTypes);
}

/**
* Types of the arguments after the format at positions known from the call,
* and how many arguments are passed at least. An unpacked constant list
Expand Down
7 changes: 4 additions & 3 deletions src/Type/Php/RoundFunctionThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\VoidType;
use ValueError;
use function count;

/**
Expand All @@ -27,7 +28,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
return $functionReflection->getName() === 'round';
}

public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type
public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): Type
{
if ($scope->getPhpVersion()->throwsValueErrorForInvalidRoundingMode()->no()) {
return new VoidType();
Expand All @@ -36,7 +37,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
$args = $funcCall->getArgs();
foreach ($args as $arg) {
if ($arg->unpack || $arg->name !== null) {
return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}
}

Expand All @@ -52,7 +53,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
return new VoidType();
}

return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}

}
8 changes: 5 additions & 3 deletions src/Type/Php/StrSplitFunctionThrowTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\IntegerRangeType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\VoidType;
use ValueError;
use function count;

/**
Expand All @@ -24,7 +26,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo
return $functionReflection->getName() === 'str_split';
}

public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): ?Type
public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $funcCall, Scope $scope): Type
{
if ($scope->getPhpVersion()->throwsValueErrorForInternalFunctions()->no()) {
return new VoidType();
Expand All @@ -33,7 +35,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
$args = $funcCall->getArgs();
foreach ($args as $arg) {
if ($arg->unpack || $arg->name !== null) {
return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}
}

Expand All @@ -45,7 +47,7 @@ public function getThrowTypeFromFunctionCall(FunctionReflection $functionReflect
return new VoidType();
}

return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\DynamicFunctionThrowTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use ValueError;

#[AutowiredService]
final class VersionCompareFunctionDynamicThrowTypeExtension implements DynamicFunctionThrowTypeExtension
Expand Down Expand Up @@ -35,7 +37,7 @@ public function getThrowTypeFromFunctionCall(

$operatorStrings = $scope->getType($args[2]->value)->getConstantStrings();
if (VersionCompareFunctionDynamicReturnTypeExtension::mightBeInvalidOperator($operatorStrings)) {
return $functionReflection->getThrowType();
return $functionReflection->getThrowType() ?? new ObjectType(ValueError::class);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,85 @@ public function rangeWithUnknownBoundary(int $i): void
}
}

public function arrayChunk(int $length): void
{
try {
array_chunk([1], $length);
} catch (\ValueError $e) { // a length below 1 throws on PHP 8

}
}

public function strSplit(string $s, int $length): void
{
try {
str_split($s, $length);
} catch (\ValueError $e) { // a length below 1 throws on PHP 8

}
}

public function minOfArray(array $a): void
{
try {
min($a);
} catch (\ValueError $e) { // an empty array throws on PHP 8

}
}

public function maxOfArray(array $a): void
{
try {
max($a);
} catch (\ValueError $e) { // an empty array throws on PHP 8

}
}

public function sprintfWithUnknownFormat(string $format): void
{
try {
sprintf($format, 1);
} catch (\ValueError $e) { // an invalid format throws on PHP 8

}
}

public function vprintfWithUnknownFormat(string $format, array $values): void
{
try {
vprintf($format, $values);
} catch (\ValueError $e) { // an invalid format throws on PHP 8

}
}

public function versionCompareWithUnknownOperator(string $operator): void
{
try {
version_compare('1.0', '2.0', $operator);
} catch (\ValueError $e) { // an invalid operator throws on PHP 8

}
}

public function arrayCombine(array $keys, array $values): void
{
try {
array_combine($keys, $values);
} catch (\ValueError $e) { // arrays of different sizes throw on PHP 8

}
}

public function roundWithUnknownMode(float $f, int $mode): void
{
try {
round($f, 0, $mode);
} catch (\ValueError $e) { // an invalid mode throws on PHP 8.4+

}
}

}
Loading