Skip to content

Commit

Permalink
Consider nullability for properties
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed May 16, 2024
1 parent 05bded4 commit fdae3d8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Phpro\SoapClient\Exception\AssemblerException;
use Laminas\Code\Generator\PropertyGenerator;
use Soap\Engine\Metadata\Model\TypeMeta;
use Soap\WsdlReader\Metadata\Predicate\IsConsideredNullableType;

/**
* Class PropertyAssembler
Expand Down Expand Up @@ -57,7 +58,8 @@ public function assemble(ContextInterface $context)
$propertyGenerator = PropertyGenerator::fromArray([
'name' => $property->getName(),
'visibility' => $this->options->visibility(),
'omitdefaultvalue' => !$this->options->useOptionalValue(),
'omitdefaultvalue' => !$this->options->useOptionalValue()
&& !(new IsConsideredNullableType())($property->getMeta()),
]);

if ($this->options->useDocBlocks()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,35 @@ class MyType
$this->assertEquals($expected, $code);
}

/**
* @test
*/
function it_assembles_property_with_null()
{
$assembler = new PropertyAssembler(
PropertyAssemblerOptions::create()
);
$context = $this->createContextWithNullableType();
$assembler->assemble($context);
$code = $context->getClass()->generate();
$expected = <<<CODE
namespace MyNamespace;
class MyType
{
/**
* Type specific docs
*
* @var null | string
*/
private ?string \$prop1 = null;
}
CODE;

$this->assertEquals($expected, $code);
}


/**
* @return PropertyContext
Expand Down Expand Up @@ -295,4 +324,19 @@ private function createContextWithLongType()
], new TypeMeta());
return new PropertyContext($class, $type, $property);
}

/**
* @return PropertyContext
*/
private function createContextWithNullableType()
{
$class = new ClassGenerator('MyType', 'MyNamespace');
$type = new Type('MyNamespace', 'MyType', [
$property = Property::fromMetaData('ns1', new MetaProperty('prop1', XsdType::guess('string')->withMeta(
static fn (TypeMeta $meta): TypeMeta => $meta->withDocs('Type specific docs')->withIsNullable(true)
))),
], new TypeMeta());

return new PropertyContext($class, $type, $property);
}
}

0 comments on commit fdae3d8

Please sign in to comment.