S
S
Stanislav2019-11-06 11:27:39
phpstorm
Stanislav, 2019-11-06 11:27:39

How to make PhpStorm treat JsonException as throwing json_decode exception?

The point is simple. There is a code, it has this:

try {
  $decoded_content = json_decode($content, true, 512, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
  ...
}

The problem is that PhpStorm does not consider that the json_decode function throws a JsonException, so the exception itself is grayed out and underlined with text like this: Exception 'JsonException' is never thrown in the corresponding try block.
Question: is it possible to somehow make PhpStorm think that, without making changes to its and php source files, it is natural that json_decode (and any other piece of code, this is just an example) still throws exceptions? Some phpDoc annotation, maybe, or something else.
PS: I know about jetbrains/phpstorm-stubs, I know about Analysis tab in File | settings | Languages ​​& Frameworks | PHP. Stubs are added, extensions are synchronized (there are definitely json among them), there are no ignored exceptions, the analysis nesting level is 3, and nothing helped.
PPS: I don't want to fork and make changes to jetbrains/phpstorm-stubs either.
PPPS: php is listed as 7.3 in settings. Installed locally of the same version. He is also listed as an interpreter.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2019-11-06
@stanislav-belichenko

either wait until they fix it, or create your own stub (work for a couple of minutes, you don’t need to fork anything).
create a .phpstorm.meta.php file in the project and put in it

the following code:
<?php

/**
 * (PHP 5 &gt;= 5.2.0, PECL json &gt;= 1.2.0)<br/>
 * Returns the JSON representation of a value
 *
 * @link https://php.net/manual/en/function.json-encode.php
 *
 * @param mixed $value   <p>
 *                       The <i>value</i> being encoded. Can be any type except
 *                       a resource.
 *                       </p>
 *                       <p>
 *                       All string data must be UTF-8 encoded.
 *                       </p>
 *                       <p>PHP implements a superset of
 *                       JSON - it will also encode and decode scalar types and <b>NULL</b>. The JSON standard
 *                       only supports these values when they are nested inside an array or an object.
 *                       </p>
 * @param int   $options [optional] <p>
 *                       Bitmask consisting of <b>JSON_HEX_QUOT</b>,
 *                       <b>JSON_HEX_TAG</b>,
 *                       <b>JSON_HEX_AMP</b>,
 *                       <b>JSON_HEX_APOS</b>,
 *                       <b>JSON_NUMERIC_CHECK</b>,
 *                       <b>JSON_PRETTY_PRINT</b>,
 *                       <b>JSON_UNESCAPED_SLASHES</b>,
 *                       <b>JSON_FORCE_OBJECT</b>,
 *                       <b>JSON_UNESCAPED_UNICODE</b>.
 *                       <b>JSON_THROW_ON_ERROR</b> The behaviour of these
 *                       constants is described on
 *                       the JSON constants page.
 *                       </p>
 * @param int   $depth   [optional] <p>
 *                       Set the maximum depth. Must be greater than zero.
 *                       </p>
 *
 * @throws JsonException
 * @return string|false a JSON encoded string on success or <b>FALSE</b> on failure.
 */
function json_encode( $value, $options = 0, $depth = 512 ) {}

/**
 * (PHP 5 &gt;= 5.2.0, PECL json &gt;= 1.2.0)<br/>
 * Decodes a JSON string
 *
 * @link https://php.net/manual/en/function.json-decode.php
 *
 * @param string $json    <p>
 *                        The <i>json</i> string being decoded.
 *                        </p>
 *                        <p>
 *                        This function only works with UTF-8 encoded strings.
 *                        </p>
 *                        <p>PHP implements a superset of
 *                        JSON - it will also encode and decode scalar types and <b>NULL</b>. The JSON standard
 *                        only supports these values when they are nested inside an array or an object.
 *                        </p>
 * @param bool   $assoc   [optional] <p>
 *                        When <b>TRUE</b>, returned objects will be converted into
 *                        associative arrays.
 *                        </p>
 * @param int    $depth   [optional] <p>
 *                        User specified recursion depth.
 *                        </p>
 * @param int    $options [optional] <p>
 *                        Bitmask of JSON decode options. Currently only
 *                        <b>JSON_BIGINT_AS_STRING</b>
 *                        is supported (default is to cast large integers as floats)
 *
 * <b>JSON_THROW_ON_ERROR</b> when passed this flag, the error behaviour of these functions is changed. The global error state is left untouched, and if an error occurs that would otherwise set it, these functions instead throw a JsonException
 * </p>
 *
 * @throws JsonException
 * @return mixed the value encoded in <i>json</i> in appropriate
 * PHP type. Values true, false and
 * null (case-insensitive) are returned as <b>TRUE</b>, <b>FALSE</b>
 * and <b>NULL</b> respectively. <b>NULL</b> is returned if the
 * <i>json</i> cannot be decoded or if the encoded
 * data is deeper than the recursion limit.
 */
function json_decode( $json, $assoc = false, $depth = 512, $options = 0 ) {}
result:
5dc3166be08d0938887803.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question