diff options
-rw-r--r-- | .github/workflows/ci-build.yml | 85 | ||||
-rw-r--r-- | .travis.yml | 3 | ||||
-rw-r--r-- | composer.json | 6 | ||||
-rw-r--r-- | phpstan.neon.dist | 5 | ||||
-rw-r--r-- | src/Diactoros/StreamFactory.php | 16 | ||||
-rw-r--r-- | src/Diactoros/UploadedFileFactory.php | 3 | ||||
-rw-r--r-- | src/HttpClient.php | 8 | ||||
-rw-r--r-- | src/Metrics.php | 32 | ||||
-rw-r--r-- | src/MetricsInterface.php | 28 | ||||
-rw-r--r-- | src/PSR7Client.php | 14 | ||||
-rw-r--r-- | src/Worker.php | 8 |
11 files changed, 114 insertions, 94 deletions
diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml index 360c4f30..339c841a 100644 --- a/.github/workflows/ci-build.yml +++ b/.github/workflows/ci-build.yml @@ -2,59 +2,72 @@ name: CI on: push: - branches: - tags: + branches: '**' + tags: '**' pull_request: jobs: - build: - name: Build + name: Build (PHP ${{ matrix.php }}, Go ${{ matrix.go }}) runs-on: ubuntu-latest strategy: + fail-fast: false matrix: php: [7.1, 7.2, 7.3, 7.4] + go: [1.12] + env: + GO111MODULE: on steps: - - - name: Set up Go 1.12 + - name: Set up Go ${{ matrix.go }} uses: actions/setup-go@v1 with: - go-version: 1.12 - id: go - - - name: Check out code into the Go module directory - uses: actions/checkout@v1 + go-version: ${{ matrix.go }} - - name: Install PHP - uses: shivammathur/setup-php@master + - name: Set up PHP ${{ matrix.php }} + uses: shivammathur/setup-php@v1 with: - php-version: ${{ matrix.php }} - extension-csv: dom + php-version: ${{ matrix.php }} + extensions: dom coverage: xdebug + - name: Check out code + uses: actions/checkout@v2 + with: + fetch-depth: 1 + + - name: Show versions + run: php -v && composer -V && go version + - name: Debug if needed - run: | - export DEBUG=${DEBUG:-false} - if [[ "$DEBUG" == "true" ]]; then - env - go env - fi env: DEBUG: ${{ secrets.DEBUG }} + run: if [[ "$DEBUG" == "true" ]]; then env && go env; fi - - name: Show versions - run: | - php -v - composer -V - go version + - name: Syntax check only (lint) + run: find ./src/ -name "*.php" -print0 | xargs -0 -n1 -P8 php -l - - name: Install dependencies and download binary roadrunner - run: | - export GO111MODULE=on - go mod download - composer install --no-interaction --prefer-source - find src/ -name "*.php" -print0 | xargs -0 -n1 -P8 php -l - chmod +x bin/rr && bin/rr get-binary + - name: Get Composer Cache Directory # Docs: <https://github.com/actions/cache/blob/master/examples.md#php---composer> + id: composer-cache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache dependencies # Docs: <https://github.com/actions/cache/blob/master/examples.md#php---composer> + uses: actions/cache@v1 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-composer- + + - name: Install Composer dependencies + run: composer install --prefer-dist --no-interaction --no-suggest # --prefer-source + + - name: Analyze PHP sources + run: composer analyze + + - name: Install Go dependencies + run: go mod download + + - name: Download binary roadrunner + run: php ./bin/rr get-binary - name: Run golang tests run: | @@ -69,7 +82,10 @@ jobs: go test ./service/headers -race -v -coverprofile=headers.txt -covermode=atomic go test ./service/metrics -race -v -coverprofile=metrics.txt -covermode=atomic go test ./service/health -race -v -coverprofile=health.txt -covermode=atomic + - name: Run code coverage + env: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} run: | if [[ "$CODECOV_TOKEN" != "" ]]; then curl https://codecov.io/bash -o codecov-bash @@ -86,9 +102,6 @@ jobs: ./codecov-bash -f metrics.txt ./codecov-bash -f health.txt fi - env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - if: image: name: Build docker image diff --git a/.travis.yml b/.travis.yml index f3955eb9..a501f7ce 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ install: - php composer-setup.php - php composer.phar install --no-interaction --prefer-source - find src/ -name "*.php" -print0 | xargs -0 -n1 -P8 php -l + - composer analyze - chmod +x bin/rr && bin/rr get-binary script: @@ -72,4 +73,4 @@ jobs: - sudo add-apt-repository -y ppa:ondrej/php - sudo apt-get update - sudo apt-get install -y php7.4-cli php7.4-curl - - sudo cp `which php7.4` `which php`
\ No newline at end of file + - sudo cp `which php7.4` `which php` diff --git a/composer.json b/composer.json index ef09fe20..89a837ac 100644 --- a/composer.json +++ b/composer.json @@ -21,6 +21,12 @@ "symfony/console": "^2.5.0 || ^3.0.0 || ^4.0.0 || ^5.0.0", "zendframework/zend-diactoros": "^1.3 || ^2.0" }, + "require-dev": { + "phpstan/phpstan": "~0.12" + }, + "scripts": { + "analyze": "@php ./vendor/bin/phpstan analyze -c ./phpstan.neon.dist --no-progress --ansi" + }, "autoload": { "psr-4": { "Spiral\\RoadRunner\\": "src/" diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 00000000..c9ffb648 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,5 @@ +parameters: + level: 'max' + checkMissingIterableValueType: false + paths: + - src diff --git a/src/Diactoros/StreamFactory.php b/src/Diactoros/StreamFactory.php index da0d52ec..cc0a5306 100644 --- a/src/Diactoros/StreamFactory.php +++ b/src/Diactoros/StreamFactory.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace Spiral\RoadRunner\Diactoros; +use RuntimeException; use Psr\Http\Message\StreamFactoryInterface; use Psr\Http\Message\StreamInterface; use Zend\Diactoros\Stream; @@ -17,10 +18,16 @@ final class StreamFactory implements StreamFactoryInterface { /** * @inheritdoc + * @throws RuntimeException */ public function createStream(string $content = ''): StreamInterface { - $resource = fopen('php://temp', 'r+'); + $resource = fopen('php://temp', 'rb+'); + + if (! \is_resource($resource)) { + throw new RuntimeException('Cannot create stream'); + } + fwrite($resource, $content); rewind($resource); return $this->createStreamFromResource($resource); @@ -29,9 +36,14 @@ final class StreamFactory implements StreamFactoryInterface /** * @inheritdoc */ - public function createStreamFromFile(string $file, string $mode = 'r'): StreamInterface + public function createStreamFromFile(string $file, string $mode = 'rb'): StreamInterface { $resource = fopen($file, $mode); + + if (! \is_resource($resource)) { + throw new RuntimeException('Cannot create stream'); + } + return $this->createStreamFromResource($resource); } diff --git a/src/Diactoros/UploadedFileFactory.php b/src/Diactoros/UploadedFileFactory.php index 4f09fb23..45773287 100644 --- a/src/Diactoros/UploadedFileFactory.php +++ b/src/Diactoros/UploadedFileFactory.php @@ -27,9 +27,10 @@ final class UploadedFileFactory implements UploadedFileFactoryInterface string $clientMediaType = null ): UploadedFileInterface { if ($size === null) { - $size = $stream->getSize(); + $size = (int) $stream->getSize(); } + /** @var resource $stream */ return new UploadedFile($stream, $size, $error, $clientFilename, $clientMediaType); } } diff --git a/src/HttpClient.php b/src/HttpClient.php index 6308eabc..42c434a8 100644 --- a/src/HttpClient.php +++ b/src/HttpClient.php @@ -31,8 +31,8 @@ final class HttpClient } /** - * @return array|null Request information as ['ctx'=>[], 'body'=>string] - * or null if termination request or invalid context. + * @return mixed[]|null Request information as ['ctx'=>[], 'body'=>string] + * or null if termination request or invalid context. */ public function acceptRequest() { @@ -43,7 +43,7 @@ final class HttpClient } $ctx = json_decode($ctx, true); - if (is_null($ctx)) { + if ($ctx === null) { // invalid context return null; } @@ -69,7 +69,7 @@ final class HttpClient $this->getWorker()->send( $body, - json_encode(['status' => $status, 'headers' => $headers]) + (string) json_encode(['status' => $status, 'headers' => $headers]) ); } } diff --git a/src/Metrics.php b/src/Metrics.php index 6fe4c4f5..d6b6e1da 100644 --- a/src/Metrics.php +++ b/src/Metrics.php @@ -31,13 +31,7 @@ final class Metrics implements MetricsInterface } /** - * Add collector value. Fallback to appropriate method of related collector. - * - * @param string $name - * @param float $value - * @param array $labels - * - * @throws MetricException + * @inheritDoc */ public function add(string $name, float $value, array $labels = []): void { @@ -49,13 +43,7 @@ final class Metrics implements MetricsInterface } /** - * Subtract the collector value, only for gauge collector. - * - * @param string $name - * @param float $value - * @param array $labels - * - * @throws MetricException + * @inheritDoc */ public function sub(string $name, float $value, array $labels = []): void { @@ -67,13 +55,7 @@ final class Metrics implements MetricsInterface } /** - * Observe collector value, only for histogram and summary collectors. - * - * @param string $name - * @param float $value - * @param array $labels - * - * @throws MetricException + * @inheritDoc */ public function observe(string $name, float $value, array $labels = []): void { @@ -85,13 +67,7 @@ final class Metrics implements MetricsInterface } /** - * Set collector value, only for gauge collector. - * - * @param string $name - * @param float $value - * @param array $labels - * - * @throws MetricException + * @inheritDoc */ public function set(string $name, float $value, array $labels = []): void { diff --git a/src/MetricsInterface.php b/src/MetricsInterface.php index e0e2260a..ec2009b0 100644 --- a/src/MetricsInterface.php +++ b/src/MetricsInterface.php @@ -17,44 +17,48 @@ interface MetricsInterface /** * Add collector value. Fallback to appropriate method of related collector. * - * @param string $collector - * @param float $value - * @param array $labels + * @param string $collector + * @param float $value + * @param mixed[] $labels * * @throws MetricException + * @return void */ public function add(string $collector, float $value, array $labels = []); /** * Subtract the collector value, only for gauge collector. * - * @param string $collector - * @param float $value - * @param array $labels + * @param string $collector + * @param float $value + * @param mixed[] $labels * * @throws MetricException + * @return void */ public function sub(string $collector, float $value, array $labels = []); /** * Observe collector value, only for histogram and summary collectors. * - * @param string $collector - * @param float $value - * @param array $labels + * @param string $collector + * @param float $value + * @param mixed[] $labels * * @throws MetricException + * @return void */ public function observe(string $collector, float $value, array $labels = []); /** * Set collector value, only for gauge collector. * - * @param string $collector - * @param float $value - * @param array $labels + * @param string $collector + * @param float $value + * @param mixed[] $labels * * @throws MetricException + * @return void */ public function set(string $collector, float $value, array $labels = []); } diff --git a/src/PSR7Client.php b/src/PSR7Client.php index 7393848a..98897890 100644 --- a/src/PSR7Client.php +++ b/src/PSR7Client.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Spiral\RoadRunner; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\UploadedFileInterface; use Psr\Http\Message\ServerRequestFactoryInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamFactoryInterface; @@ -29,12 +30,13 @@ class PSR7Client /** @var StreamFactoryInterface */ private $streamFactory; - /*** @var UploadedFileFactoryInterface */ + /** @var UploadedFileFactoryInterface */ private $uploadsFactory; + /** @var mixed[] */ private $originalServer = []; - /** @var array Valid values for HTTP protocol version */ + /** @var string[] Valid values for HTTP protocol version */ private static $allowedVersions = ['1.0', '1.1', '2',]; /** @@ -127,8 +129,8 @@ class PSR7Client * Returns altered copy of _SERVER variable. Sets ip-address, * request-time and other values. * - * @param array $ctx - * @return array + * @param mixed[] $ctx + * @return mixed[] */ protected function configureServer(array $ctx): array { @@ -156,9 +158,9 @@ class PSR7Client /** * Wraps all uploaded files with UploadedFile. * - * @param array $files + * @param array[] $files * - * @return array + * @return UploadedFileInterface[]|mixed[] */ private function wrapUploads($files): array { diff --git a/src/Worker.php b/src/Worker.php index 87dcc9ce..35294221 100644 --- a/src/Worker.php +++ b/src/Worker.php @@ -66,7 +66,7 @@ class Worker } if ($flags & Relay::PAYLOAD_ERROR) { - return new \Error($body); + return new \Error((string) $body); } return $body; @@ -83,13 +83,13 @@ class Worker */ public function send(string $payload = null, string $header = null): void { - if (is_null($header)) { - $this->relay->send($header, Relay::PAYLOAD_CONTROL | Relay::PAYLOAD_NONE); + if ($header === null) { + $this->relay->send('', Relay::PAYLOAD_CONTROL | Relay::PAYLOAD_NONE); } else { $this->relay->send($header, Relay::PAYLOAD_CONTROL | Relay::PAYLOAD_RAW); } - $this->relay->send($payload, Relay::PAYLOAD_RAW); + $this->relay->send((string) $payload, Relay::PAYLOAD_RAW); } /** |