Php_codesniffer: Disallow class calls from arrays

Created on 25 Oct 2016  路  5Comments  路  Source: squizlabs/PHP_CodeSniffer

Let's say that I have the following code:

$myarray['class1'] = new MyClass();
$myarray['class2'] = new MyOtherClass();

I need to disallow someone from calling a function like this:

$result = $myarray['class1']->myFunction();

Is there any kind of sniff to do so?

Question

Most helpful comment

When you are saying Looks doable are you willing to work on it?

Sure. Contact me directly (e.g. over email) if interested.

All 5 comments

  1. The questions can be asked in Gitter chat.
  2. Not sure there is, because it looks very specific.

To generalize you want to disallow all constructs looking like:

$any_variable_name['any_string_key']->any_method(any_parameters);

Looks doable.

Yes.. That's the general idea.. most of the times the classes that are contained in the array are not Instances of the same Class and this messes autocomplete in many cases. Disallowing this kind of call the user will be enforced to assign the Class to a variable first, like:

/** AnyClass $anyClass */
$anyClass = $any_variable_name['any_string_key'];
$anyClass->any_method(any_parameters);

When you are saying Looks doable are you willing to work on it?

There is no current included sniff to do this. I personally don't have time to work on it

When you are saying Looks doable are you willing to work on it?

Sure. Contact me directly (e.g. over email) if interested.

Thank you for your offer. I have figured this out and I was able at last to create sniff on my own.

Thnx again.

Was this page helpful?
0 / 5 - 0 ratings