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

feat: Add support to php 8 attributes #2241

Closed
wants to merge 1 commit into from

Conversation

djpremier
Copy link

@djpremier djpremier commented Jul 16, 2021

@djpremier
Copy link
Author

Helps are very much welcome 🙂

*
* @return void
*/
public function __construct($on = 'update', $field = null, $value = null)
Copy link

Choose a reason for hiding this comment

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

Suggested change
public function __construct($on = 'update', $field = null, $value = null)
public function __construct(string $on = 'update', $field = null, $value = null)

Copy link

Choose a reason for hiding this comment

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

The other fields should probably be typed as well then? ?string

Copy link

Choose a reason for hiding this comment

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

Unfortunatly no, according to the PHPDoc, they can have multiple types, and union types is only available in PHP 8

/** @var string|array */
public $field;

/** @var mixed */
public $value;

*
* @return void
*/
public function __construct($on = 'update', $field = null, $value = null)
Copy link

Choose a reason for hiding this comment

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

Suggested change
public function __construct($on = 'update', $field = null, $value = null)
public function __construct(string $on = 'update', $field = null, $value = null)

*
* @return void
*/
public function __construct($logEntryClass = null)
Copy link

Choose a reason for hiding this comment

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

Suggested change
public function __construct($logEntryClass = null)
public function __construct(?string $logEntryClass = null)

Comment on lines +58 to +67
$fields = [],
$updatable = true,
$style = 'default',
$unique = true,
$unique_base = null,
$separator = '-',
$prefix = '',
$suffix = '',
$handlers = [],
$dateFormat = 'Y-m-d-H:i'
Copy link

Choose a reason for hiding this comment

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

Suggested change
$fields = [],
$updatable = true,
$style = 'default',
$unique = true,
$unique_base = null,
$separator = '-',
$prefix = '',
$suffix = '',
$handlers = [],
$dateFormat = 'Y-m-d-H:i'
array $fields = [],
bool $updatable = true,
string $style = 'default',
bool $unique = true,
?string $unique_base = null,
string $separator = '-',
string $prefix = '',
string $suffix = '',
array $handlers = [],
string $dateFormat = 'Y-m-d-H:i'

*
* @return void
*/
public function __construct($fieldName = 'deletedAt', $timeAware = false, $hardDelete = true)
Copy link

Choose a reason for hiding this comment

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

Suggested change
public function __construct($fieldName = 'deletedAt', $timeAware = false, $hardDelete = true)
public function __construct(string $fieldName = 'deletedAt', bool $timeAware = false, bool $hardDelete = true)

/**
* Annotation to identify the fields which manages the reference integrity
*/
const REFERENCE_INTEGRITY = 'Gedmo\\Mapping\\Annotation\\ReferenceIntegrity';
Copy link

Choose a reason for hiding this comment

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

Suggested change
const REFERENCE_INTEGRITY = 'Gedmo\\Mapping\\Annotation\\ReferenceIntegrity';
const REFERENCE_INTEGRITY = ReferenceIntegrity::class;

/**
* ReferenceIntegrityAction extension annotation
*/
const ACTION = 'Gedmo\\Mapping\\Annotation\\ReferenceIntegrityAction';
Copy link

Choose a reason for hiding this comment

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

Suggested change
const ACTION = 'Gedmo\\Mapping\\Annotation\\ReferenceIntegrityAction';
const ACTION = ReferenceIntegrityAction::class;

Copy link

@ndench ndench left a comment

Choose a reason for hiding this comment

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

Thanks for you contribution! I think all of @ker0x suggestions are valid, we might as well type the properties since the min supported version is 7.2.

*
* @return void
*/
public function __construct($on = 'update', $field = null, $value = null)
Copy link

Choose a reason for hiding this comment

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

The other fields should probably be typed as well then? ?string

/**
* {@inheritdoc}
*/
public function readExtendedMetadata($meta, array &$config)
Copy link

Choose a reason for hiding this comment

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

This appears to be copy pasted from \Gedmo\ReferenceIntegrity\Mapping\Driver\Annotation. Should it maybe extend the Annotation driver and overwrite the AnnotationReader? Or perhaps decorate it?

*
* @return void
*/
public function __construct($on = 'update', $field = null, $value = null)
Copy link
Member

Choose a reason for hiding this comment

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

This is a breaking change (removing the Annotation parent and changing the constructor signature). It would require a major release. Is it necessary, or can we find a backwards-compatible option?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's technically a break, but in terms of usage it should be fine because of the @NamedArgumentConstructor annotation: https://www.doctrine-project.org/projects/doctrine-annotations/en/1.13/custom.html#optional-constructors-with-named-parameters

So there is no need of extending Annotation class anymore.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a bit of a weird case. From a purely PHP API perspective, it is 100% a B/C break:

  • Class inheritance changes ($foo instanceof Doctrine\Common\Annotations\Annotation goes from passing to failing with this PR applied)
  • Constructor signature changes

From a practical perspective, it really starts to get murky. Gedmo\Mapping\Driver\AnnotationDriverInterface doesn't have a hard dependency on the Doctrine\Common\Annotations\Reader interface, so there isn't an assurance that the doctrine/annotations package is even being used to read the annotations. Even if there were, if someone is implementing the interface themselves and not using the readers or parser from that package (Doctrine\Common\Annotations\DocParser is where the logic to deal with the @NamedArgumentConstructor annotation lies), that reader might not be able to construct the annotation classes with the changed constructor signatures. Granted, the odds of someone using a custom annotation reader and NOT using their parser are pretty low, but even if Doctrine's interfaces were typehinted, you can't guarantee a reader is using that compatibility layer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for the explanation, so what we can do is to keep the array $data = [] as first parameter of the constructor and add the rest after it.

I've re-added it in #2253

Comment on lines +34 to +35
*
* @return void
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
*
* @return void

In all constructors

*
* @return void
*/
public function __construct($on = 'update', $field = null, $value = null)
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's technically a break, but in terms of usage it should be fine because of the @NamedArgumentConstructor annotation: https://www.doctrine-project.org/projects/doctrine-annotations/en/1.13/custom.html#optional-constructors-with-named-parameters

So there is no need of extending Annotation class anymore.

@art-cg
Copy link

art-cg commented Dec 6, 2021

Hi. @djpremier Thanks for starting the PR. Are there any news of the actual state?
I saw that 3.4.0 includes the first PHP8-Attributes
https://github.com/doctrine-extensions/DoctrineExtensions/releases/tag/v3.4.0

@franmomu
Copy link
Collaborator

franmomu commented Dec 6, 2021

Hi. @djpremier Thanks for starting the PR. Are there any news of the actual state? I saw that 3.4.0 includes the first PHP8-Attributes https://github.com/doctrine-extensions/DoctrineExtensions/releases/tag/v3.4.0

I've created #2363 to keep track of the needed work.

@franmomu
Copy link
Collaborator

franmomu commented Dec 19, 2021

Closing in favor of #2363, thanks for starting this @djpremier!

@franmomu franmomu closed this Dec 19, 2021
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.

None yet

8 participants