magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

[Question] Have traits been considered for the Interceptor classes?

Open ghost opened this issue 11 years ago • 14 comments

The auto generated Interceptor classes currently duplicate the code when they get generated. Since Magento is now 5.4+ have traits been considered to bring the common functionality into the generated classes?

It would also make this code https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php#L170 easier to discover, read and maintain.

ghost avatar Jan 16 '15 08:01 ghost

Generated code doesn't lead to maintainance problems because its logic is in single place. Traits would not bring any value but would just add one more file to interceptor code.

antonkril avatar Jan 16 '15 10:01 antonkril

Does this answer your question? Can we close the issue?

antonkril avatar Feb 11 '15 16:02 antonkril

@antonkril I think you might have misunderstood my question.

My comment in regards to maintainability, readability and so forth is about the generating code not the generated code. I haven't yet been able to run the performance tests however this is what I am referring to:

For example this code here https://github.com/fooman/magento2/compare/interceptor-as-trait#diff-7fea50965bcc6aeb72cc4f333e0f9912R1 is easier to read than this https://github.com/fooman/magento2/compare/interceptor-as-trait#diff-4c790e1d84b8acca1907cade0a2ae3d7L122

The code can also be subjected to code analysis plus the trait can be discovered from the generated code.

ghost avatar Feb 16 '15 00:02 ghost

The trait approach is more developer friendly as you can directly hack in the trait and try things out.

:+1: for @fooman

SchumacherFM avatar Feb 16 '15 01:02 SchumacherFM

If I understand it correctly, its about the shared code the generator adds to many classes, which is always the same. Not knowing much of the code here, but does it change behavior if someone has an own __sleep method defined in a class?

Flyingmana avatar Feb 16 '15 01:02 Flyingmana

@Flyingmana the Interceptor classes, living under var/generated, are autogenerated here https://github.com/fooman/magento2/blob/master/lib/internal/Magento/Framework/Interception/Code/Generator/Interceptor.php#L205

The generating code copies over the public methods from your plugin code but excludes __sleep,__wakeup and __clone, so with current behaviour you would not be able to have your own implementations of these.

General trait behaviour would be The precedence order is that methods from the current class override Trait methods, which in turn override methods from the base class.

ghost avatar Feb 16 '15 04:02 ghost

:+1: for trait approach. Traits are there, they make code more readable/maintainable therefore we should use it.

Marko-M avatar Feb 16 '15 07:02 Marko-M

@fooman we considered template approach for code generation. With templates Interceptor code would be in separate file and generator would be simpler. But we considered this low priority task because 3PDs are not supposed to work with this code anyway.

Traits are for code reuse, code can be reused by aggregation but we kept that code in interceptor class to make it easier to debug interceptor code (3PD will rarely/never encounter generator code, but will encounter Interceptor code quite often during debugging).

@Flyingmana Intercetpor calls parent::__sleep() so it will be executed.

antonkril avatar Feb 16 '15 11:02 antonkril

@antonkril In reply to this comment:

But we considered this low priority task because 3PDs are not supposed to work with this code anyway.

3PD = third party developers?

While it might not be envisaged that this code will be changed by third parties Interceptors are one of the major new features of Magento 2 and developers would/should be working all the time with this code.

As this is a low priority on your end: If I finish the pull request and provided there is no major impact on performance what is the chance of this being accepted?

ghost avatar Feb 16 '15 21:02 ghost

3PDs (yes, third party developers) will be working with Interceptors frequently (so we try to keep them simple) but not with their generation.

Refactoring of generation is not high priority but Interceptors should stay simple. So in my opinion any pull request that spreads Interceptor code should only be considered if it has performance improvement.

But this question is not important (two options are not that different and I don't think there will be any performance difference) and trait version seems to have some support in community so we can consider it.

antonkril avatar Feb 17 '15 09:02 antonkril

Update: Trait implementation is considered as a possible way of simplifying debugging of intercepted code

antonkril avatar Mar 12 '15 09:03 antonkril

Guys, can we consider this question answered?

vpelipenko avatar Apr 21 '15 13:04 vpelipenko

from my point of view: yes

Flyingmana avatar Apr 21 '15 13:04 Flyingmana

Hi @fooman. Thank you for your report. The issue has been fixed in magento/graphql-ce#997 by @atwixfirster in 2.3-develop branch Related commit(s):

The fix will be available with the upcoming 2.3.4 release.

magento-engcom-team avatar Oct 13 '19 01:10 magento-engcom-team