Skip to content

[TwigComponent] exposePublicProps + Unitialized public property = Exception  #1920

Closed as not planned
@smnandre

Description

@smnandre

TL;DR; i regret the revert and think we should throw an expection


So...

I think i now regret to have reverted #1780 (after #1909 but also messages on Slack)

On that moment, i just wanted to limit / avoid pressure, stress, and conflict.

But now... i'm more than ever convinced we should (must?) throw an exception when a component has an unitialized public property and exposePublicProps = true

Why is this not a BC break

It has never been said one could use unitialized property to not expose one inside the template.

Source code

In the source code of the AsTwigComponent attribute

        /**
         * Whether to expose every public property as a Twig variable.
         *
         * @see https://symfony.com/bundles/ux-twig-component#passing-data-props-into-your-component
         */
        private bool $exposePublicProps = true,

Please note the every here.

Documentation

In the online documentation, you can read the following

In the template, the Alert instance is available via the this variable and public properties are available directly. Use them to render the two new properties:

No mention to "initialized". And if you look, the example is matching our discussed scenario

  #[AsTwigComponent]
  class Alert
  {
     public string $message;

Then, it explicitely gives a method if you don't want to expose them, and access them via this. object (your component)

You can disable exposing public properties for a component. When disabled, this.property must be used:

#[AsTwigComponent(exposePublicProps: false)]
class Alert
{
    // ...
}

Why we should throw an Error

Well, we cannot expose a "undefined" in Twig. Every public property must be given to Twig.

Responsability

If a component exposes a property, one should be able to call them without having to fear any "undefined".

And if the message is typed "string", there is no reason to test this in the template.

The responsability to pass this value must be set on the person calling the component.

And if you need a string|null value... well, you also can make the property nullable.

DX / Debug

Same, in the debug command, we would not say "this prop is unitialized" but only its type and eventually default value.


Now what ?

So i really think we made the good choice initially.

We now need to decide what we want to do now... two options:
1- keeping the current status (that caused and still causes issues), while not enforcing the contract we documented
2- explaining people they used an undocumented "gray area", and showing them solution to change/adapt (as the change is possible on this direction, while the other one maintain a problematic situation)

--

I do think the 2 is the way to go, and i'm open to any feedback on this :)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions