Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SocketCreateReturnTypeExtension #3667

Closed
wants to merge 1 commit into from
Closed
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: 10 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,16 @@ services:
tags:
- phpstan.broker.dynamicMethodReturnTypeExtension

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

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

-
class: PHPStan\Type\Php\StrSplitFunctionReturnTypeExtension
tags:
Expand Down
5 changes: 5 additions & 0 deletions src/Php/PhpVersions.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public function producesWarningForFinalPrivateMethods(): TrinaryLogic
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
}

public function socketFunctionsUseObject(): TrinaryLogic
{
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
}

public function supportsNamedArguments(): TrinaryLogic
{
return IntegerRangeType::fromInterval(80000, null)->isSuperTypeOf($this->phpVersions)->result;
Expand Down
43 changes: 43 additions & 0 deletions src/Type/Php/AddressInfoObjectReturnTypeFunctionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ResourceType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use function in_array;

final class AddressInfoObjectReturnTypeFunctionExtension implements DynamicFunctionReturnTypeExtension
{

private const FUNCTIONS = [
'socket_addrinfo_lookup',
];

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return in_array($functionReflection->getName(), self::FUNCTIONS, true);
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if ($scope->getPhpVersion()->socketFunctionsUseObject()->yes()) {
return new UnionType([new ConstantBooleanType(false), new ArrayType(new MixedType(), new ObjectType('\\AddressInfo'))]);
}

if ($scope->getPhpVersion()->socketFunctionsUseObject()->no()) {
return new UnionType([new ConstantBooleanType(false), new ArrayType(new MixedType(), new ResourceType())]);
}

return null;
}

}
47 changes: 47 additions & 0 deletions src/Type/Php/SocketObjectReturnTypeFunctionExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
use PHPStan\Type\ObjectType;
use PHPStan\Type\ResourceType;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
use function in_array;

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 in_array($functionReflection->getName(), self::FUNCTIONS, true);
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type
{
if ($scope->getPhpVersion()->socketFunctionsUseObject()->yes()) {
return new UnionType([new ConstantBooleanType(false), new ObjectType('\\Socket')]);
}

if ($scope->getPhpVersion()->socketFunctionsUseObject()->no()) {
return new UnionType([new ConstantBooleanType(false), new ResourceType()]);
}

return null;
}

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Analyser/nsrt/socket-create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php // lint >= 7.4

declare(strict_types=1);

namespace SocketCreate;

use function PHPStan\Testing\assertType;

class HelloWorld
{
public function createSocket(): void
{
if (PHP_VERSION_ID < 80000) {
assertType('resource|false', socket_create(AF_INET, SOCK_DGRAM, SOL_UDP));
}

if (PHP_VERSION_ID >= 80000) {
assertType('\Socket|false', socket_create(AF_INET, SOCK_DGRAM, SOL_UDP));
}
}

public function addrinfo($host): void
{
if (PHP_VERSION_ID < 80000) {
assertType('array<resource>|false', socket_addrinfo_lookup($host));
}

if (PHP_VERSION_ID >= 80000) {
assertType('array<\AddressInfo>|false', socket_addrinfo_lookup($host));
}
}
}
Loading