From e05c1b4660d4c50c1954051e1483eb4bddb389ac Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 11 May 2023 15:14:22 +0200 Subject: [PATCH] Cleanup --- .gitignore | 2 +- .php_cs => .php-cs-fixer.dist.php | 2 +- composer.json | 4 ++-- phpstan.neon | 8 ++++---- src/Conversations/ConversationLinks.php | 16 ++++++++-------- src/Conversations/CustomField.php | 3 ++- src/Conversations/Status.php | 12 ++++++------ src/Customers/CustomerLinks.php | 12 ++++++------ src/Customers/Entry/CustomerEntryEndpoint.php | 2 +- src/Endpoint.php | 8 ++++---- src/Entity/PagedCollection.php | 4 ++-- src/Http/Hal/HalDeserializer.php | 4 ++-- src/Http/Hal/HalLink.php | 12 ++++++------ src/Http/RestClient.php | 3 ++- src/Http/RestClientBuilder.php | 3 +++ src/Mailboxes/MailboxLinks.php | 4 ++-- .../AuthenticationIntegrationTest.php | 7 ++----- .../Threads/Support/HasCustomerTest.php | 2 +- .../Support/HasPartiesToBeNotifiedTest.php | 2 +- .../Threads/Support/HasUserTest.php | 2 +- tests/Http/Hal/Entity/StubPayloads.php | 5 ----- tests/Http/RestClientTest.php | 1 + 22 files changed, 58 insertions(+), 60 deletions(-) rename .php_cs => .php-cs-fixer.dist.php (92%) diff --git a/.gitignore b/.gitignore index 64588ece..f755727d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ -.php_cs.cache +.php-cs-fixer.cache phpunit.xml composer.lock coverage diff --git a/.php_cs b/.php-cs-fixer.dist.php similarity index 92% rename from .php_cs rename to .php-cs-fixer.dist.php index 69038727..d84e0ca0 100644 --- a/.php_cs +++ b/.php-cs-fixer.dist.php @@ -4,7 +4,7 @@ ->in(__DIR__.'/src') ->in(__DIR__.'/tests'); -return PhpCsFixer\Config::create() +return (new PhpCsFixer\Config()) ->setFinder($finder) ->setRules([ '@Symfony' => true, diff --git a/composer.json b/composer.json index 561f900b..48384ba0 100644 --- a/composer.json +++ b/composer.json @@ -20,8 +20,8 @@ }, "require-dev": { "phpunit/phpunit": "^8.4", - "phpstan/phpstan": "^0.10.1", - "friendsofphp/php-cs-fixer": "^2.3", + "phpstan/phpstan": "^1.10", + "friendsofphp/php-cs-fixer": "^3.4", "mockery/mockery": "^1.1", "ramsey/uuid": "^4.1" }, diff --git a/phpstan.neon b/phpstan.neon index d604515c..3d47d111 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,5 @@ parameters: - ignoreErrors: - # Ignore type errors for passing a Mock - - '#.*MockObject given.*#' - - '#.*MockInterface given.*#' + level: 4 + paths: + - src + - tests diff --git a/src/Conversations/ConversationLinks.php b/src/Conversations/ConversationLinks.php index 083cd23e..813a90de 100644 --- a/src/Conversations/ConversationLinks.php +++ b/src/Conversations/ConversationLinks.php @@ -6,12 +6,12 @@ class ConversationLinks { - const MAILBOX = 'mailbox'; - const PRIMARY_CUSTOMER = 'primaryCustomer'; - const CREATED_BY_CUSTOMER = 'createdByCustomer'; - const CREATED_BY_USER = 'createdByUser'; - const CLOSED_BY = 'closedBy'; - const THREADS = 'threads'; - const ASSIGNEE = 'assignee'; - const WEB = 'web'; + public const MAILBOX = 'mailbox'; + public const PRIMARY_CUSTOMER = 'primaryCustomer'; + public const CREATED_BY_CUSTOMER = 'createdByCustomer'; + public const CREATED_BY_USER = 'createdByUser'; + public const CLOSED_BY = 'closedBy'; + public const THREADS = 'threads'; + public const ASSIGNEE = 'assignee'; + public const WEB = 'web'; } diff --git a/src/Conversations/CustomField.php b/src/Conversations/CustomField.php index 81a08b3a..5f50bcca 100644 --- a/src/Conversations/CustomField.php +++ b/src/Conversations/CustomField.php @@ -104,7 +104,8 @@ public function setValue($value): self return $this; } - public function getText(): ?string { + public function getText(): ?string + { return $this->text; } diff --git a/src/Conversations/Status.php b/src/Conversations/Status.php index b5f0c3fb..e3e69640 100644 --- a/src/Conversations/Status.php +++ b/src/Conversations/Status.php @@ -9,10 +9,10 @@ class Status /** * We're using the verbiage "ANY" instead of "ALL" because a conversation can only have a single status, not multiple. */ - const ANY = 'all'; - const ACTIVE = 'active'; - const CLOSED = 'closed'; - const OPEN = 'open'; - const PENDING = 'pending'; - const SPAM = 'spam'; + public const ANY = 'all'; + public const ACTIVE = 'active'; + public const CLOSED = 'closed'; + public const OPEN = 'open'; + public const PENDING = 'pending'; + public const SPAM = 'spam'; } diff --git a/src/Customers/CustomerLinks.php b/src/Customers/CustomerLinks.php index eb0f4685..e046b90f 100644 --- a/src/Customers/CustomerLinks.php +++ b/src/Customers/CustomerLinks.php @@ -6,10 +6,10 @@ class CustomerLinks { - const ADDRESS = 'address'; - const CHATS = 'chats'; - const EMAILS = 'emails'; - const PHONES = 'phones'; - const SOCIAL_PROFILES = 'social-profiles'; - const WEBSITES = 'websites'; + public const ADDRESS = 'address'; + public const CHATS = 'chats'; + public const EMAILS = 'emails'; + public const PHONES = 'phones'; + public const SOCIAL_PROFILES = 'social-profiles'; + public const WEBSITES = 'websites'; } diff --git a/src/Customers/Entry/CustomerEntryEndpoint.php b/src/Customers/Entry/CustomerEntryEndpoint.php index a628076d..6eba5e0d 100644 --- a/src/Customers/Entry/CustomerEntryEndpoint.php +++ b/src/Customers/Entry/CustomerEntryEndpoint.php @@ -20,7 +20,7 @@ class CustomerEntryEndpoint extends Endpoint public const CUSTOMER_SOCIAL = '/v2/customers/%d/social-profiles/%d'; public const CREATE_CUSTOMER_WEBSITE = '/v2/customers/%d/websites'; public const CUSTOMER_WEBSITE = '/v2/customers/%d/websites/%d'; - public const CUSTOMER_PROPERTIES= '/v2/customers/%d/properties'; + public const CUSTOMER_PROPERTIES = '/v2/customers/%d/properties'; public function createAddress(int $customerId, Address $address): ?int { diff --git a/src/Endpoint.php b/src/Endpoint.php index d0d467b5..106aba21 100644 --- a/src/Endpoint.php +++ b/src/Endpoint.php @@ -44,10 +44,10 @@ protected function loadPage( ): PagedCollection { /** @var HalPagedResources $pagedResources */ $pagedResources = $this->restClient->getResources( - $entityClass, - $rel, - $uri - ); + $entityClass, + $rel, + $uri + ); $mapClosure = function (HalResource $resource) { return $resource->getEntity(); diff --git a/src/Entity/PagedCollection.php b/src/Entity/PagedCollection.php index 310b8255..ffbc2f8d 100644 --- a/src/Entity/PagedCollection.php +++ b/src/Entity/PagedCollection.php @@ -13,12 +13,12 @@ class PagedCollection extends Collection /** * The link name for the templated page URI. */ - const REL_PAGE = 'page'; + public const REL_PAGE = 'page'; /** * The variable name for the page number in the templated URI. */ - const PAGE_VARIABLE = 'page'; + public const PAGE_VARIABLE = 'page'; /** * @var HalPageMetadata diff --git a/src/Http/Hal/HalDeserializer.php b/src/Http/Hal/HalDeserializer.php index b4f0c55b..8a4287fe 100644 --- a/src/Http/Hal/HalDeserializer.php +++ b/src/Http/Hal/HalDeserializer.php @@ -9,8 +9,8 @@ class HalDeserializer { - const LINKS = '_links'; - const EMBEDDED = '_embedded'; + public const LINKS = '_links'; + public const EMBEDDED = '_embedded'; public static function deserializeDocument(string $json): HalDocument { diff --git a/src/Http/Hal/HalLink.php b/src/Http/Hal/HalLink.php index a23ac52a..c5d0602c 100644 --- a/src/Http/Hal/HalLink.php +++ b/src/Http/Hal/HalLink.php @@ -12,27 +12,27 @@ class HalLink /** * The first page. */ - const REL_FIRST = 'first'; + public const REL_FIRST = 'first'; /** * The last page. */ - const REL_LAST = 'last'; + public const REL_LAST = 'last'; /** * The next page. */ - const REL_NEXT = 'next'; + public const REL_NEXT = 'next'; /** * The previous page. */ - const REL_PREVIOUS = 'previous'; + public const REL_PREVIOUS = 'previous'; /** * The self relation. */ - const REL_SELF = 'self'; + public const REL_SELF = 'self'; /** * @var string @@ -77,6 +77,6 @@ public function expand(array $params): string throw new RuntimeException(sprintf('The link "%s" is not templated', $this->getRel())); } - return (new UriTemplate)->expand($this->getHref(), $params); + return (new UriTemplate())->expand($this->getHref(), $params); } } diff --git a/src/Http/RestClient.php b/src/Http/RestClient.php index e459493a..6d670ae1 100644 --- a/src/Http/RestClient.php +++ b/src/Http/RestClient.php @@ -14,6 +14,7 @@ use HelpScout\Api\Http\Hal\HalResource; use HelpScout\Api\Http\Hal\HalResources; use HelpScout\Api\Reports\Report; +use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; class RestClient @@ -167,7 +168,7 @@ private function encodeEntity(Extractable $entity): string /** * @return mixed|ResponseInterface */ - private function send(Request $request) + private function send(RequestInterface $request) { $options = [ 'base_uri' => self::BASE_URI, diff --git a/src/Http/RestClientBuilder.php b/src/Http/RestClientBuilder.php index 5efafcd1..50d05d22 100644 --- a/src/Http/RestClientBuilder.php +++ b/src/Http/RestClientBuilder.php @@ -46,6 +46,9 @@ public function build($tokenRefreshedCallback = null): RestClient ); } + /** + * @internal + */ protected function getGuzzleClient(): Client { $options = $this->getOptions(); diff --git a/src/Mailboxes/MailboxLinks.php b/src/Mailboxes/MailboxLinks.php index e1d4c5a4..94ab3001 100644 --- a/src/Mailboxes/MailboxLinks.php +++ b/src/Mailboxes/MailboxLinks.php @@ -6,6 +6,6 @@ class MailboxLinks { - const FIELDS = 'fields'; - const FOLDERS = 'folders'; + public const FIELDS = 'fields'; + public const FOLDERS = 'folders'; } diff --git a/tests/Authentication/AuthenticationIntegrationTest.php b/tests/Authentication/AuthenticationIntegrationTest.php index 42f4df4a..8ca20a5c 100644 --- a/tests/Authentication/AuthenticationIntegrationTest.php +++ b/tests/Authentication/AuthenticationIntegrationTest.php @@ -119,9 +119,8 @@ public function testAuthenticatorFetchesTokensWithClientCredentials() 'Authorization' => 'Bearer fdsafdas', ]; - $authenticator = new Authenticator(new Client(), $auth); + $authenticator = new Authenticator($this->guzzle, $auth); - $authenticator->setClient($this->guzzle); $result = $authenticator->getAuthHeader(); $this->assertSame($expectedResult, $result); } @@ -162,10 +161,8 @@ public function testAuthenticatorFetchesTokensWithRefreshCredentials() 'Authorization' => 'Bearer fdsafdas', ]; - $authenticator = new Authenticator(new Client(), $auth); + $authenticator = new Authenticator($this->guzzle, $auth); - /* @var Client $client */ - $authenticator->setClient($this->guzzle); $result = $authenticator->getAuthHeader(); $this->assertSame($expectedResult, $result); diff --git a/tests/Conversations/Threads/Support/HasCustomerTest.php b/tests/Conversations/Threads/Support/HasCustomerTest.php index b1de952c..bf4f94fb 100644 --- a/tests/Conversations/Threads/Support/HasCustomerTest.php +++ b/tests/Conversations/Threads/Support/HasCustomerTest.php @@ -6,7 +6,7 @@ use HelpScout\Api\Customers\Customer; use HelpScout\Api\Support\HasCustomer; -use PHPStan\Testing\TestCase; +use PHPUnit\Framework\TestCase; class HasCustomerTest extends TestCase { diff --git a/tests/Conversations/Threads/Support/HasPartiesToBeNotifiedTest.php b/tests/Conversations/Threads/Support/HasPartiesToBeNotifiedTest.php index f7ca973f..0cd41287 100644 --- a/tests/Conversations/Threads/Support/HasPartiesToBeNotifiedTest.php +++ b/tests/Conversations/Threads/Support/HasPartiesToBeNotifiedTest.php @@ -5,7 +5,7 @@ namespace HelpScout\Api\Tests\Conversations\Threads\Support; use HelpScout\Api\Conversations\Threads\Support\HasPartiesToBeNotified; -use PHPStan\Testing\TestCase; +use PHPUnit\Framework\TestCase; class HasPartiesToBeNotifiedTest extends TestCase { diff --git a/tests/Conversations/Threads/Support/HasUserTest.php b/tests/Conversations/Threads/Support/HasUserTest.php index 3661c27b..6d50843e 100644 --- a/tests/Conversations/Threads/Support/HasUserTest.php +++ b/tests/Conversations/Threads/Support/HasUserTest.php @@ -6,7 +6,7 @@ use HelpScout\Api\Conversations\Threads\Support\HasUser; use HelpScout\Api\Users\User; -use PHPStan\Testing\TestCase; +use PHPUnit\Framework\TestCase; class HasUserTest extends TestCase { diff --git a/tests/Http/Hal/Entity/StubPayloads.php b/tests/Http/Hal/Entity/StubPayloads.php index ee8223b1..3d084b72 100644 --- a/tests/Http/Hal/Entity/StubPayloads.php +++ b/tests/Http/Hal/Entity/StubPayloads.php @@ -91,11 +91,6 @@ public static function getPagedResources(): string ], ]; - if (empty($entities)) { - // The _embedded key is not set when empty - unset($data['_embedded']); - } - return json_encode($data); } diff --git a/tests/Http/RestClientTest.php b/tests/Http/RestClientTest.php index 00c7bed0..50db2199 100644 --- a/tests/Http/RestClientTest.php +++ b/tests/Http/RestClientTest.php @@ -121,6 +121,7 @@ public function testSendingRequestRefreshesToken() ->with(\Mockery::on(function (Request $request) { // Ensure retry request includes new auth headers. $this->assertSame(['the-value'], $request->getHeader('the-header')); + return true; }), \Mockery::any()) ->andReturn($response);