Skip to content

Commit

Permalink
Fix typing to allow for string returns from the API
Browse files Browse the repository at this point in the history
  • Loading branch information
sfreytag committed Jul 17, 2024
1 parent 5f763e6 commit bcdc3c5
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/ApiClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,13 @@ public function getHeadersWithAccessBearer()

/**
* Extract the data property from the mixed return of json_decode in a safe
* way for phpstan analysis.
* way for phpstan analysis. Use this when you expect the JSON data to be
* an array.
* @param mixed $content
* @return array<string, JsonType>
* @throws Exception
*/
public function getData($content)
public function getDataAsArray($content)
{
if (is_array($content) && array_key_exists('data', $content)) {
$data = $content['data'];
Expand All @@ -220,7 +221,27 @@ public function getData($content)
}
}

throw new \Exception('ApiModuleBase->getData: decoded JSON does not have a valid data property');
throw new \Exception('ApiModuleBase->getDataAsArray: decoded JSON does not have a valid data property');
}

/**
* Extract the data property from the mixed return of json_decode in a safe
* way for phpstan analysis. Use this when you expect the JSON data to be
* a string.
* @param mixed $content string
* @return string
* @throws Exception
*/
public function getDataAsString($content)
{
if (is_array($content) && array_key_exists('data', $content)) {
$data = $content['data'];
if (is_string($data)) {
return $data;
}
}

throw new \Exception('ApiModuleBase->getDataAsString: decoded JSON does not have a valid data property');
}

/**
Expand Down Expand Up @@ -291,7 +312,7 @@ public function exchange($tempToken)

$body = $response->getBody();
$content = json_decode($body, true);
$data = $this->getData($content);
$data = $this->getDataAsArray($content);
if (array_key_exists('access', $data) && array_key_exists('refresh', $data) && is_string($data['access']) && is_string($data['refresh'])) {
return [
'access' => $data['access'],
Expand Down Expand Up @@ -323,8 +344,8 @@ public function __construct($id, $secret, $host, $api)
* @todo Expose more properties such as description, latitidue and
* longitude. This can be done in July 2024.
*
* @param ListingDto $listing The new listing
* @return mixed The new listing as an associative array
* @param ListingDto $listing The new listing to send to CloudForest
* @return string The UUID of the resulting listing
* @throws GuzzleException
*/
public function create(ListingDto $listing)
Expand All @@ -343,7 +364,7 @@ public function create(ListingDto $listing)

$body = $response->getBody();
$content = json_decode($body, true);
$data = $this->getData($content);
$data = $this->getDataAsArray($content);

if (is_string($data['id'])) {
$userId = $data['id'];
Expand Down Expand Up @@ -385,7 +406,7 @@ public function create(ListingDto $listing)

$body = $response->getBody();
$content = json_decode($body, true);
$data = $this->getData($content);
$data = $this->getDataAsString($content);
return $data;
}
}
Expand Down Expand Up @@ -459,7 +480,7 @@ public function refresh(string $refresh)

$body = $response->getBody();
$content = json_decode($body, true);
$data = $this->getData($content);
$data = $this->getDataAsArray($content);
return $data;
}
}
Expand Down

0 comments on commit bcdc3c5

Please sign in to comment.