Skip to content

Commit

Permalink
support all socket_* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 21, 2024
1 parent d146e0e commit c6a6860
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,7 @@ services:
- phpstan.broker.dynamicMethodReturnTypeExtension

-
class: PHPStan\Type\Php\SocketCreateReturnTypeExtension
class: PHPStan\Type\Php\SocketObjectReturnTypeFunctionExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

Expand Down
2 changes: 1 addition & 1 deletion src/Php/PhpVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
}

Expand Down

0 comments on commit c6a6860

Please sign in to comment.