diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..3c59058 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,17 @@ +{ + "name": "microcms-php-sdk", + "image": "mcr.microsoft.com/devcontainers/php:8.5", + "features": { + "ghcr.io/devcontainers/features/github-cli:1": {} + }, + "customizations": { + "vscode": { + "extensions": [ + "xdebug.php-debug", + "bmewburn.vscode-intelephense-client", + "junstyle.php-cs-fixer" + ] + } + }, + "postCreateCommand": "composer install" +} diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6b5d349..f21ac85 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -6,50 +6,61 @@ on: pull_request: branches: [ main ] -jobs: - tests: +env: + MAIN_PHP_VERSION: '8.5' +jobs: + lint: runs-on: ubuntu-latest - - strategy: - matrix: - php-versions: ['7.3', '7.4', '8.0'] - + timeout-minutes: 5 steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 + with: + persist-credentials: false - name: Setup PHP - uses: shivammathur/setup-php@master + uses: step-security/setup-php@0b4c3f57a1fb50d5cc8172a9700f35b97b3b5d97 # v2.37.0 with: - php-version: ${{ matrix.php-versions }} + php-version: ${{ env.MAIN_PHP_VERSION }} + tools: composer, php-cs-fixer + env: + fail-fast: true - name: Validate composer.json and composer.lock run: composer validate --strict - - name: Cache Composer packages - id: composer-cache - uses: actions/cache@v2 - with: - path: vendor - key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-php- - - name: Install dependencies run: composer install --prefer-dist --no-progress - - name: Run formatter (src/) - uses: docker://oskarstark/php-cs-fixer-ga + - name: Run formatter + run: php-cs-fixer fix --diff --dry-run + + - name: Run linter + run: vendor/bin/phpstan analyse + + tests: + runs-on: ubuntu-latest + timeout-minutes: 5 + strategy: + fail-fast: true + matrix: + php-versions: ['7.4', '8.5'] + + steps: + - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0 with: - args: --diff --dry-run src/ + persist-credentials: false - - name: Run formatter (tests/) - uses: docker://oskarstark/php-cs-fixer-ga + - name: Setup PHP + uses: step-security/setup-php@0b4c3f57a1fb50d5cc8172a9700f35b97b3b5d97 # v2.37.0 with: - args: --diff --dry-run tests/ + php-version: ${{ matrix.php-versions }} + tools: composer + env: + fail-fast: true - - name: Run linter - run: vendor/bin/phpstan analyse --level 5 src/ tests/ + - name: Install dependencies + run: composer install --prefer-dist --no-progress - name: Run test suite run: vendor/bin/phpunit tests/ diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..08469fd --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,12 @@ +in([__DIR__ . '/src', __DIR__ . '/tests']); + +return (new PhpCsFixer\Config()) + ->setRules([ + '@PER-CS' => true, + 'trailing_comma_in_multiline' => false, + 'operator_linebreak' => ['position' => 'end'], + ]) + ->setFinder($finder); diff --git a/composer.json b/composer.json index 99740a4..e958a62 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ ], "require-dev": { "psy/psysh": "@stable", - "phpstan/phpstan": "^0.12.99", + "phpstan/phpstan": "^2.0", "phpunit/phpunit": "^9.5" } } diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..1cd333b --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: 8 + paths: + - src + - tests diff --git a/src/Client.php b/src/Client.php index 3e8929b..5161781 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,12 +4,12 @@ class Client { - private $serviceDomain; - private $apiKey; + private string $serviceDomain; + private string $apiKey; - private $client; + private \GuzzleHttp\Client $client; - public function __construct(string $serviceDomain, string $apiKey, \GuzzleHttp\ClientInterface $client = null) + public function __construct(string $serviceDomain, string $apiKey, ?\GuzzleHttp\Client $client = null) { $this->serviceDomain = $serviceDomain; $this->apiKey = $apiKey; @@ -21,6 +21,11 @@ public function __construct(string $serviceDomain, string $apiKey, \GuzzleHttp\C } } + /** + * @param array $options + * + * @return mixed + */ public function list(string $endpoint, array $options = []) { $path = $endpoint; @@ -33,6 +38,11 @@ public function list(string $endpoint, array $options = []) return json_decode($response->getBody()); } + /** + * @param array $options + * + * @return mixed + */ public function get(string $endpoint, string $contentId = "", array $options = []) { $path = $contentId ? implode("/", [$endpoint, $contentId]) : $endpoint; @@ -45,6 +55,12 @@ public function get(string $endpoint, string $contentId = "", array $options = [ return json_decode($response->getBody()); } + /** + * @param array $body + * @param array $options + * + * @return mixed + */ public function create(string $endpoint, array $body = [], array $options = []) { if (array_key_exists("id", $body)) { @@ -66,6 +82,11 @@ public function create(string $endpoint, array $body = [], array $options = []) return json_decode($response->getBody()); } + /** + * @param array $body + * + * @return mixed + */ public function update(string $endpoint, array $body = []) { $response = $this->client->patch( @@ -83,13 +104,18 @@ function ($v, $k) { return json_decode($response->getBody()); } - public function delete(string $endpoint, string $id) + public function delete(string $endpoint, string $id): void { $path = implode("/", [$endpoint, $id]); $this->client->delete($path, $this->buildOption()); } - private function buildOption(array $option = []) + /** + * @param array $option + * + * @return array + */ + private function buildOption(array $option = []): array { return array_merge( [ @@ -102,7 +128,12 @@ private function buildOption(array $option = []) ); } - private function buildQuery(array $options) + /** + * @param array $options + * + * @return array + */ + private function buildQuery(array $options): array { return array_filter( [ diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 644b4ae..9273330 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -11,19 +11,22 @@ final class ClientTest extends TestCase { - private $handlerStack; - private $mock; + private HandlerStack $handlerStack; + private MockHandler $mock; + + /** @var array> */ private $container; protected function setUp(): void { $this->handlerStack = HandlerStack::create(); $this->container = []; + $container = &$this->container; $this->mock = new MockHandler([]); $this->handlerStack->setHandler($this->mock); - $history = Middleware::history($this->container); + $history = Middleware::history($container); $this->handlerStack->push($history); }