Passed
Push — master ( 5fda7a...160783 )
by George
03:22
created

Error::getErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
namespace JsonTable\Analyse;
3
4
/**
5
 * Details of errors found during an analysis.
6
 *
7
 * @package    JSON table
8
 */
9
class Error
10
{
11
    /**
12
     * @static
13
     *
14
     * @var array   Error messages.
15
     */
16
    private static $errors = [];
17
18
19
    /**
20
     * Reset the errors.
21
     */
22 81
    public static function reset()
23
    {
24 81
        self::$errors = [];
25 81
    }
26
27
28
    /**
29
     * Get all errors that have been set during the analysis.
30
     *
31
     * @return  array   The error messages.
32
     */
33 76
    public function getErrors()
34
    {
35 76
        $errorsFormatted = [];
36
37
        // Format the error type with the number of errors of that type.
38 76
        foreach (self::$errors as $errorType => $errors) {
39 50
            $errorTypeFormatted = sprintf($errorType, count($errors));
40 50
            $errorsFormatted[$errorTypeFormatted] = $errors;
41 76
        }
42
43 76
        return $errorsFormatted;
44
    }
45
46
47
    /**
48
     * Add an error message.
49
     *
50
     * @param   string  $type   The type of error.
51
     * @param   string  $error  The error message (or field).
52
     *
53
     * @return  void
54
     */
55 50
    public function setError($type, $error)
56
    {
57 50
        if (!array_key_exists($type, self::$errors)) {
58 50
            self::$errors[$type] = [];
59 50
        }
60
61 50
        array_push(self::$errors[$type], $error);
62
    }
63
}