Skip to content

Commit

Permalink
PLT-71 Fix Undefined array key in remove triple (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrliptontea authored Dec 5, 2023
1 parent e7c7b58 commit 428acd7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/classes/ExtendedGraph.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public function get_first_literal($s, $p, $default = null, $preferred_language =
* @return string the first resource value found or the supplied default if no values were found
*/
public function get_first_resource($s, $p, $default = null) {
if ( array_key_exists($s, $this->_index) && array_key_exists($p, $this->_index[$s]) ) {
if (isset($this->_index[$s][$p])) {
foreach ($this->_index[$s][$p] as $value) {
if ($value['type'] == 'uri' || $value['type'] == 'bnode' ) {
return $value['value'];
Expand All @@ -483,7 +483,12 @@ public function get_first_resource($s, $p, $default = null) {
* @param string $p the predicate URI of the triple
* @param string $o the object of the triple, either a URI or a blank node in the format _:name
*/
public function remove_resource_triple( $s, $p, $o) {
public function remove_resource_triple($s, $p, $o) {
// Already removed
if (!isset($this->_index[$s]) || !isset($this->_index[$s][$p])) {
return;
}

for ($i = count($this->_index[$s][$p]) - 1; $i >= 0; $i--) {
if (($this->_index[$s][$p][$i]['type'] == 'uri' || $this->_index[$s][$p][$i]['type'] == 'bnode') && $this->_index[$s][$p][$i]['value'] == $o) {
array_splice($this->_index[$s][$p], $i, 1);
Expand All @@ -493,10 +498,10 @@ public function remove_resource_triple( $s, $p, $o) {
if (count($this->_index[$s][$p]) == 0) {
unset($this->_index[$s][$p]);
}

if (count($this->_index[$s]) == 0) {
unset($this->_index[$s]);
}

}

/**
Expand All @@ -505,6 +510,11 @@ public function remove_resource_triple( $s, $p, $o) {
* @param string $o
*/
public function remove_literal_triple($s, $p, $o) {
// Already removed
if (!isset($this->_index[$s]) || !isset($this->_index[$s][$p])) {
return;
}

for ($i = count($this->_index[$s][$p]) - 1; $i >= 0; $i--) {
if ($this->_index[$s][$p][$i]['type'] == 'literal' && $this->_index[$s][$p][$i]['value'] == $o) {
array_splice($this->_index[$s][$p], $i, 1);
Expand All @@ -514,10 +524,10 @@ public function remove_literal_triple($s, $p, $o) {
if (count($this->_index[$s][$p]) == 0) {
unset($this->_index[$s][$p]);
}

if (count($this->_index[$s]) == 0) {
unset($this->_index[$s]);
}

}

/**
Expand Down
62 changes: 62 additions & 0 deletions test/unit/ExtendedGraphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,68 @@ public function testRemoveProperties()
$this->assertFalse($graph->subject_has_property('http://some/subject/3','http://some/predicate/to/remove'), 'should have removed triple about subject 3');
}

public function testGetFirstResource()
{
$graph = new ExtendedGraph();

$graph->add_literal_triple('http://some/subject/1', 'http://some/predicate', 'value 1');
$graph->add_resource_triple('http://some/subject/1', 'http://some/predicate', 'http://value/2');

$this->assertEquals('http://value/2', $graph->get_first_resource('http://some/subject/1', 'http://some/predicate'), 'should have returned first resource');
$this->assertEquals(null, $graph->get_first_resource('http://some/subject/2', 'http://other/predicate'), 'should have returned default value');
$this->assertEquals('my default', $graph->get_first_resource('http://some/subject/3', 'http://other/predicate', 'my default'), 'should have returned default value');
}

public function testRemoveResourceTriple()
{
$graph = new ExtendedGraph();

// Add some triples
$graph->add_resource_triple('http://some/subject/1', 'http://some/predicate', 'http://value/1');
$graph->add_resource_triple('http://some/subject/2', 'http://some/predicate', 'http://value/2');
$this->assertEquals(2, $graph->get_triple_count(), 'should have 2 triples');

// Try to remove triples that don't exist
$graph->remove_resource_triple('http://some/subject/3', 'http://some/predicate', 'http://value/3');
$graph->remove_literal_triple('http://some/subject/3', 'http://some/predicate', 'value 3');
$this->assertEquals(2, $graph->get_triple_count(), 'should have 2 triples');

// Remove a triple that does exist
$graph->remove_resource_triple('http://some/subject/1', 'http://some/predicate', 'http://value/1');
$this->assertEquals(1, $graph->get_triple_count(), 'should have 1 triple');

// Remove the last triple
$graph->remove_resource_triple('http://some/subject/2', 'http://some/predicate', 'http://value/2');
$this->assertEquals(0, $graph->get_triple_count(), 'should have 0 triples');
$this->assertTrue($graph->is_empty(), 'should be empty');
$this->assertEquals([], $graph->get_index(), 'should have empty index');
}

public function testRemoveLiteralTriple()
{
$graph = new ExtendedGraph();

// Add some triples
$graph->add_literal_triple('http://some/subject/1', 'http://some/predicate', 'value 1');
$graph->add_literal_triple('http://some/subject/2', 'http://some/predicate', 'value 2');
$this->assertEquals(2, $graph->get_triple_count(), 'should have 2 triples');

// Try to remove triples that don't exist
$graph->remove_literal_triple('http://some/subject/3', 'http://some/predicate', 'value 3');
$graph->remove_resource_triple('http://some/subject/3', 'http://some/predicate', 'http://value/3');
$this->assertEquals(2, $graph->get_triple_count(), 'should have 2 triples');

// Remove a triple that does exist
$graph->remove_literal_triple('http://some/subject/1', 'http://some/predicate', 'value 1');
$this->assertEquals(1, $graph->get_triple_count(), 'should have 1 triple');

// Remove the last triple
$graph->remove_literal_triple('http://some/subject/2', 'http://some/predicate', 'value 2');
$this->assertEquals(0, $graph->get_triple_count(), 'should have 0 triples');
$this->assertTrue($graph->is_empty(), 'should be empty');
$this->assertEquals([], $graph->get_index(), 'should have empty index');
}

public function testGetResourceProperties()
{
$graph = new ExtendedGraph();
Expand Down

0 comments on commit 428acd7

Please sign in to comment.