Completed
Branch Gutenberg/form-system (1dc3a2)
by
unknown
75:13 queued 66:52
created

JsonValidator::isValid()   F

Complexity

Conditions 17
Paths 384

Size

Total Lines 57

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
nc 384
nop 3
dl 0
loc 57
rs 2.0833
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace EventEspresso\core\services\validators;
4
5
use EE_Error;
6
7
// phpcs:disable PHPCompatibility.PHP.NewConstants.json_error_invalid_property_nameFound
8
// phpcs:disable PHPCompatibility.PHP.NewConstants.json_error_utf16Found
9
10
/**
11
 * Class JsonValidator
12
 * Tools for validating JSON data and handling errors
13
 *
14
 * @package EventEspresso\core\services\validators
15
 * @author  Brent Christensen
16
 * @since   4.9.70.p
17
 */
18
class JsonValidator
19
{
20
21
    /**
22
     * Call this method IMMEDIATELY after json_decode() and
23
     * it will will return true if the decoded JSON was valid,
24
     * or return false after adding an error if not valid.
25
     * The actual JSON file does not need to be supplied,
26
     * but details re: code execution location are required.
27
     * ex:
28
     * JsonValidator::isValid(__FILE__, __METHOD__, __LINE__)
29
     *
30
     * @param string $file
31
     * @param string $func
32
     * @param string $line
33
     * @return boolean
34
     * @since 4.9.70.p
35
     */
36
    public function isValid($file, $func, $line)
37
    {
38
        if (! defined('JSON_ERROR_RECURSION')) {
39
            define('JSON_ERROR_RECURSION', 6);
40
        }
41
        if (! defined('JSON_ERROR_INF_OR_NAN')) {
42
            define('JSON_ERROR_INF_OR_NAN', 7);
43
        }
44
        if (! defined('JSON_ERROR_UNSUPPORTED_TYPE')) {
45
            define('JSON_ERROR_UNSUPPORTED_TYPE', 8);
46
        }
47
        if (! defined('JSON_ERROR_INVALID_PROPERTY_NAME')) {
48
            define('JSON_ERROR_INVALID_PROPERTY_NAME', 9);
49
        }
50
        if (! defined('JSON_ERROR_UTF16')) {
51
            define('JSON_ERROR_UTF16', 10);
52
        }
53
        switch (json_last_error()) {
54
            case JSON_ERROR_NONE:
55
                return true;
56
            case JSON_ERROR_DEPTH:
57
                $error = ': Maximum stack depth exceeded';
58
                break;
59
            case JSON_ERROR_STATE_MISMATCH:
60
                $error = ': Invalid or malformed JSON';
61
                break;
62
            case JSON_ERROR_CTRL_CHAR:
63
                $error = ': Control character error, possible malformed JSON';
64
                break;
65
            case JSON_ERROR_SYNTAX:
66
                $error = ': Syntax error, malformed JSON';
67
                break;
68
            case JSON_ERROR_UTF8:
69
                $error = ': Malformed UTF-8 characters, possible malformed JSON';
70
                break;
71
            case JSON_ERROR_RECURSION:
72
                $error = ': One or more recursive references in the value to be encoded';
73
                break;
74
            case JSON_ERROR_INF_OR_NAN:
75
                $error = ': One or more NAN or INF values in the value to be encoded';
76
                break;
77
            case JSON_ERROR_UNSUPPORTED_TYPE:
78
                $error = ': A value of a type that cannot be encoded was given';
79
                break;
80
            case JSON_ERROR_INVALID_PROPERTY_NAME:
81
                $error = ': A property name that cannot be encoded was given';
82
                break;
83
            case JSON_ERROR_UTF16:
84
                $error = ': Malformed UTF-16 characters, possibly incorrectly encoded';
85
                break;
86
            default:
87
                $error = ': Unknown error';
88
                break;
89
        }
90
        EE_Error::add_error('JSON decoding failed' . $error, $file, $func, $line);
91
        return false;
92
    }
93
}
94