Skip to content

Commit

Permalink
Merge pull request #7 from ColonelMoutarde/master
Browse files Browse the repository at this point in the history
Upgrade code to php7.3 minimum version
  • Loading branch information
allebb authored Apr 5, 2023
2 parents 908d7b2 + 56c1261 commit 03e1ccb
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 28 deletions.
10 changes: 6 additions & 4 deletions lib/Helpers/MetarHTTPClient.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<?php

declare(strict_types=1);

namespace Ballen\Metar\Helpers;

use GuzzleHttp\Client as HttpClient;
use \Exception;

/**
* Metar
Expand All @@ -29,17 +31,17 @@ class MetarHTTPClient
* HTTP Client
* @param array $config Optional Guzzle configuration.
*/
public function __construct($config = [])
public function __construct(array $config = [])
{
$this->guzzleConf = $config;
}

/**
* Make a HTTP request and retrieve the body.
* @param string $url The URL to request
* @return \Psr\Http\Message\StreamInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getMetarAPIResponse($url)
public function getMetarAPIResponse(string $url): string
{
$client = new HttpClient($this->guzzleConf);
$response = $client->get($url);
Expand Down
15 changes: 9 additions & 6 deletions lib/Metar.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Ballen\Metar;

/**
Expand All @@ -19,7 +22,7 @@ class Metar
/**
* Stores the default namespace for loading new METAR providers from.
*/
const DEFAULT_PROVIDER = 'Ballen\Metar\Providers\Noaa';
public const DEFAULT_PROVIDER = 'Ballen\Metar\Providers\Noaa';

/**
* Stores the requested airfield/port ICAO code.
Expand All @@ -29,7 +32,7 @@ class Metar

/**
* The METAR string retrieved from the web service.
* @var string The raw METAR string retrieved from the web service.
* @var string The raw METAR string retrieved from the web service.
*/
private $metar;

Expand All @@ -43,7 +46,7 @@ class Metar
* Initiates a new METAR object.
* @param string $icao The airfeild/airport ICAO code.
*/
public function __construct($icao)
public function __construct(string $icao)
{

// We force the format of the ICAO code to be upper case!
Expand Down Expand Up @@ -73,9 +76,9 @@ public function __toString()
* @param string $icao ICAO code, eg. EGSS
* @throws \InvalidArgumentException
*/
private function validateIcao($icao)
private function validateIcao(string $icao): void
{
if (strlen($icao) != 4) {
if (strlen($icao) !== 4) {
throw new \InvalidArgumentException('ICAO code does not appear to be a valid format');
}
}
Expand All @@ -84,7 +87,7 @@ private function validateIcao($icao)
* Changes the default 'NOAA' METAR service provider to another one eg. 'VATSIM'.
* @param string $provider METAR Provider Class/File name.
*/
public function setProvider($provider = self::DEFAULT_PROVIDER)
public function setProvider(string $provider = self::DEFAULT_PROVIDER): self
{
if (!class_exists($provider)) {
throw new \InvalidArgumentException('The service provider your specified does not exist in the namespace \'' . $provider . '\'');
Expand Down
15 changes: 8 additions & 7 deletions lib/Providers/Ivao.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Ballen\Metar\Providers;

/**
Expand All @@ -21,23 +24,21 @@
*/
class Ivao extends MetarHTTPClient implements MetarProviderInterface
{

private $serviceUrl = 'http://wx.ivao.aero/metar.php/?id={{_ICAO_}}';
private $icao;

public function __construct($icao)
public function __construct(string $icao)
{
$this->icao = $icao;
}

private function getMetarDataString()
/** @throws \GuzzleHttp\Exception\GuzzleException */
private function getMetarDataString(): string
{

$data = $this->getMetarAPIResponse(str_replace('{{_ICAO_}}', $this->icao, $this->serviceUrl));
return $data;
return $this->getMetarAPIResponse(str_replace('{{_ICAO_}}', $this->icao, $this->serviceUrl));
}

public function __toString()
public function __toString(): string
{
return $this->getMetarDataString();
}
Expand Down
8 changes: 5 additions & 3 deletions lib/Providers/MetarProviderInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Ballen\Metar\Providers;

/**
Expand All @@ -15,8 +18,7 @@
*/
interface MetarProviderInterface
{
public function __construct(string $icao);

function __construct($icao);

function __toString();
public function __toString();
}
8 changes: 6 additions & 2 deletions lib/Providers/Noaa.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Ballen\Metar\Providers;

/**
Expand All @@ -25,12 +28,13 @@ class Noaa extends MetarHTTPClient implements MetarProviderInterface
private $serviceUrl = 'http://tgftp.nws.noaa.gov/data/observations/metar/stations/{{_ICAO_}}.TXT';
private $icao;

public function __construct($icao)
public function __construct(string $icao)
{
$this->icao = $icao;
}

private function getMetarDataString()
/** @throws \GuzzleHttp\Exception\GuzzleException */
private function getMetarDataString(): string
{

$data = $this->getMetarAPIResponse(str_replace('{{_ICAO_}}', $this->icao, $this->serviceUrl));
Expand Down
8 changes: 6 additions & 2 deletions lib/Providers/Vatsim.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Ballen\Metar\Providers;

/**
Expand All @@ -25,12 +28,13 @@ class Vatsim extends MetarHTTPClient implements MetarProviderInterface
private $serviceUrl = 'http://metar.vatsim.net/metar.php?id={{_ICAO_}}';
private $icao;

public function __construct($icao)
public function __construct(string $icao)
{
$this->icao = $icao;
}

private function getMetarDataString()
/** @throws \GuzzleHttp\Exception\GuzzleException */
private function getMetarDataString(): string
{
$data = $this->getMetarAPIResponse(str_replace('{{_ICAO_}}', $this->icao, $this->serviceUrl));
return trim($data);
Expand Down
11 changes: 7 additions & 4 deletions tests/MetarHttpClientTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
use GuzzleHttp\Handler\MockHandler;

declare(strict_types=1);

use G0uzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -38,7 +41,7 @@ class MetarHttpClientTest extends TestCase
/**
* Test recieving a METAR report.
*/
public function testDownloadReport()
public function testDownloadReport(): void
{
$mock = new MockHandler([
new Response(200, [], self::EXAMPLE_METAR_REPORT),
Expand All @@ -51,7 +54,7 @@ public function testDownloadReport()
/**
* Test recieving an invalid report.
*/
public function testInvalidReport()
public function testInvalidReport(): void
{
$mock = new MockHandler([
new Response(404),
Expand All @@ -65,7 +68,7 @@ public function testInvalidReport()
/**
* Test recieving an unprocessable response (eg. a 502 response).
*/
public function testServiceUnavailableRepsonse()
public function testServiceUnavailableRepsonse(): void
{
$mock = new MockHandler([
new Response(502),
Expand Down
3 changes: 3 additions & 0 deletions tests/MetarTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

use Ballen\Metar\Metar;
use PHPUnit\Framework\TestCase;

Expand Down

0 comments on commit 03e1ccb

Please sign in to comment.