Skip to content

Commit

Permalink
Use self-explanatory string value for ObjectLike::UNDEFINED (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Jan 4, 2022
1 parent e567c4c commit fa32ab7
Show file tree
Hide file tree
Showing 27 changed files with 57 additions and 43 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## v0.17.0

### Changed

- Use self-explanatory string value for `ObjectLike::UNDEFINED`

## v0.16.0

### Added
Expand Down
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,24 +307,24 @@ The following call *should* result in the previous JSON payload.
SomeInput::make(requiredId: 1, firstOptional: null, secondOptional: 2);
```

In order to generate partial inputs by default, optional named arguments have a special default value
of `PHP_FLOAT_MAX - 1` which is equivalent to `1.7976931348623157E+308`. This allows Sailor to differentiate
between explicitly passing `null` and not passing a value at all.
In order to generate partial inputs by default, optional named arguments have a special default value:

```php
Spawnia\Sailor\ObjectLike::UNDEFINED = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.';
```

```php
class SomeInput extends \Spawnia\Sailor\ObjectLike
{
const UNDEFINED = PHP_FLOAT_MAX - 1;

/**
* @param int $requiredId
* @param int|null $firstOptional
* @param int|null $secondOptional
*/
public static function make(
$requiredId,
$firstOptional = self::UNDEFINED,
$secondOptional = self::UNDEFINED,
$firstOptional = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.',
$secondOptional = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.',
): self {
$instance = new self;

Expand All @@ -343,12 +343,11 @@ class SomeInput extends \Spawnia\Sailor\ObjectLike
}
```

In the unlikely case where you need to pass a float value that might be close to or exactly match
the special value, you can assign it directly:
In the unlikely case where you need to pass exactly this value, you can assign it directly:

```php
$input = SomeInput::make(requiredId: 1);
$input->secondOptional = $someFloat;
$input->secondOptional = Spawnia\Sailor\ObjectLike::UNDEFINED;
```

## Testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MyBenSampoEnumQuery extends \Spawnia\Sailor\Operation
/**
* @param \Spawnia\Sailor\CustomTypes\Types\BenSampoEnum|null $value
*/
public static function execute($value = 1.7976931348623157E+308): MyBenSampoEnumQuery\MyBenSampoEnumQueryResult
public static function execute($value = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): MyBenSampoEnumQuery\MyBenSampoEnumQueryResult
{
return self::executeOperation(
$value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyBenSampoEnumQuery extends \Spawnia\Sailor\ObjectLike
/**
* @param \Spawnia\Sailor\CustomTypes\Types\BenSampoEnum|null $withBenSampoEnum
*/
public static function make($withBenSampoEnum = 1.7976931348623157E+308): self
public static function make($withBenSampoEnum = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MyCustomEnumQuery extends \Spawnia\Sailor\Operation
/**
* @param \Spawnia\Sailor\CustomTypes\Types\CustomEnum|null $value
*/
public static function execute($value = 1.7976931348623157E+308): MyCustomEnumQuery\MyCustomEnumQueryResult
public static function execute($value = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): MyCustomEnumQuery\MyCustomEnumQueryResult
{
return self::executeOperation(
$value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyCustomEnumQuery extends \Spawnia\Sailor\ObjectLike
/**
* @param \Spawnia\Sailor\CustomTypes\Types\CustomEnum|null $withCustomEnum
*/
public static function make($withCustomEnum = 1.7976931348623157E+308): self
public static function make($withCustomEnum = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MyEnumInputQuery extends \Spawnia\Sailor\Operation
/**
* @param \Spawnia\Sailor\CustomTypes\Types\EnumInput|null $input
*/
public static function execute($input = 1.7976931348623157E+308): MyEnumInputQuery\MyEnumInputQueryResult
public static function execute($input = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): MyEnumInputQuery\MyEnumInputQueryResult
{
return self::executeOperation(
$input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyEnumInputQuery extends \Spawnia\Sailor\ObjectLike
/**
* @param \Spawnia\Sailor\CustomTypes\Operations\MyEnumInputQuery\WithEnumInput\EnumObject|null $withEnumInput
*/
public static function make($withEnumInput = 1.7976931348623157E+308): self
public static function make($withEnumInput = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class EnumObject extends \Spawnia\Sailor\ObjectLike
* @param \Spawnia\Sailor\CustomTypes\Types\CustomEnum|null $custom
* @param string|null $default
*/
public static function make($custom = 1.7976931348623157E+308, $default = 1.7976931348623157E+308): self
{
public static function make(
$custom = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.',
$default = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'
): self {
$instance = new self;

$instance->__typename = 'EnumObject';
Expand Down
6 changes: 4 additions & 2 deletions examples/custom-types/expected/Types/EnumInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class EnumInput extends \Spawnia\Sailor\ObjectLike
* @param string|null $default
* @param \Spawnia\Sailor\CustomTypes\Types\CustomEnum|null $custom
*/
public static function make($default = 1.7976931348623157E+308, $custom = 1.7976931348623157E+308): self
{
public static function make(
$default = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.',
$custom = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'
): self {
$instance = new self;

if ($default !== self::UNDEFINED) {
Expand Down
2 changes: 1 addition & 1 deletion examples/input/expected/Operations/TakeSomeInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TakeSomeInput extends \Spawnia\Sailor\Operation
/**
* @param \Spawnia\Sailor\Input\Types\SomeInput|null $input
*/
public static function execute($input = 1.7976931348623157E+308): TakeSomeInput\TakeSomeInputResult
public static function execute($input = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): TakeSomeInput\TakeSomeInputResult
{
return self::executeOperation(
$input,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TakeSomeInput extends \Spawnia\Sailor\ObjectLike
/**
* @param int|null $takeSomeInput
*/
public static function make($takeSomeInput = 1.7976931348623157E+308): self
public static function make($takeSomeInput = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
4 changes: 2 additions & 2 deletions examples/input/expected/Types/SomeInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class SomeInput extends \Spawnia\Sailor\ObjectLike
public static function make(
$required,
$matrix,
$optional = 1.7976931348623157E+308,
$nested = 1.7976931348623157E+308
$optional = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.',
$nested = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'
): self {
$instance = new self;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class User extends \Spawnia\Sailor\ObjectLike
/**
* @param string|null $name
*/
public static function make($name = 1.7976931348623157E+308): self
public static function make($name = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class Post extends \Spawnia\Sailor\ObjectLike
* @param string $id
* @param string|null $title
*/
public static function make($id, $title = 1.7976931348623157E+308): self
{
public static function make(
$id,
$title = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'
): self {
$instance = new self;

if ($id !== self::UNDEFINED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class User extends \Spawnia\Sailor\ObjectLike
* @param string $id
* @param string|null $name
*/
public static function make($id, $name = 1.7976931348623157E+308): self
{
public static function make(
$id,
$name = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'
): self {
$instance = new self;

if ($id !== self::UNDEFINED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyObjectNestedQuery extends \Spawnia\Sailor\ObjectLike
/**
* @param \Spawnia\Sailor\Simple\Operations\MyObjectNestedQuery\SingleObject\SomeObject|null $singleObject
*/
public static function make($singleObject = 1.7976931348623157E+308): self
public static function make($singleObject = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SomeObject extends \Spawnia\Sailor\ObjectLike
/**
* @param int|null $value
*/
public static function make($value = 1.7976931348623157E+308): self
public static function make($value = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SomeObject extends \Spawnia\Sailor\ObjectLike
/**
* @param \Spawnia\Sailor\Simple\Operations\MyObjectNestedQuery\SingleObject\Nested\SomeObject|null $nested
*/
public static function make($nested = 1.7976931348623157E+308): self
public static function make($nested = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyObjectQuery extends \Spawnia\Sailor\ObjectLike
/**
* @param \Spawnia\Sailor\Simple\Operations\MyObjectQuery\SingleObject\SomeObject|null $singleObject
*/
public static function make($singleObject = 1.7976931348623157E+308): self
public static function make($singleObject = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SomeObject extends \Spawnia\Sailor\ObjectLike
/**
* @param int|null $value
*/
public static function make($value = 1.7976931348623157E+308): self
public static function make($value = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
2 changes: 1 addition & 1 deletion examples/simple/expected/Operations/MyScalarQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MyScalarQuery extends \Spawnia\Sailor\Operation
/**
* @param string|null $arg
*/
public static function execute($arg = 1.7976931348623157E+308): MyScalarQuery\MyScalarQueryResult
public static function execute($arg = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): MyScalarQuery\MyScalarQueryResult
{
return self::executeOperation(
$arg,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MyScalarQuery extends \Spawnia\Sailor\ObjectLike
/**
* @param string|null $scalarWithArg
*/
public static function make($scalarWithArg = 1.7976931348623157E+308): self
public static function make($scalarWithArg = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
4 changes: 2 additions & 2 deletions examples/simple/expected/Operations/TwoArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class TwoArgs extends \Spawnia\Sailor\Operation
* @param int|null $second
*/
public static function execute(
$first = 1.7976931348623157E+308,
$second = 1.7976931348623157E+308
$first = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.',
$second = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'
): TwoArgs\TwoArgsResult {
return self::executeOperation(
$first,
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/expected/Operations/TwoArgs/TwoArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class TwoArgs extends \Spawnia\Sailor\ObjectLike
/**
* @param string|null $twoArgs
*/
public static function make($twoArgs = 1.7976931348623157E+308): self
public static function make($twoArgs = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.'): self
{
$instance = new self;

Expand Down
7 changes: 4 additions & 3 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ parameters:
- '#Call to deprecated method getNamespace\(\) of class Nette\\PhpGenerator\\ClassType#'
- '#Unsafe usage of new static.*#'

- '#Default value of the parameter .+ \(float\) of method .+::make\(\) is incompatible with type .+#'
- '#Default value of the parameter .+ \(float\) of method .+::execute\(\) is incompatible with type .+#'
- '#Strict comparison using !== between .+ and 1\.7976931348623E\+308 will always evaluate to true#'
# Due to the workaround with ObjectLike::UNDEFINED
- '#Default value of the parameter .+ \(string\) of method .+::make\(\) is incompatible with type .+#'
- '#Default value of the parameter .+ \(string\) of method .+::execute\(\) is incompatible with type .+#'
- "#Strict comparison using !== between .+ and 'Special default…' will always evaluate to true#"
2 changes: 1 addition & 1 deletion src/ObjectLike.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

abstract class ObjectLike implements TypeConverter, BelongsToEndpoint
{
public const UNDEFINED = PHP_FLOAT_MAX - 1;
public const UNDEFINED = 'Special default value that allows Sailor to differentiate between explicitly passing null and not passing a value at all.';

/**
* Necessary in order to be able to determine between explicit null and unset properties.
Expand Down

0 comments on commit fa32ab7

Please sign in to comment.