Type API methods and properties (#14476)

* Type properties

* Comment method

* Update base_uri child property

* Update BingApi.php

* Update GoogleMapsApi.php

* Update MapquestApi.php

* Update NominatimApi.php

* Update RipeApi.php

* Update phpstan-baseline.neon

* Update phpstan-baseline.neon

* Fix indent

* Fix escaping

* Update phpstan-baseline.neon

* Update phpstan-baseline.neon

* Update phpstan-baseline.neon

* Update phpstan-baseline.neon

* Update phpstan-baseline.neon

* Update phpstan-baseline.neon

* Update phpstan-baseline.neon

* Update phpstan-baseline.neon

* StyleCI indent

* Update phpstan-baseline.neon

* Make possible for $client to be null

* Remove comments

* Remove comments

* Remove comments

* Update MapquestApi.php

* Update NominatimApi.php

* Remove comments

* Remove comments

* $base_uri not nullable

* $base_uri not nullable

* $base_uri not nullable

* $base_uri not nullable

* $base_uri not nullable

* $base_uri not nullable

* Type method and properties

* Type method and properties

* Type method and properties

* Type method and properties

* Type method and properties

* Type method and properties

* Type $client

* Type method and properties

* Remove errors not matched anymore

* Fix type errors in graylogapi

* Mixed can't be ORed

* uri never null

* Update app/ApiClients/GraylogApi.php

* Fix getAdresses Type

* Collection changed its folder?

* Fix directory, there was just a backslash missing

Co-authored-by: Tony Murray <murraytony@gmail.com>
This commit is contained in:
Félix Bouynot
2022-11-02 01:06:15 +01:00
committed by GitHub
parent 70d37adf66
commit b59cf980ca
9 changed files with 37 additions and 177 deletions

View File

@@ -30,10 +30,9 @@ use LibreNMS\Util\Proxy;
class BaseApi
{
protected $base_uri;
/** @var int */
protected $timeout = 3;
private $client;
protected string $base_uri;
protected int $timeout = 3;
private ?\Illuminate\Http\Client\PendingRequest $client;
protected function getClient(): \Illuminate\Http\Client\PendingRequest
{

View File

@@ -34,16 +34,13 @@ class BingApi extends BaseApi implements Geocoder
{
use GeocodingHelper;
protected $base_uri = 'http://dev.virtualearth.net';
protected $geocoding_uri = '/REST/v1/Locations';
protected string $base_uri = 'http://dev.virtualearth.net';
protected string $geocoding_uri = '/REST/v1/Locations';
/**
* Get latitude and longitude from geocode response
*
* @param array $data
* @return array
*/
protected function parseLatLng($data)
protected function parseLatLng(array $data): array
{
return [
'lat' => isset($data['resourceSets'][0]['resources'][0]['point']['coordinates'][0]) ? $data['resourceSets'][0]['resources'][0]['point']['coordinates'][0] : 0,
@@ -54,12 +51,9 @@ class BingApi extends BaseApi implements Geocoder
/**
* Build Guzzle request option array
*
* @param string $address
* @return array
*
* @throws \Exception you may throw an Exception if validation fails
*/
protected function buildGeocodingOptions($address)
protected function buildGeocodingOptions(string $address): array
{
$api_key = Config::get('geoloc.api_key');
if (! $api_key) {

View File

@@ -34,16 +34,13 @@ class GoogleMapsApi extends BaseApi implements Geocoder
{
use GeocodingHelper;
protected $base_uri = 'https://maps.googleapis.com';
protected $geocoding_uri = '/maps/api/geocode/json';
protected string $base_uri = 'https://maps.googleapis.com';
protected string $geocoding_uri = '/maps/api/geocode/json';
/**
* Get latitude and longitude from geocode response
*
* @param array $data
* @return array
*/
protected function parseLatLng($data)
protected function parseLatLng(array $data): array
{
return [
'lat' => isset($data['results'][0]['geometry']['location']['lat']) ? $data['results'][0]['geometry']['location']['lat'] : 0,
@@ -53,11 +50,8 @@ class GoogleMapsApi extends BaseApi implements Geocoder
/**
* Get messages from response.
*
* @param array $data
* @return array
*/
protected function parseMessages($data)
protected function parseMessages(array $data): array
{
return [
'error' => isset($data['error_message']) ? $data['error_message'] : '',
@@ -68,12 +62,9 @@ class GoogleMapsApi extends BaseApi implements Geocoder
/**
* Build Guzzle request option array
*
* @param string $address
* @return array
*
* @throws \Exception you may throw an Exception if validation fails
*/
protected function buildGeocodingOptions($address)
protected function buildGeocodingOptions(string $address): array
{
$api_key = Config::get('geoloc.api_key');
if (! $api_key) {

View File

@@ -31,8 +31,8 @@ use LibreNMS\Config;
class GraylogApi
{
private $client;
private $api_prefix = '';
private Client $client;
private string $api_prefix = '';
public function __construct(array $config = [])
{
@@ -56,7 +56,7 @@ class GraylogApi
$this->client = new Client($config);
}
public function getStreams()
public function getStreams(): array
{
if (! $this->isConfigured()) {
return [];
@@ -72,16 +72,8 @@ class GraylogApi
/**
* Query the Graylog server
*
* @param string $query
* @param int $range
* @param int $limit
* @param int $offset
* @param string $sort field:asc or field:desc
* @param string $filter
* @return array
*/
public function query($query = '*', $range = 0, $limit = 0, $offset = 0, $sort = null, $filter = null)
public function query(string $query = '*', int $range = 0, int $limit = 0, int $offset = 0, ?string $sort = null, ?string $filter = null): array
{
if (! $this->isConfigured()) {
return [];
@@ -109,12 +101,8 @@ class GraylogApi
/**
* Build a simple query string that searches the messages field and/or filters by device
*
* @param string $search Search the message field for this string
* @param Device $device
* @return string
*/
public function buildSimpleQuery($search = null, $device = null)
public function buildSimpleQuery(?string $search = null, ?Device $device = null): string
{
$query = [];
if ($search) {
@@ -132,7 +120,7 @@ class GraylogApi
return implode(' && ', $query);
}
public function getAddresses(Device $device)
public function getAddresses(Device $device): \Illuminate\Support\Collection
{
$addresses = collect([
gethostbyname($device->hostname),
@@ -158,8 +146,8 @@ class GraylogApi
return $addresses->filter()->unique();
}
public function isConfigured()
public function isConfigured(): bool
{
return isset($this->client->getConfig()['base_uri']);
return (bool) Config::get('graylog.server');
}
}

View File

@@ -34,16 +34,13 @@ class MapquestApi extends BaseApi implements Geocoder
{
use GeocodingHelper;
protected $base_uri = 'https://open.mapquestapi.com';
protected $geocoding_uri = '/geocoding/v1/address';
protected string $base_uri = 'https://open.mapquestapi.com';
protected string $geocoding_uri = '/geocoding/v1/address';
/**
* Get latitude and longitude from geocode response
*
* @param array $data
* @return array
*/
protected function parseLatLng($data)
protected function parseLatLng(array $data): array
{
return [
'lat' => isset($data['results'][0]['locations'][0]['latLng']['lat']) ? $data['results'][0]['locations'][0]['latLng']['lat'] : 0,
@@ -54,12 +51,9 @@ class MapquestApi extends BaseApi implements Geocoder
/**
* Build Guzzle request option array
*
* @param string $address
* @return array
*
* @throws \Exception you may throw an Exception if validation fails
*/
protected function buildGeocodingOptions($address)
protected function buildGeocodingOptions(string $address): array
{
$api_key = Config::get('geoloc.api_key');
if (! $api_key) {

View File

@@ -31,16 +31,13 @@ class NominatimApi extends BaseApi implements Geocoder
{
use GeocodingHelper;
protected $base_uri = 'https://nominatim.openstreetmap.org';
protected $geocoding_uri = '/search';
protected string $base_uri = 'https://nominatim.openstreetmap.org';
protected string $geocoding_uri = '/search';
/**
* Get latitude and longitude from geocode response
*
* @param array $data
* @return array
*/
protected function parseLatLng($data)
protected function parseLatLng(array $data): array
{
return [
'lat' => isset($data[0]['lat']) ? $data[0]['lat'] : 0,
@@ -51,12 +48,9 @@ class NominatimApi extends BaseApi implements Geocoder
/**
* Build Guzzle request option array
*
* @param string $address
* @return array
*
* @throws \Exception you may throw an Exception if validation fails
*/
protected function buildGeocodingOptions($address)
protected function buildGeocodingOptions(string $address): array
{
return [
'query' => [

View File

@@ -29,15 +29,12 @@ use LibreNMS\Config;
class Oxidized extends BaseApi
{
/**
* @var bool if Oxidized is enabled
*/
private $enabled;
private bool $enabled;
public function __construct()
{
$this->timeout = 90;
$this->base_uri = Config::get('oxidized.url');
$this->base_uri = Config::get('oxidized.url') ?? '';
$this->enabled = Config::get('oxidized.enabled') === true && $this->base_uri;
}

View File

@@ -30,20 +30,17 @@ use LibreNMS\Exceptions\ApiException;
class RipeApi extends BaseApi
{
protected $base_uri = 'https://stat.ripe.net';
protected string $base_uri = 'https://stat.ripe.net';
protected $whois_uri = '/data/whois/data.json';
protected $abuse_uri = '/data/abuse-contact-finder/data.json';
protected string $whois_uri = '/data/whois/data.json';
protected string $abuse_uri = '/data/abuse-contact-finder/data.json';
/**
* Get whois info
*
* @param string $resource ASN/IPv4/IPv6
* @return array
*
* @throws ApiException
*/
public function getWhois($resource)
public function getWhois(string $resource): array
{
return $this->makeApiCall($this->whois_uri, [
'query' => [
@@ -55,12 +52,9 @@ class RipeApi extends BaseApi
/**
* Get Abuse contact
*
* @param string $resource prefix, single IP address or ASN
* @return array|mixed
*
* @throws ApiException
*/
public function getAbuseContact($resource)
public function getAbuseContact(string $resource): mixed
{
return $this->makeApiCall($this->abuse_uri, [
'query' => [
@@ -70,11 +64,9 @@ class RipeApi extends BaseApi
}
/**
* @return array|mixed
*
* @throws ApiException
*/
private function makeApiCall(string $uri, array $options)
private function makeApiCall(string $uri, array $options): mixed
{
try {
$response_data = $this->getClient()->get($uri, $options)->json();

View File

@@ -6365,96 +6365,6 @@ parameters:
count: 1
path: app/Actions/Device/ValidateDeviceAndCreate.php
-
message: "#^Property App\\\\ApiClients\\\\BaseApi\\:\\:\\$base_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/BaseApi.php
-
message: "#^Property App\\\\ApiClients\\\\BaseApi\\:\\:\\$client has no type specified\\.$#"
count: 1
path: app/ApiClients/BaseApi.php
-
message: "#^Property App\\\\ApiClients\\\\BingApi\\:\\:\\$base_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/BingApi.php
-
message: "#^Property App\\\\ApiClients\\\\BingApi\\:\\:\\$geocoding_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/BingApi.php
-
message: "#^Property App\\\\ApiClients\\\\GoogleMapsApi\\:\\:\\$base_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/GoogleMapsApi.php
-
message: "#^Property App\\\\ApiClients\\\\GoogleMapsApi\\:\\:\\$geocoding_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/GoogleMapsApi.php
-
message: "#^Method App\\\\ApiClients\\\\GraylogApi\\:\\:getAddresses\\(\\) has no return type specified\\.$#"
count: 1
path: app/ApiClients/GraylogApi.php
-
message: "#^Method App\\\\ApiClients\\\\GraylogApi\\:\\:getStreams\\(\\) has no return type specified\\.$#"
count: 1
path: app/ApiClients/GraylogApi.php
-
message: "#^Method App\\\\ApiClients\\\\GraylogApi\\:\\:isConfigured\\(\\) has no return type specified\\.$#"
count: 1
path: app/ApiClients/GraylogApi.php
-
message: "#^Property App\\\\ApiClients\\\\GraylogApi\\:\\:\\$api_prefix has no type specified\\.$#"
count: 1
path: app/ApiClients/GraylogApi.php
-
message: "#^Property App\\\\ApiClients\\\\GraylogApi\\:\\:\\$client has no type specified\\.$#"
count: 1
path: app/ApiClients/GraylogApi.php
-
message: "#^Property App\\\\ApiClients\\\\MapquestApi\\:\\:\\$base_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/MapquestApi.php
-
message: "#^Property App\\\\ApiClients\\\\MapquestApi\\:\\:\\$geocoding_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/MapquestApi.php
-
message: "#^Property App\\\\ApiClients\\\\NominatimApi\\:\\:\\$base_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/NominatimApi.php
-
message: "#^Property App\\\\ApiClients\\\\NominatimApi\\:\\:\\$geocoding_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/NominatimApi.php
-
message: "#^Property App\\\\ApiClients\\\\RipeApi\\:\\:\\$abuse_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/RipeApi.php
-
message: "#^Property App\\\\ApiClients\\\\RipeApi\\:\\:\\$base_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/RipeApi.php
-
message: "#^Property App\\\\ApiClients\\\\RipeApi\\:\\:\\$whois_uri has no type specified\\.$#"
count: 1
path: app/ApiClients/RipeApi.php
-
message: "#^Method App\\\\Checks\\:\\:postAuth\\(\\) has no return type specified\\.$#"
count: 1
@@ -11400,3 +11310,4 @@ parameters:
count: 1
path: tests/YamlSchemaTest.php