C39Object::C39Object()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 51
rs 9.069
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
//============================================================+
3
// File name   : c39object.php
4
// Begin       : 2002-07-31
5
// Last Update : 2004-12-29
6
// Author      : Karim Mribti [[email protected]]
7
//             : Nicola Asuni [[email protected]]
8
// Version     : 0.0.8a  2001-04-01 (original code)
9
// License     : GNU LGPL (Lesser General Public License) 2.1
10
//               http://www.gnu.org/copyleft/lesser.txt
11
// Source Code : http://www.mribti.com/barcode/
12
//
13
// Description : Code 39 Barcode Render Class for PHP using
14
//               the GD graphics library.
15
//               Code 39 is an alphanumeric bar code that can
16
//               encode decimal number, case alphabet and some
17
//               special symbols.
18
//
19
// NOTE:
20
// This version contains changes by Nicola Asuni:
21
//  - porting to PHP4
22
//  - code style and formatting
23
//  - automatic php documentation in PhpDocumentor Style
24
//    (www.phpdoc.org)
25
//  - minor bug fixing
26
//============================================================+
27
28
/**
29
 * Code 39 Barcode Render Class.<br>
30
 * Code 39 is an alphanumeric bar code that can encode decimal number, case alphabet and some special symbols.
31
 * @author Karim Mribti, Nicola Asuni
32
 * @name BarcodeObject
33
 * @package com.tecnick.tcpdf
34
 * @version 0.0.8a  2001-04-01 (original code)
35
 * @since 2001-03-25
36
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
37
 */
38
39
/**
40
 * Code 39 Barcode Render Class.<br>
41
 * Code 39 is an alphanumeric bar code that can encode decimal number, case alphabet and some special symbols.
42
 * @author Karim Mribti, Nicola Asuni
43
 * @name BarcodeObject
44
 * @package com.tecnick.tcpdf
45
 * @version 0.0.8a  2001-04-01 (original code)
46
 * @since 2001-03-25
47
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
48
 */
49
class C39Object extends BarcodeObject {
50
	
51
	/**
52
	 * Class Constructor.
53
	 * @param int $Width Image width in pixels.
54
	 * @param int $Height Image height in pixels. 
55
	 * @param int $Style Barcode style.
56
	 * @param int $Value value to print on barcode.
57
	 */
58
	function C39Object($Width, $Height, $Style, $Value) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
		$this->BarcodeObject($Width, $Height, $Style);
60
		$this->mValue = $Value;
0 ignored issues
show
Bug introduced by
The property mValue does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
61
		$this->mChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%";
0 ignored issues
show
Documentation Bug introduced by
It seems like '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-. *$/+%' of type string is incompatible with the declared type object<Allowed> of property $mChars.

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...
62
		$this->mCharSet = array (
0 ignored issues
show
Documentation Bug introduced by
It seems like array('000110100', '1001...10001010', '000101010') of type array<integer,string,{"0...string","43":"string"}> is incompatible with the declared type object<Character> of property $mCharSet.

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...
63
		/* 0  */ "000110100",
64
		/* 1  */ "100100001",
65
		/* 2  */ "001100001",
66
		/* 3  */ "101100000",
67
		/* 4  */ "000110001",
68
		/* 5  */ "100110000",
69
		/* 6  */ "001110000",
70
		/* 7  */ "000100101",
71
		/* 8  */ "100100100",
72
		/* 9  */ "001100100",
73
		/* A  */ "100001001",
74
		/* B  */ "001001001",
75
		/* C  */ "101001000",
76
		/* D  */ "000011001",
77
		/* E  */ "100011000",
78
		/* F  */ "001011000",
79
		/* G  */ "000001101",
80
		/* H  */ "100001100",
81
		/* I  */ "001001100",
82
		/* J  */ "000011100",
83
		/* K  */ "100000011",
84
		/* L  */ "001000011",
85
		/* M  */ "101000010",
86
		/* N  */ "000010011",
87
		/* O  */ "100010010",
88
		/* P  */ "001010010",
89
		/* Q  */ "000000111",
90
		/* R  */ "100000110",
91
		/* S  */ "001000110",
92
		/* T  */ "000010110",
93
		/* U  */ "110000001",
94
		/* V  */ "011000001",
95
		/* W  */ "111000000",
96
		/* X  */ "010010001",
97
		/* Y  */ "110010000",
98
		/* Z  */ "011010000",
99
		/* -  */ "010000101",
100
		/* .  */ "110000100",
101
		/* SP */ "011000100",
102
		/* *  */ "010010100",
103
		/* $  */ "010101000",
104
		/* /  */ "010100010",
105
		/* +  */ "010001010",
106
		/* %  */ "000101010"
107
		);
108
	}
