From 1b98d32680835fb2293db9ee6c2c7e3284dd0c60 Mon Sep 17 00:00:00 2001 From: Gary PEGEOT Date: Sat, 21 Nov 2020 16:15:00 +0100 Subject: [PATCH] Add PHPStan integration --- .travis.yml | 5 ++++- composer.json | 8 ++++++-- src/NamingStrategy/PathNamingStrategy.php | 8 ++++---- src/Recorder/FilesystemRecorder.php | 25 +++++++++++++++++------ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0199d6b..b1ad346 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ php: - 7.1 - 7.2 - 7.3 + - 7.4 env: global: @@ -23,7 +24,9 @@ matrix: fast_finish: true include: - php: 7.1 - env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci" + env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" + - php: 7.4 + env: COMPOSER_FLAGS="--prefer-stable" COVERAGE=true TEST_COMMAND="composer test-ci" before_install: - if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi diff --git a/composer.json b/composer.json index 8db42f5..7089b70 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ }, "require-dev": { "symfony/phpunit-bridge": "^4.2|^5.0", - "friendsofphp/php-cs-fixer": "^2.14" + "friendsofphp/php-cs-fixer": "^2.14", + "phpstan/phpstan": "^0.12.57" }, "autoload": { "psr-4": { @@ -34,7 +35,10 @@ }, "scripts": { "test": "vendor/bin/simple-phpunit", - "test-ci": "vendor/bin/simple-phpunit --coverage-text --coverage-clover=build/coverage.xml" + "test-ci": [ + "vendor/bin/simple-phpunit --coverage-text --coverage-clover=build/coverage.xml", + "vendor/bin/phpstan analyse src -l 8" + ] }, "extra": { "branch-alias": { diff --git a/src/NamingStrategy/PathNamingStrategy.php b/src/NamingStrategy/PathNamingStrategy.php index 8c5cd9d..1084eeb 100644 --- a/src/NamingStrategy/PathNamingStrategy.php +++ b/src/NamingStrategy/PathNamingStrategy.php @@ -16,14 +16,14 @@ class PathNamingStrategy implements NamingStrategyInterface { /** - * @var array + * @var array */ private $options; /** - * @param array $options available options: - * - hash_headers: the list of header names to hash, - * - hash_body_methods: Methods for which the body will be hashed (Default: PUT, POST, PATCH) + * @param array $options available options: + * - hash_headers: the list of header names to hash, + * - hash_body_methods: Methods for which the body will be hashed (Default: PUT, POST, PATCH) */ public function __construct(array $options = []) { diff --git a/src/Recorder/FilesystemRecorder.php b/src/Recorder/FilesystemRecorder.php index 0860de4..4bc2814 100644 --- a/src/Recorder/FilesystemRecorder.php +++ b/src/Recorder/FilesystemRecorder.php @@ -8,6 +8,7 @@ use Psr\Http\Message\ResponseInterface; use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareTrait; +use Psr\Log\NullLogger; use Symfony\Component\Filesystem\Exception\IOException; use Symfony\Component\Filesystem\Filesystem; @@ -32,10 +33,13 @@ final class FilesystemRecorder implements RecorderInterface, PlayerInterface, Lo private $filesystem; /** - * @var array + * @var array */ private $filters; + /** + * @param array $filters + */ public function __construct(string $directory, ?Filesystem $filesystem = null, array $filters = []) { $this->filesystem = $filesystem ?? new Filesystem(); @@ -50,6 +54,7 @@ public function __construct(string $directory, ?Filesystem $filesystem = null, a $this->directory = realpath($directory).\DIRECTORY_SEPARATOR; $this->filters = $filters; + $this->logger = new NullLogger(); } public function replay(string $name): ?ResponseInterface @@ -65,7 +70,11 @@ public function replay(string $name): ?ResponseInterface $this->log('Response replayed from {filename}', $context); - return Psr7\parse_response(file_get_contents($filename)); + if (false === $content = file_get_contents($filename)) { + throw new \RuntimeException(sprintf('Unable to read "%s" file content', $filename)); + } + + return Psr7\parse_response($content); } public function record(string $name, ResponseInterface $response): void @@ -73,16 +82,20 @@ public function record(string $name, ResponseInterface $response): void $filename = "{$this->directory}$name.txt"; $context = compact('name', 'filename'); - $content = preg_replace(array_keys($this->filters), array_values($this->filters), Psr7\str($response)); + if (null === $content = preg_replace(array_keys($this->filters), array_values($this->filters), Psr7\str($response))) { + throw new \RuntimeException('Some of the provided response filters are invalid.'); + } + $this->filesystem->dumpFile($filename, $content); $this->log('Response for {name} stored into {filename}', $context); } + /** + * @param array $context + */ private function log(string $message, array $context = []): void { - if ($this->logger) { - $this->logger->debug("[VCR-PLUGIN][FilesystemRecorder] $message", $context); - } + $this->logger->debug("[VCR-PLUGIN][FilesystemRecorder] $message", $context); } }