Skip to content

Commit

Permalink
Fix PhpStan issues (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
trejjam authored Dec 27, 2021
1 parent c4d98dc commit da19d9e
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 43 deletions.
3 changes: 3 additions & 0 deletions src/Entity/AEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ abstract class AEntity
*/
protected $readOnly = [];

/**
* @var array<string, array<class-string>|class-string>
*/
protected $associations = [];

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Entity/Entries.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
final class Entries extends Schematic\Entries
{
/**
* @return Schematic\Entry[]
* @return array<string, Schematic\Entry>
*/
public function toArray() : array
{
Expand Down
20 changes: 10 additions & 10 deletions src/Group/Lists.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(Request $apiRequest)
*/
public function getAll(?PaginationOption $paginationOption = null) : EntityLists
{
return $this->apiRequest->get($this->getEndpointPath(), EntityLists::class, $paginationOption);
return $this->apiRequest->getTyped($this->getEndpointPath(), EntityLists::class, $paginationOption);
}

public function getAllIterator(string $listId) : \Generator
Expand Down Expand Up @@ -75,7 +75,7 @@ public function getAllIterator(string $listId) : \Generator
public function get(string $listId) : ListItem
{
try {
return $this->apiRequest->get($this->getListEndpointPath($listId), ListItem::class);
return $this->apiRequest->getTyped($this->getListEndpointPath($listId), ListItem::class);
} catch (ClientException $clientException) {
throw new ListNotFoundException("List '{$listId}' not found", $clientException);
}
Expand All @@ -88,7 +88,7 @@ public function get(string $listId) : ListItem
public function getMembers(string $listId, ?PaginationOption $paginationOption = null) : EntityMemberLists
{
try {
return $this->apiRequest->get($this->getMemberEndpointPath($listId), EntityMemberLists::class, $paginationOption);
return $this->apiRequest->getTyped($this->getMemberEndpointPath($listId), EntityMemberLists::class, $paginationOption);
} catch (ClientException $clientException) {
throw new ListNotFoundException("List '{$listId}' not found", $clientException);
}
Expand Down Expand Up @@ -126,7 +126,7 @@ public function getMembersIterator(string $listId) : \Generator
public function getMember(string $listId, string $memberHash) : MemberItem
{
try {
return $this->apiRequest->get($this->getOneMemberEndpointPath($listId, $memberHash), MemberItem::class);
return $this->apiRequest->getTyped($this->getOneMemberEndpointPath($listId, $memberHash), MemberItem::class);
} catch (ClientException $clientException) {
throw new MemberNotFoundException("Member '{$memberHash}' not found in the list '{$listId}'", $clientException);
}
Expand All @@ -139,7 +139,7 @@ public function getMember(string $listId, string $memberHash) : MemberItem
public function addMember(MemberItem $memberItem) : MemberItem
{
try {
return $this->apiRequest->put(
return $this->apiRequest->putTyped(
$this->getOneMemberEndpointPath(
$memberItem->list_id,
$memberItem->id
Expand All @@ -159,7 +159,7 @@ public function addMember(MemberItem $memberItem) : MemberItem
public function updateMember(MemberItem $memberItem) : MemberItem
{
try {
return $this->apiRequest->patch(
return $this->apiRequest->patchTyped(
$this->getOneMemberEndpointPath(
$memberItem->list_id,
$memberItem->id
Expand Down Expand Up @@ -219,7 +219,7 @@ public function removePermanentMember(MemberItem $memberItem) : ?array
public function getSegments(string $listId, ?PaginationOption $paginationOption = null) : EntitySegmentLists
{
try {
return $this->apiRequest->get($this->getSegmentEndpointPath($listId), EntitySegmentLists::class, $paginationOption);
return $this->apiRequest->getTyped($this->getSegmentEndpointPath($listId), EntitySegmentLists::class, $paginationOption);
} catch (ClientException $clientException) {
throw new ListNotFoundException("List '{$listId}' not found", $clientException);
}
Expand Down Expand Up @@ -257,7 +257,7 @@ public function getSegmentsIterator(string $listId) : \Generator
public function getSegment(string $listId, int $segmentId) : Segment
{
try {
return $this->apiRequest->get($this->getOneSegmentEndpointPath($listId, $segmentId), Segment::class);
return $this->apiRequest->getTyped($this->getOneSegmentEndpointPath($listId, $segmentId), Segment::class);
} catch (ClientException $clientException) {
throw new ListNotFoundException("Segment '{$segmentId}' not found in the list '{$listId}'", $clientException);
}
Expand All @@ -275,7 +275,7 @@ public function addSegment(string $listId, string $segmentName) : Segment
}

try {
return $this->apiRequest->post(
return $this->apiRequest->postTyped(
$this->getSegmentEndpointPath($listId),
[
'name' => $segmentName,
Expand All @@ -295,7 +295,7 @@ public function addSegment(string $listId, string $segmentName) : Segment
public function addSegmentMember(int $segmentId, MemberItem $memberItem) : MemberItem
{
try {
return $this->apiRequest->post(
return $this->apiRequest->postTyped(
$this->getOneSegmentEndpointPath($memberItem->list_id, $segmentId) . self::GROUP_MEMBER_PREFIX,
[
'email_address' => $memberItem->email_address,
Expand Down
2 changes: 1 addition & 1 deletion src/Group/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public function __construct(Request $apiRequest)
*/
public function get() : EntityRoot
{
return $this->apiRequest->get('/', EntityRoot::class);
return $this->apiRequest->getTyped('/', EntityRoot::class);
}
}
116 changes: 89 additions & 27 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
namespace Trejjam\MailChimp;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
use Schematic\Entry;
use Trejjam\MailChimp\Exception\RequestException;

final class Request
Expand All @@ -32,74 +32,121 @@ public function __construct(
Client $httpClient,
string $apiUrl,
string $apiKey
) {
)
{
$this->httpClient = $httpClient;
$this->apiUrl = $apiUrl;
$this->apiKey = $apiKey;
}

/**
* @return array|Entry|mixed
* @template T
* @param class-string<T> $endpointClass
* @return T
* @throws JsonException
* @throws RequestException
* @throws GuzzleException
*/
public function getTyped(string $endpointPath, string $endpointClass, ?PaginationOption $paginationOption = null)
{
return $this->makeTypedRequest(__FUNCTION__, $endpointPath, $endpointClass, [], $paginationOption);
}

/**
* @template T
* @param class-string<T> $endpointClass
* @return T
* @throws JsonException
* @throws RequestException
* @throws GuzzleException
*/
public function get(string $endpointPath, ?string $endpointClass = null, ?PaginationOption $paginationOption = null)
public function putTyped(string $endpointPath, array $body, string $endpointClass)
{
return $this->makeRequest(__FUNCTION__, $endpointPath, $endpointClass, [], $paginationOption);
return $this->makeTypedRequest(__FUNCTION__, $endpointPath, $endpointClass, [
RequestOptions::BODY => Json::encode($body),
]);
}

/**
* @return array|mixed|Entry
* @template T
* @param class-string<T> $endpointClass
* @return T
* @throws JsonException
* @throws RequestException
* @throws GuzzleException
*/
public function put(string $endpointPath, array $body, ?string $endpointClass = null)
public function patchTyped(string $endpointPath, array $body, string $endpointClass)
{
return $this->makeRequest(__FUNCTION__, $endpointPath, $endpointClass, [
return $this->makeTypedRequest(__FUNCTION__, $endpointPath, $endpointClass, [
RequestOptions::BODY => Json::encode($body),
]);
}

/**
* @return array|mixed|Entry
* @return array
* @throws JsonException
* @throws RequestException
* @throws GuzzleException
*/
public function patch(string $endpointPath, array $body, ?string $endpointClass = null)
public function post(string $endpointPath, array $body)
{
return $this->makeRequest(__FUNCTION__, $endpointPath, $endpointClass, [
return $this->makeRequest(__FUNCTION__, $endpointPath, [
RequestOptions::BODY => Json::encode($body),
]);
}

/**
* @return array|mixed|Entry
* @template T
* @param class-string<T> $endpointClass
* @return T
* @throws JsonException
* @throws RequestException
* @throws GuzzleException
*/
public function post(string $endpointPath, array $body, ?string $endpointClass = null)
public function postTyped(string $endpointPath, array $body, string $endpointClass)
{
return $this->makeRequest(__FUNCTION__, $endpointPath, $endpointClass, [
return $this->makeTypedRequest(__FUNCTION__, $endpointPath, $endpointClass, [
RequestOptions::BODY => Json::encode($body),
]);
}

/**
* @return array|Entry|mixed
* @return array
* @throws JsonException
* @throws RequestException
* @throws GuzzleException
*/
public function delete(string $endpointPath, ?string $endpointClass = null)
public function delete(string $endpointPath)
{
return $this->makeRequest(__FUNCTION__, $endpointPath, $endpointClass);
return $this->makeRequest(__FUNCTION__, $endpointPath);
}

/**
* @return array|Entry|mixed
* @template T
* @param class-string<T> $endpointClass
* @return T
* @throws JsonException
* @throws RequestException
* @throws GuzzleException
*/
public function deleteTyped(string $endpointPath, string $endpointClass)
{
return $this->makeTypedRequest(__FUNCTION__, $endpointPath, $endpointClass);
}

/**
* @return array
* @throws JsonException
* @throws RequestException
* @throws GuzzleException
*/
private function makeRequest(
string $method,
string $endpointPath,
?string $endpointClass = null,
array $requestOptions = [],
string $method,
string $endpointPath,
array $requestOptions = [],
?PaginationOption $paginationOption = null
) {
)
{
$mergedRequestOptions = array_merge_recursive(
[
RequestOptions::AUTH => [self::API_USER, $this->apiKey],
Expand All @@ -124,11 +171,26 @@ private function makeRequest(
))->setResponse($response);
}

$returnArray = Json::decode($response->getBody()->getContents(), Json::FORCE_ARRAY);
return (array)Json::decode($response->getBody()->getContents(), Json::FORCE_ARRAY);
}

if ($endpointClass === null || $endpointClass === '') {
return $returnArray;
}
/**
* @template T
* @param class-string<T> $endpointClass
* @return T
* @throws JsonException
* @throws RequestException
* @throws GuzzleException
*/
private function makeTypedRequest(
string $method,
string $endpointPath,
string $endpointClass,
array $requestOptions = [],
?PaginationOption $paginationOption = null
)
{
$returnArray = $this->makeRequest($method, $endpointPath, $requestOptions, $paginationOption);

return new $endpointClass($returnArray);
}
Expand Down
7 changes: 3 additions & 4 deletions tests/config/phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ parameters:
checkMissingIterableValueType: false
treatPhpDocTypesAsCertain: false
ignoreErrors:
- '~^Property Trejjam\\MailChimp\\Entity\\[a-zA-Z\\]+::\$[a-z]+ has no typehint specified\.$~'
-
message: '~^Method Trejjam\\MailChimp\\Entity\\AEntity::__set\(\) has parameter \$value with no typehint specified\.$~'
message: '~^Method Trejjam\\MailChimp\\Entity\\AEntity::__set\(\) has parameter \$value with no type specified\.$~'
path: %currentWorkingDirectory%/src/Entity/AEntity.php
-
message: '~^Method Trejjam\\MailChimp\\Entity\\AEntity::__get\(\) has no return typehint specified\.$~'
message: '~^Method Trejjam\\MailChimp\\Entity\\AEntity::__get\(\) has no return type specified\.$~'
path: %currentWorkingDirectory%/src/Entity/AEntity.php
-
message: '~^Method Trejjam\\MailChimp\\Entity\\Entries::toArray\(\) should return array<Schematic\\Entry> but returns array<array|Schematic\\Entry>\.$~'
message: '~^Method Trejjam\\MailChimp\\Entity\\Entries::toArray\(\) should return array<string, Schematic\\Entry> but returns array<array\|Schematic\\Entry>\.$~'
path: %currentWorkingDirectory%/src/Entity/Entries.php

earlyTerminatingMethodCalls:
Expand Down

0 comments on commit da19d9e

Please sign in to comment.