Skip to content

Commit

Permalink
Improve nullable formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Sep 29, 2023
1 parent 541792a commit 1cb51a7
Show file tree
Hide file tree
Showing 32 changed files with 214 additions and 51 deletions.
3 changes: 2 additions & 1 deletion src/Formatter/XsdTypeFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
namespace Soap\WsdlReader\Formatter;

use Soap\Engine\Metadata\Model\XsdType;
use Soap\WsdlReader\Metadata\Predicate\IsConsideredNullableType;

final class XsdTypeFormatter
{
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);

Expand Down
29 changes: 29 additions & 0 deletions src/Metadata/Predicate/IsConsideredNullableType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

namespace Soap\WsdlReader\Metadata\Predicate;

use Soap\Engine\Metadata\Model\TypeMeta;

final class IsConsideredNullableType
{
public function __invoke(TypeMeta $meta): bool
{
if ($meta->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;
}
}
15 changes: 15 additions & 0 deletions src/Metadata/Predicate/IsConsideredScalarType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace Soap\WsdlReader\Metadata\Predicate;

use Soap\Engine\Metadata\Model\TypeMeta;

final class IsConsideredScalarType
{
public function __invoke(TypeMeta $meta): bool
{
return $meta->isSimple()->unwrapOr(false)
|| $meta->isAttribute()->unwrapOr(false);
}
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema037.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ Methods:
Types:
> http://test-uri/:testType {
string $str
@int $int
@?int $int
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema038.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ Methods:
Types:
> http://test-uri/:testType {
string $str
@int $int
@?int $int
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema039.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ Methods:
Types:
> http://test-uri/:testType {
string $str
@int $int
@?int $int
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema040.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ Methods:
Types:
> http://test-uri/:testType {
string $str
@int $int
@?int $int
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema042.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ Methods:
Types:
> http://test-uri/:testType extends int {
int $_
@int $int
@?int $int
}
6 changes: 3 additions & 3 deletions tests/PhpCompatibility/schema043.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema044.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ Methods:
Types:
> http://test-uri/:testType extends int {
int $_
@int $int
@?int $int
}
6 changes: 3 additions & 3 deletions tests/PhpCompatibility/schema045.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions tests/PhpCompatibility/schema046.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema047.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ Types:
}
> http://test-uri/:testType extends testType2 {
int $int
@int $int2
@?int $int2
}
6 changes: 3 additions & 3 deletions tests/PhpCompatibility/schema048.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema062.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ Methods:
Types:
> http://test-uri/:testType extends int {
int $_
@int $int
@?int $int
}
4 changes: 2 additions & 2 deletions tests/PhpCompatibility/schema065.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Methods:

Types:
> http://test-uri/:testType {
@string $str
@int $int
@?string $str
@?int $int
}
4 changes: 2 additions & 2 deletions tests/PhpCompatibility/schema066.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Methods:

Types:
> http://test-uri/:testType {
@string $str
@int $int
@?string $str
@?int $int
}
4 changes: 2 additions & 2 deletions tests/PhpCompatibility/schema067.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Methods:

Types:
> http://test-uri/:testType {
@string $str
@int $int
@?string $str
@?int $int
}
4 changes: 2 additions & 2 deletions tests/PhpCompatibility/schema068.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Methods:

Types:
> http://test-uri/:testType {
@string $str
@int $int
@?string $str
@?int $int
}
4 changes: 2 additions & 2 deletions tests/PhpCompatibility/schema069.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ Methods:

Types:
> http://test-uri/:testType {
@string $str
@int $int
@?string $str
@?int $int
}
4 changes: 2 additions & 2 deletions tests/PhpCompatibility/schema070.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ Methods:

Types:
> http://test-uri/:testType {
@string $str
@int $int
@?string $str
@?int $int
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema074.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ Methods:
Types:
> http://test-uri/:testType {
string $str
@int $int
@?int $int
}
6 changes: 3 additions & 3 deletions tests/PhpCompatibility/schema075.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Methods:

Types:
> http://test-uri/:testType {
@int $int1
@int $int2
@int $int3
@?int $int1
@?int $int2
@?int $int3
}
6 changes: 3 additions & 3 deletions tests/PhpCompatibility/schema076.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Methods:

Types:
> http://test-uri/:testType {
@int $int1
@int $int2
@int $int3
@?int $int1
@?int $int2
@?int $int3
}
6 changes: 3 additions & 3 deletions tests/PhpCompatibility/schema077.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Methods:

Types:
> http://test-uri/:testType {
@int $int1
@int $int2
@int $int3
@?int $int1
@?int $int2
@?int $int3
}
6 changes: 3 additions & 3 deletions tests/PhpCompatibility/schema1002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema1003.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ Types:
array<int<1, 15>, SpecialEquipPref> $SpecialEquipPref
}
> http://test-uri/:SpecialEquipPref {
@string $Action
@?string $Action
}
2 changes: 1 addition & 1 deletion tests/PhpCompatibility/schema1004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions tests/PhpCompatibility/schema1007.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ Methods:
Types:
> http://test-uri/:LocationType extends string {
string $_
@string $LocationCode
@?string $LocationCode
}
> http://test-uri/:VerificationType {
?string $Email
?StartLocation $StartLocation
}
> http://test-uri/:StartLocation extends LocationType {
string $_
@string $LocationCode
@dateTime $AssociatedDateTime
@?string $LocationCode
@?dateTime $AssociatedDateTime
}
22 changes: 22 additions & 0 deletions tests/PhpCompatibility/schema1008.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
SOAP XML Schema 66: Required Attribute
--FILE--
<?php
include __DIR__."/test_schema.inc";
$schema = <<<EOF
<complexType name="testType">
<attribute name="optional" type="string" use="optional" />
<attribute name="required" type="string" use="required" />
</complexType>
EOF;
test_schema($schema,'type="tns:testType"');
?>
--EXPECTF--
Methods:
> test(testType $testParam): void

Types:
> http://test-uri/:testType {
@?string $optional
@string $required
}
60 changes: 60 additions & 0 deletions tests/Unit/Metadata/Predicate/IsConsideredNullableTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
declare(strict_types=1);

namespace Soap\WsdlReader\Test\Unit\Metadata\Predicate;

use PHPUnit\Framework\TestCase;
use Soap\Engine\Metadata\Model\TypeMeta;
use Soap\WsdlReader\Metadata\Predicate\IsConsideredNullableType;

final class IsConsideredNullableTypeTest extends TestCase
{
/**
* @dataProvider provideTests
*
*/
public function test_it_knows_if_a_type_is_considered_nullable(TypeMeta $meta, bool $expected): void
{
static::assertSame($expected, (new IsConsideredNullableType())($meta));
}

public static function provideTests()
{
yield 'empty' => [
(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,
];
}
}
Loading

0 comments on commit 1cb51a7

Please sign in to comment.