From bd362e9e7714fd71f59fd9cd16a79f98ff1b4323 Mon Sep 17 00:00:00 2001 From: core23 Date: Thu, 12 Oct 2023 20:45:56 +0200 Subject: [PATCH 1/2] Remove deprecations --- src/Client/Client.php | 20 +++----------------- src/Client/ClientInterface.php | 21 ++------------------- tests/Client/ClientTest.php | 9 +-------- 3 files changed, 6 insertions(+), 44 deletions(-) diff --git a/src/Client/Client.php b/src/Client/Client.php index 6b357227..b93eaeeb 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -30,24 +30,15 @@ final class Client implements ClientInterface public function __construct(ConnectionInterface $connection, string $token = 'anonymous') { $this->connection = $connection; - $this->setToken($token); + $this->token = $token; } - public function setToken(string $token): void - { - $this->token = $token; - } - - public function call(string $method, array $params = [], string $format = 'php') + public function call(string $method, array $params = []) { $params['method'] = $method; $params['token_auth'] = $this->token; $params['format'] = 'json'; - $data = $this->getConnection()->send($params); - - if ('php' !== $format) { - @trigger_error(sprintf('Argument #3 of %s is deprecated and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED); - } + $data = $this->connection->send($params); try { $object = json_decode($data, true, 512, JSON_THROW_ON_ERROR); @@ -57,9 +48,4 @@ public function call(string $method, array $params = [], string $format = 'php') return $object; } - - public function getConnection(): ConnectionInterface - { - return $this->connection; - } } diff --git a/src/Client/ClientInterface.php b/src/Client/ClientInterface.php index 82375e07..93307355 100644 --- a/src/Client/ClientInterface.php +++ b/src/Client/ClientInterface.php @@ -11,22 +11,12 @@ namespace Nucleos\MatomoBundle\Client; -use Nucleos\MatomoBundle\Connection\ConnectionInterface; use Nucleos\MatomoBundle\Exception\MatomoException; interface ClientInterface { /** - * Set Matomo API token. - * - * @param string $token auth token - * - * @deprecated use constructor to set client token - */ - public function setToken(string $token): void; - - /** - * Call specific method & return it's response. + * Call specific method & return its response. * * @param string $method method name * @param array $params method parameters @@ -35,12 +25,5 @@ public function setToken(string $token): void; * * @throws MatomoException */ - public function call(string $method, array $params = [], string $format = 'php'); - - /** - * Return active connection. - * - * @deprecated without any replacement - */ - public function getConnection(): ConnectionInterface; + public function call(string $method, array $params = []); } diff --git a/tests/Client/ClientTest.php b/tests/Client/ClientTest.php index e860e6b4..859e2720 100644 --- a/tests/Client/ClientTest.php +++ b/tests/Client/ClientTest.php @@ -29,13 +29,6 @@ protected function setUp(): void $this->connection = $this->createMock(ConnectionInterface::class); } - public function testGetConnection(): void - { - $client = new Client($this->connection); - - static::assertSame($this->connection, $client->getConnection()); - } - public function testCall(): void { $this->connection @@ -72,7 +65,7 @@ public function testCallWithCustomFormat(): void ; $client = new Client($this->connection, 'MY_TOKEN'); - $result = $client->call('foo/method', ['foo' => 'bar'], 'custom'); + $result = $client->call('foo/method', ['foo' => 'bar']); static::assertCount(30, $result); } From 326457fff05819ae53cfc4d943a9c043dba38de0 Mon Sep 17 00:00:00 2001 From: core23 Date: Thu, 12 Oct 2023 20:48:44 +0200 Subject: [PATCH 2/2] Add type hints --- src/Block/Service/MatomoStatisticBlockService.php | 13 +++++-------- src/Block/Service/MatomoTrackerBlockService.php | 4 ++-- src/Client/Client.php | 8 +++----- src/Client/ClientFactoryInterface.php | 3 --- src/Client/ClientInterface.php | 4 +--- src/Client/PsrClientFactory.php | 7 ++----- src/Connection/ConnectionInterface.php | 2 -- src/Connection/PsrClientConnection.php | 8 +++----- 8 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/Block/Service/MatomoStatisticBlockService.php b/src/Block/Service/MatomoStatisticBlockService.php index ec200c59..bc62c4b2 100644 --- a/src/Block/Service/MatomoStatisticBlockService.php +++ b/src/Block/Service/MatomoStatisticBlockService.php @@ -34,12 +34,9 @@ final class MatomoStatisticBlockService extends AbstractBlockService implements EditableBlockService, LoggerAwareInterface { - /** - * @var LoggerInterface - */ - private LoggerInterface|NullLogger $logger; + private readonly ClientFactoryInterface $factory; - private ClientFactoryInterface $factory; + private LoggerInterface $logger; public function __construct(Environment $twig, ClientFactoryInterface $factory) { @@ -67,9 +64,9 @@ public function configureCreateForm(FormMapper $form, BlockInterface $block): vo /** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ - public function configureEditForm(FormMapper $formMapper, BlockInterface $block): void + public function configureEditForm(FormMapper $form, BlockInterface $block): void { - $formMapper->add('settings', ImmutableArrayType::class, [ + $form->add('settings', ImmutableArrayType::class, [ 'keys' => [ ['title', TextType::class, [ 'required' => false, @@ -177,7 +174,7 @@ public function getName(): string * * @return array */ - protected function getData(array $settings = []): ?array + private function getData(array $settings = []): ?array { try { $client = $this->factory->createClient((string) $settings['host'], (string) $settings['token']); diff --git a/src/Block/Service/MatomoTrackerBlockService.php b/src/Block/Service/MatomoTrackerBlockService.php index e44919bd..bf078ee8 100644 --- a/src/Block/Service/MatomoTrackerBlockService.php +++ b/src/Block/Service/MatomoTrackerBlockService.php @@ -43,9 +43,9 @@ public function configureCreateForm(FormMapper $form, BlockInterface $block): vo $this->configureEditForm($form, $block); } - public function configureEditForm(FormMapper $formMapper, BlockInterface $block): void + public function configureEditForm(FormMapper $form, BlockInterface $block): void { - $formMapper->add('settings', ImmutableArrayType::class, [ + $form->add('settings', ImmutableArrayType::class, [ 'keys' => [ ['host', TextType::class, [ 'required' => false, diff --git a/src/Client/Client.php b/src/Client/Client.php index b93eaeeb..37a3cf90 100644 --- a/src/Client/Client.php +++ b/src/Client/Client.php @@ -17,13 +17,11 @@ final class Client implements ClientInterface { - private ConnectionInterface $connection; + private readonly ConnectionInterface $connection; - private string $token; + private readonly string $token; /** - * Initialize Matomo client. - * * @param ConnectionInterface $connection Matomo active connection * @param string $token auth token */ @@ -33,7 +31,7 @@ public function __construct(ConnectionInterface $connection, string $token = 'an $this->token = $token; } - public function call(string $method, array $params = []) + public function call(string $method, array $params = []): mixed { $params['method'] = $method; $params['token_auth'] = $this->token; diff --git a/src/Client/ClientFactoryInterface.php b/src/Client/ClientFactoryInterface.php index b09279d6..fa6c9134 100644 --- a/src/Client/ClientFactoryInterface.php +++ b/src/Client/ClientFactoryInterface.php @@ -13,8 +13,5 @@ interface ClientFactoryInterface { - /** - * Creates new Matomo Client. - */ public function createClient(string $host, string $token): ClientInterface; } diff --git a/src/Client/ClientInterface.php b/src/Client/ClientInterface.php index 93307355..bf92c801 100644 --- a/src/Client/ClientInterface.php +++ b/src/Client/ClientInterface.php @@ -16,8 +16,6 @@ interface ClientInterface { /** - * Call specific method & return its response. - * * @param string $method method name * @param array $params method parameters * @@ -25,5 +23,5 @@ interface ClientInterface * * @throws MatomoException */ - public function call(string $method, array $params = []); + public function call(string $method, array $params = []): mixed; } diff --git a/src/Client/PsrClientFactory.php b/src/Client/PsrClientFactory.php index e4c69f71..35214fb6 100644 --- a/src/Client/PsrClientFactory.php +++ b/src/Client/PsrClientFactory.php @@ -17,13 +17,10 @@ final class PsrClientFactory implements ClientFactoryInterface { - private PsrClientInterface $client; + private readonly PsrClientInterface $client; - private RequestFactoryInterface $requestFactory; + private readonly RequestFactoryInterface $requestFactory; - /** - * Initialize client. - */ public function __construct(PsrClientInterface $client, RequestFactoryInterface $requestFactory) { $this->client = $client; diff --git a/src/Connection/ConnectionInterface.php b/src/Connection/ConnectionInterface.php index 2e14dfa9..8a6a8649 100644 --- a/src/Connection/ConnectionInterface.php +++ b/src/Connection/ConnectionInterface.php @@ -16,8 +16,6 @@ interface ConnectionInterface { /** - * Calls specific method on Matomo API. - * * @param array $params * * @return string response diff --git a/src/Connection/PsrClientConnection.php b/src/Connection/PsrClientConnection.php index 56461686..8311221f 100644 --- a/src/Connection/PsrClientConnection.php +++ b/src/Connection/PsrClientConnection.php @@ -20,15 +20,13 @@ final class PsrClientConnection implements ConnectionInterface { - private PsrClientInterface $client; + private readonly PsrClientInterface $client; - private RequestFactoryInterface $requestFactory; + private readonly RequestFactoryInterface $requestFactory; - private string $apiUrl; + private readonly string $apiUrl; /** - * Initialize client. - * * @param string $apiUrl base API URL */ public function __construct(PsrClientInterface $client, RequestFactoryInterface $requestFactory, string $apiUrl)