Deprecate MoveCurrentDateTimeDefaultInEntityToConstructorRector, drop force_nullable option - #504
Merged
Merged
Conversation
… force_nullable option The database-level default and the PHP-level default are not interchangeable, so moving the default into the constructor can produce buggy code. Also drop the force_nullable option of TypedPropertyFromToOneRelationTypeRector, as the relation can always be unset while the entity is being built.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
1) Deprecate
MoveCurrentDateTimeDefaultInEntityToConstructorRectorThe database-level default and the PHP-level default are not interchangeable. The rule moved the default into the constructor, which changes when the value is created — the database default is evaluated on INSERT by the database, the constructor default on object creation by PHP. That is a behavior change the rule cannot verify, and it needs manual care per entity.
use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity() */ class User { /** * @var DateTimeInterface - * @ORM\Column(type="datetime", nullable=false, options={"default"="now()"}) + * @ORM\Column(type="datetime", nullable=false) */ - private $when = 'now()'; + private $when; + + public function __construct() + { + $this->when = new \DateTime(); + } }The rule is removed from the
doctrine-code-qualityset and now throws on use, like the other deprecated rules.Its 3 exclusive helper services are removed as well:
ValueAssignFactory,ConstructorManipulator,ConstructorAssignPropertyAnalyzer.2) Drop
force_nullableoption ofTypedPropertyFromToOneRelationTypeRectorWe never know how the entity is used — it can always be constructed before the relation is set, so the property must be nullable. The option defaulted to
trueanyway; thefalsebranch produced a non-nullable typed property that fatals on read before assignment.use Doctrine\ORM\Mapping as ORM; class SimpleColumn { /** * @ORM\OneToOne(targetEntity="App\Company\Entity\Company") * @ORM\JoinColumn(nullable=false) */ - private $company; + private ?\App\Company\Entity\Company $company = null; }ToOneRelationPropertyTypeResolverloses the nullable detection it no longer needs.3) Cleanup
Removed leftover empty test config dir of the already-removed
AddReturnDocBlockToCollectionPropertyGetterByToManyAnnotationRector.