Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider nullability for properties #516

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
Loading