From 1cb51a734e7ee3f8921eb5457ea805ffbf61417e Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Fri, 29 Sep 2023 07:53:26 +0200 Subject: [PATCH] Improve nullable formatting --- src/Formatter/XsdTypeFormatter.php | 3 +- .../Predicate/IsConsideredNullableType.php | 29 +++++++++ .../Predicate/IsConsideredScalarType.php | 15 +++++ tests/PhpCompatibility/schema037.phpt | 2 +- tests/PhpCompatibility/schema038.phpt | 2 +- tests/PhpCompatibility/schema039.phpt | 2 +- tests/PhpCompatibility/schema040.phpt | 2 +- tests/PhpCompatibility/schema042.phpt | 2 +- tests/PhpCompatibility/schema043.phpt | 6 +- tests/PhpCompatibility/schema044.phpt | 2 +- tests/PhpCompatibility/schema045.phpt | 6 +- tests/PhpCompatibility/schema046.phpt | 6 +- tests/PhpCompatibility/schema047.phpt | 2 +- tests/PhpCompatibility/schema048.phpt | 6 +- tests/PhpCompatibility/schema062.phpt | 2 +- tests/PhpCompatibility/schema065.phpt | 4 +- tests/PhpCompatibility/schema066.phpt | 4 +- tests/PhpCompatibility/schema067.phpt | 4 +- tests/PhpCompatibility/schema068.phpt | 4 +- tests/PhpCompatibility/schema069.phpt | 4 +- tests/PhpCompatibility/schema070.phpt | 4 +- tests/PhpCompatibility/schema074.phpt | 2 +- tests/PhpCompatibility/schema075.phpt | 6 +- tests/PhpCompatibility/schema076.phpt | 6 +- tests/PhpCompatibility/schema077.phpt | 6 +- tests/PhpCompatibility/schema1002.phpt | 6 +- tests/PhpCompatibility/schema1003.phpt | 2 +- tests/PhpCompatibility/schema1004.phpt | 2 +- tests/PhpCompatibility/schema1007.phpt | 6 +- tests/PhpCompatibility/schema1008.phpt | 22 +++++++ .../IsConsideredNullableTypeTest.php | 60 +++++++++++++++++++ .../Predicate/IsConsideredScalarTypeTest.php | 36 +++++++++++ 32 files changed, 214 insertions(+), 51 deletions(-) create mode 100644 src/Metadata/Predicate/IsConsideredNullableType.php create mode 100644 src/Metadata/Predicate/IsConsideredScalarType.php create mode 100644 tests/PhpCompatibility/schema1008.phpt create mode 100644 tests/Unit/Metadata/Predicate/IsConsideredNullableTypeTest.php create mode 100644 tests/Unit/Metadata/Predicate/IsConsideredScalarTypeTest.php diff --git a/src/Formatter/XsdTypeFormatter.php b/src/Formatter/XsdTypeFormatter.php index 9870cd2..a077ec2 100644 --- a/src/Formatter/XsdTypeFormatter.php +++ b/src/Formatter/XsdTypeFormatter.php @@ -4,6 +4,7 @@ namespace Soap\WsdlReader\Formatter; use Soap\Engine\Metadata\Model\XsdType; +use Soap\WsdlReader\Metadata\Predicate\IsConsideredNullableType; final class XsdTypeFormatter { @@ -11,7 +12,7 @@ public function __invoke(XsdType $xsdType): string { $meta = $xsdType->getMeta(); $isList = $meta->isList()->unwrapOr(false) && !$meta->isAlias()->unwrapOr(false); - $isNullable = $meta->isNullable()->unwrapOr(false); + $isNullable = (new IsConsideredNullableType())($meta); $min = $meta->minOccurs()->unwrapOr(1); $max = $meta->maxOccurs()->unwrapOr(1); diff --git a/src/Metadata/Predicate/IsConsideredNullableType.php b/src/Metadata/Predicate/IsConsideredNullableType.php new file mode 100644 index 0000000..250c03e --- /dev/null +++ b/src/Metadata/Predicate/IsConsideredNullableType.php @@ -0,0 +1,29 @@ +isNullable()->unwrapOr(false)) { + return true; + } + + if ($meta->isNil()->unwrapOr(false)) { + return true; + } + + if ($meta->isAttribute()->unwrapOr(false)) { + // If no 'use' is defined, XSD attributes get an implicit default "optional" value + if ($meta->use()->unwrapOr('optional') === 'optional') { + return true; + } + } + + return false; + } +} diff --git a/src/Metadata/Predicate/IsConsideredScalarType.php b/src/Metadata/Predicate/IsConsideredScalarType.php new file mode 100644 index 0000000..91d237d --- /dev/null +++ b/src/Metadata/Predicate/IsConsideredScalarType.php @@ -0,0 +1,15 @@ +isSimple()->unwrapOr(false) + || $meta->isAttribute()->unwrapOr(false); + } +} diff --git a/tests/PhpCompatibility/schema037.phpt b/tests/PhpCompatibility/schema037.phpt index d209ae3..12addb3 100644 --- a/tests/PhpCompatibility/schema037.phpt +++ b/tests/PhpCompatibility/schema037.phpt @@ -20,5 +20,5 @@ Methods: Types: > http://test-uri/:testType { string $str - @int $int + @?int $int } diff --git a/tests/PhpCompatibility/schema038.phpt b/tests/PhpCompatibility/schema038.phpt index 96d0fdc..fdc8eaa 100644 --- a/tests/PhpCompatibility/schema038.phpt +++ b/tests/PhpCompatibility/schema038.phpt @@ -21,5 +21,5 @@ Methods: Types: > http://test-uri/:testType { string $str - @int $int + @?int $int } diff --git a/tests/PhpCompatibility/schema039.phpt b/tests/PhpCompatibility/schema039.phpt index 85b8ab3..a958c60 100644 --- a/tests/PhpCompatibility/schema039.phpt +++ b/tests/PhpCompatibility/schema039.phpt @@ -23,5 +23,5 @@ Methods: Types: > http://test-uri/:testType { string $str - @int $int + @?int $int } diff --git a/tests/PhpCompatibility/schema040.phpt b/tests/PhpCompatibility/schema040.phpt index bd9ae72..6187acd 100644 --- a/tests/PhpCompatibility/schema040.phpt +++ b/tests/PhpCompatibility/schema040.phpt @@ -24,5 +24,5 @@ Methods: Types: > http://test-uri/:testType { string $str - @int $int + @?int $int } diff --git a/tests/PhpCompatibility/schema042.phpt b/tests/PhpCompatibility/schema042.phpt index 86929d7..2eb9c7d 100644 --- a/tests/PhpCompatibility/schema042.phpt +++ b/tests/PhpCompatibility/schema042.phpt @@ -21,5 +21,5 @@ Methods: Types: > http://test-uri/:testType extends int { int $_ - @int $int + @?int $int } diff --git a/tests/PhpCompatibility/schema043.phpt b/tests/PhpCompatibility/schema043.phpt index f39cb2c..32bbdd9 100644 --- a/tests/PhpCompatibility/schema043.phpt +++ b/tests/PhpCompatibility/schema043.phpt @@ -28,10 +28,10 @@ Methods: Types: > http://test-uri/:testType2 extends int { int $_ - @int $int + @?int $int } > http://test-uri/:testType extends testType2 { int $_ - @int $int - @int $int2 + @?int $int + @?int $int2 } diff --git a/tests/PhpCompatibility/schema044.phpt b/tests/PhpCompatibility/schema044.phpt index a123377..47fd099 100644 --- a/tests/PhpCompatibility/schema044.phpt +++ b/tests/PhpCompatibility/schema044.phpt @@ -21,5 +21,5 @@ Methods: Types: > http://test-uri/:testType extends int { int $_ - @int $int + @?int $int } diff --git a/tests/PhpCompatibility/schema045.phpt b/tests/PhpCompatibility/schema045.phpt index 2060d94..8ca2a21 100644 --- a/tests/PhpCompatibility/schema045.phpt +++ b/tests/PhpCompatibility/schema045.phpt @@ -28,10 +28,10 @@ Methods: Types: > http://test-uri/:testType2 extends int { int $_ - @int $int + @?int $int } > http://test-uri/:testType extends testType2 { int $_ - @int $int - @int $int2 + @?int $int + @?int $int2 } diff --git a/tests/PhpCompatibility/schema046.phpt b/tests/PhpCompatibility/schema046.phpt index 20f1350..0367955 100644 --- a/tests/PhpCompatibility/schema046.phpt +++ b/tests/PhpCompatibility/schema046.phpt @@ -28,10 +28,10 @@ Methods: Types: > http://test-uri/:testType2 extends int { int $_ - @int $int + @?int $int } > http://test-uri/:testType extends testType2 { int $_ - @int $int - @int $int2 + @?int $int + @?int $int2 } diff --git a/tests/PhpCompatibility/schema047.phpt b/tests/PhpCompatibility/schema047.phpt index 6ba31a0..f62f9dc 100644 --- a/tests/PhpCompatibility/schema047.phpt +++ b/tests/PhpCompatibility/schema047.phpt @@ -29,5 +29,5 @@ Types: } > http://test-uri/:testType extends testType2 { int $int - @int $int2 + @?int $int2 } diff --git a/tests/PhpCompatibility/schema048.phpt b/tests/PhpCompatibility/schema048.phpt index c74e1f8..1a7e0b7 100644 --- a/tests/PhpCompatibility/schema048.phpt +++ b/tests/PhpCompatibility/schema048.phpt @@ -28,10 +28,10 @@ Methods: Types: > http://test-uri/:testType2 extends int { int $_ - @int $int + @?int $int } > http://test-uri/:testType extends testType2 { int $_ - @int $int - @int $int2 + @?int $int + @?int $int2 } diff --git a/tests/PhpCompatibility/schema062.phpt b/tests/PhpCompatibility/schema062.phpt index 137f8de..b625bdc 100644 --- a/tests/PhpCompatibility/schema062.phpt +++ b/tests/PhpCompatibility/schema062.phpt @@ -21,5 +21,5 @@ Methods: Types: > http://test-uri/:testType extends int { int $_ - @int $int + @?int $int } diff --git a/tests/PhpCompatibility/schema065.phpt b/tests/PhpCompatibility/schema065.phpt index a89ba91..3b38ed2 100644 --- a/tests/PhpCompatibility/schema065.phpt +++ b/tests/PhpCompatibility/schema065.phpt @@ -17,6 +17,6 @@ Methods: Types: > http://test-uri/:testType { - @string $str - @int $int + @?string $str + @?int $int } diff --git a/tests/PhpCompatibility/schema066.phpt b/tests/PhpCompatibility/schema066.phpt index cda7198..1d84d17 100644 --- a/tests/PhpCompatibility/schema066.phpt +++ b/tests/PhpCompatibility/schema066.phpt @@ -17,6 +17,6 @@ Methods: Types: > http://test-uri/:testType { - @string $str - @int $int + @?string $str + @?int $int } diff --git a/tests/PhpCompatibility/schema067.phpt b/tests/PhpCompatibility/schema067.phpt index d4794fb..9929c88 100644 --- a/tests/PhpCompatibility/schema067.phpt +++ b/tests/PhpCompatibility/schema067.phpt @@ -17,6 +17,6 @@ Methods: Types: > http://test-uri/:testType { - @string $str - @int $int + @?string $str + @?int $int } diff --git a/tests/PhpCompatibility/schema068.phpt b/tests/PhpCompatibility/schema068.phpt index e315e11..034664c 100644 --- a/tests/PhpCompatibility/schema068.phpt +++ b/tests/PhpCompatibility/schema068.phpt @@ -17,6 +17,6 @@ Methods: Types: > http://test-uri/:testType { - @string $str - @int $int + @?string $str + @?int $int } diff --git a/tests/PhpCompatibility/schema069.phpt b/tests/PhpCompatibility/schema069.phpt index b4034a9..34c93a6 100644 --- a/tests/PhpCompatibility/schema069.phpt +++ b/tests/PhpCompatibility/schema069.phpt @@ -18,6 +18,6 @@ Methods: Types: > http://test-uri/:testType { - @string $str - @int $int + @?string $str + @?int $int } diff --git a/tests/PhpCompatibility/schema070.phpt b/tests/PhpCompatibility/schema070.phpt index da0a10d..a4bb76c 100644 --- a/tests/PhpCompatibility/schema070.phpt +++ b/tests/PhpCompatibility/schema070.phpt @@ -20,6 +20,6 @@ Methods: Types: > http://test-uri/:testType { - @string $str - @int $int + @?string $str + @?int $int } diff --git a/tests/PhpCompatibility/schema074.phpt b/tests/PhpCompatibility/schema074.phpt index 7026122..ba48ac8 100644 --- a/tests/PhpCompatibility/schema074.phpt +++ b/tests/PhpCompatibility/schema074.phpt @@ -21,5 +21,5 @@ Methods: Types: > http://test-uri/:testType { string $str - @int $int + @?int $int } diff --git a/tests/PhpCompatibility/schema075.phpt b/tests/PhpCompatibility/schema075.phpt index 502e0f9..3be4fa1 100644 --- a/tests/PhpCompatibility/schema075.phpt +++ b/tests/PhpCompatibility/schema075.phpt @@ -19,7 +19,7 @@ Methods: Types: > http://test-uri/:testType { - @int $int1 - @int $int2 - @int $int3 + @?int $int1 + @?int $int2 + @?int $int3 } diff --git a/tests/PhpCompatibility/schema076.phpt b/tests/PhpCompatibility/schema076.phpt index 8bc59d1..09749ff 100644 --- a/tests/PhpCompatibility/schema076.phpt +++ b/tests/PhpCompatibility/schema076.phpt @@ -19,7 +19,7 @@ Methods: Types: > http://test-uri/:testType { - @int $int1 - @int $int2 - @int $int3 + @?int $int1 + @?int $int2 + @?int $int3 } diff --git a/tests/PhpCompatibility/schema077.phpt b/tests/PhpCompatibility/schema077.phpt index b7b0f8a..225c884 100644 --- a/tests/PhpCompatibility/schema077.phpt +++ b/tests/PhpCompatibility/schema077.phpt @@ -19,7 +19,7 @@ Methods: Types: > http://test-uri/:testType { - @int $int1 - @int $int2 - @int $int3 + @?int $int1 + @?int $int2 + @?int $int3 } diff --git a/tests/PhpCompatibility/schema1002.phpt b/tests/PhpCompatibility/schema1002.phpt index 37b8bd6..4a47811 100644 --- a/tests/PhpCompatibility/schema1002.phpt +++ b/tests/PhpCompatibility/schema1002.phpt @@ -25,9 +25,9 @@ Methods: Types: > http://test-uri/:VoluntaryChangesType { ?Penalty $Penalty - @boolean $VolChangeInd + @?boolean $VolChangeInd } > http://test-uri/:Penalty { - @string $PenaltyType - @string $DepartureStatus + @?string $PenaltyType + @?string $DepartureStatus } diff --git a/tests/PhpCompatibility/schema1003.phpt b/tests/PhpCompatibility/schema1003.phpt index 45e7c43..2518ac7 100644 --- a/tests/PhpCompatibility/schema1003.phpt +++ b/tests/PhpCompatibility/schema1003.phpt @@ -30,5 +30,5 @@ Types: array, SpecialEquipPref> $SpecialEquipPref } > http://test-uri/:SpecialEquipPref { - @string $Action + @?string $Action } diff --git a/tests/PhpCompatibility/schema1004.phpt b/tests/PhpCompatibility/schema1004.phpt index 7bef49c..2b5531e 100644 --- a/tests/PhpCompatibility/schema1004.phpt +++ b/tests/PhpCompatibility/schema1004.phpt @@ -34,7 +34,7 @@ Types: > http://test-uri/:StringLength1to128 extends string > http://test-uri/:EmailType extends StringLength1to128 { StringLength1to128 $_ - @string $EmailType + @?string $EmailType } > http://test-uri/:VerificationType { ?EmailType $Email diff --git a/tests/PhpCompatibility/schema1007.phpt b/tests/PhpCompatibility/schema1007.phpt index f97e9d2..c814a19 100644 --- a/tests/PhpCompatibility/schema1007.phpt +++ b/tests/PhpCompatibility/schema1007.phpt @@ -36,7 +36,7 @@ Methods: Types: > http://test-uri/:LocationType extends string { string $_ - @string $LocationCode + @?string $LocationCode } > http://test-uri/:VerificationType { ?string $Email @@ -44,6 +44,6 @@ Types: } > http://test-uri/:StartLocation extends LocationType { string $_ - @string $LocationCode - @dateTime $AssociatedDateTime + @?string $LocationCode + @?dateTime $AssociatedDateTime } diff --git a/tests/PhpCompatibility/schema1008.phpt b/tests/PhpCompatibility/schema1008.phpt new file mode 100644 index 0000000..b616a90 --- /dev/null +++ b/tests/PhpCompatibility/schema1008.phpt @@ -0,0 +1,22 @@ +--TEST-- +SOAP XML Schema 66: Required Attribute +--FILE-- + + + + +EOF; +test_schema($schema,'type="tns:testType"'); +?> +--EXPECTF-- +Methods: + > test(testType $testParam): void + +Types: + > http://test-uri/:testType { + @?string $optional + @string $required + } diff --git a/tests/Unit/Metadata/Predicate/IsConsideredNullableTypeTest.php b/tests/Unit/Metadata/Predicate/IsConsideredNullableTypeTest.php new file mode 100644 index 0000000..0f04141 --- /dev/null +++ b/tests/Unit/Metadata/Predicate/IsConsideredNullableTypeTest.php @@ -0,0 +1,60 @@ + [ + (new TypeMeta()), + false, + ]; + yield 'nullable' => [ + (new TypeMeta())->withIsNullable(true), + true, + ]; + yield 'not-nullable' => [ + (new TypeMeta())->withIsNullable(false), + false, + ]; + yield 'nillable' => [ + (new TypeMeta())->withIsNil(true), + true, + ]; + yield 'not-nillable' => [ + (new TypeMeta())->withIsNil(false), + false, + ]; + yield 'default-attribute' => [ + (new TypeMeta())->withIsAttribute(true), + true, + ]; + yield 'optional-attribute' => [ + (new TypeMeta())->withIsAttribute(true)->withUse('optional'), + true, + ]; + yield 'required-attribute' => [ + (new TypeMeta())->withIsAttribute(true)->withUse('required'), + false, + ]; + yield 'prohibit-attribute' => [ + (new TypeMeta())->withIsAttribute(true)->withUse('prohibit'), + false, + ]; + } +} diff --git a/tests/Unit/Metadata/Predicate/IsConsideredScalarTypeTest.php b/tests/Unit/Metadata/Predicate/IsConsideredScalarTypeTest.php new file mode 100644 index 0000000..e08bac5 --- /dev/null +++ b/tests/Unit/Metadata/Predicate/IsConsideredScalarTypeTest.php @@ -0,0 +1,36 @@ + [ + (new TypeMeta()), + false, + ]; + yield 'simple' => [ + (new TypeMeta())->withIsSimple(true), + true, + ]; + yield 'attribute' => [ + (new TypeMeta())->withIsSimple(true), + true, + ]; + } +}