From e210d6b8e7041e8704a39428db106367e7fa86de Mon Sep 17 00:00:00 2001 From: Baspa Date: Fri, 30 Aug 2024 16:37:21 +0200 Subject: [PATCH] Add PHPStan --- .github/workflows/phpstan.yml | 29 ++++++++ composer.json | 1 + phpstan.neon.dist | 7 ++ src/Client.php | 125 ++++++++++++++++++++++------------ src/ClientFactory.php | 4 +- src/Company/Address.php | 12 +++- src/Company/Company.php | 38 +++++++---- 7 files changed, 156 insertions(+), 60 deletions(-) create mode 100644 .github/workflows/phpstan.yml create mode 100644 phpstan.neon.dist diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..3793d13 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,29 @@ +name: PHPStan + +on: + pull_request: + paths: + - "**.php" + - "phpstan.neon.dist" + push: + branches: + - main + +jobs: + phpstan: + name: phpstan + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: "8.2" + coverage: none + + - name: Install composer dependencies + uses: ramsey/composer-install@v3 + + - name: Run PHPStan + run: ./vendor/bin/phpstan analyse --error-format=github --debug diff --git a/composer.json b/composer.json index 9f1d9a4..b139406 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "require-dev": { "friendsofphp/php-cs-fixer": "^3.0", "pestphp/pest": "^1.20", + "phpstan/phpstan": "^1.12", "spatie/ray": "^1.28" }, "autoload": { diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..9250404 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,7 @@ +parameters: + level: 8 + paths: + - src + tmpDir: build/phpstan + parallel: + maximumNumberOfProcesses: 1 \ No newline at end of file diff --git a/src/Client.php b/src/Client.php index 2939390..6186e72 100644 --- a/src/Client.php +++ b/src/Client.php @@ -2,137 +2,174 @@ namespace Vormkracht10\KvKApi; +use GuzzleHttp\ClientInterface; +use GuzzleHttp\Psr7\Response; use Illuminate\Support\Collection; +use Psr\Http\Message\ResponseInterface; +use stdClass; use Vormkracht10\KvKApi\Company\Company; class Client { - private $httpClient; - private $baseUrl; - private array $results; - private int $page; - private int $resultsPerPage; - - public function __construct($httpClient) + private ClientInterface $httpClient; + private string $baseUrl; + /** @var array */ + private array $results = []; + private int $page = 1; + private int $resultsPerPage = 10; + + public function __construct(ClientInterface $httpClient) { $this->httpClient = $httpClient; $this->baseUrl = 'https://api.kvk.nl/api/v2/'; } - public function search(string $search, array $params = []) + /** + * @param array $params + * @return array + */ + public function search(string $search, array $params = []): array { $queryParams = array_merge([ 'naam' => $search, - 'pagina' => $this->page ?? 1, - 'resultatenPerPagina' => $this->resultsPerPage ?? 10, + 'pagina' => $this->page, + 'resultatenPerPagina' => $this->resultsPerPage, ], $params); $data = $this->getData($queryParams); $parsedData = $this->parseData($this->decodeJson($data)); - $parsedData->each(function ($item) { - $data = json_decode($this->getRelatedData($item)); + foreach ($parsedData as $item) { + $data = $this->decodeJson($this->getRelatedData($item)); $this->results[] = new Company( - $data->kvkNummer ?? null, + $data->kvkNummer ?? '', $data->vestigingsnummer ?? null, $data->naam ?? null, $data->adres ?? null, $data->websites ?? null ); - }); + }; return $this->results; } - private function getData(array $params) + /** + * @param array $params + */ + private function getData(array $params): string { $url = $this->baseUrl . 'zoeken?' . http_build_query($params); - $response = $this->httpClient->get($url); + $response = $this->httpClient->request('GET', $url); return $this->getJson($response); } - private function getJson(object $response) + private function getJson(ResponseInterface $response): string { - return $response->getBody()->getContents(); + return (string) $response->getBody()->getContents(); } - private function decodeJson(string $json) + /** + * @return stdClass + */ + private function decodeJson(string $json): stdClass { - return json_decode($json); + return json_decode($json, false) ?: new stdClass(); } - public function setPage(int $page) + public function setPage(int $page): self { $this->page = $page; return $this; } - public function setResultsPerPage(int $resultsPerPage) + public function setResultsPerPage(int $resultsPerPage): self { $this->resultsPerPage = $resultsPerPage; return $this; } - public function searchByKvkNumber(string $kvkNumber, array $params = []) + /** + * @param array $params + * @return array + */ + public function searchByKvkNumber(string $kvkNumber, array $params = []): array { return $this->search('', array_merge(['kvkNummer' => $kvkNumber], $params)); } - public function searchByRsin(string $rsin, array $params = []) + /** + * @param array $params + * @return array + */ + public function searchByRsin(string $rsin, array $params = []): array { return $this->search('', array_merge(['rsin' => $rsin], $params)); } - public function searchByVestigingsnummer(string $vestigingsnummer, array $params = []) + /** + * @param array $params + * @return array + */ + public function searchByVestigingsnummer(string $vestigingsnummer, array $params = []): array { return $this->search('', array_merge(['vestigingsnummer' => $vestigingsnummer], $params)); } - private function parseData(object $data) + /** + * @return array + */ + private function parseData(stdClass $data): array { - $data = collect($data->resultaten); + $resultaten = $data->resultaten ?? []; + /** @var array $resultatenArray */ + $resultatenArray = is_array($resultaten) ? $resultaten : []; - $data = $data->map(function ($value) { + return array_map(function ($value) { $value = (object) $value; - $value->attributes = collect((array) $value)->except(['type', 'links']); + /** @var array $attributes */ + $attributes = array_diff_key((array) $value, array_flip(['type', 'links'])); + $value->attributes = $attributes; $value->id = uniqid(); if (isset($value->links)) { - $links = collect($value->links); - $links = $links->mapWithKeys(function ($linkObj) { - return [$linkObj->rel => $linkObj->href]; - }); - $value->links = $links; + /** @var array $links */ + $links = $value->links; + /** @var array $mappedLinks */ + $mappedLinks = array_column($links, 'href', 'rel'); + $value->links = $mappedLinks; } else { - $value->links = collect(); + /** @var array $emptyLinks */ + $emptyLinks = []; + $value->links = $emptyLinks; } $value->actief = $value->actief ?? null; $value->vervallenNaam = $value->vervallenNaam ?? null; return $value; - }); - - return $data; + }, $resultatenArray); } - private function getRelatedData($parsedData): Collection + private function getRelatedData(stdClass $parsedData): string { - $relatedData = collect(); + $relatedData = []; + + /** @var Collection $links */ + $links = collect((array)($parsedData->links ?? [])); - collect($parsedData->links)->each(function ($link, $key) use (&$relatedData) { - $response = $this->httpClient->get($link); + $links->each(function (string $link) use (&$relatedData) { + $response = $this->httpClient->request('GET', $link); $data = $this->decodeJson($this->getJson($response)); - $relatedData = $relatedData->merge($data); + $relatedData = array_merge($relatedData, (array) $data); }); - return $relatedData; + return json_encode($relatedData) ?: '{}'; } } diff --git a/src/ClientFactory.php b/src/ClientFactory.php index 212232e..f9d4f70 100644 --- a/src/ClientFactory.php +++ b/src/ClientFactory.php @@ -17,7 +17,7 @@ public static function create(string $apiKey, ?string $rootCertificate = null): private static function createHttpClient( string $apiKey, ?string $rootCertificate = null - ) { + ): Client { if ($rootCertificate === null) { return new Client([ 'headers' => [ @@ -33,4 +33,4 @@ private static function createHttpClient( 'verify' => $rootCertificate, ]); } -} +} \ No newline at end of file diff --git a/src/Company/Address.php b/src/Company/Address.php index 808e4d3..7150663 100644 --- a/src/Company/Address.php +++ b/src/Company/Address.php @@ -4,6 +4,13 @@ class Address { + private string $type; + private ?string $street; + private ?int $houseNumber; + private string $postalCode; + private string $city; + private string $country; + public function __construct( string $type, ?string $street, @@ -50,6 +57,9 @@ public function getCountry(): string return $this->country; } + /** + * @return array + */ public function get(): array { return [ @@ -61,4 +71,4 @@ public function get(): array 'country' => $this->country, ]; } -} +} \ No newline at end of file diff --git a/src/Company/Company.php b/src/Company/Company.php index 6497958..8c874f1 100644 --- a/src/Company/Company.php +++ b/src/Company/Company.php @@ -4,16 +4,18 @@ class Company { - private $kvkNumber; - - private $establishmentNumber; - - private $tradeName; - - private $addresses; - - private $websites; + private string $kvkNumber; + private ?string $establishmentNumber; + private ?string $tradeName; + /** @var array|null */ + private ?array $addresses; + /** @var array|null */ + private ?array $websites; + /** + * @param array|null $addresses + * @param array|null $websites + */ public function __construct( string $kvkNumber, ?string $establishmentNumber, @@ -33,7 +35,7 @@ public function getKvkNumber(): string return $this->kvkNumber; } - public function getEstablishmentNumber(): string + public function getEstablishmentNumber(): ?string { return $this->establishmentNumber; } @@ -44,10 +46,14 @@ public function getTradeName(): ?string } /** - * @return Address[] + * @return array
|null */ public function getAddresses(): ?array { + if ($this->addresses === null) { + return null; + } + $addresses = []; foreach ($this->addresses as $address) { @@ -64,11 +70,17 @@ public function getAddresses(): ?array return $addresses; } - public function getWebsites(): array + /** + * @return array|null + */ + public function getWebsites(): ?array { return $this->websites; } + /** + * @return array + */ public function get(): array { return [ @@ -79,4 +91,4 @@ public function get(): array 'websites' => $this->websites, ]; } -} +} \ No newline at end of file