Completed
Push — master ( 8ad790...335763 )
by Caio
02:46
created

BarcodeGenerator   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 18
c 2
b 1
f 0
lcom 1
cbo 0
dl 0
loc 165
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B getBarcodeData() 0 26 3
C barcode_i25() 0 65 9
A checksum_s25() 0 20 4
A convertBarcodeArrayToNewStyle() 0 21 2
1
<?php
2
// Copyright (C) 2002-2015 Nicola Asuni - Tecnick.com LTD
3
//
4
// This file is part of TCPDF software library.
5
//
6
// TCPDF is free software: you can redistribute it and/or modify it
7
// under the terms of the GNU Lesser General Public License as
8
// published by the Free Software Foundation, either version 3 of the
9
// License, or (at your option) any later version.
10
//
11
// TCPDF is distributed in the hope that it will be useful, but
12
// WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14
// See the GNU Lesser General Public License for more details.
15
//
16
// You should have received a copy of the License
17
// along with TCPDF. If not, see
18
// <http://www.tecnick.com/pagefiles/tcpdf/LICENSE.TXT>.
19
//
20
// See LICENSE.TXT file for more information.
21
namespace CbCaio\Boletos\Generators\Base;
22
23
abstract class BarcodeGenerator
24
{
25
    const TYPE_INTERLEAVED_2_5          = 'I25';
26
    const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+';
27
28
    /**
29
     * Get the barcode data
30
     *
31
     * @param string $code code to print
32
     * @param string $type type of barcode
33
     * @return array barcode array
34
     * @public
35
     */
36
    protected function getBarcodeData($code, $type)
37
    {
38
        switch (strtoupper($type))
39
        {
40
41
            case self::TYPE_INTERLEAVED_2_5:
0 ignored issues
show
Coding Style introduced by
CASE statements must be defined using a colon

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
42
            { // Interleaved 2 of 5
43
                $arrcode = $this->barcode_i25($code, FALSE);
44
                break;
45
            }
46
            case self::TYPE_INTERLEAVED_2_5_CHECKSUM:
0 ignored issues
show
Coding Style introduced by
CASE statements must be defined using a colon

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
47
            { // Interleaved 2 of 5 + CHECKSUM
48
                $arrcode = $this->barcode_i25($code, TRUE);
49
                break;
50
            }
51
            default:
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
52
            {
53
                $arrcode = FALSE;
54
                break;
55
            }
56
        }
57
58
        $arrcode = $this->convertBarcodeArrayToNewStyle($arrcode);
59
60
        return $arrcode;
61
    }
62
63
    /**
64
     * Interleaved 2 of 5 barcodes.
65
     * Compact numeric code, widely used in industry, air cargo
66
     * Contains digits (0 to 9) and encodes the data in the width of both bars and spaces.
67
     *
68
     * @param string $code     (string) code to represent.
69
     * @param        $checksum (boolean) if true add a checksum to the code
70
     * @return array barcode representation.
71
     * @protected
72
     */
73
    protected function barcode_i25($code, $checksum = FALSE)
74
    {
75
        $chr['0'] = '11221';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$chr was never initialized. Although not strictly required by PHP, it is generally a good practice to add $chr = 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...
76
        $chr['1'] = '21112';
77
        $chr['2'] = '12112';
78
        $chr['3'] = '22111';
79
        $chr['4'] = '11212';
80
        $chr['5'] = '21211';
81
        $chr['6'] = '12211';
82
        $chr['7'] = '11122';
83
        $chr['8'] = '21121';
84
        $chr['9'] = '12121';
85
        $chr['A'] = '11';
86
        $chr['Z'] = '21';
87
        if ($checksum)
88
        {
89
            // add checksum
90
            $code .= $this->checksum_s25($code);
91
        }
92
        if ((strlen($code) % 2) != 0)
93
        {
94
            // add leading zero if code-length is odd
95
            $code = '0' . $code;
96
        }
97
        // add start and stop codes
98
        $code = 'AA' . strtolower($code) . 'ZA';
99
100
        $bararray = ['code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => []];
101
        $k        = 0;
102
        $clen     = strlen($code);
103
        for ($i = 0; $i < $clen; $i = ($i + 2))
104
        {
105
            $char_bar   = $code{$i};
106
            $char_space = $code{$i + 1};
107
            if ((!isset($chr[ $char_bar ])) OR (!isset($chr[ $char_space ])))
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
108
            {
109
                // invalid character
110
                return FALSE;
111
            }
112
            // create a bar-space sequence
113
            $seq    = '';
114
            $chrlen = strlen($chr[ $char_bar ]);
115
            for ($s = 0; $s < $chrlen; $s++)
116
            {
117
                $seq .= $chr[ $char_bar ]{$s} . $chr[ $char_space ]{$s};
118
            }
119
            $seqlen = strlen($seq);
120
            for ($j = 0; $j < $seqlen; ++$j)
121
            {
122
                if (($j % 2) == 0)
123
                {
124
                    $t = TRUE; // bar
125
                } else
126
                {
127
                    $t = FALSE; // space
128
                }
129
                $w                       = $seq{$j};
130
                $bararray['bcode'][ $k ] = ['t' => $t, 'w' => $w, 'h' => 1, 'p' => 0];
131
                $bararray['maxw'] += $w;
132
                ++$k;
133
            }
134
        }
135
136
        return $bararray;
137
    }
138
    /**
139
     * Checksum for standard 2 of 5 barcodes.
140
     *
141
     * @param string $code (string) code to process.
142
     * @return int checksum.
143
     * @protected
144
     */
145
    protected function checksum_s25($code)
146
    {
147
        $len = strlen($code);
148
        $sum = 0;
149
        for ($i = 0; $i < $len; $i += 2)
150
        {
151
            $sum += $code{$i};
152
        }
153
        $sum *= 3;
154
        for ($i = 1; $i < $len; $i += 2)
155
        {
156
            $sum += ($code{$i});
157
        }
158
        $r = $sum % 10;
159
        if ($r > 0)
160
        {
161
            $r = (10 - $r);
162
        }
163
        return $r;
164
    }
165
166
    protected function convertBarcodeArrayToNewStyle($oldBarcodeArray)
167
    {
168
        $newBarcodeArray              = [];
169
        $newBarcodeArray['code']      = $oldBarcodeArray['code'];
170
        $newBarcodeArray['maxWidth']  = $oldBarcodeArray['maxw'];
171
        $newBarcodeArray['maxHeight'] = $oldBarcodeArray['maxh'];
172
        $newBarcodeArray['bars']      = [];
173
        foreach ($oldBarcodeArray['bcode'] as $oldbar)
174
        {
175
            $newBar                     = [];
176
            $newBar['width']            = $oldbar['w'];
177
            $newBar['height']           = $oldbar['h'];
178
            $newBar['positionVertical'] = $oldbar['p'];
179
            $newBar['drawBar']          = $oldbar['t'];
180
            $newBar['drawSpacing']      = !$oldbar['t'];
181
182
            $newBarcodeArray['bars'][] = $newBar;
183
        }
184
185
        return $newBarcodeArray;
186
    }
187
}
188