-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generics - fix handling optional parameter
Closes phpstan/phpstan#2573 Closes phpstan/phpstan#4603
- Loading branch information
1 parent
c9b5d27
commit 7306405
Showing
8 changed files
with
138 additions
and
1 deletion.
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,44 @@ | ||
<?php | ||
|
||
namespace GenericsDefault; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
/** | ||
* @template T of int | ||
* @param T $test | ||
* @return T | ||
*/ | ||
public function doFoo(int $test = 0): int | ||
{ | ||
return $test; | ||
} | ||
|
||
public function doBar(): void | ||
{ | ||
assertType('0', $this->doFoo()); | ||
assertType('1', $this->doFoo(1)); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param T $default | ||
*/ | ||
public function doBaz($default = null): void | ||
{ | ||
assertType('T (method GenericsDefault\Foo::doBaz(), argument)', $default); | ||
} | ||
|
||
/** | ||
* @template T | ||
* @param T|null $default | ||
*/ | ||
public function doLorem($default = null): void | ||
{ | ||
assertType('T (method GenericsDefault\Foo::doLorem(), argument)|null', $default); | ||
} | ||
|
||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace Bug2573; | ||
|
||
class Bar | ||
{ | ||
|
||
/** | ||
* @template V | ||
* @template D | ||
* | ||
* @param array<V> $array | ||
* @param array-key $key | ||
* @param D $default | ||
* @return V|D | ||
*/ | ||
function idx($array, $key, $default = null) { | ||
if (array_key_exists($key, $array)) { | ||
return $array[$key]; | ||
} | ||
return $default; | ||
} | ||
|
||
/** | ||
* @param array<int, int> $arr | ||
* @return int | ||
*/ | ||
function example($arr) { | ||
return $this->idx($arr, 5, 42); | ||
} | ||
|
||
} |
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 | ||
|
||
namespace Bug2573; | ||
|
||
class Foo | ||
{ | ||
|
||
/** | ||
* @template T1 | ||
* @template T2 | ||
* @param T1 $a | ||
* @param T2 $b | ||
* @return T1|T2 | ||
*/ | ||
function chooseOne($a, $b = []) { | ||
return rand(0, 1) ? $a : $b; | ||
} | ||
|
||
} |
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,20 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace Bug4603; | ||
|
||
class Foo | ||
{ | ||
|
||
/** | ||
* @param T $val | ||
* | ||
* @return T | ||
* | ||
* @template T | ||
*/ | ||
function fcn(mixed $val = null) | ||
{ | ||
return $val; | ||
} | ||
|
||
} |