Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5f91da6
commit 6754df5
Showing
6 changed files
with
149 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,35 @@ | ||
<?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\ArrayType; | ||
use PHPStan\Type\Type; | ||
|
||
class ArraySpliceFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension | ||
{ | ||
|
||
public function isFunctionSupported(FunctionReflection $functionReflection): bool | ||
{ | ||
return $functionReflection->getName() === 'array_splice'; | ||
} | ||
|
||
public function getTypeFromFunctionCall( | ||
FunctionReflection $functionReflection, | ||
FuncCall $functionCall, | ||
Scope $scope | ||
): Type | ||
{ | ||
if (!isset($functionCall->getArgs()[0])) { | ||
return ParametersAcceptorSelector::selectFromArgs($scope, $functionCall->getArgs(), $functionReflection->getVariants())->getReturnType(); | ||
} | ||
|
||
$arrayArg = $scope->getType($functionCall->getArgs()[0]->value); | ||
|
||
return new ArrayType($arrayArg->getIterableKeyType(), $arrayArg->getIterableValueType()); | ||
} | ||
|
||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace Bug4743; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Node {} | ||
|
||
/** | ||
* @template T of Node | ||
*/ | ||
class NodeList { | ||
|
||
/** | ||
* @phpstan-var array<T> | ||
*/ | ||
private $nodes; | ||
|
||
/** | ||
* @phpstan-param array<T> $nodes | ||
*/ | ||
public function __construct(array $nodes) | ||
{ | ||
$this->nodes = $nodes; | ||
} | ||
|
||
public function splice(int $offset, int $length): void | ||
{ | ||
$newNodes = array_splice($this->nodes, $offset, $length); | ||
|
||
assertType('array<T of Bug4743\\Node (class Bug4743\\NodeList, argument)>', $this->nodes); | ||
assertType('array<T of Bug4743\\Node (class Bug4743\\NodeList, argument)>', $newNodes); | ||
} | ||
} |
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,56 @@ | ||
<?php | ||
|
||
namespace Bug5017; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo() | ||
{ | ||
$items = [0, 1, 2, 3, 4]; | ||
|
||
while ($items) { | ||
assertType('array<0|1|2|3|4, 0|1|2|3|4>&nonEmpty', $items); | ||
$batch = array_splice($items, 0, 2); | ||
assertType('array<0|1|2|3|4, 0|1|2|3|4>', $items); | ||
assertType('array<0|1|2|3|4, 0|1|2|3|4>', $batch); | ||
} | ||
} | ||
|
||
/** | ||
* @param int[] $items | ||
*/ | ||
public function doBar($items) | ||
{ | ||
while ($items) { | ||
assertType('array<int>&nonEmpty', $items); | ||
$batch = array_splice($items, 0, 2); | ||
assertType('array<int>', $items); | ||
assertType('array<int>', $batch); | ||
} | ||
} | ||
|
||
public function doBar2() | ||
{ | ||
$items = [0, 1, 2, 3, 4]; | ||
assertType('array(0, 1, 2, 3, 4)', $items); | ||
$batch = array_splice($items, 0, 2); | ||
assertType('array<0|1|2|3|4, 0|1|2|3|4>', $items); | ||
assertType('array<0|1|2|3|4, 0|1|2|3|4>', $batch); | ||
} | ||
|
||
/** | ||
* @param int[] $ints | ||
* @param string[] $strings | ||
*/ | ||
public function doBar3(array $ints, array $strings) | ||
{ | ||
$removed = array_splice($ints, 0, 2, $strings); | ||
assertType('array<int>', $removed); | ||
assertType('array<int|string>', $ints); | ||
assertType('array<string>', $strings); | ||
} | ||
|
||
} |