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