-
Notifications
You must be signed in to change notification settings - Fork 468
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement SocketCreateReturnTypeExtension
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?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 Socket; | ||
use function count; | ||
|
||
final class SocketCreateReturnTypeExtension implements DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'socket_create'; | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): ?Type | ||
{ | ||
if (count($functionCall->getArgs()) < 3) { | ||
return null; | ||
} | ||
|
||
if ($scope->getPhpVersion()->socketCreateUsesResource()->no()) { | ||
return new UnionType([new ConstantBooleanType(false), new ObjectType(Socket::class)]); | ||
Check failure on line 32 in src/Type/Php/SocketCreateReturnTypeExtension.php GitHub Actions / PHPStan (7.4, ubuntu-latest)
|
||
} | ||
|
||
if ($scope->getPhpVersion()->socketCreateUsesResource()->yes()) { | ||
return new UnionType([new ConstantBooleanType(false), new ResourceType()]); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php // lint >= 7.4 | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SocketCreate; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class HelloWorld | ||
{ | ||
public function sayHello(): void | ||
{ | ||
if (PHP_VERSION_ID < 80000) { | ||
assertType('resource|false', socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)); | ||
return; | ||
} | ||
assertType('Socket|false', socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)); | ||
} | ||
} |