Passed
Push — master ( 8f5e6a...061d3e )
by Felipe
03:26
created

TextProperty::Set()   A

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 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Amenadiel\JpGraph\Text;
3
4
//===================================================
5
// CLASS TextProperty
6
// Description: Holds properties for a text
7
//===================================================
8
class TextProperty
9
{
10
    public $iShow       = true;
11
    public $csimtarget  = '';
12
    public $csimwintarget  = '';
13
    public $csimalt  = '';
14
    private $iFFamily   = FF_FONT1;
15
    private $iFStyle   = FS_NORMAL;
16
    private $iFSize   = 10;
17
    private $iFontArray = array();
18
    private $iColor     = "black";
19
    private $iText      = "";
20
    private $iHAlign    = "left";
21
    private $iVAlign    = "bottom";
22
23
    //---------------
24
    // CONSTRUCTOR
25
    public function __construct($aTxt = '')
26
    {
27
        $this->iText = $aTxt;
28
    }
29
30
    //---------------
31
    // PUBLIC METHODS
32
    public function Set($aTxt)
33
    {
34
        $this->iText = $aTxt;
35
    }
36
37
    public function SetCSIMTarget($aTarget, $aAltText = '', $aWinTarget = '')
38
    {
39
        if (is_string($aTarget)) {
40
            $aTarget = array($aTarget);
41
        }
42
43
        $this->csimtarget = $aTarget;
44
45
        if (is_string($aWinTarget)) {
46
            $aWinTarget = array($aWinTarget);
47
        }
48
49
        $this->csimwintarget = $aWinTarget;
0 ignored issues
show
Documentation Bug introduced by
It seems like $aWinTarget of type array<integer,string,{"0":"string"}> is incompatible with the declared type string of property $csimwintarget.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
51
        if (is_string($aAltText)) {
52
            $aAltText = array($aAltText);
53
        }
54
55
        $this->csimalt = $aAltText;
0 ignored issues
show
Documentation Bug introduced by
It seems like $aAltText of type array<integer,string,{"0":"string"}> is incompatible with the declared type string of property $csimalt.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
56
    }
57
58
    public function SetCSIMAlt($aAltText)
59
    {
60
        if (is_string($aAltText)) {
61
            $aAltText = array($aAltText);
62
        }
63
64
        $this->csimalt = $aAltText;
65
    }
66
67
    // Set text color
68
    public function SetColor($aColor)
69
    {
70
        $this->iColor = $aColor;
71
    }
72
73
    public function HasTabs()
74
    {
75
        if (is_string($this->iText)) {
76
            return substr_count($this->iText, "\t") > 0;
77
        } elseif (is_array($this->iText)) {
78
            return false;
79
        }
80
    }
81
82
    // Get number of tabs in string
83
    public function GetNbrTabs()
84
    {
85
        if (is_string($this->iText)) {
86
            return substr_count($this->iText, "\t");
87
        } else {
88
            return 0;
89
        }
90
    }
91
92
    // Set alignment
93
    public function Align($aHAlign, $aVAlign = "bottom")
94
    {
95
        $this->iHAlign = $aHAlign;
96
        $this->iVAlign = $aVAlign;
97
    }
98
99
    // Synonym
100
    public function SetAlign($aHAlign, $aVAlign = "bottom")
101
    {
102
        $this->iHAlign = $aHAlign;
103
        $this->iVAlign = $aVAlign;
104
    }
105
106
    // Specify font
107
    public function SetFont($aFFamily, $aFStyle = FS_NORMAL, $aFSize = 10)
108
    {
109
        $this->iFFamily = $aFFamily;
110
        $this->iFStyle  = $aFStyle;
111
        $this->iFSize   = $aFSize;
112
    }
113
114
    public function SetColumnFonts($aFontArray)
115
    {
116
        if (!is_array($aFontArray) || count($aFontArray[0]) != 3) {
117
            JpGraphError::RaiseL(6033);
118
            // 'Array of fonts must contain arrays with 3 elements, i.e. (Family, Style, Size)'
119
        }
120
        $this->iFontArray = $aFontArray;
121
    }
122
123
    public function IsColumns()
124
    {
125
        return is_array($this->iText);
126
    }
127
128
    // Get width of text. If text contains several columns separated by
129
    // tabs then return both the total width as well as an array with a
130
    // width for each column.
131
    public function GetWidth($aImg, $aUseTabs = false, $aTabExtraMargin = 1.1)
132
    {
133
        $extra_margin = 4;
134
        $aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize);
135
        if (is_string($this->iText)) {
136
            if (strlen($this->iText) == 0) {
137
                return 0;
138
            }
139
140
            $tmp = preg_split('/\t/', $this->iText);
141
            if (count($tmp) <= 1 || !$aUseTabs) {
142
                $w = $aImg->GetTextWidth($this->iText);
143
                return $w + 2 * $extra_margin;
144
            } else {
145
                $tot = 0;
146
                $n   = count($tmp);
147
                for ($i = 0; $i < $n; ++$i) {
148
                    $res[$i] = $aImg->GetTextWidth($tmp[$i]);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$res was never initialized. Although not strictly required by PHP, it is generally a good practice to add $res = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
149
                    $tot += $res[$i] * $aTabExtraMargin;
0 ignored issues
show
Bug introduced by
The variable $res does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
150
                }
151
                return array(round($tot), $res);
152
            }
153
        } elseif (is_object($this->iText)) {
154
            // A single icon
155
            return $this->iText->GetWidth() + 2 * $extra_margin;
156
        } elseif (is_array($this->iText)) {
157
            // Must be an array of texts. In this case we return the sum of the
158
            // length + a fixed margin of 4 pixels on each text string
159
            $n  = count($this->iText);
160
            $nf = count($this->iFontArray);
161
            for ($i = 0, $w = 0; $i < $n; ++$i) {
162 View Code Duplication
                if ($i < $nf) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
                    $aImg->SetFont($this->iFontArray[$i][0], $this->iFontArray[$i][1], $this->iFontArray[$i][2]);
164
                } else {
165
                    $aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize);
166
                }
167
                $tmp = $this->iText[$i];
168
                if (is_string($tmp)) {
169
                    $w += $aImg->GetTextWidth($tmp) + $extra_margin;
170
                } else {
171
                    if (is_object($tmp) === false) {
172
                        JpGraphError::RaiseL(6012);
173
                    }
174
                    $w += $tmp->GetWidth() + $extra_margin;
175
                }
176
            }
