I'm currently trying to remove all errors and warnings I have in my project the Inspection tool from my PHPStorm give to me.
I encounter a snippet PHPStorm says "Unused private method _xxx" while it's actually used, but in a dynamical way. Here is a simplifyed snippet:
<?php
class A
{
private function _iAmUsed()
{
//Do Stuff...
}
public function run($whoAreYou)
{
$methodName = '_iAm' . $whoAreYou;
if (method_exists($this, $methodName)) {
$this->$methodName();
}
}
}
$a = new A();
$a->run('Used');
?>
In this snippet, PHPStorm will tell me "Unused private method _iAmUsed" while, in fact, it is used... How can I, by adding PHPDocs or something, whatever, make my IDE understand my method is actually used?
Note that I give to my "run" call, a static string, but we can imagine also this:
<?php
$a->run($_POST['whoYouAre']); //$_POST['whoYouAre'] == 'Used'
?>
Thanks a lot!
There are two methods for doing this. One is directly calling function by variable name using bracket and parameters and the other is by using call_user_func() Function but in both method variable name is to be used. call_user_func( $var );
If name of a variable has parentheses (with or without parameters in it) in front of it, PHP parser tries to find a function whose name corresponds to value of the variable and executes it. Such a function is called variable function. This feature is useful in implementing callbacks, function tables etc.
Variable functions ¶ PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it.
You need to instantiate (create) $newVar outside of the function first. Then it will be view-able by your other function. You see, scope determines what objects can be seen other objects. If you create a variable within a function, it will only be usable from within that function.
mark used methods in phpdoc as @used example
/**
* @uses _iAmUsed()
* @param string $whoAreYou
*/
public function run($whoAreYou)
{
$methodName = '_iAm' . $whoAreYou;
if (method_exists($this, $methodName)) {
$this->$methodName();
}
}
Add a noinspection
annotation above the method:
/** @noinspection PhpUnusedPrivateMethodInspection */
private function _iAmUsed()
{
//Do Stuff...
}
Or after running code analysis you can right-click any inspection in the results window and choose Suppress for statement to have PHPStorm add the proper annotation itself. For more information see http://www.jetbrains.com/phpstorm/webhelp/suppressing-inspections.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With