Text::generateOutput()   C
last analyzed

Complexity

Conditions 8
Paths 49

Size

Total Lines 39
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 30
nc 49
nop 0
dl 0
loc 39
rs 5.3846
c 0
b 0
f 0
1
<?php
2
namespace HuasoFoundries\Histogram\Printer;
3
4
/**
5
 * Class to print text representations of  a Math_Histogram object
6
 *
7
 * @author  Jesus M. Castagnetto <[email protected]>
8
 * @version 0.9.1beta
9
 * @access  public
10
 * @package Math_Histogram
11
 */
12
class Text extends Common
13
{
14
15
    /**
16
     * Returns a string representation of a Histogram plot
17
     *
18
     * @access public
19
     * @return string|PEAR_Error A string on succcess, a \PEAR_Error otherwise
0 ignored issues
show
Bug introduced by
The type HuasoFoundries\Histogram\Printer\PEAR_Error was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
     */
21
    public function generateOutput()
22
    {
23
        if (is_null($this->_hist)) {
24
            throw new \PEAR_Exception('Math_Histogram object has not been set');
25
        }
26
        if (!$this->_hist->isCalculated()) {
27
            if (isset($this->_options['histogramStatsMode'])) {
28
                $this->_hist->calculate($this->_options['histogramStatsMode']);
29
            } else {
30
                $this->_hist->calculate();
31
            }
32
        }
33
34
        if (isset($this->_options['histogramBinMode'])) {
35
            $binmode = $this->_options['histogramBinMode'];
36
        } else {
37
            $binmode = \HuasoFoundries\Histogram\Histogram::HISTOGRAM_HI_BINS;
38
        }
39
        $bins    = $this->_hist->getBins($binmode);
40
        $binopts = $this->_hist->getBinOptions();
41
        $hdata   = $this->_hist->getHistogramData();
42
        $data    = $this->_hist->getData();
43
        $fmt     = "%-4.3f (%-4d) |%s\n";
44
        $maxfreq = max(array_values($bins));
45
        $total   = count($hdata);
46
        $out     = ($this->_hist->_type == \HuasoFoundries\Histogram\Histogram::HISTOGRAM_CUMMULATIVE) ? "Cummulative Frequency" : "Histogram";
47
        $out .= "\n\tNumber of bins: {$binopts['nbins']}\n";
48
        $out .= "\tPlot range: [{$binopts['rangeLow']}, {$binopts['rangeHigh']}]\n";
49
        $out .= "\tData range: [" . min($hdata) . ", " . max($hdata) . "]\n";
50
        $out .= "\tOriginal data range: [" . min($data) . ", " . max($data) . "]\n";
51
        $out .= "BIN (FREQUENCY) ASCII_BAR (%)\n";
52
        foreach ($bins as $bin => $freq) {
53
            $out .= sprintf($fmt, $bin, $freq, $this->_bar($freq, $maxfreq, $total));
54
        }
55
        if ($this->_options['outputStats']) {
56
            $out .= "\n --- Histogram Statistics ---\n";
57
            $out .= $this->_printStats($this->_hist->getHistogramDataStats());
58
        }
59
        return $out;
60
    }
61
62
    /**
63
     * Prints out a graphic representation of a Histogram
64
     *
65
     * @access public
66
     * @return boolean|PEAR_Error TRUE on success, a \PEAR_Error otherwise
67
     */
68
    public function printOutput()
69
    {
70
        $plot = $this->generateOutput($hist);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $hist seems to be never defined.
Loading history...
Unused Code introduced by
The call to HuasoFoundries\Histogram...\Text::generateOutput() has too many arguments starting with $hist. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
        /** @scrutinizer ignore-call */ 
71
        $plot = $this->generateOutput($hist);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
71
        if (\PEAR::isError($plot)) {
72
            return $plot;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $plot returns the type string which is incompatible with the documented return type boolean|HuasoFoundries\H...gram\Printer\PEAR_Error.
Loading history...
73
        } else {
74
            if ($this->_options['useHTTPHeaders']) {
75
                header('Content-type: text/plain');
76
            }
77
            echo $plot;
78
        }
79
    }
80
81
    /**
82
     * Static method to print out a graphic representation of a Histogram
83
     *
84
     * @static
85
     * @access public
86
     * @param object Math_Histogram $hist A Math_Histogram instance
0 ignored issues
show
Bug introduced by
The type HuasoFoundries\Histogram\Printer\Math_Histogram was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
87
     * @param array $options An array of options for the printer object
88
     * @return boolean|PEAR_Error TRUE on success, a \PEAR_Error otherwise
89
     */
90
    public function printHistogram(&$hist, $options = array())
91
    {
92
        $printer = new \HuasoFoundries\Histogram\Printer\Text();
93
        return \HuasoFoundries\Histogram\Printer\Common::_doStaticPrint($printer, $hist, $options);
0 ignored issues
show
Bug Best Practice introduced by
The method HuasoFoundries\Histogram...ommon::_doStaticPrint() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

93
        return \HuasoFoundries\Histogram\Printer\Common::/** @scrutinizer ignore-call */ _doStaticPrint($printer, $hist, $options);
Loading history...
94
    }
95
96
    /**
97
     * Prints a simple ASCII bar
98
     *
99
     * @access  private
100
     * @param   int $freq   the frequency
101
     * @param   int $maxfreq    the maximum frequency
102
     * @param   int $total  the total count
103
     * @return  string
104
     */
105
    public function _bar($freq, $maxfreq, $total)
106
    {
107
        $fact  = floatval(($maxfreq > 40) ? 40 / $maxfreq : 1);
108
        $niter = round($freq * $fact);
109
        $out   = "";
110
        for ($i = 0; $i < $niter; $i++) {
111
            $out .= "*";
112
        }
113
114
        return $out . sprintf(" (%.1f%%)", $freq / $total * 100);
115
    }
116
117
    /**
118
     * Prints the histogram data statistics
119
     *
120
     * @access private
121
     * @param array $stats Associative array of statistics
122
     * @param optional string $prefix Prefix to use when printing out statistics
123
     * @return string
124
     */
125
    public function _printStats($stats, $prefix = '')
126
    {
127
        $out = '';
128
        foreach ($stats as $name => $value) {
129
            if (is_array($value)) {
130
                $out .= $prefix . $name . ":\n";
131
                $out .= $this->_printStats($value, $prefix . "\t");
132
            } else {
133
                $out .= "{$prefix}{$name}: $value\n";
134
            }
135
        }
136
        return $out;
137
    }
138
}
139
140
// vim: ts=4:sw=4:et:
141
// vim6: fdl=1:
142