109
110
	/**
111
	 * Returns the character index.
112
	 * @param char $char character.
113
	 * @return int character index or -1 in case of error.
114
	 * @access private
115
	 */
116
	function GetCharIndex($char) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
117
		for ($i=0;$i<44;$i++) {
118
			if ($this->mChars[$i] == $char) {
119
				return $i;
120
			}
121
		}
122
		return -1;
123
	}
124
	
125
	/**
126
	 * Returns barcode size.
127
	 * @param int $xres Horizontal resolution.
128
	 * @return barcode size.
0 ignored issues
show
Documentation introduced by
Should the return type not be false|integer?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
129
	 * @access private
130
	 */
131
	function GetSize($xres) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
132
		$len = strlen($this->mValue);
133
134
		if ($len == 0)  {
135
			$this->mError = "Null value";
0 ignored issues
show
Documentation Bug introduced by
It seems like 'Null value' of type string is incompatible with the declared type object<Error> of property $mError.

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...
136
			return false;
137
		}
138
139
		for ($i=0;$i<$len;$i++) {
140
			if ($this->GetCharIndex($this->mValue[$i]) == -1 || $this->mValue[$i] == '*') {
141
				/* The asterisk is only used as a start and stop code */
142
				$this->mError = "C39 not include the char '".$this->mValue[$i]."'";
0 ignored issues
show
Documentation Bug introduced by
It seems like 'C39 not include the cha...this->mValue[$i] . '\'' of type string is incompatible with the declared type object<Error> of property $mError.

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...
143
				return false;
144
			}
145
		}
146
147
		/* Start, Stop is 010010100 == '*'  */
148
		$StartSize = BCD_C39_NARROW_BAR * $xres * 6 + BCD_C39_WIDE_BAR * $xres * 3;
149
		$StopSize  = BCD_C39_NARROW_BAR * $xres * 6 + BCD_C39_WIDE_BAR * $xres * 3;
150
		$CharSize  = BCD_C39_NARROW_BAR * $xres * 6 + BCD_C39_WIDE_BAR * $xres * 3; /* Same for all chars */
151
152
		return $CharSize * $len + $StartSize + $StopSize + /* Space between chars */ BCD_C39_NARROW_BAR * $xres * ($len-1);
153
	}
154
155
	/**
156
	 * Draws the start code.
157
	 * @param int $DrawPos Drawing position.
158
	 * @param int $yPos Vertical position.
159
	 * @param int $ySize Vertical size.
160
	 * @param int $xres Horizontal resolution.
161
	 * @return int drawing position.
162
	 * @access private
163
	 */
164 View Code Duplication
	function DrawStart($DrawPos, $yPos, $ySize, $xres) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
165
		/* Start code is '*' */
166
		$narrow = BCD_C39_NARROW_BAR * $xres;
167
		$wide   = BCD_C39_WIDE_BAR * $xres;
168
		$this->DrawSingleBar($DrawPos, $yPos, $narrow , $ySize);
169
		$DrawPos += $narrow;
170
		$DrawPos += $wide;
171
		$this->DrawSingleBar($DrawPos, $yPos, $narrow , $ySize);
172
		$DrawPos += $narrow;
173
		$DrawPos += $narrow;
174
		$this->DrawSingleBar($DrawPos, $yPos, $wide , $ySize);
175
		$DrawPos += $wide;
176
		$DrawPos += $narrow;
177
		$this->DrawSingleBar($DrawPos, $yPos, $wide , $ySize);
178
		$DrawPos += $wide;
179
		$DrawPos += $narrow;
180
		$this->DrawSingleBar($DrawPos, $yPos, $narrow, $ySize);
181
		$DrawPos += $narrow;
182
		$DrawPos += $narrow; /* Space between chars */
183
		return $DrawPos;
184
	}
185
	
186
	/**
187
	 * Draws the stop code.
188
	 * @param int $DrawPos Drawing position.
189
	 * @param int $yPos Vertical position.
190
	 * @param int $ySize Vertical size.
191
	 * @param int $xres Horizontal resolution.
192
	 * @return int drawing position.
193
	 * @access private
194
	 */
195 View Code Duplication
	function DrawStop($DrawPos, $yPos, $ySize, $xres) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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
		/* Stop code is '*' */
197
		$narrow = BCD_C39_NARROW_BAR * $xres;
198
		$wide   = BCD_C39_WIDE_BAR * $xres;
199
		$this->DrawSingleBar($DrawPos, $yPos, $narrow , $ySize);
200
		$DrawPos += $narrow;
201
		$DrawPos += $wide;
202
		$this->DrawSingleBar($DrawPos, $yPos, $narrow , $ySize);
203
		$DrawPos += $narrow;