177
            return $w;
178
        } else {
179
            JpGraphError::RaiseL(6012);
180
        }
181
    }
182
183
    // for the case where we have multiple columns this function returns the width of each
184
    // column individually. If there is no columns just return the width of the single
185
    // column as an array of one
186
    public function GetColWidth($aImg, $aMargin = 0)
187
    {
188
        $aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize);
189
        if (is_array($this->iText)) {
190
            $n  = count($this->iText);
191
            $nf = count($this->iFontArray);
192
            for ($i = 0, $w = array(); $i < $n; ++$i) {
193
                $tmp = $this->iText[$i];
194
                if (is_string($tmp)) {
195 View Code Duplication
                    if ($i < $nf) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
                        $aImg->SetFont($this->iFontArray[$i][0], $this->iFontArray[$i][1], $this->iFontArray[$i][2]);
197
                    } else {
198
                        $aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize);
199
                    }
200
                    $w[$i] = $aImg->GetTextWidth($tmp) + $aMargin;
201
                } else {
202
                    if (is_object($tmp) === false) {
203
                        JpGraphError::RaiseL(6012);
204
                    }
205
                    $w[$i] = $tmp->GetWidth() + $aMargin;
206
                }
207
            }
208
            return $w;
209
        } else {
210
            return array($this->GetWidth($aImg));
211
        }
212
    }
213
214
    // Get total height of text
215
    public function GetHeight($aImg)
216
    {
217
        $nf        = count($this->iFontArray);
218
        $maxheight = -1;
219
220
        if ($nf > 0) {
221
            // We have to find out the largest font and take that one as the
222
            // height of the row
223
            for ($i = 0; $i < $nf; ++$i) {
224
                $aImg->SetFont($this->iFontArray[$i][0], $this->iFontArray[$i][1], $this->iFontArray[$i][2]);
225
                $height    = $aImg->GetFontHeight();
226
                $maxheight = max($height, $maxheight);
227
            }
228
        }
229
230
        $aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize);
231
        $height    = $aImg->GetFontHeight();
232
        $maxheight = max($height, $maxheight);
233
        return $maxheight;
234
    }
235
236
    // Unhide/hide the text
237
    public function Show($aShow = true)
238
    {
239
        $this->iShow = $aShow;
240
    }
241
242
    // Stroke text at (x,y) coordinates. If the text contains tabs then the
243
    // x parameter should be an array of positions to be used for each successive
244
    // tab mark. If no array is supplied then the tabs will be ignored.
245
    public function Stroke($aImg, $aX, $aY)
246
    {
247
        if ($this->iShow) {
248
            $aImg->SetColor($this->iColor);
249
            $aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize);
250
            $aImg->SetTextAlign($this->iHAlign, $this->iVAlign);
251
            if ($this->GetNbrTabs() < 1) {
252
                if (is_string($this->iText)) {
253
                    if (is_array($aX)) {
254
                        $aX = $aX[0];
255
                    }
256
257
                    if (is_array($aY)) {
258
                        $aY = $aY[0];
259
                    }
260
261
                    $aImg->StrokeText($aX, $aY, $this->iText);
262
                } elseif (is_array($this->iText) && ($n = count($this->iText)) > 0) {
263
                    $ax = is_array($aX);
264
                    $ay = is_array($aY);
265
                    if ($ax && $ay) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
266
                        // Nothing; both are already arrays
267
                    } elseif ($ax) {
268
                        $aY = array_fill(0, $n, $aY);
269
                    } elseif ($ay) {
270
                        $aX = array_fill(0, $n, $aX);
271
                    } else {
272
                        $aX = array_fill(0, $n, $aX);
273
                        $aY = array_fill(0, $n, $aY);
274
                    }
275
                    $n = min($n, count($aX));
276
                    $n = min($n, count($aY));
277
                    for ($i = 0; $i < $n; ++$i) {
278
                        $tmp = $this->iText[$i];
279
                        if (is_object($tmp)) {
280
                            $tmp->Stroke($aImg, $aX[$i], $aY[$i]);
281
                        } else {
282 View Code Duplication
                            if ($i < count($this->iFontArray)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
283
                                $font = $this->iFontArray[$i];
284
                                $aImg->SetFont($font[0], $font[1], $font[2]);
285
                            } else {
286
                                $aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize);
287
                            }
288
                            $aImg->StrokeText($aX[$i], $aY[$i], str_replace("\t", " ", $tmp));
289
                        }
290
                    }
291
                }
292
            } else {
293
                $tmp = preg_split('/\t/', $this->iText);
294
                $n   = min(count($tmp), count($aX));
295
                for ($i = 0; $i < $n; ++$i) {
296 View Code Duplication
                    if ($i < count($this->iFontArray)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
297
                        $font = $this->iFontArray[$i];
298
                        $aImg->SetFont($font[0], $font[1], $font[2]);
299
                    } else {
300
                        $aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize);
301
                    }
302
                    $aImg->StrokeText($aX[$i], $aY, $tmp[$i]);
303
                }
304
            }
305
        }
306
    }
307
}
308