Make MenuLinkTreeElement's properties protected, add getters, add limited setters

Created on 16 July 2014, over 10 years ago
Updated 30 December 2023, 11 months ago

From #2301273-29: MenuLinkNG part2 (no UI or conversions): MenuLinkTree API and unit tests β†’ :

+++ b/core/lib/Drupal/Core/Menu/MenuLinkTreeElement.php
@@ -0,0 +1,128 @@
+  public $link;
...
+  public $subtree;
...
+  public $depth;
...
+  public $hasChildren;
...
+  public $inActiveTrail;
...
+  public $access;
...
+  public $options = array();

Allegedly there is a followup to convert (at the request of Dries) this to protected - and they are public cause of performance. However I think we should protect them first and look at performance implications later. Especially since the class does not make any use of some of these properties so we are just setting and using them from outside of the class.

To which dawehner and I responded:

These properties are public not for performance reasons. They're only public because this object is merely a simple value object. Before, this used to be an associative array. We basically want this to be the PHP equivalent of a C struct, to 1) ensure that all (and only) the expected key/value pairs exist and 2) ensure that each of the key/value pairs is fully documented (rather than the mess that we used to have). Adding getters and setters for each of these is utterly useless, and only creates unnecessary verbosity. I agree with the general rule of "ensure classes have interfaces and methods and protected properties, this helps when overriding things", but this is just a value object that will never be overridden. If that's still not sufficiently convincing, I'll fix this in a follow-up.

After which Alex Pott committed it with the following comment:

Thanks for answering my questions - I can see why the decision to use public properties has been made this way - if we want to change this we can do this is a follow up.

However, in the mean time, I was already working to do the conversion anyway, to prevent this patch from being slowed down. Posting the patch here in case Alex/Dries/… decide they want the properties to be protected anyway.

πŸ“Œ Task
Status

Needs work

Version

11.0 πŸ”₯

Component
Menu systemΒ  β†’

Last updated about 23 hours ago

Created by

πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024