204
		$DrawPos += $narrow;
205
		$this->DrawSingleBar($DrawPos, $yPos, $wide , $ySize);
206
		$DrawPos += $wide;
207
		$DrawPos += $narrow;
208
		$this->DrawSingleBar($DrawPos, $yPos, $wide , $ySize);
209
		$DrawPos += $wide;
210
		$DrawPos += $narrow;
211
		$this->DrawSingleBar($DrawPos, $yPos, $narrow, $ySize);
212
		$DrawPos += $narrow;
213
		return $DrawPos;
214
	}
215
	
216
	/**
217
	 * Draws the barcode object.
218
	 * @param int $xres Horizontal resolution.
219
	 * @return bool true in case of success.
220
	 */
221
	function DrawObject($xres) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
222
		$len = strlen($this->mValue);
223
224
		$narrow = BCD_C39_NARROW_BAR * $xres;
225
		$wide   = BCD_C39_WIDE_BAR * $xres;
226
227
		if (($size = $this->GetSize($xres))==0) {
228
			return false;
229
		}
230
231
		$cPos = 0;
232
		if ($this->mStyle & BCS_ALIGN_CENTER) $sPos = (integer)(($this->mWidth - $size ) / 2);
233
		else if ($this->mStyle & BCS_ALIGN_RIGHT) $sPos = $this->mWidth - $size;
234
		else $sPos = 0;
235
236
		/* Total height of bar code -Bars only- */
237
		if ($this->mStyle & BCS_DRAW_TEXT) $ysize = $this->mHeight - BCD_DEFAULT_MAR_Y1 - BCD_DEFAULT_MAR_Y2 - $this->GetFontHeight($this->mFont);
238
		else $ysize = $this->mHeight - BCD_DEFAULT_MAR_Y1 - BCD_DEFAULT_MAR_Y2;
239
240
		/* Draw text */
241
		if ($this->mStyle & BCS_DRAW_TEXT) {
242
			if ($this->mStyle & BCS_STRETCH_TEXT) {
243 View Code Duplication
				for ($i=0;$i<$len;$i++) {
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...
244
					$this->DrawChar($this->mFont, $sPos+($narrow*6+$wide*3)+($size/$len)*$i,
245
					$ysize + BCD_DEFAULT_MAR_Y1 + BCD_DEFAULT_TEXT_OFFSET, $this->mValue[$i]);
246
				}
247 View Code Duplication
			} else {/* Center */
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...
248
			$text_width = $this->GetFontWidth($this->mFont) * strlen($this->mValue);
249
			$this->DrawText($this->mFont, $sPos+(($size-$text_width)/2)+($narrow*6+$wide*3),
250
			$ysize + BCD_DEFAULT_MAR_Y1 + BCD_DEFAULT_TEXT_OFFSET, $this->mValue);
251
			}
252
		}
253
254
		$DrawPos = $this->DrawStart($sPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres);
255
		do {
256
			$c     = $this->GetCharIndex($this->mValue[$cPos]);
257
			$cset  = $this->mCharSet[$c];
258
			$this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[0] == '0') ? $narrow : $wide , $ysize);
259
			$DrawPos += ($cset[0] == '0') ? $narrow : $wide;
260
			$DrawPos += ($cset[1] == '0') ? $narrow : $wide;
261
			$this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[2] == '0') ? $narrow : $wide , $ysize);
262
			$DrawPos += ($cset[2] == '0') ? $narrow : $wide;
263
			$DrawPos += ($cset[3] == '0') ? $narrow : $wide;
264
			$this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[4] == '0') ? $narrow : $wide , $ysize);
265
			$DrawPos += ($cset[4] == '0') ? $narrow : $wide;
266
			$DrawPos += ($cset[5] == '0') ? $narrow : $wide;
267
			$this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[6] == '0') ? $narrow : $wide , $ysize);
268
			$DrawPos += ($cset[6] == '0') ? $narrow : $wide;
269
			$DrawPos += ($cset[7] == '0') ? $narrow : $wide;
270
			$this->DrawSingleBar($DrawPos, BCD_DEFAULT_MAR_Y1, ($cset[8] == '0') ? $narrow : $wide , $ysize);
271
			$DrawPos += ($cset[8] == '0') ? $narrow : $wide;
272
			$DrawPos += $narrow; /* Space between chars */
273
			$cPos++;
274
		} while ($cPos<$len);
275
		$DrawPos =  $this->DrawStop($DrawPos, BCD_DEFAULT_MAR_Y1 , $ysize, $xres);
0 ignored issues
show
Unused Code introduced by
$DrawPos is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
276
		return true;
277
	}
278
}
279
280
//============================================================+
281
// END OF FILE
282
//============================================================+
283
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...