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

Check is nullable doctrine annotation #324

Merged

Conversation

marcelthole
Copy link
Contributor

fix #305

@@ -39,7 +39,7 @@ class DoctrineManyToOne
* @ORM\ManyToOne(targetEntity="Rector\Doctrine\Tests\CodeQuality\Rector\Property\TypedPropertyFromToOneRelationTypeRector\Source\Company", inversedBy="userBuildings")
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=false)
*/
private ?\Rector\Doctrine\Tests\CodeQuality\Rector\Property\TypedPropertyFromToOneRelationTypeRector\Source\Company $company3 = null;
private \Rector\Doctrine\Tests\CodeQuality\Rector\Property\TypedPropertyFromToOneRelationTypeRector\Source\Company $company3;
Copy link
Member

@TomasVotruba TomasVotruba Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would crash, as this property is not defined via constructor. Often Symfony/forms use magic setters to set value here and they would crash as well:

https://3v4l.org/lO6Kg

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that's true. But isn't this than the issue of the framework or the usage?

So yeah, if the field is nullable: false i would say the property itself should also be not null. Doctrine will load the property correctly. if someone will use a partial load of a entity this will also break.

But what is the alternative? Mark the properties as nullable and look for getter and setter and mark them as not nullable?

or what do you think about an configurable option for that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code has to be valid after migration, and we have to take context of use into an account.

Here e.g. the property would have to be set at least in the constructor to make it typed.

In case constructor is present and property is set, the ? can be removed. I think best way would be to make this optionally configurable as you suggest 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first time i used this configured rules :D
Hopefully i implemented them correct. Sadly i didn't find a good way to show it in the automated docs which option is available, which one is the default behavior and how the configuration will change the output.
I only added two examples for the Rule and add a new fixture folder for the configured tests

@marcelthole marcelthole force-pushed the check-is-nullable-doctrine-annotation branch from e4ed6cc to 59142f6 Compare July 1, 2024 16:00
@marcelthole marcelthole force-pushed the check-is-nullable-doctrine-annotation branch from 59142f6 to 7c3e131 Compare July 1, 2024 18:14
{
public const FORCE_NULLABLE = 'force_nullable';

private bool $forceNullable = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default value should be false, not to change current behavior and create BC break.

@TomasVotruba
Copy link
Member

I love it, very good work 👍

@marcelthole
Copy link
Contributor Author

marcelthole commented Jul 1, 2024 via email

@TomasVotruba
Copy link
Member

Ah, you're right. I assumed "force" is the more strict case :)

All good, thank you 👏

@TomasVotruba TomasVotruba merged commit 659a29d into rectorphp:main Jul 1, 2024
6 checks passed
@marcelthole marcelthole deleted the check-is-nullable-doctrine-annotation branch July 2, 2024 13:43
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.

TypedPropertyFromToOneRelationTypeRector should taken into account parent definition
2 participants