Common::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace HuasoFoundries\Histogram\Printer;
3
4
/**
5
 * Base class for histogram printer objects
6
 *
7
 * @author  Jesus M. Castagnetto <[email protected]>
8
 * @version 0.9.1beta
9
 * @access  public
10
 * @package Histogram
11
 */
12
class Common
13
{
14
15
    // properties
16
17
    /**
18
     * An associative array of options for the printer object
19
     *
20
     * @access private
21
     * @var array
22
     */
23
    public $_options;
24
25
    /**
26
     * The Histogram object
27
     *
28
     * @access private
29
     * @var object Histogram
30
     * @see Histogram
31
     */
32
    public $_hist;
33
34
    //
35
36
    /**
37
     * Constructor
38
     *
39
     * @access  public
40
     * @param   optional array $options     An associative array of printer options
41
     * @param   optional object Histogram $hist    A Histogram object
42
     * @return  object  Histogram_Printer_Common
43
     */
44
    public function __construct($hist = null, $options = null)
45
    {
46
        $this->setHistogram($hist);
47
        $this->setOptions($options);
48
    }
49
50
    /**
51
     * Sets the printer options
52
     *
53
     * @access  public
54
     * @param   array $options     An associative array of printer options
55
     *              Common options:
56
     *              'useHTTPHeaders' (default = false), whether to output HTTP headers when using printOutput()
57
     *              'outputStatistics' (default = false), whether to include histogram statistics when generating the output
58
     * @return  boolean TRUE on success, FALSE otherwise
59
     */
60
    public function setOptions($options)
61
    {
62
        if (!is_array($options)) {
0 ignored issues
show
introduced by
The condition is_array($options) is always true.
Loading history...
63
            $this->_options = null;
64
            return false;
65
        } else {
66
            $this->_options = $options;
67
            return true;
68
        }
69
        if (!array_key_exists('useHTTPHeaders', $this->_options)) {
0 ignored issues
show
Unused Code introduced by
IfNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
70
            $this->_options['useHTTPHeaders'] = false;
71
        }
72
        if (!array_key_exists('outputStatistics', $this->_options)) {
73
            $this->_options['outputStatistics'] = false;
74
        }
75
    }
76
77
    /**
78
     * Sets the Histogram object to plot
79
     *
80
     * @access  public
81
     * @param   object Histogram $hist A Histogram instance
0 ignored issues
show
Bug introduced by
The type HuasoFoundries\Histogram\Printer\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...
82
     * @return  boolean TRUE on success, FALSE otherwise
83
     */
84
    public function setHistogram(&$hist)
85
    {
86
        if (\HuasoFoundries\Histogram\Histogram::isValidHistogram($hist)) {
87
            $this->_hist = &$hist;
88
            return true;
89
        } else {
90
            $this->_hist = null;
91
            return false;
92
        }
93
    }
94
95
    // override this method in child classes
96
    /**
97
     * Returns a (binary safe) string representation of a Histogram plot
98
     *
99
     * @access public
100
     * @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...
101
     */
102
    public function generateOutput()
103
    {
104
        throw new \PEAR_Exception('Unimplemented method');
105
    }
106
107
    // override this method in child classes
108
    /**
109
     * Prints out a graphic representation of a Histogram
110
     *
111
     * @access public
112
     * @return boolean|PEAR_Error TRUE on success, a \PEAR_Error otherwise
113
     */
114
    public function printOutput()
115
    {
116
        throw new \PEAR_Exception('Unimplemented method');
117
    }
118
119
    // override this method in child classes
120
    /**
121
     * Static method to print out a graphic representation of a Histogram
122
     *
123
     * @static
124
     * @access public
125
     * @param object Histogram $hist A Histogram instance
126
     * @param array $options An array of options for the printer object
127
     * @return boolean|PEAR_Error TRUE on success, a \PEAR_Error otherwise
128
     */
129
    public function printHistogram(&$hist, $options = array())
130
    {
131
        throw new \PEAR_Exception('Unimplemented method');
132
    }
133
134
    /**
135
     * Utility method to do static printing
136
     *
137
     * @static
138
     * @access private
139
     * @param object $printer An instance of a Histogram_Printer_* class
140
     * @param object Histogram $hist A Histogram instance
141
     * @param array $options An array of options for the printer object
142
     * @return boolean|PEAR_Error TRUE on success, a \PEAR_Error otherwise
143
     */
144
    public function _doStaticPrint(&$printer, &$hist, $options)
145
    {
146
        if (!$printer->setHistogram($hist)) {
147
            throw new \PEAR_Exception('Not a valid Histogram object');
148
        }
149
        if (!$printer->setOptions($options)) {
150
            throw new \PEAR_Exception('Expecting an associative array of options');
151
        }
152
        // try to plot, clean up object, and return
153
        $err = $printer->printOutput();
154
        unset($printer);
155
        return $err;
156
    }
157
}
158
159
// vim: ts=4:sw=4:et:
160
// vim6: fdl=1:
161