Skip to content

Commit

Permalink
Codebase updates:
Browse files Browse the repository at this point in the history
 - Add editorconfig
 - Add PHPCS/PHPCBF
 - Update code according to PSR-12
 - Make test suite run inside Docker
 - Allow custom log level for errors
  • Loading branch information
BusterNeece committed Jul 1, 2024
1 parent a2e5ab4 commit 8268a97
Show file tree
Hide file tree
Showing 17 changed files with 233 additions and 200 deletions.
37 changes: 0 additions & 37 deletions .devcontainer/devcontainer.json

This file was deleted.

38 changes: 0 additions & 38 deletions .devcontainer/docker-compose.yml

This file was deleted.

132 changes: 82 additions & 50 deletions .editorconfig

Large diffs are not rendered by default.

5 changes: 0 additions & 5 deletions .github/FUNDING.yml

This file was deleted.

38 changes: 17 additions & 21 deletions .github/workflows/testsuite.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
name: Test Suite

on: [ push, pull_request ]

on:
push :
pull_request :
workflow_dispatch :
jobs:
test_suite:
runs-on: ubuntu-latest

strategy:
matrix:
php-version: [ '8.1', '8.2' ]
steps:
- uses: actions/checkout@v1

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: composer:v2, cs2pr
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name : Run Composer install
run : |
composer install --no-interaction
- name : Run PHP Linter
run : |
vendor/bin/parallel-lint . --exclude vendor --checkstyle | cs2pr
- name: Build Test Image
uses: docker/build-push-action@v5
with:
context: .
load: true
tags: azuracast_nowplaying:testing
cache-from: type=gha,mode=max
cache-to: type=gha,mode=max

- name : Run PHPStan
run : |
vendor/bin/phpstan analyze --error-format=checkstyle | cs2pr
- name: Run CI Test Suite
run: |
docker run --rm azuracast_nowplaying:testing composer run ci
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
FROM mlocati/php-extension-installer AS php-extension-installer

FROM php:8.3-cli-alpine3.19 AS base

ENV TZ=UTC

COPY --from=php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/

RUN install-php-extensions @composer gd curl xml zip bcmath mbstring intl sockets

RUN apk add --update --no-cache \
zip git curl bash \
su-exec

WORKDIR /app
32 changes: 26 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,35 @@
"roave/security-advisories": "dev-latest",
"php-parallel-lint/php-console-highlighter": "^1",
"php-parallel-lint/php-parallel-lint": "^1.3",
"php-http/discovery": "^1.9"
"php-http/discovery": "^1.9",
"squizlabs/php_codesniffer": "^3.10",
"slevomat/coding-standard": "^8.15",
"staabm/annotate-pull-request-from-checkstyle": "^1.8"
},
"scripts": {
"cleanup-and-test": [
"@phpcbf",
"@dev-test"
],
"test": [
"@composer install",
"@phplint",
"@phpstan"
"@composer install",
"@dev-test"
],
"dev-test": [
"@phplint",
"@phpstan",
"@phpcs"
],
"phpcbf": "phpcbf",
"phpcs": "phpcs",
"phplint": "parallel-lint . --exclude vendor",
"phpstan": "phpstan analyze"
"phpstan": "phpstan analyze --memory-limit=-1",
"ci": [
"@composer install",
"parallel-lint . --exclude vendor --checkstyle | cs2pr",
"phpstan analyze --memory-limit=-1 --error-format=checkstyle | cs2pr",
"phpcs --report=checkstyle | cs2pr"
]
},
"authors": [
{
Expand All @@ -43,7 +62,8 @@
"config": {
"preferred-install": "dist",
"allow-plugins": {
"php-http/discovery": true
"php-http/discovery": true,
"dealerdirect/phpcodesniffer-composer-installer": true
}
},
"prefer-stable": true,
Expand Down
10 changes: 3 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
version: '2.2'

services:
cli:
image: azuracast/azuracast_testing:latest
image: azuracast/nowplaying:latest
build:
context: .
volumes:
- ./:/app
- composer_data:/tmp
command: composer test

volumes:
composer_data: {}
32 changes: 32 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0"?>
<ruleset name="AzuraCast">
<description>The AzuraCast PHP coding standard.</description>

<file>src</file>

<arg name="basepath" value="."/>
<arg name="extensions" value="php"/>
<arg name="colors"/>

<!-- relative path from PHPCS source location -->
<config name="installed_paths" value="../../slevomat/coding-standard"/>

<rule ref="PSR12"/>

<rule ref="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification">
<exclude name="SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification"/>
</rule>

<rule ref="SlevomatCodingStandard.Arrays.TrailingArrayComma"/>
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses"/>
<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash"/>

<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
<properties>
<property name="declareOnFirstLine" value="false"/>
<property name="linesCountBeforeDeclare" value="1"/>
<property name="linesCountAfterDeclare" value="1"/>
<property name="spacesCountAroundEqualsSign" value="0"/>
</properties>
</rule>
</ruleset>
34 changes: 26 additions & 8 deletions src/Adapter/AdapterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\UriInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use SimpleXMLElement;

abstract class AdapterAbstract implements AdapterInterface
Expand All @@ -33,7 +34,8 @@ public function __construct(
protected RequestFactoryInterface $requestFactory,
protected ClientInterface $client,
protected LoggerInterface $logger,
UriInterface $baseUri
UriInterface $baseUri,
protected string $errorLogLevel = LogLevel::WARNING,
) {
// Detect a username/password in the base URI itself.
$uriUserInfo = $baseUri->getUserInfo();
Expand Down Expand Up @@ -130,12 +132,12 @@ protected function getUrl(RequestInterface $request): PromiseInterface
return $this->client->sendAsync(
$request,
[
RequestOptions::HTTP_ERRORS => false
RequestOptions::HTTP_ERRORS => false,
]
)->then(
function(ResponseInterface $response) use ($request) {
function (ResponseInterface $response) use ($request) {
if ($response->getStatusCode() !== 200) {
$this->logger->error(
$this->logError(
sprintf('Request returned status code %d.', $response->getStatusCode()),
[
'body' => (string)$request->getBody(),
Expand Down Expand Up @@ -169,7 +171,7 @@ protected function baseUriWithPathAndQuery(

if ('' !== $path) {
$uri = $uri->withPath(
rtrim($uri->getPath(), '/').$path
rtrim($uri->getPath(), '/') . $path
);
}
if (0 !== count($query)) {
Expand All @@ -193,13 +195,13 @@ function ($promises) {
$exception = $promises[self::PROMISE_NOW_PLAYING]['reason'] ?? null;

if ($exception instanceof \Exception) {
$this->logger->error(
$this->logError(
sprintf(
'NowPlaying request encountered an exception: %s',
$exception->getMessage()
),
[
'exception' => $exception
'exception' => $exception,
]
);
}
Expand Down Expand Up @@ -276,7 +278,7 @@ protected function getSimpleXml(string $xmlString): ?SimpleXMLElement

libxml_clear_errors();

$this->logger->error(
$this->logError(
'Error parsing XML response.',
[
'response' => $xmlString,
Expand All @@ -288,4 +290,20 @@ protected function getSimpleXml(string $xmlString): ?SimpleXMLElement

return $xml;
}

/**
* @param string|\Stringable $message
* @param mixed[] $context
* @return void
*/
protected function logError(
string|\Stringable $message,
array $context = []
): void {
$this->logger->log(
$this->errorLogLevel,
$message,
$context
);
}
}
Loading

0 comments on commit 8268a97

Please sign in to comment.