-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Make sure `coerce` doesn't update original value * Check against union and intersection types * Differentiate between v1 and v2
- Loading branch information
1 parent
8741be8
commit 60eb6cb
Showing
5 changed files
with
73 additions
and
3 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
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 declare(strict_types=1); | ||
|
||
namespace PslComplexV1Test; | ||
|
||
use Psl\Type; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
interface Bike {} | ||
interface Plane {} | ||
|
||
/** | ||
* For PSL < 2.0.0 | ||
*/ | ||
class ComplexTypesV1 | ||
{ | ||
|
||
public function coerceShapeWithComplexTypes($input): void | ||
{ | ||
$intNullOrString = Type\union(Type\int(), Type\nullable(Type\string())); | ||
$bikeAndPlane = Type\intersection(Type\object(Bike::class), Type\object(Plane::class)); | ||
$shape = Type\shape([ | ||
'name_or_length' => $intNullOrString, | ||
'transportation' => $bikeAndPlane, | ||
'something' => Type\union($intNullOrString, $bikeAndPlane) | ||
]); | ||
|
||
$output = $shape->coerce($input); | ||
assertType('array{name_or_length: int|string|null, transportation: PslComplexV1Test\Bike&PslComplexV1Test\Plane, something: int|(PslComplexV1Test\Bike&PslComplexV1Test\Plane)|string|null}', $output); | ||
} | ||
|
||
} |
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,28 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace PslComplexV2Test; | ||
|
||
use Psl\Type; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
|
||
/** | ||
* For PSL >= 2.0.0 | ||
*/ | ||
class ComplexTypesV2 | ||
{ | ||
public function coerceShapeWithComplexTypes($input): void | ||
{ | ||
$intNullOrString = Type\union(Type\int(), Type\nullable(Type\string())); | ||
$bikeAndPlane = Type\intersection(Type\instance_of(Bike::class), Type\instance_of(Plane::class)); | ||
$shape = Type\shape([ | ||
'name_or_length' => $intNullOrString, | ||
'transportation' => $bikeAndPlane, | ||
'something' => Type\union($intNullOrString, $bikeAndPlane) | ||
]); | ||
|
||
$output = $shape->coerce($input); | ||
assertType('array{name_or_length: int|string|null, transportation: PslComplexV2Test\Bike&PslComplexV2Test\Plane, something: int|(PslComplexV2Test\Bike&PslComplexV2Test\Plane)|string|null}', $output); | ||
} | ||
} |