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

Statistics::reset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
namespace JsonTable\Analyse;
3
4
/**
5
 * Statistics information regarding the analysis.
6
 *
7
 * @package    JSON table
8
 */
9
class Statistics
10
{
11
    /**
12
     * @var array   Statistics relating to the file analysis.
13
     */
14
    protected static $statistics = [
15
        'rows_with_errors' => [],
16
        'percent_rows_with_errors' => 0,
17
        'rows_analysed' => 0
18
    ];
19
20
21
    /**
22
     * Reset the statistics.
23
     */
24 81
    public static function reset()
25
    {
26 81
        self::$statistics = [
27 81
            'rows_with_errors' => [],
28 81
            'percent_rows_with_errors' => 0,
29
            'rows_analysed' => 0
30 81
        ];
31 81
    }
32
33
34
    /**
35
     * Get the statistics about the file analysis.
36
     *
37
     * @return  array   The statistics.
38
     */
39 54
    public function getStatistics()
40
    {
41 54
        $this->cleanErrorRow();
42
43 54
        if (self::$statistics['rows_analysed'] > 0) {
44 54
            self::$statistics['percent_rows_with_errors'] = $this->getErrorRowPercent();
45 54
        }
46
47 54
        return self::$statistics;
48
    }
49
    
50
    
51
    /**
52
     * Add the row number of a row with an error to the analysis statistics.
53
     *
54
     * @param   int $rowNumber   The position of the row with the error in the CSV file.
55
     *
56
     * @return  void
57
     */
58 48
    public function setErrorRow($rowNumber)
59
    {
60 48
        self::$statistics['rows_with_errors'][] = $rowNumber;
61 48
    }
62
63
64
    /**
65
     * Set the number of rows that have been analysed.
66
     *
67
     * @param   int $rowsAnalysedCount   The number of rows analysed.
68
     *
69
     * @return  void
70
     */
71 72
    public function setRowsAnalysed($rowsAnalysedCount)
72
    {
73 72
        self::$statistics['rows_analysed'] = $rowsAnalysedCount;
74 72
    }
75
76
77
    /**
78
     * Clean the rows with errors statistic.
79
     * This removes duplicated records where the same row has had multiple errors.
80
     *
81
     * @return  void
82
     */
83 54
    private function cleanErrorRow()
84
    {
85 54
        self::$statistics['rows_with_errors'] = array_unique(self::$statistics['rows_with_errors']);
86 54
    }
87
88
89
    /**
90
     * Get the percentage of analysed rows that have had a error with them.
91
     *
92
     * @return  int The percentage.
93
     */
94 54
    private function getErrorRowPercent()
95
    {
96 54
        return round((count(self::$statistics['rows_with_errors']) / self::$statistics['rows_analysed']) * 100);
97
    }
98
}