?
avatar NickSdot
NickSdot
30 Mar 2018

Steps to reproduce the issue

Use PHPStorm.
Include Joomla! as External Library

use \Joomla\CMS\Factory;
$app   = Factory::getApplication();

Expected result

No warning and highlighting.

Actual result

Warning and highlighting.
screen shot 2018-03-30 at 20 44 52

System information (as much as possible)

Joomla 3.8.6

Additional comments

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.

avatar NickSdot NickSdot - open - 30 Mar 2018
avatar joomla-cms-bot joomla-cms-bot - change - 30 Mar 2018
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 30 Mar 2018
avatar NickSdot NickSdot - change - 30 Mar 2018
The description was changed
avatar NickSdot NickSdot - edited - 30 Mar 2018
avatar trananhmanh89
trananhmanh89 - comment - 30 Mar 2018

Check here, we already have solution

#18014

avatar brianteeman brianteeman - change - 30 Mar 2018
Status New Closed
Closed_Date 0000-00-00 00:00:00 2018-03-30 13:29:41
Closed_By brianteeman
avatar brianteeman brianteeman - close - 30 Mar 2018
avatar roland-d
roland-d - comment - 30 Mar 2018

@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.

avatar NickSdot
NickSdot - comment - 30 Mar 2018

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.

avatar roland-d
roland-d - comment - 30 Mar 2018

@NickSdot There is no reason to re-open as this is not a Joomla issue. Yes, you don't have your doc block but you either add the docblock or enclose it in a try-catch. Regardless, the code is going to throw an exception if something occurs.

avatar NickSdot
NickSdot - comment - 30 Mar 2018

@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.

avatar mbabker
mbabker - comment - 30 Mar 2018

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:

  1. Add a @throws annotation to your doc block
use Joomla\CMS\Factory;

class MyClass
{
    /**
     * @throws \Exception
     */
    public function processDataFromRequest()
    {
        $app = Factory::getApplication();

        // Do stuff
    }
}
  1. Wrap the getApplication() call in a try/catch block
use 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).

avatar NickSdot
NickSdot - comment - 13 Apr 2018

@mbabker valuable answer as always, got it, thank you!

Add a Comment

Login with GitHub to post a comment