Completed
Push — master ( 1e6292...4ff32b )
by Philip
02:04
created

AbstractCRC8::generateTable()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 22
Ratio 100 %

Importance

Changes 0
Metric Value
dl 22
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 14
nc 4
nop 1
1
<?php
2
3
namespace PBurggraf\CRC\CRC8;
4
5
use PBurggraf\CRC\AbstractCRC;
6
7
/**
8
 * @author Philip Burggraf <[email protected]>
9
 */
10
abstract class AbstractCRC8 extends AbstractCRC
11
{
12
    /**
13
     * @param string $buffer
14
     *
15
     * @return int
16
     */
17
    public function calculate(string $buffer): int
18
    {
19
        $crc = $this->init;
20
21
        $bufferLength = strlen($buffer);
22
23
        for ($bufferPosition = 0; $bufferPosition < $bufferLength; ++$bufferPosition) {
24
            $character = ord($buffer[$bufferPosition]);
25
26
            if ($this->reverseIn) {
27
                $character = $this->binaryReverse($character, 8);
28
            }
29
30
            $crc = $this->lookupTable[($crc ^ $character) & 0xff];
31
        }
32
33
        if ($this->reverseOut) {
34
            $crc = $this->binaryReverse($crc, 8);
35
        }
36
37
        return $crc ^ $this->xorOut;
38
    }
39
40
    /**
41
     * @param int $polynomial
42
     *
43
     * @return array
44
     */
45 View Code Duplication
    public function generateTable(int $polynomial): array
0 ignored issues
show
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...
46
    {
47
        $tableSize = 256;
48
49
        $table = [];
50
51
        for ($iterator = 0; $iterator < $tableSize; ++$iterator) {
52
            $temp = 0;
53
            $a = $iterator;
54
            for ($j = 0; $j < 8; ++$j) {
55
                if ((($temp ^ $a) & 0x80) !== 0) {
56
                    $temp = (($temp << 1) ^ $polynomial);
57
                } else {
58
                    $temp <<= 1;
59
                }
60
                $a <<= 1;
61
            }
62
            $table[$iterator] = $temp & 0xff;
63
        }
64
65
        return $table;
66
    }
67
}
68