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

Statistics   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 1
cbo 0
dl 0
loc 90
ccs 23
cts 23
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A reset() 0 8 1
A getStatistics() 0 10 2
A setErrorRow() 0 4 1
A setRowsAnalysed() 0 4 1
A cleanErrorRow() 0 4 1
A getErrorRowPercent() 0 4 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
}