-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows the usage of float values, as follows: ```php class Foo { /** @var 404.42|1337.42 */ public readonly float $value; } ```
- Loading branch information
Showing
20 changed files
with
407 additions
and
36 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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type; | ||
|
||
/** @api */ | ||
interface FloatType extends ScalarType | ||
{ | ||
public function cast($value): float; | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Parser\Lexer\Token; | ||
|
||
use CuyZ\Valinor\Type\Parser\Lexer\TokenStream; | ||
use CuyZ\Valinor\Type\Type; | ||
use CuyZ\Valinor\Type\Types\FloatValueType; | ||
|
||
/** @internal */ | ||
final class FloatValueToken implements TraversingToken | ||
{ | ||
private float $value; | ||
|
||
public function __construct(float $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function traverse(TokenStream $stream): Type | ||
{ | ||
return new FloatValueType($this->value); | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Types\Exception; | ||
|
||
use RuntimeException; | ||
|
||
/** @api */ | ||
final class InvalidFloatValue extends RuntimeException implements CastError | ||
{ | ||
public function __construct(float $value, float $expected) | ||
{ | ||
parent::__construct( | ||
"Value `$value` does not match expected value `$expected`.", | ||
1652110115 | ||
); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Types\Exception; | ||
|
||
use CuyZ\Valinor\Utility\Polyfill; | ||
use RuntimeException; | ||
|
||
/** @api */ | ||
final class InvalidFloatValueType extends RuntimeException implements CastError | ||
{ | ||
/** | ||
* @param mixed $value | ||
*/ | ||
public function __construct($value, float $floatValue) | ||
{ | ||
$baseType = Polyfill::get_debug_type($value); | ||
|
||
parent::__construct( | ||
"Value of type `$baseType` does not match float value `$floatValue`.", | ||
1652110003 | ||
); | ||
} | ||
} |
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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Type\Types; | ||
|
||
use CuyZ\Valinor\Type\FixedType; | ||
use CuyZ\Valinor\Type\FloatType; | ||
use CuyZ\Valinor\Type\Type; | ||
use CuyZ\Valinor\Type\Types\Exception\InvalidFloatValue; | ||
use CuyZ\Valinor\Type\Types\Exception\InvalidFloatValueType; | ||
|
||
/** @api */ | ||
final class FloatValueType implements FloatType, FixedType | ||
{ | ||
private float $value; | ||
|
||
public function __construct(float $value) | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function accepts($value): bool | ||
{ | ||
return $value === $this->value; | ||
} | ||
|
||
public function matches(Type $other): bool | ||
{ | ||
if ($other instanceof UnionType) { | ||
return $other->isMatchedBy($this); | ||
} | ||
|
||
if ($other instanceof self) { | ||
return $this->value === $other->value; | ||
} | ||
|
||
return $other instanceof NativeFloatType || $other instanceof MixedType; | ||
} | ||
|
||
public function canCast($value): bool | ||
{ | ||
return is_numeric($value); | ||
} | ||
|
||
public function cast($value): float | ||
{ | ||
if (! $this->canCast($value)) { | ||
throw new InvalidFloatValueType($value, $this->value); | ||
} | ||
|
||
$value = (float)$value; // @phpstan-ignore-line | ||
|
||
if (! $this->accepts($value)) { | ||
throw new InvalidFloatValue($value, $this->value); | ||
} | ||
|
||
return $value; | ||
} | ||
|
||
public function value() | ||
{ | ||
return $this->value; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return (string)$this->value; | ||
} | ||
} |
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
Oops, something went wrong.