Skip to content

Commit

Permalink
Merge pull request #8 from mailerlite/phpstan
Browse files Browse the repository at this point in the history
Integrate static analysis for improved code quality
  • Loading branch information
nklmilojevic authored May 2, 2023
2 parents 2e1f96c + d593841 commit 811d50e
Show file tree
Hide file tree
Showing 23 changed files with 344 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Code style and static analysis
name: Code style

on:
push:
Expand All @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.4','8.0']
php-versions: ['8.0']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}

steps:
Expand All @@ -31,9 +31,9 @@ jobs:
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.json') }}
key: ${{ runner.os }}-php-cs-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ runner.os }}-php-
${{ runner.os }}-php-cs-${{ matrix.php-versions }}-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
Expand Down
11 changes: 6 additions & 5 deletions .github/workflows/php.yml → .github/workflows/static.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build
name: Static analysis

on:
push:
Expand All @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.4','8.0']
php-versions: ['8.2']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}

steps:
Expand All @@ -33,10 +33,11 @@ jobs:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ runner.os }}-php-
${{ runner.os }}-php-${{ matrix.php-versions }}-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run test suite
run: composer run-script test
- name: Run PHPStan
run: vendor/bin/phpstan analyse --ansi
43 changes: 43 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Run Tests

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
run:

runs-on: ubuntu-latest
strategy:
matrix:
operating-system: [ubuntu-latest]
php-versions: ['7.4','8.0', '8.1', '8.2']
name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}

steps:
- uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, pdo, pdo_mysql, intl, zip
coverage: none

- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ matrix.php-versions }}-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ runner.os }}-php-${{ matrix.php-versions }}-
- name: Install dependencies
if: steps.composer-cache.outputs.cache-hit != 'true'
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run test suite
run: composer run-script test
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[![Latest Version on Packagist](https://img.shields.io/packagist/v/mailerlite/mailerlite-php.svg?style=flat-square)](https://packagist.org/packages/mailerlite/mailerlite-php)
[![Total Downloads](https://img.shields.io/packagist/dt/mailerlite/mailerlite-php.svg?style=flat-square)](https://packagist.org/packages/mailerlite/mailerlite-php)
![GitHub Actions](https://github.com/mailerlite/mailerlite-php/actions/workflows/main.yml/badge.svg)
[![Latest Version on Packagist](https://img.shields.io/packagist/v/mailerlite/mailerlite-php.svg)](https://packagist.org/packages/mailerlite/mailerlite-php)
[![Tests](https://github.com/mailerlite/mailerlite-php/actions/workflows/test.yml/badge.svg?branch=main)](https://github.com/mailerlite/mailerlite-php/actions/workflows/test.yml)
[![PHPStan](https://github.com/mailerlite/mailerlite-php/actions/workflows/static.yml/badge.svg)](https://github.com/mailerlite/mailerlite-php/actions/workflows/static.yml)
[![Total Downloads](https://img.shields.io/packagist/dt/mailerlite/mailerlite-php.svg)](https://packagist.org/packages/mailerlite/mailerlite-php)

# Table of Contents

Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"guzzlehttp/psr7": "^1.5.2",
"http-interop/http-factory-guzzle": "^1.0",
"php-http/guzzle7-adapter": "^0.1",
"friendsofphp/php-cs-fixer": "^2.18"
"friendsofphp/php-cs-fixer": "^2.18",
"phpstan/phpstan": "^1.10"
},
"autoload": {
"psr-4": {
Expand All @@ -48,11 +49,14 @@
}
},
"scripts": {
"analyse": [
"@php vendor/bin/phpstan analyse --ansi"
],
"test": [
"@php vendor/bin/phpunit tests/"
],
"format": [
"@php vendor/bin/php-cs-fixer fix --verbose --diff"
]
}
}
}
9 changes: 9 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
includes:

parameters:
level: max

paths:
- src

treatPhpDocTypesAsCertain: false
2 changes: 1 addition & 1 deletion src/Common/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class Constants
{
public const SDK_VERSION = 'v0.0.1';
public const SDK_VERSION = 'v1.0.0';
}
55 changes: 50 additions & 5 deletions src/Common/HttpLayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@

class HttpLayer
{
protected ?HttpClient $pluginClient;
protected ?RequestFactoryInterface $requestFactory;
protected ?StreamFactoryInterface $streamFactory;
protected HttpClient $pluginClient;
protected RequestFactoryInterface $requestFactory;
protected StreamFactoryInterface $streamFactory;

/**
* @var array<string, mixed>
*/
protected array $options;

/**
* @param array<string, mixed> $options
*/
public function __construct(
array $options = [],
?ClientInterface $httpClient = null,
Expand All @@ -40,26 +46,51 @@ public function __construct(
$this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory();
}

/**
* @param array<string, mixed> $body
*
* @return array<string, mixed>
*/
public function get(string $uri, array $body = []): array
{
return $this->callMethod('GET', $uri, $body);
}

/**
* @param array<string, mixed> $body
*
* @return array<string, mixed>
*/
public function post(string $uri, array $body = []): array
{
return $this->callMethod('POST', $uri, $body);
}

/**
* @param array<string, mixed> $body
*
* @return array<string, mixed>
*/
public function put(string $uri, array $body): array
{
return $this->callMethod('PUT', $uri, $body);
}

/**
* @param array<string, mixed> $body
*
* @return array<string, mixed>
*/
public function delete(string $uri, array $body = []): array
{
return $this->callMethod('DELETE', $uri, $body);
}

/**
* @param array<string, mixed> $body
*
* @return array<string, mixed>
*/
protected function callMethod(string $method, string $uri, array $body): array
{
$request = $this->requestFactory->createRequest($method, $uri)
Expand All @@ -68,6 +99,9 @@ protected function callMethod(string $method, string $uri, array $body): array
return $this->buildResponse($this->pluginClient->sendRequest($request));
}

/**
* @return array<string, mixed>
*/
public function request(string $method, string $uri, string $body = ''): array
{
$request = $this->requestFactory->createRequest($method, $uri);
Expand All @@ -79,13 +113,19 @@ public function request(string $method, string $uri, string $body = ''): array
return $this->buildResponse($this->pluginClient->sendRequest($request));
}

/**
* @param array<string, mixed>|string $body
*/
protected function buildBody($body): StreamInterface
{
$stringBody = is_array($body) ? json_encode($body, JSON_THROW_ON_ERROR) : $body;

return $this->streamFactory->createStream($stringBody);
}

/**
* @return array<string, mixed>
*/
protected function buildResponse(ResponseInterface $response): array
{
$contentTypes = $response->getHeader('Content-Type');
Expand All @@ -112,15 +152,20 @@ protected function buildResponse(ResponseInterface $response): array
];
}

/**
* @return array<int, \Http\Client\Common\Plugin>
*/
protected function buildPlugins(): array
{
$authentication = new Bearer($this->options['api_key']);
/** @var string $apiKey */
$apiKey = $this->options['api_key'];
$authentication = new Bearer($apiKey);
$authenticationPlugin = new AuthenticationPlugin($authentication);

$contentTypePlugin = new ContentTypePlugin();

$headerDefaultsPlugin = new HeaderDefaultsPlugin([
'User-Agent' => 'mailersend-php/'.Constants::SDK_VERSION
'User-Agent' => 'mailersend-php/' . Constants::SDK_VERSION
]);

$httpErrorPlugin = new HttpErrorHelper();
Expand Down
10 changes: 10 additions & 0 deletions src/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@
abstract class AbstractEndpoint
{
protected HttpLayer $httpLayer;

/**
* @var array<string, mixed>
*/
protected array $options;

/**
* @param array<string, mixed> $options
*/
public function __construct(HttpLayer $httpLayer, array $options)
{
$this->httpLayer = $httpLayer;
$this->options = $options;
}

/**
* @param array<string, mixed> $params
*/
protected function buildUri(string $path, array $params = []): string
{
return (new BuildUri($this->options))->execute($path, $params);
Expand Down
13 changes: 13 additions & 0 deletions src/Endpoints/Automation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,33 @@ class Automation extends AbstractEndpoint
{
protected string $endpoint = 'automations';

/**
* @return array<string, mixed>
*/
public function find(string $automationId): array
{
return $this->httpLayer->get(
$this->buildUri($this->endpoint) . "/{$automationId}"
);
}

/**
* @param array<string, mixed> $params
*
* @return array<string, mixed>
*/
public function get(array $params): array
{
return $this->httpLayer->get(
$this->buildUri($this->endpoint, $params)
);
}

/**
* @param array<string, mixed> $params
*
* @return array<string, mixed>
*/
public function activity(string $automationId, array $params): array
{
return $this->httpLayer->get(
Expand Down
5 changes: 5 additions & 0 deletions src/Endpoints/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ class Batch extends AbstractEndpoint
{
protected string $endpoint = 'batch';

/**
* @param array<string, mixed> $params
*
* @return array<string, mixed>
*/
public function send(array $params): array
{
return $this->httpLayer->post(
Expand Down
Loading

0 comments on commit 811d50e

Please sign in to comment.