Completed
Push — master ( 825f24...2aed2e )
by Henri
04:32 queued 22s
created

SkosmosTurtleParser   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 1
dl 0
loc 136
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getNamespaces() 0 4 1
A peek() 0 10 3
A read() 0 16 3
A unread() 0 11 3
A unskipWS() 0 16 4
B parseTriples() 0 39 4
1
<?php
2
3
class SkosmosTurtleParser extends EasyRdf_Parser_Turtle
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    private $bytePos = 0;
6
    private $dataLength = null;
7
8
    /**
9
     * Returns the namespace prefixes as an array of prefix => URI
10
     * @return array $namespaces
11
     */
12
    public function getNamespaces()
13
    {
14
        return $this->namespaces;
15
    }
16
17
    /**
18
     * A lot faster since we're now only reading the next 4 bytes 
19
     * instead of the whole string.
20
     * Gets the next character to be returned by read().
21
     */
22
    protected function peek()
23
    {
24
        if(!$this->dataLength) { $this->dataLength = strlen($this->data); }
25
        if ($this->dataLength > $this->bytePos) {
26
            $slice = substr($this->data, $this->bytePos, 4);
27
            return mb_substr($slice, 0, 1, "UTF-8");
28
        } else {
29
            return -1;
30
        }
31
    }
32
33
    /**
34
     * Does not manipulate the data variable. Keeps track of the
35
     * byte position instead.
36
     * Read a single character from the input buffer.
37
     * Returns -1 when the end of the file is reached.
38
     */
39
    protected function read()
40
    {
41
        $char = $this->peek();
42
        if ($char == -1) {
43
            return -1;
44
        }
45
        $this->bytePos += strlen($char);
46
        // Keep tracks of which line we are on (0A = Line Feed)
47
        if ($char == "\x0A") {
48
            $this->line += 1;
49
            $this->column = 1;
50
        } else {
51
            $this->column += 1;
52
        }
53
        return $char;
54
    }
55
56
    /**
57
     * Steps back, restoring the previous character or statement read() to the input buffer.
58
     */
59
    protected function unread($chars)
60
    {
61
        $this->column -= mb_strlen($chars, "UTF-8");
62
        $this->bytePos -= strlen($chars);
63
        if ($this->bytePos < 0) {
64
            $this->bytePos = 0;
65
        }
66
        if ($this->column < 1) {
67
            $this->column = 1;
68
        }
69
    }
70
71
72
    /**
73
     * Reverse skips through whitespace in 4 byte increments.
74
     * (Keeps the byte pointer accurate when unreading.)
75
     * @ignore
76
     */
77
    protected function unskipWS()
78
    {
79
        if ($this->bytePos - 4 > 0) {
80
            $slice = substr($this->data, $this->bytePos - 4, 4);
81
            while($slice != '') {
82
                if (!self::isWhitespace(mb_substr($slice, -1, 1, "UTF-8"))) {
83
                    return;
84
                }
85
                $slice = substr($slice, 0, -1);
86
                $this->bytePos -= 1;
87
            }
88
            // This 4 byte slice was full of whitespace.
89
            // We need to check that there isn't more in the next slice.
90
            $this->unSkipWS();
91
        }
92
    }
93
94
    /**
95
     * Parse triples with unskipWS (doesn't loose the pointer position in blank nodes).
96
     * @ignore
97
     */
98
    protected function parseTriples()
99
    {
100
        $c = $this->peek();
101
102
        // If the first character is an open bracket we need to decide which of
103
        // the two parsing methods for blank nodes to use
104
        if ($c == '[') {
105
            $c = $this->read();
0 ignored issues
show
Unused Code introduced by
$c 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...
106
            $this->skipWSC();
107
            $c = $this->peek();
108
            if ($c == ']') {
109
                $c = $this->read();
0 ignored issues
show
Unused Code introduced by
$c 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...
110
                $this->subject = $this->createBNode();
111
                $this->skipWSC();
112
                $this->parsePredicateObjectList();
113
            } else {
114
                $this->unskipWS();
115
                $this->unread('[');
116
                $this->subject = $this->parseImplicitBlank();
117
            }
118
            $this->skipWSC();
119
            $c = $this->peek();
120
121
            // if this is not the end of the statement, recurse into the list of
122
            // predicate and objects, using the subject parsed above as the subject
123
            // of the statement.
124
            if ($c != '.') {
125
                $this->parsePredicateObjectList();
126
            }
127
        } else {
128
            $this->parseSubject();
129
            $this->skipWSC();
130
            $this->parsePredicateObjectList();
131
        }
132
133
        $this->subject = null;
134
        $this->predicate = null;
135
        $this->object = null;
136
    }
137
138
}
139