Completed
Push — master ( 751599...c13dee )
by Osma
02:10 queued 10s
created

WDQSResource   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 2
lcom 1
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 37 2
1
<?php
2
3
class WDQSResource extends RemoteResource
4
{
5
    
6
    const WDQS_ENDPOINT = "https://query.wikidata.org/sparql";
7
8
    public function resolve(int $timeout) : ?EasyRdf\Resource {
9
        $client = new EasyRdf\Sparql\Client(self::WDQS_ENDPOINT);
10
        try {
11
            // unregister the legacy "json" format as it causes problems with CONSTRUCT requests
12
            EasyRdf\Format::unregister('json');
13
            // change the timeout setting for external requests
14
            $httpclient = EasyRdf\Http::getDefaultHttpClient();
15
            $httpclient->setConfig(array('timeout' => $timeout));
16
            $httpclient->setHeaders('Accept', 'text/turtle');
17
            EasyRdf\Http::setDefaultHttpClient($httpclient);
18
            
19
            $uri = $this->uri;
20
            $query = <<<EOQ
21
PREFIX wd:     <http://www.wikidata.org/entity/>
22
PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>
23
PREFIX schema: <http://schema.org/>
24
25
CONSTRUCT {
26
  <$uri> rdfs:label ?label .
27
  ?link schema:about <$uri> .
28
}
29
WHERE
30
{
31
  { <$uri> rdfs:label ?label }
32
  UNION
33
  { ?link schema:about <$uri> }
34
}
35
EOQ;
36
37
            $client = new EasyRdf\Sparql\Client(self::WDQS_ENDPOINT);
38
            $graph = $client->query($query);
39
            return $graph->resource($this->uri);
0 ignored issues
show
Bug introduced by
The method resource does only exist in EasyRdf\Graph, but not in EasyRdf\Http\Response and EasyRdf\Sparql\Result.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
40
        } catch (Exception $e) {
41
            $this->model->getLogger()->info("WDQS resolution failed for <{$this->uri}>: $e");
42
            return null;
43
        }
44
    }
45
}
46