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

Mark non-intersecting types as nullable #562

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

veewee
Copy link
Contributor

@veewee veewee commented Jan 9, 2025

This PR marks non-intersecting types as null whilst using the IntersectDuplicateTypesStrategy.

This results in nullable properties (getters, ...) during code generation with a default value of null.
So when the item is not available and you do try to access the property, you won't get an uninitialized error but null instead.

For example

    /**
     * @var null | string
     */
    private ?string $Time = null;

    /**
     * @return null | string
     */
    public function getTime() : ?string
    {
        return $this->Time;
    }

This means that your code needs to validate wether the value is present or not (at all times) before using non intersecting properties.

@veewee
Copy link
Contributor Author

veewee commented Jan 9, 2025

@rauanmayemir

I'm moving my service to v4 and thought this might be a sensible change.
Does it impact your services?

@rauanmayemir
Copy link

Does it just set default null value for nullable types?

I'm not sure if it's just my generator config, but I already have that in place, nullable types are already marked e.g like this:

    /**
     * Document number
     *
     * @var null | string
     */
    protected ?string $DocumentNum = null;

    /**
     * @return null | string
     */
    public function getDocumentNum() : ?string
    {
        return $this->DocumentNum;
    }

    /**
     * @param null | string $DocumentNum
     * @return $this
     */
    public function setDocumentNum(?string $DocumentNum) : static
    {
        $this->DocumentNum = $DocumentNum;
        return $this;
    }

@veewee
Copy link
Contributor Author

veewee commented Jan 9, 2025

@rauanmayemir it depends on how you configured code generation.
For nullable types, the code generation automatically sets this null default value.
However, you can control to do this for all properties by specifying PropertyAssemblerOptions::optionalValue

This PR is about when there are duplicate type-names and when you choose to intersect those types.
For example:

DuplicateType:

  • a
  • b

DuplicateType:

  • a
  • c

In this case the intersection will:

DuplicateType:

  • a
  • b -> always nullable
  • c-> always nullable

I think your answer answered my question as well. :)

@rauanmayemir
Copy link

Indeed, I played with options and see that deliberately left out the optional value:

                    new Rules\AssembleRule(
                        new Assembler\PropertyAssembler(
                            Assembler\PropertyAssemblerOptions::create()
                                ->withVisibility(PropertyGenerator::VISIBILITY_PROTECTED)
//                                ->withOptionalValue()
                            ,
                        )
                    ),

It's odd that the default value is still applied.

@rauanmayemir
Copy link

Anyway, I can always tweak the generator and change the behavior if I need it to be different from default.

@veewee
Copy link
Contributor Author

veewee commented Jan 9, 2025

It's odd that the default value is still applied.

There is a overwrite during code generation for nullable types.
When a type is nullable, we always set the default to null (to avoid non initialized errors)

@veewee veewee merged commit 2ce8101 into phpro:v4.x Jan 9, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants