diff --git a/README.md b/README.md index d6a5a1b..156b305 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,16 @@ [![wikidata](https://raw.githubusercontent.com/maxlath/wikidata-cli/master/assets/wikidata_logo_alone.jpg)](https://wikidata.org) -# Wikidata [![Build Status](https://travis-ci.org/freearhey/wikidata.svg?branch=master)](https://travis-ci.org/freearhey/wikidata) +# Wikidata Wikidata provides a API for searching and retrieving data from [wikidata.org](https://www.wikidata.org). +Fork of https://github.com/freearhey/wikidata +@todo: consider https://github.com/freearhey/wikidata/compare/master...JamesFrost:wikidata:master + ## Installation ```sh -composer require freearhey/wikidata +composer require survos/wikidata ``` ## Usage @@ -208,7 +211,7 @@ vendor/bin/phpunit ### Contribution -If you find a bug or want to contribute to the code or documentation, you can help by submitting an [issue](https://github.com/freearhey/wikidata/issues) or a [pull request](https://github.com/freearhey/wikidata/pulls). +If you find a bug or want to contribute to the code or documentation, you can help by submitting an [issue](https://github.com/survos/wikidata/issues) or a [pull request](https://github.com/freearhey/wikidata/pulls). ### License diff --git a/composer.json b/composer.json index bea5296..11377a0 100644 --- a/composer.json +++ b/composer.json @@ -1,23 +1,28 @@ { - "name": "freearhey/wikidata", + "name": "survos/wikidata", "description": "A PHP client for working with Wikidata API.", "keywords": [ "wikidata", "client", "php" ], - "homepage": "https://github.com/freearhey/wikidata", + "homepage": "https://github.com/survos/wikidata", "license": "MIT", - "version": "3.6.0", + "version": "5.0.0", "authors": [ { "name": "Arhey", "role": "developer" + }, + { + "name": "tacman", + "role": "developer" } ], "require": { - "tightenco/collect": "^8.0", - "guzzlehttp/guzzle": "^7.0" + "php": "^8.2", + "illuminate/collections": "^10.0|^11.0", + "symfony/http-client": "^6.4||^7.0" }, "autoload": { "psr-4": { @@ -36,6 +41,8 @@ ] }, "require-dev": { - "phpunit/phpunit": "^9.0" + "phpunit/phpunit": "^9.0", + "phpstan/phpstan": "^1.10", + "rector/rector": "^1.1" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..562fa66 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: 4 +# inferPrivatePropertyTypeFromConstructor: true + paths: + - ./src/ diff --git a/rector.php b/rector.php new file mode 100644 index 0000000..0174aa4 --- /dev/null +++ b/rector.php @@ -0,0 +1,23 @@ +withPaths([ + __DIR__ . '/src', + __DIR__ . '/tests', + ]) + // uncomment to reach your current PHP version + ->withPhpSets(php82: true) + ->withSets([ + SetList::TYPE_DECLARATION, + ]) + ->withRules([ + TypedPropertyFromAssignsRector::class, + AddVoidReturnTypeWhereNoReturnRector::class, + ]); diff --git a/src/ApiClient.php b/src/ApiClient.php index 08c38ff..175947f 100644 --- a/src/ApiClient.php +++ b/src/ApiClient.php @@ -2,86 +2,83 @@ namespace Wikidata; -use GuzzleHttp\Client; +use Illuminate\Support\Collection; +use Symfony\Component\HttpClient\HttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; class ApiClient { - const API_ENDPOINT = 'https://www.wikidata.org/w/api.php'; + public const API_ENDPOINT = 'https://www.wikidata.org/w/api.php'; - /** - * @var string Value Id - */ - private $client; - /** - * @param array $data - */ - public function __construct() - { - $this->client = new Client(); - } + public function __construct(private ?HttpClientInterface $client=null) + { + if (!$this->client) { + $this->client = HttpClient::create(); + } + } - /** - * Get all entities by their ids from wikidata api - * - * @param string $ids The IDs of the entities to get the data from (eg.: Q2) - * @param string $lang Language (default: en) - * @param string $props Array of the properties to get back from each entity (supported: aliases, claims, datatype, descriptions, info, labels, sitelinks, sitelinks/urls) - * - * @return \Illuminate\Support\Collection - */ - public function getEntities($ids, $lang = 'en', $props = []) - { - $ids = is_array($ids) ? implode('|', $ids) : $ids; + /** + * Get all entities by their ids from wikidata api + * + * @param array|string $ids The IDs of the entities to get the data from (eg.: Q2, Q2|Q3) + * @param string $lang Language (default: en) + * @param array|string $props Array of the properties to get back from each entity (supported: aliases, claims, datatype, descriptions, info, labels, sitelinks, sitelinks/urls) + * + * @return \Illuminate\Support\Collection + */ + public function getEntities(array|string $ids, string $lang = 'en', array|string $props = []): Collection + { + $ids = is_array($ids) ? implode('|', $ids) : $ids; - $props = $props ? implode('|', $props) : null; + $props = $props ? implode('|', $props) : null; - $response = $this->client->get(self::API_ENDPOINT, [ - 'query' => [ - 'action' => 'wbgetentities', - 'format' => 'json', - 'languages' => $lang, - 'ids' => $ids, - 'sitefilter' => $lang . 'wiki', - 'props' => $props, - ], - ]); + $response = $this->client->request('GET', self::API_ENDPOINT, [ + 'query' => [ + 'action' => 'wbgetentities', + 'format' => 'json', + 'languages' => $lang, + 'ids' => $ids, + 'sitefilter' => $lang . 'wiki', + 'props' => $props, + ], + ]); - $results = json_decode($response->getBody(), true); + $results = json_decode($response->getContent(), true); - $data = isset($results['entities']) ? $results['entities'] : []; + $data = $results['entities'] ?? []; - return collect($data); - } + return collect($data); + } - /** - * Searches for entities using labels and aliases - * - * @param string $query - * @param string $lang Language (default: en) - * @param string $limit Max count of returning items (default: 10) - * - * @return \Illuminate\Support\Collection - */ - public function searchEntities($query, $lang = 'en', $limit = 10) - { - $response = $this->client->get(self::API_ENDPOINT, [ - 'query' => [ - 'action' => 'wbsearchentities', - 'format' => 'json', - 'strictlanguage' => true, - 'language' => $lang, - 'uselang' => $lang, - 'search' => $query, - 'limit' => $limit, - 'props' => '', - ], - ]); + /** + * Searches for entities using labels and aliases + * + * @param string $query + * @param string $lang Language (default: en) + * @param int $limit Max count of returning items (default: 10) + * + * @return \Illuminate\Support\Collection + */ + public function searchEntities($query, $lang = 'en', int $limit = 10) + { + $response = $this->client->request('GET', self::API_ENDPOINT, [ + 'query' => [ + 'action' => 'wbsearchentities', + 'format' => 'json', + 'strictlanguage' => true, + 'language' => $lang, + 'uselang' => $lang, + 'search' => $query, + 'limit' => $limit, + 'props' => '', + ], + ]); - $results = json_decode($response->getBody(), true); + $results = json_decode($response->getContent(), true); - $data = isset($results['search']) ? $results['search'] : []; + $data = $results['search'] ?? []; - return collect($data); - } + return collect($data); + } } diff --git a/src/Entity.php b/src/Entity.php index dc5aae4..a240e09 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -2,6 +2,7 @@ namespace Wikidata; +use Illuminate\Support\Collection; use Wikidata\Property; class Entity @@ -11,11 +12,6 @@ class Entity */ public $id; - /** - * @var string Entity language - */ - public $lang; - /** * @var string Entity label */ @@ -34,20 +30,17 @@ class Entity /** * @var string Entity description */ - public $description; + public string $description; - /** - * @var \Illuminate\Support\Collection Collection of entity properties - */ - public $properties = []; + public Collection $properties; /** * @param array $data * @param string $lang */ - public function __construct($data, $lang) + public function __construct($data, public $lang) { - $this->lang = $lang; + $this->properties = new Collection(); $this->parseData($data); } @@ -56,7 +49,7 @@ public function __construct($data, $lang) * * @param array $data */ - private function parseData($data) + private function parseData(array $data): void { $lang = $this->lang; $site = $lang . 'wiki'; @@ -71,19 +64,19 @@ private function parseData($data) /** * Parse entity properties from sparql result * - * @param array $data + * @param Collection $data */ - public function parseProperties($data) + public function parseProperties(array|Collection $data): void { - $collection = collect($data)->groupBy('prop'); - $this->properties = $collection->mapWithKeys(function ($item) { + $collection = (new Collection($data))->groupBy('prop'); + $this->properties = $collection->mapWithKeys(function ($item): array { $property = new Property($item); return [$property->id => $property]; }); } - public function toArray() + public function toArray(): array { return [ 'id' => $this->id, diff --git a/src/Property.php b/src/Property.php index b15d3e9..f3a69fb 100644 --- a/src/Property.php +++ b/src/Property.php @@ -31,18 +31,16 @@ public function __construct($data) /** * Parse input data - * + * * @param array $data */ - private function parseData($data) + private function parseData($data): void { $grouped = collect($data)->groupBy('statement'); $flatten = $grouped->flatten(1); $this->id = get_id($flatten[0]['prop']); $this->label = $flatten[0]['propertyLabel']; - $this->values = $grouped->values()->map(function($v) { - return new Value($v->toArray()); - }); + $this->values = $grouped->values()->map(fn($v): \Wikidata\Value => new Value($v->toArray())); } } diff --git a/src/Qualifier.php b/src/Qualifier.php index ceeb59b..bbafe5c 100644 --- a/src/Qualifier.php +++ b/src/Qualifier.php @@ -32,7 +32,7 @@ public function __construct($data) * * @param array $data */ - private function parseData($data) + private function parseData(array $data): void { $this->id = get_id($data['qualifier']); $this->label = $data['qualifierLabel']; diff --git a/src/SearchResult.php b/src/SearchResult.php index 56d3d81..f0f0a59 100644 --- a/src/SearchResult.php +++ b/src/SearchResult.php @@ -9,11 +9,6 @@ class SearchResult */ public $id; - /** - * @var string - */ - public $lang; - /** * @var string */ @@ -36,19 +31,19 @@ class SearchResult /** * @param array $data + * @param string $lang */ - public function __construct($data, $lang = 'en') + public function __construct($data, public $lang = 'en') { $this->parseData($data); - $this->lang = $lang; } - private function parseData($data) + private function parseData($data): void { - $this->id = isset($data['id']) ? $data['id'] : null; - $this->label = isset($data['label']) ? $data['label'] : null; - $this->aliases = isset($data['aliases']) ? $data['aliases'] : []; - $this->description = isset($data['description']) ? $data['description'] : null; - $this->wiki_url = isset($data['wiki_url']) ? $data['wiki_url'] : null; + $this->id = $data['id'] ?? null; + $this->label = $data['label'] ?? null; + $this->aliases = $data['aliases'] ?? []; + $this->description = $data['description'] ?? null; + $this->wiki_url = $data['wiki_url'] ?? null; } } diff --git a/src/SparqlClient.php b/src/SparqlClient.php index c170d03..8443706 100644 --- a/src/SparqlClient.php +++ b/src/SparqlClient.php @@ -3,32 +3,41 @@ namespace Wikidata; use Exception; -use GuzzleHttp\Client; +use Symfony\Component\HttpClient\HttpClient; +use Symfony\Contracts\HttpClient\HttpClientInterface; class SparqlClient { + public function __construct( + private ?HttpClientInterface $client=null + ) + { + if (!$this->client) { + $this->client = HttpClient::create(); + } + } + /** * Limit on how long can be the query to be sent by GET. */ - const MAX_GET_SIZE = 2048; + public const MAX_GET_SIZE = 2048; /** * SPARQL endpoint URL * @var string */ - const SPARQL_ENDPOINT = 'https://query.wikidata.org/sparql'; + public const SPARQL_ENDPOINT = 'https://query.wikidata.org/sparql'; /** * Query timeout (seconds) - * @var int */ - private $timeout = 30; + private int $timeout = 30; /** * Request method - * @var array + * @var string */ - private $method = 'GET'; + private string $method = 'GET'; /** * Query SPARQL endpoint @@ -38,7 +47,6 @@ class SparqlClient { * * @return array List of results, one row per array element * Each row will contain fields indexed by variable name. - * @throws SparqlException */ public function execute( $query, $rawData = false ) { @@ -47,9 +55,7 @@ public function execute( $query, $rawData = false ) { $this->method = 'POST'; } - $client = new Client(); - - $response = $client->request( $this->method, self::SPARQL_ENDPOINT, [ + $response = $this->client->request( $this->method, self::SPARQL_ENDPOINT, [ 'query' => [ "query" => $query, "format" => "json", @@ -63,7 +69,7 @@ public function execute( $query, $rawData = false ) { throw new Exception( 'HTTP Error' ); } - $result = $response->getBody(); + $result = $response->getContent(); $data = json_decode( $result, true ); @@ -85,7 +91,7 @@ public function execute( $query, $rawData = false ) { * * @return array List of results, one row per element. */ - private function extractData( $data, $rawData = false ) { + private function extractData( array $data, $rawData = false ): array { $result = []; if ( $data && !empty( $data['results'] ) ) { $vars = $data['head']['vars']; diff --git a/src/Value.php b/src/Value.php index 2215dd0..31aebd9 100644 --- a/src/Value.php +++ b/src/Value.php @@ -34,11 +34,11 @@ public function __construct($data) * * @param array $data */ - private function parseData($data) + private function parseData(array $data): void { $this->id = get_id($data[0]['propertyValue']); $this->label = $data[0]['propertyValueLabel']; - $this->qualifiers = collect($data)->map(function($item) { + $this->qualifiers = collect($data)->map(function(array $item): ?\Wikidata\Qualifier { if($item['qualifier']) { return new Qualifier($item); } diff --git a/src/Wikidata.php b/src/Wikidata.php index d14abfb..08da2ef 100644 --- a/src/Wikidata.php +++ b/src/Wikidata.php @@ -15,11 +15,11 @@ class Wikidata * * @param string $query * @param string $lang Language (default: en) - * @param string $limit Max count of returning items (default: 10) + * @param int $limit Max count of returning items (default: 10) * * @return \Illuminate\Support\Collection Return collection of \Wikidata\SearchResult */ - public function search($query, $lang = 'en', $limit = 10) + public function search($query, $lang = 'en', int $limit = 10) { $client = new ApiClient(); @@ -29,7 +29,7 @@ public function search($query, $lang = 'en', $limit = 10) $entities = $client->getEntities($ids, $lang, ['sitelinks/urls', 'aliases', 'descriptions', 'labels']); - $output = $entities->map(function ($item) use ($lang) { + $output = $entities->map(function ($item) use ($lang): \Wikidata\SearchResult { $entity = new Entity($item, $lang); return new SearchResult($entity->toArray(), $lang); }); @@ -43,11 +43,11 @@ public function search($query, $lang = 'en', $limit = 10) * @param string $property Wikidata ID of property (e.g.: P646) * @param string $value String value of property or Wikidata entity ID (e.g.: Q11696) * @param string $lang Language (default: en) - * @param string $limit Max count of returning items (default: 10) + * @param int $limit Max count of returning items (default: 10) * * @return \Illuminate\Support\Collection Return collection of \Wikidata\SearchResult */ - public function searchBy($property, $value = null, $lang = 'en', $limit = 10) + public function searchBy(string $property, $value = null, $lang = 'en', int $limit = 10) { if (!is_pid($property)) { throw new Exception("First argument in searchBy() must be a valid Wikidata property ID (e.g.: P646).", 1); @@ -69,15 +69,13 @@ public function searchBy($property, $value = null, $lang = 'en', $limit = 10) $data = $client->execute($query); - $ids = collect($data)->map(function ($data) { - return str_replace("http://www.wikidata.org/entity/", "", $data['item']); - })->toArray(); + $ids = collect($data)->map(fn($data): string|array => str_replace("http://www.wikidata.org/entity/", "", $data['item']))->toArray(); $client = new ApiClient(); $entities = $client->getEntities($ids, $lang, ['sitelinks/urls', 'aliases', 'descriptions', 'labels']); - $output = $entities->map(function ($data) use ($lang) { + $output = $entities->map(function ($data) use ($lang): \Wikidata\SearchResult { $entity = new Entity($data, $lang); return new SearchResult($entity->toArray(), $lang); }); @@ -93,7 +91,7 @@ public function searchBy($property, $value = null, $lang = 'en', $limit = 10) * * @return \Wikidata\Entity Return entity */ - public function get($entityId, $lang = 'en') + public function get(string $entityId, string $lang = 'en'): \Wikidata\Entity { if (!is_qid($entityId)) { throw new Exception("First argument in get() must by a valid Wikidata entity ID (e.g.: Q646).", 1); @@ -120,9 +118,7 @@ public function get($entityId, $lang = 'en') $data = $client->execute($query); - if (isset($data)) { - $entity->parseProperties($data); - } + $entity->parseProperties($data); return $entity; } diff --git a/src/helpers.php b/src/helpers.php index bf12d43..305d1ca 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -1,4 +1,5 @@ entity = new Entity($this->dummy[0], $this->lang); } - public function testGetEntityId() + public function testGetEntityId(): void { $this->assertEquals($this->dummy[0]['id'], $this->entity->id); } - public function testGetEntityLang() + public function testGetEntityLang(): void { $this->assertEquals($this->lang, $this->entity->lang); } - public function testGetEntityLabel() + public function testGetEntityLabel(): void { $this->assertEquals($this->dummy[0]['labels'][$this->lang]['value'], $this->entity->label); } - public function testGetEntityDescription() + public function testGetEntityDescription(): void { $this->assertEquals($this->dummy[0]['descriptions'][$this->lang]['value'], $this->entity->description); } - public function testGetWikiUrl() + public function testGetWikiUrl(): void { $this->assertEquals($this->dummy[0]['sitelinks'][$this->lang . 'wiki']['url'], $this->entity->wiki_url); } - public function testGetEntityAliases() + public function testGetEntityAliases(): void { $aliases = collect($this->dummy[0]['aliases'][$this->lang])->pluck('value')->toArray(); $this->assertEquals($aliases, $this->entity->aliases); } - public function testGetEntityWithoutAliases() + public function testGetEntityWithoutAliases(): void { $entity = new Entity($this->dummy[1], 'en'); $this->assertEquals([], $entity->aliases); } - public function testGetEntityProperties() + public function testGetEntityProperties(): void { $this->entity->parseProperties($this->dummyProperties); $properties = $this->entity->properties; - $this->assertInstanceOf('Illuminate\Support\Collection', $properties); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $properties); - $this->assertInstanceOf('Wikidata\Property', $properties->first()); + $this->assertInstanceOf(\Wikidata\Property::class, $properties->first()); } } diff --git a/tests/Unit/PropertyTest.php b/tests/Unit/PropertyTest.php index 0df05e7..e93655e 100644 --- a/tests/Unit/PropertyTest.php +++ b/tests/Unit/PropertyTest.php @@ -13,24 +13,24 @@ public function setUp(): void $this->property = new Property($this->dummyProperties); } - public function testGetPropertyId() + public function testGetPropertyId(): void { $id = str_replace('http://www.wikidata.org/prop/', '', $this->dummyProperties[0]['prop']); $this->assertEquals($id, $this->property->id); } - public function testGetPropertyLabel() + public function testGetPropertyLabel(): void { $this->assertEquals($this->dummyProperties[0]['propertyLabel'], $this->property->label); } - public function testGetPropertyValues() + public function testGetPropertyValues(): void { $values = $this->property->values; - $this->assertInstanceOf('Illuminate\Support\Collection', $values); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $values); - $this->assertInstanceOf('Wikidata\Value', $values->first()); + $this->assertInstanceOf(\Wikidata\Value::class, $values->first()); } } diff --git a/tests/Unit/QualifierTest.php b/tests/Unit/QualifierTest.php index e638ad4..2e0b6d6 100644 --- a/tests/Unit/QualifierTest.php +++ b/tests/Unit/QualifierTest.php @@ -13,19 +13,19 @@ public function setUp(): void $this->qualifier = new Qualifier($this->dummyProperties[0]); } - public function testGetQualifierId() + public function testGetQualifierId(): void { $id = str_replace('http://www.wikidata.org/entity/', '', $this->dummyProperties[0]['qualifier']); $this->assertEquals($id, $this->qualifier->id); } - public function testGetQualifierLabel() + public function testGetQualifierLabel(): void { $this->assertEquals($this->dummyProperties[0]['qualifierLabel'], $this->qualifier->label); } - public function testGetQualifierValue() + public function testGetQualifierValue(): void { $this->assertEquals($this->dummyProperties[0]['qualifierValueLabel'], $this->qualifier->value); } diff --git a/tests/Unit/SearchResultTest.php b/tests/Unit/SearchResultTest.php index 1c78f63..2e22b73 100644 --- a/tests/Unit/SearchResultTest.php +++ b/tests/Unit/SearchResultTest.php @@ -28,32 +28,32 @@ public function setUp(): void $this->result = new SearchResult($collection); } - public function testGetResultId() + public function testGetResultId(): void { $this->assertEquals($this->dummy['id'], $this->result->id); } - public function testGetResultLang() + public function testGetResultLang(): void { $this->assertEquals($this->dummy['lang'], $this->result->lang); } - public function testGetResultLabel() + public function testGetResultLabel(): void { $this->assertEquals($this->dummy['label'], $this->result->label); } - public function testGetResultWikiUrl() + public function testGetResultWikiUrl(): void { $this->assertEquals($this->dummy['wiki_url'], $this->result->wiki_url); } - public function testGetResultAliases() + public function testGetResultAliases(): void { $this->assertEquals($this->dummy['aliases'], $this->result->aliases); } - public function testGetResultDescription() + public function testGetResultDescription(): void { $this->assertEquals($this->dummy['description'], $this->result->description); } diff --git a/tests/Unit/ValueTest.php b/tests/Unit/ValueTest.php index 1deaaa3..4e2528a 100644 --- a/tests/Unit/ValueTest.php +++ b/tests/Unit/ValueTest.php @@ -13,24 +13,24 @@ public function setUp(): void $this->value = new Value($this->dummyProperties); } - public function testGetValueId() + public function testGetValueId(): void { $id = str_replace('http://www.wikidata.org/entity/', '', $this->dummyProperties[0]['propertyValue']); $this->assertEquals($id, $this->value->id); } - public function testGetValueLabel() + public function testGetValueLabel(): void { $this->assertEquals($this->dummyProperties[0]['propertyValueLabel'], $this->value->label); } - public function testGetValueQualifiers() + public function testGetValueQualifiers(): void { $qualifiers = $this->value->qualifiers; - $this->assertInstanceOf('Illuminate\Support\Collection', $qualifiers); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $qualifiers); - $this->assertInstanceOf('Wikidata\Qualifier', $qualifiers->first()); + $this->assertInstanceOf(\Wikidata\Qualifier::class, $qualifiers->first()); } } diff --git a/tests/Unit/WikidataTest.php b/tests/Unit/WikidataTest.php index 515b981..cd5875f 100644 --- a/tests/Unit/WikidataTest.php +++ b/tests/Unit/WikidataTest.php @@ -7,22 +7,22 @@ class WikidataTest extends TestCase { - protected $wikidata; + protected Wikidata $wikidata; public function setUp(): void { $this->wikidata = new Wikidata(); } - public function testSearchByTerm() + public function testSearchByTerm(): void { $results = $this->wikidata->search('London'); - $this->assertInstanceOf('Illuminate\Support\Collection', $results); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $results); $result = $results->first(); - $this->assertInstanceOf('Wikidata\SearchResult', $result); + $this->assertInstanceOf(\Wikidata\SearchResult::class, $result); $this->assertEquals(true, property_exists($result, 'id')); $this->assertEquals(true, property_exists($result, 'lang')); @@ -32,38 +32,39 @@ public function testSearchByTerm() $this->assertEquals(true, property_exists($result, 'wiki_url')); } - public function testSearchOnAnotherLanguage() + public function testSearchOnAnotherLanguage(): void { $results = $this->wikidata->search('London', 'fr'); $this->assertEquals('fr', $results->first()->lang); } - public function testSearchWithLimit() + public function testSearchWithLimit(): void { $results = $this->wikidata->search('car', 'en', 10); $this->assertEquals(10, $results->count()); } - public function testSearchResultsCouldBeEmpty() + public function testSearchResultsCouldBeEmpty(): void { $results = $this->wikidata->search('asdfgh'); - $this->assertInstanceOf('Illuminate\Support\Collection', $results); + + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $results); $this->assertEquals(true, $results->isEmpty()); } - public function testSearchByPropertyIdAndValue() + public function testSearchByPropertyIdAndValue(): void { $results = $this->wikidata->searchBy('P646', '/m/02mjmr'); - $this->assertInstanceOf('Illuminate\Support\Collection', $results); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $results); $result = $results->first(); - $this->assertInstanceOf('Wikidata\SearchResult', $result); + $this->assertInstanceOf(\Wikidata\SearchResult::class, $result); $this->assertEquals(true, property_exists($result, 'id')); $this->assertEquals(true, property_exists($result, 'lang')); @@ -73,36 +74,36 @@ public function testSearchByPropertyIdAndValue() $this->assertEquals(true, property_exists($result, 'wiki_url')); } - public function testSearchByThrowExceptionIfSecondPropertyMissing() + public function testSearchByThrowExceptionIfSecondPropertyMissing(): void { $this->expectException(Exception::class); $this->wikidata->searchBy('P646'); } - public function testSearchByThrowExceptionIfPropertyIdInvalid() + public function testSearchByThrowExceptionIfPropertyIdInvalid(): void { $this->expectException(Exception::class); $this->wikidata->searchBy('Pasd', '/m/02mjmr'); } - public function testSearchByPropertyIdAndEntityId() + public function testSearchByPropertyIdAndEntityId(): void { $results = $this->wikidata->searchBy('P39', 'Q11696'); - $this->assertInstanceOf('Illuminate\Support\Collection', $results); + $this->assertInstanceOf(\Illuminate\Support\Collection::class, $results); $result = $results->first(); - $this->assertInstanceOf('Wikidata\SearchResult', $result); + $this->assertInstanceOf(\Wikidata\SearchResult::class, $result); } - public function testGetEntityById() + public function testGetEntityById(): void { $entity = $this->wikidata->get('Q44077'); - $this->assertInstanceOf('Wikidata\Entity', $entity); + $this->assertInstanceOf(\Wikidata\Entity::class, $entity); $this->assertEquals(true, property_exists($entity, 'id')); $this->assertEquals(true, property_exists($entity, 'lang')); @@ -112,14 +113,14 @@ public function testGetEntityById() $this->assertEquals(true, property_exists($entity, 'wiki_url')); } - public function testGetEntityOnAnotherLanguage() + public function testGetEntityOnAnotherLanguage(): void { $entity = $this->wikidata->get('Q44077', 'es'); $this->assertEquals('es', $entity->lang); } - public function testGetEntityThrowExceptionIfEntityIdInvalid() + public function testGetEntityThrowExceptionIfEntityIdInvalid(): void { $this->expectException(Exception::class);