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

doc: Add type to properties #2652

Merged
merged 5 commits into from
Jun 26, 2024
Merged

doc: Add type to properties #2652

merged 5 commits into from
Jun 26, 2024

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Jun 24, 2024

Q A
Type doc
BC Break no
Fixed issues -

Summary

Using types properties is recommended to improve type safety.

I still have to check that everything works properly if the properties are not set with a default value. Because when there's no type, the value is null by default.

@@ -281,7 +281,7 @@ Optional attributes:
],
defaultDiscriminatorValue: 'user',
)]
private $creator;
private User|Author $creator;
Copy link
Member Author

Choose a reason for hiding this comment

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

I hope I didn't make a mistake. I find that the type helps to understand the content of the variable here.

Copy link
Member

Choose a reason for hiding this comment

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

This looks correct to me based on the discriminator map.

@@ -417,7 +417,7 @@ to be considered when looking up one-to-one relationships:
cascade: 'all',
storeAs: 'id',
)]
private $items;
private Collection $items;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is a ReferenceOne. The variable is plural so I supposed it's a Collection. But I would have expected a single Item. I'll check.

Copy link
Member

Choose a reason for hiding this comment

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

This should be:

Suggested change
private Collection $items;
private Item $item;

Comment on lines +75 to +78
The properties' type hints must be ``Collection``, and cannot be
``ArrayCollection``. When the ``User`` object is retrieved from the database,
the properties ``$addresses`` and ``$articles`` are instances of
``Doctrine\ODM\MongoDB\PersistentCollection`` to track changes.
Copy link
Member Author

Choose a reason for hiding this comment

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

PersistentCollection is not internal, so I think we can mention this class.

@@ -85,11 +85,11 @@ follows:
{
// ...

private $_listeners = [];
Copy link
Member Author

Choose a reason for hiding this comment

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

Prefixing private properties with an underscore is so PHP 4.

@@ -31,7 +31,7 @@ class:
public function convertToPHPValue($value): \DateTime
{
// This is called to convert a Mongo value to a PHP representation
return new \DateTime('@' . $value->sec);
return $value->toDateTime();
Copy link
Member Author

Choose a reason for hiding this comment

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

It's so much simpler.

Copy link
Member

Choose a reason for hiding this comment

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

Unrelated, but we should try to come up with a different example, as this only reproduces the DateTime type. I've previously used the example of storing the time zone alongside the UTCDateTime instance and applying that to the date type when reading.

Copy link
Member Author

@GromNaN GromNaN Jun 25, 2024

Choose a reason for hiding this comment

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

Done: #2654

@GromNaN GromNaN marked this pull request as ready for review June 24, 2024 13:54
}

.. note::
Copy link
Member

Choose a reason for hiding this comment

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

Good to have this documented 👍

@@ -281,7 +281,7 @@ Optional attributes:
],
defaultDiscriminatorValue: 'user',
)]
private $creator;
private User|Author $creator;
Copy link
Member

Choose a reason for hiding this comment

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

This looks correct to me based on the discriminator map.

@@ -417,7 +417,7 @@ to be considered when looking up one-to-one relationships:
cascade: 'all',
storeAs: 'id',
)]
private $items;
private Collection $items;
Copy link
Member

Choose a reason for hiding this comment

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

This should be:

Suggested change
private Collection $items;
private Item $item;

@@ -31,7 +31,7 @@ class:
public function convertToPHPValue($value): \DateTime
{
// This is called to convert a Mongo value to a PHP representation
return new \DateTime('@' . $value->sec);
return $value->toDateTime();
Copy link
Member

Choose a reason for hiding this comment

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

Unrelated, but we should try to come up with a different example, as this only reproduces the DateTime type. I've previously used the example of storing the time zone alongside the UTCDateTime instance and applying that to the date type when reading.


#[Field(notSaved: true)]
public $score;
public int $score;
Copy link
Member

Choose a reason for hiding this comment

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

The type of the field defaults to string, but I agree that the property name indicates an integer. With that in mind, we should add type: 'int' to the field attribute.

#[ReferenceMany(targetDocument: Account::class)]
private $accounts = [];
private Collection $accounts;
Copy link
Member

Choose a reason for hiding this comment

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

This may apply to multiple classes, but we should default to initialising this with an empty ArrayCollection instance so it doesn't have to happen in the constructor:

Suggested change
private Collection $accounts;
private Collection $accounts = new ArrayCollection();

Copy link
Member Author

Choose a reason for hiding this comment

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

That's not allowed unless CPP is used. Adding the constructor.

Copy link
Member

Choose a reason for hiding this comment

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

Yep, this is still PHP 😁

Copy link
Member

Choose a reason for hiding this comment

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

OMFG - I could've sworn that you can initialise properties like that in general now. Boo :(

@GromNaN GromNaN requested a review from alcaeus June 25, 2024 10:51
Copy link
Member

@alcaeus alcaeus left a comment

Choose a reason for hiding this comment

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

One minor change to namespaces, but LGTM aside from that

docs/en/reference/introduction.rst Outdated Show resolved Hide resolved
#[ReferenceMany(targetDocument: Account::class)]
private $accounts = [];
private Collection $accounts;
Copy link
Member

Choose a reason for hiding this comment

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

OMFG - I could've sworn that you can initialise properties like that in general now. Boo :(

Co-authored-by: Andreas Braun <git@alcaeus.org>
@GromNaN GromNaN merged commit 1408466 into doctrine:2.9.x Jun 26, 2024
17 of 18 checks passed
@GromNaN GromNaN deleted the doc-prop-type branch June 26, 2024 09:40
@GromNaN GromNaN added this to the 2.9.0 milestone Jun 27, 2024
alcaeus added a commit that referenced this pull request Sep 6, 2024
* 2.9.x: (24 commits)
  Fix typo in code example (#2670)
  Label PRs about GH actions with "CI" (#2632)
  Review basic mapping (#2668)
  Fix wording (#2667)
  Add native type to private properties and final classes (#2666)
  Review and add tests on `ResolveTargetDocumentListener` (#2660)
  Remove soft-delete-cookbook (#2657)
  doc: Remove wakeup and clone cookbook (#2663)
  Modernize generated code for Hydrators (#2665)
  Add tests for introduction (#2664)
  doc: Review mapping ORM and ODM cookbook (#2658)
  doc: Review cookbook on blending ORM and ODM (#2656)
  doc: Review and test validation cookbook (#2662)
  Update custom mapping example (#2654)
  doc: Review Simple Search Engine Cookbook (#2659)
  doc: Add cookbook about embedding referenced documents using $lookup (#2655)
  doc: Add type to properties (#2652)
  doc: Review custom collections and repository docs (#2653)
  doc: Review Getting Started (#2650)
  Move annotations-reference to attributes-reference (#2651)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants