Skip to content

Proxies and interceptors #59

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

Merged
merged 11 commits into from
Apr 8, 2019

Conversation

maderlock
Copy link
Contributor

Fix #18 New rule to ensure proxies and interceptors are never explicitly requested in constructors

@lenaorobei lenaorobei added the event: MageTestFest2019 MageTestFest contributions label Mar 11, 2019
*
* @var string
*/
protected $warningMessage = 'Proxies and interceptors MUST never be explicitly requested in constructors.';
Copy link
Contributor

@paliarush paliarush Mar 11, 2019

Choose a reason for hiding this comment

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

It would be better to split this sniff into two sniffs to comply with single responsibility principle.

Or make it more generic and extensible.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have concerns regarding your comment:

  1. This sniff covers one statement from Technical Guidelines and thats why it sounds logical to me to have this one rule.
  2. Splitting into two sniffs doesn't sound good in performance prospective (make same check twice).
  3. On the other hand, making it more generic sounds good to me. The name can be changed to DiscouragedDependenciesSniff (or something like this) and $warnNames property can be public and will be replaceable via ruleset.xml.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  1. On the other hand, making it more generic sounds good to me. The name can be changed to DiscouragedDependenciesSniff (or something like this) and $warnNames property can be public and will be replaceable via ruleset.xml.

I do like the sound of the latter - I didn't realise that this could be done. Nice :)

*/
private $warnNames = [
'proxy',
'plugin'
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably the task was not clear enough, the expectation was to prevent Interceptor classes being referenced directly. Interceptors are auto-generated classes.
It may make sense to run these tests against Magento codebase and see if Plugin reference detection also makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. I'll add interceptors first and then look at plugins as you suggest.

/**
* // Rule find: constructor use of proxy class
*/
class Foo
Copy link
Contributor

Choose a reason for hiding this comment

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

Proxy classes are the one which have \Proxy suffix. Interceptor classes always have \Interceptor suffix. These can be accurately detected and should raise error.

Plugin classes have arbitrary names and thus are difficult to catch. These rules may have accuracy issues and can raise warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As this is based on names of constructor types and variables there will be a false-positive issue. In discussion with @lenaorobei it was felt that keeping these as warnings was of utility despite this.

Copy link
Contributor

Choose a reason for hiding this comment

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

Even is it's bad naming it's worth pointing developers that class name with Interceptor and Proxy suffix is not a good idea.

Copy link
Contributor

@paliarush paliarush left a comment

Choose a reason for hiding this comment

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

Please address suggestions above.

@maderlock
Copy link
Contributor Author

maderlock commented Mar 11, 2019

To confirm from above:

  1. Should make naming more generic and move exact types matched into ruleset.xm
  2. Plugins should be removed from this sniff as this was not in the original ticket. Will look at the codeset and see if including plugins would be a useful addition.
  3. Interceptors should be added instead.

@lenaorobei
Copy link
Contributor

@maderlock

move exact types matched into ruleset.xm

Let them be in the sniff code. Ruleset will be an extension point (if needed in future).

@paliarush do you have additional comments?

@lenaorobei
Copy link
Contributor

@maderlock thanks for your contribution!

I just had quick discussion with @paliarush and we agreed following:

  1. This sniff should cover proxy and interceptor cases only. For plugins i'll create separate task.
  2. Please change the severity to 10 and type to error.
  3. Use more generic sniff name.
  4. Please add how to fix this issue to the error message: "Proxies and interceptors MUST never be explicitly requested in constructors. Remove redundant Interceptor/Proxy suffix" (need to dynamically detect this suffix).

@maderlock
Copy link
Contributor Author

  1. Please change the severity to 10 and type to error.

I'll do as you suggest, though I'm still a little concerned about false positives given the matching of variable names.

@maderlock
Copy link
Contributor Author

With the changes above it looks like we should no longer be detecting proxy/interceptor in variable names. The logic should now be:

  • If variable type in constructor signature has a suffix of Proxy or Interceptor (or is aliased to a class matching this pattern) then this produces an error

Should we allow classes with Proxy or Interceptor in the non-suffix position, e.g. /Vendor/Module/Proxy/SomethingElse - or should this be a warning or error?

@lenaorobei
Copy link
Contributor

If variable type in constructor signature has a suffix of Proxy or Interceptor (or is aliased to a class matching this pattern) then this produces an error

Yes, that's right.

Should we allow classes with Proxy or Interceptor in the non-suffix position, e.g. /Vendor/Module/Proxy/SomethingElse - or should this be a warning or error?

Non-suffix position should be fine. We dont need to trigger any warnings in this case.

To make sure everything works as expected, I suggest running this check against Magento codebase:
vendor/bin/phpcs --standard=Magento --sniffs=Magento.Classes.ConstructorProxyInterceptor /path/to/magento2/app/code. It will show possible edge cases.

@lenaorobei lenaorobei added the waiting for feedback Additional explanation needed label Mar 18, 2019
@maderlock
Copy link
Contributor Author

Sorry for the delay - I've been rather ill. I'll jump back on this when I can. Thanks for bearing with me.

@lenaorobei
Copy link
Contributor

@maderlock do you need any help with this PR?

@maderlock maderlock dismissed paliarush’s stale review March 29, 2019 15:23

Addressed issues by rewrite

Robert Egginton added 4 commits March 29, 2019 15:36
* develop:
  REPO-66: [EQP][Sniffs Consolidation] Deliver Magento Coding Standard to magento2ce
  Fixed name in ruleset
  Remove multiple empty line
  Fix line length sniff when using translations
  Made InsecureFunctionSniff in line with UnsecureFunctionsUsageTest
  Removed CyclomaticComplexity
  Removed DateTime sniff
  Removed DateTime sniff
  Removed DateTime sniff
  magento#67: Remove Magento2.Exceptions.Namespace
  Removed ObjectInstantiation sniff
  XssTemplateSniff does not detect some use cases
  [Enhancement] DiscouragedFunction rule improvement
  [Enhancement] DiscouragedFunction rule improvement
  Namespace conflict with core rules
  Removed unstable Annotation sniffs
@maderlock
Copy link
Contributor Author

Could not get properties passing from ruleset.xml, so I've had to ignore that change request. Otherwise I've made the requested changes and merged into the updated structure under Magento2 (rather than Magento).

I've tested the rule against the Magento codebase and the only errors that come out are valid ones, so I'm happy that it's working as expected (in addition to the tests passing).

Copy link
Contributor

@lenaorobei lenaorobei left a comment

Choose a reason for hiding this comment

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

Please address my minor suggestions.

Robert Egginton added 3 commits April 8, 2019 12:12
* develop:
  Added exclude for `_files` and `*Test.php` files
  Fixed test false-positive findings
  Update README.md
@maderlock
Copy link
Contributor Author

Minor suggestions addressed and latest develop merged

@lenaorobei lenaorobei merged commit 4c5a79e into magento:develop Apr 8, 2019
magento-devops-reposync-svc pushed a commit that referenced this pull request Sep 21, 2021
AC-1102: Static test to verify self-closing tags for non-void html elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
event: MageTestFest2019 MageTestFest contributions partners-contribution waiting for feedback Additional explanation needed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants