Errors::isErrorsExist()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 25 and the first side effect is on line 8.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Asymptix\core;
4
5
/*
6
 * Global fields values array.
7
 */
8
$_FIELDS = [];
9
10
/*
11
 * Global fields associated errors array.
12
 */
13
$_ERRORS = [];
14
15
/**
16
 * Form fields errors functionality.
17
 *
18
 * @category Asymptix PHP Framework
19
 * @author Dmytro Zarezenko <[email protected]>
20
 * @copyright (c) 2009 - 2016, Dmytro Zarezenko
21
 *
22
 * @git https://github.com/Asymptix/Framework
23
 * @license http://opensource.org/licenses/MIT
24
 */
25
class Errors {
26
27
    /**
28
     * Display error of script execution.
29
     *
30
     * @param string $errorMessage Message of the error.
31
     */
32
    public static function displayError($errorMessage, $fieldName = null) {
33
        if (!is_null($fieldName)) {
34
            return ('<label for="' . $fieldName . '" class="form-error">' . $errorMessage . '</label>');
35
        } else {
36
            return ('<span class="label label-danger pull-right form-error">' . $errorMessage . '</span>');
37
        }
38
    }
39
40
    /**
41
     * Display error for field if it's exist.
42
     *
43
     * @global array $_ERRORS List of fields errors.
44
     * @param string $fieldName Name of the field.
45
     */
46
    public static function displayErrorFor($fieldName) {
47
        global $_ERRORS;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
48
49
        if (self::isSetErrorFor($fieldName)) {
50
            return self::displayError($_ERRORS[$fieldName], $fieldName);
51
        }
52
53
        return "";
54
    }
55
56
    /**
57
     * Returns error message by field name if exists.
58
     *
59
     * @global array $_ERRORS Global list of fields errors.
60
     * @param string $fieldName Name of the field.
61
     *
62
     * @return string Error message.
63
     */
64
    public static function getError($fieldName) {
65
        global $_ERRORS;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
66
67
        if (self::isSetErrorFor($fieldName)) {
68
            return $_ERRORS[$fieldName];
69
        }
70
71
        return "";
72
    }
73
74
    /**
75
     * Test if error for field is exist.
76
     *
77
     * @global array $_ERRORS Global list of fields errors.
78
     * @param string $fieldName Name of the field.
79
     *
80
     * @return bool
81
     */
82
    public static function isSetErrorFor($fieldName) {
83
        global $_ERRORS;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
84
85
        return isset($_ERRORS[$fieldName]);
86
    }
87
88
    /**
89
     * Checks if some common errors exists.
90
     *
91
     * @global array $_ERRORS
92
     *
93
     * @return bool
94
     */
95
    public static function isErrorsExist() {
96
        global $_ERRORS;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
97
98
        return isset($_ERRORS['_common']) && !empty($_ERRORS['_common']);
99
    }
100
101
    /**
102
     * Returns common errors array.
103
     *
104
     * @global array $_ERRORS
105
     *
106
     * @return array
107
     */
108
    public static function getErrors() {
109
        global $_ERRORS;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
110
111
        return isset($_ERRORS['_common']) ? $_ERRORS['_common'] : [];
112
    }
113
114
    /**
115
     * Save error message text for a field in global errors list.
116
     *
117
     * @global array $_ERRORS Global list of fields errors.
118
     * @param string $fieldName Name of the field.
119
     * @param string $errorMessageText Text of the error message.
120
     */
121
    public static function saveErrorFor($fieldName, $errorMessageText) {
122
        global $_ERRORS;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
123
124
        $_ERRORS[$fieldName] = $errorMessageText;
125
    }
126
127
    /**
128
     * Save error message text for a field in global errors list.
129
     *
130
     * @global array $_ERRORS Global list of fields errors.
131
     * @param string $errorMessageText Text of the error message.
132
     */
133
    public static function saveError($errorMessageText) {
134
        global $_ERRORS;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
135
136
        if (!isset($_ERRORS['_common'])) {
137
            $_ERRORS['_common'] = [];
138
        }
139
        $_ERRORS['_common'][] = $errorMessageText;
140
    }
141
142
}
143