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

NullabilityColumnPropertyTypeResolver - default column nullable false #191

Closed

Conversation

jirinapravnik
Copy link

Issue #190

@TomasVotruba
Copy link
Member

Thanks 👍

Tests fixtures have to be updated too. You can do it easily by:

UT=1 vendor/bin/phpunit

@samsonasik
Copy link
Member

For note, this may be related with old issues duplicated for this:

https://github.com/rectorphp/rector/issues?q=is%3Aissue+TypedPropertyFromColumnTypeRector+is%3Aclosed

as noted at rectorphp/rector#7709 (comment)

@@ -25,7 +25,7 @@ class SimpleColumn
/**
* @ORM\Column(type="string")
*/
private ?string $name = null;
private string $name;
Copy link
Member

Choose a reason for hiding this comment

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

per noted at rectorphp/rector#7709 (comment) , the rule should be configurable instead, as except `#[ORM\Id], non-initiable property will got unitialized error quickly

Copy link
Author

@jirinapravnik jirinapravnik Jul 12, 2023

Choose a reason for hiding this comment

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

IMHO nullable:false Is default, so it should be also respected by property typehint. Otherwise it Is inconsistency between ORM definition and typehint.

IMHO this problem is only for ORM\Id. Wouldn't it be better to exclude for ORM\Id? And for others properties to respect nullable:false?

Copy link
Member

Choose a reason for hiding this comment

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

Entity ID is magic, that's why allowed to be typed even never assigned inside class, other properties are not, the entity usage can be abused and cause random initialized error.

As far as the rule is configurable to allow enable fully typed based on docblock (disable by default), it is ok.

But make a typed property based on doc is incorrect generally, we seen a lot of legacy app that rely on docblock and never initialized, changing to typed broke everything.

@jirinapravnik
Copy link
Author

@TomasVotruba I tested on local before, and the tests passed. Weird :-) Updated yet. UT=1 is briliant, I didn't know easy-testing.

Copy link
Member

@samsonasik samsonasik left a comment

Choose a reason for hiding this comment

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

The rule need to be configurable #191 (review)

to avoid unitialized error

@jirinapravnik jirinapravnik force-pushed the nullable-column-default branch 2 times, most recently from fb0973a to 24aac61 Compare July 14, 2023 07:15
Revert "update tests" commit 49c6af9.
@jirinapravnik
Copy link
Author

@samsonasik ok, I did it configurable with default true. It worked on old fixtures. I have problem with one phpstan rule and don't know how to get over it.


/**
* @see \Rector\Doctrine\Tests\CodeQuality\Rector\Property\TypedPropertyFromColumnTypeRector\TypedPropertyFromColumnTypeRectorTest
*/
final class TypedPropertyFromColumnTypeRector extends AbstractRector implements MinPhpVersionInterface
final class TypedPropertyFromColumnTypeRector extends AbstractRector implements MinPhpVersionInterface, AllowEmptyConfigurableRectorInterface
Copy link
Member

Choose a reason for hiding this comment

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

AllowEmptyConfigurableRectorInterface is removed at PR:

please use ConfigurableRectorInterface with default empty value instead.

@RobertMe
Copy link

I'm facing the same issue. I don't think the proposed configuration option really solves anything with regards to id columns. In my case I would love that Rector can add all the typehints automatically, but for the id columns I would need these to be nullable. So that would mean I either need to set the new option to false and manually fix all id colums, or set the setting to true which means keeping the current behaviour which I didn't want in the first place.

So IMO there should be a second setting which is only applied to the Id columns (might be true/false/null and in case of null use the other option).

@@ -31,7 +31,7 @@ public function __construct(
) {
}

public function isNullable(Property $property): bool
public function isNullable(Property $property, bool $defaultNullableColumn): bool
{
$nullableExpr = $this->attributeFinder->findAttributeByClassArgByName(

Choose a reason for hiding this comment

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

I myself are not sure about this (existing) code. This as it threads #[Column(type: ...)], without nullable, as if the attribute isn't set at all. In which case it would test for the @Column annotation. While the existence of the attribute IMO needs to use either the configured nullable or, when absent, the default/fallback, and not looking for the @Column annotation.

@hildebro
Copy link

@jirinapravnik are you planning on fixing the outstanding problems? otherwise I wouldn't mind opening a new PR to do it myself

@TomasVotruba
Copy link
Member

Hi @samsonasik , could you look into this one and finish so we have it main? I'd like to avoid staling the work that's done here.

Thanks 🙏

@samsonasik
Copy link
Member

@TomasVotruba I think this feature should not be added, except a user need to, and want to contribute to make this happen.

Unitialized property can make a bug very quickly without make it nullable, except doctrine ID that mostly magic.

@TomasVotruba
Copy link
Member

@samsonasik I see, that's why it got stale. Let's close this PR then, thanks

How about this one? #197

@samsonasik
Copy link
Member

still no from me, nullable on typed property is always a safe belt for migration on unitialized property, even user define nullable: false, it still can be nullable in real DB afaik.

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.

5 participants