Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FORM][PROPERTY/ACCESS][PHP 7.4] access to an uninitialized property #36051

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

Closed
abdel-geniusespro opened this issue Mar 12, 2020 · 2 comments
Closed

Comments

@abdel-geniusespro
Copy link

Symfony version(s) affected: 5.0.*

Description
Error when handling request using symfony/form : Typed property Car::$brandName must not be accessed before initialization.

How to reproduce
1 - create a class with typed property :

Class Car 
{
    /**
    * @var string
    */
    private string $brandName;

   public function getBrandName(): string
    {
        return $this->brandName;
    }

    public function setBrandName(string $brandName): self
    {
        $this->brandName = $brandName;

        return $this;
    }
}

2 - Create a form type for your class.

Class CarType extends AbstractType 
{
    /**
     * {@inheritDoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('brandName')
        ;
    }

    /**
     * {@inheritDoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Car::class,
        ]);
    }
}

3 - Handle your request

Class CarController extends AbstractController
{
    public function postCar(Request $request, FormFactoryInterface $formFactory): Response
    {
        $form = $formFactory->create(CarType::class);
        $form->handleRequest($request); // this will throw the error
    }
}

Possible Solution
Check if the property is initialized before accessing.

Additional context
After consulting trace, the error origin is here : https://github.com/symfony/property-access/blob/18617a8c26b97a262f816c78765eb3cd91630e19/PropertyAccessor.php#L382

@abdel-geniusespro abdel-geniusespro changed the title [FORM][PROPERT/ACCESS][PHP 7.4] access to an uninitialized property [FORM][PROPERTY/ACCESS][PHP 7.4] access to an uninitialized property Mar 12, 2020
@HeahDude
Copy link
Contributor

Hello, @abdel-geniusespro. Indeed, the same applies for a public property:

class Car
{
    public string $brandName;
}

But I'd argue that the above code is incorrect, since it is error prone independently of any Symfony component. Instead it should be written as:

class Car
{
    public string $brandName = '';
}

or

class Car
{
    public string $brandName;

    public function __construct(string $brandName)
    {
        $this->$brandName = $brandName;
    }
}

or accept nullable ?string.

So I'm not sure we should consider this a bug and fix it in core. IMHO we should not encourage writing such fragile code in the first place.

@xabbuh
Copy link
Member

xabbuh commented Mar 15, 2020

This is not a bug to me. It's inherent of an object that serves as the source for a form to be in invalid states and being accessed by the form in this invalid state. Your code needs to account for that or add custom means to improve the error handling (see also #36022).

I am going to close here as explained in #36022 (comment).

@xabbuh xabbuh closed this as completed Mar 15, 2020
fabpot added a commit that referenced this issue Mar 16, 2020
…lized properties (HeahDude)

This PR was merged into the 3.4 branch.

Discussion
----------

[PropertyAccess][DX] Improved errors when reading uninitialized properties

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | kinda
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36051
| License       | MIT
| Doc PR        | ~

An attempt to fix #36051 by providing better error messages when trying to read uninitialized properties either via calling a return-type-hinted method from PHP 7.0 or by accessing public-typed properties from PHP 7.4.

It would be nice to have a proper exception class in master.

Commits
-------

a71023b [PropertyAccess] Improved errors when reading uninitialized properties
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants