Use PHPStorm.
Include Joomla! as External Library
use \Joomla\CMS\Factory;
$app = Factory::getApplication();
No warning and highlighting.
Joomla 3.8.6
Not sure if this really is a Joomla! issue cause there is actually a throws tag for the function
But because it's a bit annoying, I am just curious if this is to solve by the core, the PHPStorm Plugin for Joomla!, PHPStorm itself or if I myself doing something wrong.
Low prio, of course.
Labels |
Added:
?
|
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2018-03-30 13:29:41 |
Closed_By | ⇒ | brianteeman |
@NickSdot This is not a Joomla issue nor a PhpStorm issue. PhpStorm is correctly reporting that the function you are calling is going to throw an exception. You need to either place your code in a try/catch block as to deal with the possible exception or annotate with an @throws
in your docblock.
Thanks for your answer @trananhmanh89, but it is not about type hinting itself.
Do I miss something or did you misunderstood my question?
Thanks @roland-d for your answer, but I think it also not is related as well to what I tried to explain.
Let's say there is not a own function in my code.
Just place:
use \Joomla\CMS\Factory; $app = Factory::getApplication();
In a blank index.php of a template. Of course I will not have a own doc block for this.
But still I get the highlighting and warning. Got me?
@brianteeman
Please re-open. Answer was not related in my opinion.
@roland-d
In conclusion this means that theoretically in every of the 1178 occurrences of Factory::getApplication() in the Joomla! core itself, there must be another try-catch added?
It's not only about having this in my own code.
It's just everywhere, where I simply can not add a try-catch because it's not my code.
Thanks for one more answer, I really want to understand this.
The inspection is basically saying "you are calling this method which may throw an exception but you are not wrapping this method call in a try/catch block nor is the method from which you're making the call documented to throw an exception".
To appease PhpStorm, you'd need to do one of two things:
@throws
annotation to your doc blockuse Joomla\CMS\Factory;
class MyClass
{
/**
* @throws \Exception
*/
public function processDataFromRequest()
{
$app = Factory::getApplication();
// Do stuff
}
}
getApplication()
call in a try/catch blockuse Joomla\CMS\Factory;
class MyClass
{
/**
* @throws \Exception
*/
public function processDataFromRequest()
{
try {
$app = Factory::getApplication();
} catch (Exception $e) {
// Error handling
}
// Do stuff
}
}
You can also choose to ignore the inspection's warnings, something I commonly do on several projects myself (I have code where I purposely am not catching exceptions thrown by another class and I follow a coding style rule where I'm not adding doc blocks to my class methods unless the doc block can provide information that isn't already easily available from the method name and signature, so I don't have a doc block to annotate a @throws
line in).
Check here, we already have solution
#18014