Skip to content

Commit

Permalink
Add PHPStan integration
Browse files Browse the repository at this point in the history
  • Loading branch information
GaryPEGEOT authored Nov 21, 2020
1 parent a4872da commit 1b98d32
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ php:
- 7.1
- 7.2
- 7.3
- 7.4

env:
global:
Expand All @@ -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
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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": {
Expand Down
8 changes: 4 additions & 4 deletions src/NamingStrategy/PathNamingStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
class PathNamingStrategy implements NamingStrategyInterface
{
/**
* @var array
* @var array<string, string[]>
*/
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<string, string[]> $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 = [])
{
Expand Down
25 changes: 19 additions & 6 deletions src/Recorder/FilesystemRecorder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,10 +33,13 @@ final class FilesystemRecorder implements RecorderInterface, PlayerInterface, Lo
private $filesystem;

/**
* @var array
* @var array<string, string>
*/
private $filters;

/**
* @param array<string, string> $filters
*/
public function __construct(string $directory, ?Filesystem $filesystem = null, array $filters = [])
{
$this->filesystem = $filesystem ?? new Filesystem();
Expand All @@ -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
Expand All @@ -65,24 +70,32 @@ 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
{
$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<string, string> $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);
}
}

0 comments on commit 1b98d32

Please sign in to comment.