phpro / soap-client

A general purpose SOAP client for PHP
MIT License
861 stars 175 forks source link

Set null as a default value for nullable properties #515

Closed lstrojny closed 6 months ago

lstrojny commented 6 months ago

Bug Report

Q A
BC Break yes/no
Version 3.20

Summary

PropertyAssembler should assign null as a default value for nullable properties.

Current behavior

PropertyAssembler can be configured to assume that all properties are nullable but it will not correctly check for nullability and only then assign null as a default.

How to reproduce

Use PropertyAssembler as is with a nullable property and the generated code will be private ?Type $prop;.

Expected behavior

Use PropertyAssembler as is with a nullable property and the generated code will be private ?Type $prop = null; to prevent "uninitialized property" errors.

lstrojny commented 6 months ago

This should do the trick:

$propertyGenerator = PropertyGenerator::fromArray([
                'name' => $property->getName(),
                'visibility' => $this->options->visibility(),
-                'omitdefaultvalue' => !$this->options->useOptionalValue(),
+                'omitdefaultvalue' => !$this->options->useOptionalValue() && !(new IsConsideredNullableType())($property->getMeta()),
            ]);
veewee commented 6 months ago

Thanks for reporting @lstrojny,

Could you give me an example in which scenario you get a 'uninitialized property' error? Is it in the Request or Response side or maybe in both?

I'm not opposed to having that fix, but I would love to understand where your error comes from exactly first.

lstrojny commented 6 months ago

It’s for a response with an element like this:

<xs:element name="SomeProp" type="tns:SomeType" minOccurs="0"/>

When I get 0 occurances of SomeProp, the generated PHP code will bail with "Typed property …::$SomeProp must not be accessed before initialization"

veewee commented 6 months ago

That could make sense indeed.

Care to provide a PR for this?

I assume the fix is rather omit = !(useOptionalValue || nullable) making it null if the property is null or if the configuration tells to add optional values.

lstrojny commented 6 months ago

@veewee done