Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
next() dynamic return type extension
- Loading branch information
1 parent
d675602
commit 599f59b
Showing
6 changed files
with
169 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Php; | ||
|
||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\FunctionReflection; | ||
use PHPStan\Reflection\ParametersAcceptorSelector; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
|
||
class ArrayNextDynamicReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'next'; | ||
} | ||
|
||
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type | ||
{ | ||
if (!isset($functionCall->args[0])) { | ||
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType(); | ||
} | ||
|
||
$argType = $scope->getType($functionCall->args[0]->value); | ||
$iterableAtLeastOnce = $argType->isIterableAtLeastOnce(); | ||
if ($iterableAtLeastOnce->no()) { | ||
return new ConstantBooleanType(false); | ||
} | ||
|
||
$valueType = $argType->getIterableValueType(); | ||
|
||
return TypeCombinator::union($valueType, new ConstantBooleanType(false)); | ||
} | ||
|
||
} |
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,77 @@ | ||
<?php | ||
|
||
namespace ArrayNext; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo() | ||
{ | ||
$array = []; | ||
assertType('false', next($array)); | ||
} | ||
|
||
/** | ||
* @param int[] $a | ||
*/ | ||
public function doBar(array $a) | ||
{ | ||
assertType('int|false', next($a)); | ||
} | ||
|
||
/** | ||
* @param non-empty-array<int, string> $a | ||
*/ | ||
public function doBaz(array $a) | ||
{ | ||
assertType('string|false', next($a)); | ||
} | ||
|
||
} | ||
|
||
interface HttpClientPoolItem | ||
{ | ||
public function isDisabled(): bool; | ||
} | ||
|
||
final class RoundRobinClientPool | ||
{ | ||
/** | ||
* @var HttpClientPoolItem[] | ||
*/ | ||
protected $clientPool = []; | ||
|
||
protected function chooseHttpClient(): HttpClientPoolItem | ||
{ | ||
$last = current($this->clientPool); | ||
assertType(HttpClientPoolItem::class . '|false', $last); | ||
|
||
do { | ||
$client = next($this->clientPool); | ||
assertType(HttpClientPoolItem::class . '|false', $client); | ||
|
||
if (false === $client) { | ||
$client = reset($this->clientPool); | ||
assertType(HttpClientPoolItem::class . '|false', $client); | ||
|
||
if (false === $client) { | ||
throw new \Exception(); | ||
} | ||
|
||
assertType(HttpClientPoolItem::class, $client); | ||
} | ||
|
||
assertType(HttpClientPoolItem::class, $client); | ||
|
||
// Case when there is only one and the last one has been disabled | ||
if ($last === $client) { | ||
assertType(HttpClientPoolItem::class, $client); | ||
throw new \Exception(); | ||
} | ||
} while ($client->isDisabled()); | ||
|
||
return $client; | ||
} | ||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace Bug5253; | ||
|
||
interface HttpClientPoolItem | ||
{ | ||
public function isDisabled(): bool; | ||
} | ||
|
||
final class RoundRobinClientPool | ||
{ | ||
/** | ||
* @var HttpClientPoolItem[] | ||
*/ | ||
protected $clientPool = []; | ||
|
||
protected function chooseHttpClient(): HttpClientPoolItem | ||
{ | ||
$last = current($this->clientPool); | ||
|
||
do { | ||
$client = next($this->clientPool); | ||
|
||
if (false === $client) { | ||
$client = reset($this->clientPool); | ||
|
||
if (false === $client) { | ||
throw new \Exception(); | ||
} | ||
} | ||
|
||
// Case when there is only one and the last one has been disabled | ||
if ($last === $client && $client->isDisabled()) { | ||
throw new \Exception(); | ||
} | ||
} while ($client->isDisabled()); | ||
|
||
return $client; | ||
} | ||
} |