Skip to content

Commit 1b98d32

Browse files
authored
Add PHPStan integration
1 parent a4872da commit 1b98d32

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ php:
1010
- 7.1
1111
- 7.2
1212
- 7.3
13+
- 7.4
1314

1415
env:
1516
global:
@@ -23,7 +24,9 @@ matrix:
2324
fast_finish: true
2425
include:
2526
- php: 7.1
26-
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" COVERAGE=true TEST_COMMAND="composer test-ci"
27+
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
28+
- php: 7.4
29+
env: COMPOSER_FLAGS="--prefer-stable" COVERAGE=true TEST_COMMAND="composer test-ci"
2730

2831
before_install:
2932
- if [[ $COVERAGE != true ]]; then phpenv config-rm xdebug.ini || true; fi

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
},
2121
"require-dev": {
2222
"symfony/phpunit-bridge": "^4.2|^5.0",
23-
"friendsofphp/php-cs-fixer": "^2.14"
23+
"friendsofphp/php-cs-fixer": "^2.14",
24+
"phpstan/phpstan": "^0.12.57"
2425
},
2526
"autoload": {
2627
"psr-4": {
@@ -34,7 +35,10 @@
3435
},
3536
"scripts": {
3637
"test": "vendor/bin/simple-phpunit",
37-
"test-ci": "vendor/bin/simple-phpunit --coverage-text --coverage-clover=build/coverage.xml"
38+
"test-ci": [
39+
"vendor/bin/simple-phpunit --coverage-text --coverage-clover=build/coverage.xml",
40+
"vendor/bin/phpstan analyse src -l 8"
41+
]
3842
},
3943
"extra": {
4044
"branch-alias": {

src/NamingStrategy/PathNamingStrategy.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
class PathNamingStrategy implements NamingStrategyInterface
1717
{
1818
/**
19-
* @var array
19+
* @var array<string, string[]>
2020
*/
2121
private $options;
2222

2323
/**
24-
* @param array $options available options:
25-
* - hash_headers: the list of header names to hash,
26-
* - hash_body_methods: Methods for which the body will be hashed (Default: PUT, POST, PATCH)
24+
* @param array<string, string[]> $options available options:
25+
* - hash_headers: the list of header names to hash,
26+
* - hash_body_methods: Methods for which the body will be hashed (Default: PUT, POST, PATCH)
2727
*/
2828
public function __construct(array $options = [])
2929
{

src/Recorder/FilesystemRecorder.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Psr\Http\Message\ResponseInterface;
99
use Psr\Log\LoggerAwareInterface;
1010
use Psr\Log\LoggerAwareTrait;
11+
use Psr\Log\NullLogger;
1112
use Symfony\Component\Filesystem\Exception\IOException;
1213
use Symfony\Component\Filesystem\Filesystem;
1314

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

3435
/**
35-
* @var array
36+
* @var array<string, string>
3637
*/
3738
private $filters;
3839

40+
/**
41+
* @param array<string, string> $filters
42+
*/
3943
public function __construct(string $directory, ?Filesystem $filesystem = null, array $filters = [])
4044
{
4145
$this->filesystem = $filesystem ?? new Filesystem();
@@ -50,6 +54,7 @@ public function __construct(string $directory, ?Filesystem $filesystem = null, a
5054

5155
$this->directory = realpath($directory).\DIRECTORY_SEPARATOR;
5256
$this->filters = $filters;
57+
$this->logger = new NullLogger();
5358
}
5459

5560
public function replay(string $name): ?ResponseInterface
@@ -65,24 +70,32 @@ public function replay(string $name): ?ResponseInterface
6570

6671
$this->log('Response replayed from {filename}', $context);
6772

68-
return Psr7\parse_response(file_get_contents($filename));
73+
if (false === $content = file_get_contents($filename)) {
74+
throw new \RuntimeException(sprintf('Unable to read "%s" file content', $filename));
75+
}
76+
77+
return Psr7\parse_response($content);
6978
}
7079

7180
public function record(string $name, ResponseInterface $response): void
7281
{
7382
$filename = "{$this->directory}$name.txt";
7483
$context = compact('name', 'filename');
7584

76-
$content = preg_replace(array_keys($this->filters), array_values($this->filters), Psr7\str($response));
85+
if (null === $content = preg_replace(array_keys($this->filters), array_values($this->filters), Psr7\str($response))) {
86+
throw new \RuntimeException('Some of the provided response filters are invalid.');
87+
}
88+
7789
$this->filesystem->dumpFile($filename, $content);
7890

7991
$this->log('Response for {name} stored into {filename}', $context);
8092
}
8193

94+
/**
95+
* @param array<string, string> $context
96+
*/
8297
private function log(string $message, array $context = []): void
8398
{
84-
if ($this->logger) {
85-
$this->logger->debug("[VCR-PLUGIN][FilesystemRecorder] $message", $context);
86-
}
99+
$this->logger->debug("[VCR-PLUGIN][FilesystemRecorder] $message", $context);
87100
}
88101
}

0 commit comments

Comments
 (0)