diff --git a/conf/config.neon b/conf/config.neon index a8c8335306..61269e7485 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -1830,7 +1830,7 @@ services: - phpstan.broker.dynamicMethodReturnTypeExtension - - class: PHPStan\Type\Php\SocketCreateReturnTypeExtension + class: PHPStan\Type\Php\SocketObjectReturnTypeFunctionExtension tags: - phpstan.broker.dynamicFunctionReturnTypeExtension diff --git a/src/Php/PhpVersions.php b/src/Php/PhpVersions.php index 6ba173f663..66cc5d8417 100644 --- a/src/Php/PhpVersions.php +++ b/src/Php/PhpVersions.php @@ -33,7 +33,7 @@ public function producesWarningForFinalPrivateMethods(): TrinaryLogic return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result; } - public function socketCreateReturnsObject(): TrinaryLogic + public function socketFunctionsUseObject(): TrinaryLogic { return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result; } diff --git a/src/Type/Php/SocketCreateReturnTypeExtension.php b/src/Type/Php/SocketObjectReturnTypeFunctionExtension.php similarity index 59% rename from src/Type/Php/SocketCreateReturnTypeExtension.php rename to src/Type/Php/SocketObjectReturnTypeFunctionExtension.php index b203c54bbb..2307771ec6 100644 --- a/src/Type/Php/SocketCreateReturnTypeExtension.php +++ b/src/Type/Php/SocketObjectReturnTypeFunctionExtension.php @@ -11,27 +11,33 @@ use PHPStan\Type\ResourceType; use PHPStan\Type\Type; use PHPStan\Type\UnionType; -use function count; +use function in_array; -final class SocketCreateReturnTypeExtension implements DynamicFunctionReturnTypeExtension +final class SocketObjectReturnTypeFunctionExtension implements DynamicFunctionReturnTypeExtension { + private const FUNCTIONS = [ + 'socket_accept', + 'socket_addrinfo_bind', + 'socket_addrinfo_connect', + 'socket_create', + 'socket_create_listen', + 'socket_import_stream', + 'socket_wsaprotocol_info_import', + ]; + public function isFunctionSupported(FunctionReflection $functionReflection): bool { - return $functionReflection->getName() === 'socket_create'; + return in_array($functionReflection->getName(), self::FUNCTIONS, true); } public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type { - if (count($functionCall->getArgs()) < 3) { - return null; - } - - if ($scope->getPhpVersion()->socketCreateReturnsObject()->yes()) { + if ($scope->getPhpVersion()->socketFunctionsUseObject()->yes()) { return new UnionType([new ConstantBooleanType(false), new ObjectType('\\Socket')]); } - if ($scope->getPhpVersion()->socketCreateReturnsObject()->no()) { + if ($scope->getPhpVersion()->socketFunctionsUseObject()->no()) { return new UnionType([new ConstantBooleanType(false), new ResourceType()]); }