Skip to content

Commit 6b4ebd2

Browse files
committed
refactor(src) add phpstan
1 parent 3578f3b commit 6b4ebd2

File tree

9 files changed

+141
-82
lines changed

9 files changed

+141
-82
lines changed

.gitattributes

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/tests export-ignore
22
/.gitattributes export-ignore
33
/.gitignore export-ignore
4-
/.travis.yml export-ignore
4+
/.github export-ignore
55
/.codeception.dist.yml export-ignore
6-
/composer.lock export-ignore
6+
/composer.lock export-ignore
77
/Makefile export-ignore

.github/main.workflow

Lines changed: 0 additions & 14 deletions
This file was deleted.

.github/workflows/ci.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@ name: CI
33
on: push
44

55
jobs:
6+
sniff;
7+
name: Code validation
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@master
11+
- uses: MilesChou/composer-action/install@master
12+
- uses: docker://php:7.3-alpine
13+
with:
14+
args: vendor/bin/phpcs --colors -p --standard=PSR2 -s src
15+
- uses: docker://php:7.3-alpine
16+
with:
17+
args: phpstan analyze src --level=7
618
php56:
719
name: Test on PHP 5.6
820
runs-on: ubuntu-latest

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9-
## [0.2.3] - 2020-03-03
9+
## [0.2.4] - 2020-02-05
10+
### Added
11+
- data visitor support in all assertions to allow pre-processing of the expected and current values before assertions
12+
13+
## [0.2.3] - 2020-02-03
1014
### Added
1115
- data visitor support in all assertions to allow pre-processing of the expected and current values before assertions
1216

@@ -32,5 +36,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3236
[0.2.1]: https://github.com/lucatume/codeception-snapshot-assertions/compare/0.2.0...0.2.1
3337
[0.2.2]: https://github.com/lucatume/codeception-snapshot-assertions/compare/0.2.1...0.2.2
3438
[0.2.3]: https://github.com/lucatume/codeception-snapshot-assertions/compare/0.2.2...0.2.3
35-
[Unreleased]: https://github.com/lucatume/codeception-snapshot-assertions/compare/0.2.3...HEAD
39+
[0.2.4]: https://github.com/lucatume/codeception-snapshot-assertions/compare/0.2.3...0.2.4
40+
[Unreleased]: https://github.com/lucatume/codeception-snapshot-assertions/compare/0.2.4...HEAD
3641

Makefile

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
.PHONY: wp_dump cs_sniff cs_fix cs_fix_n_sniff phpstan
1+
.PHONY: sniff fix fix_n_sniff phpstan
22

33
# Sniff the source files.
4-
cs_sniff:
5-
vendor/bin/phpcs --colors -p --standard=PSR2 $(SRC) --ignore=src/data,src/includes,src/tad/scripts -s src
4+
sniff:
5+
vendor/bin/phpcs --colors -p --standard=PSR2 -s src
66

77
# Fix the source and test files.
8-
cs_fix:
9-
vendor/bin/phpcbf --colors -p --standard=PSR2 $(SRC) --ignore=src/data,src/includes,src/tad/scripts -s src tests
8+
fix:
9+
vendor/bin/phpcbf --colors -p --standard=PSR2 -s src tests
1010

1111
# Fix and then sniff the source files.
12-
cs_fix_n_sniff: cs_fix cs_sniff
13-
14-
# Updates Composer dependencies using PHP 5.6.
15-
composer_update: composer.lock
16-
docker run --rm -v ${CURDIR}:/app composer/composer:master-php5 update
12+
fix_n_sniff: cs_fix cs_sniff
1713

1814
# Runs phpstan on the source files.
1915
phpstan:
20-
docker run --rm -v ${CURDIR}:/app phpstan/phpstan analyse -l 5 /app/src
16+
phpstan analyze src --level=7

composer.json

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
{
2-
"name": "lucatume/codeception-snapshot-assertions",
3-
"description": "Snapshot assertions sugar for Codeception.",
4-
"type": "library",
5-
"license": "MIT",
6-
"authors": [
7-
{
8-
"name": "theAverageDev (Luca Tumedei)",
9-
"email": "luca@theaveragedev.com"
10-
}
11-
],
12-
"require": {
13-
"ext-dom": "*",
14-
"gajus/dindent": "^2.0",
15-
"codeception/codeception": "^2.5 || ^3.0 || ^4.0"
16-
},
17-
"require-dev": {
18-
"squizlabs/php_codesniffer": "*"
19-
},
20-
"autoload": {
21-
"psr-4": {
22-
"tad\\Codeception\\SnapshotAssertions\\": "src"
23-
}
24-
},
25-
"autoload-dev": {
26-
"psr-0": {
27-
"tad\\Codeception\\SnapshotAssertions" : "tests/unit"
28-
}
2+
"name": "lucatume/codeception-snapshot-assertions",
3+
"description": "Snapshot assertions sugar for Codeception.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "theAverageDev (Luca Tumedei)",
9+
"email": "luca@theaveragedev.com"
2910
}
11+
],
12+
"require": {
13+
"ext-dom": "*",
14+
"gajus/dindent": "^2.0",
15+
"codeception/codeception": "^2.5 || ^3.0 || ^4.0"
16+
},
17+
"require-dev": {
18+
"php": ">=7.1",
19+
"squizlabs/php_codesniffer": "*",
20+
"phpstan/phpstan": "^0.12.9"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"tad\\Codeception\\SnapshotAssertions\\": "src"
25+
}
26+
},
27+
"autoload-dev": {
28+
"psr-0": {
29+
"tad\\Codeception\\SnapshotAssertions": "tests/unit"
30+
}
31+
}
3032
}

src/AbstractSnapshot.php

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ class AbstractSnapshot extends Snapshot
2424
/**
2525
* Keeps a counter for each class, function and data-set combination.
2626
*
27-
* @var array
27+
* @var array<string,array>
2828
*/
2929
protected static $counters = [];
3030

3131
/**
3232
* A list of method names provided by the SnapshotAssertions trait.
3333
*
34-
* @var array
34+
* @var array<string>
3535
*/
3636
protected static $traitMethods = [];
3737

@@ -95,6 +95,11 @@ static function (array $backtraceEntry) use ($traitMethods) {
9595
$classFrags = explode('\\', $class);
9696
$classBasename = array_pop($classFrags);
9797
$classFile = (new \ReflectionClass($class))->getFileName();
98+
99+
if ($classFile === false) {
100+
throw new \RuntimeException('Cannot get the filename of the class ' . $class);
101+
}
102+
98103
$classDir = dirname($classFile);
99104
$function = $backtrace[0]['function'];
100105
$dataSetFrag = '';
@@ -121,12 +126,11 @@ static function (array $backtraceEntry) use ($traitMethods) {
121126
}
122127

123128
/**
129+
* Returns an array of the trait method names.
124130
*
131+
* @return array<string> An array of the trait method names.
125132
*
126-
* @return array
127-
* @throws \ReflectionException
128-
* @since TBD
129-
*
133+
* @throws \ReflectionException If a reflection cannot be done on a trait method.
130134
*/
131135
protected static function getTraitMethods()
132136
{
@@ -177,7 +181,11 @@ public function fileExtension()
177181
*
178182
* This method is useful to create, or overwrite, the contents of the snapshot during tests.
179183
*
180-
* @param mixed $contents The snapshot contents.
184+
* @param mixed $contents The snapshot contents.
185+
*
186+
* @throws \ReflectionException
187+
*
188+
* @return void
181189
*/
182190
public function snapshotPutContents($contents)
183191
{
@@ -189,17 +197,18 @@ public function snapshotPutContents($contents)
189197

190198
/**
191199
* Saves the snapshot contents to the snapshot file.
192-
* @throws \RejectionException If there's an issue while building the snapshot filename.
193-
* @throws \RuntimeException If the snapshots folder cannot be created.
194-
* @throws \ReflectionException If there's an issue building the file name from the test case.
200+
*
201+
* @throws \Exception If there's an issue reading or saving the snapshot.
202+
*
203+
* @return void
195204
*/
196205
protected function save()
197206
{
198207
$fileName = $this->getFileName();
199208
$snapshotsDir = dirname($fileName);
200209

201210
if (!is_dir($snapshotsDir) && !mkdir($snapshotsDir, 0777, true) && !is_dir($snapshotsDir)) {
202-
throw new RuntimeException(sprintf('Snapshots directory "%s" was not created', $snapshotsDir));
211+
throw new \RuntimeException(sprintf('Snapshots directory "%s" was not created', $snapshotsDir));
203212
}
204213

205214
file_put_contents($fileName, $this->prepareSnapshotForDump());
@@ -217,6 +226,10 @@ public function prepareSnapshotForDump()
217226

218227
/**
219228
* Asserts the current contents match the contents of the snapshot.
229+
*
230+
* @return void
231+
*
232+
* @throws \ReflectionException If there's an issue building the snapshot file name.
220233
*/
221234
public function assert()
222235
{
@@ -285,6 +298,13 @@ protected function isEmptyData($data)
285298
return !$data;
286299
}
287300

301+
/**
302+
* Loads the data set from the snapshot.
303+
*
304+
* @return void
305+
*
306+
* @throws \ReflectionException
307+
*/
288308
protected function load()
289309
{
290310
if (!file_exists($this->getFileName())) {
@@ -301,6 +321,8 @@ protected function load()
301321
* Copy and paste of the bae method to allow for easier debug.
302322
*
303323
* @param string $message The message to print in debug.
324+
*
325+
* @return void
304326
*/
305327
protected function printDebug($message)
306328
{
@@ -309,7 +331,7 @@ protected function printDebug($message)
309331
/**
310332
* Returns the data name taking care of doing so in a way that is compatible with different PHPUnit versions.
311333
*
312-
* @param TestCase|\PHPUnit_Framework_TestCase $testCase The current test case.
334+
* @param TestCase $testCase The current test case.
313335
*
314336
* @return string The data name if available or an empty string if not available.
315337
*/
@@ -322,21 +344,25 @@ protected function getDataName(TestCase $testCase)
322344
$candidates = array_reverse(class_parents($testCase));
323345
$testCaseClass = get_class($testCase);
324346
$candidates[$testCaseClass] = $testCaseClass;
347+
$read = '';
325348
foreach (array_reverse($candidates) as $class) {
326349
try {
327350
$read = (string)ReflectionHelper::readPrivateProperty($testCase, 'dataName', $class);
328351
} catch (\ReflectionException $e) {
329352
continue;
330353
}
331-
332-
return $read;
354+
break;
333355
}
356+
357+
return $read;
334358
}
335359

336360
/**
337361
* Overrides the base implementation to add a pre-assertion data handler.
338362
*
339-
* {@inheritDoc}
363+
* @param mixed $data The data to check.
364+
*
365+
* @return void
340366
*/
341367
protected function assertData($data)
342368
{
@@ -354,6 +380,8 @@ protected function assertData($data)
354380
* @param callable $dataVisitor The data visitor that will be called on each visit of a snapshot "node".
355381
* The parameters passed to the visitor will be different for each snapshot; usually
356382
* the expected data and the current data.
383+
*
384+
* @return void
357385
*/
358386
public function setDataVisitor(callable $dataVisitor)
359387
{
@@ -366,6 +394,8 @@ public function setDataVisitor(callable $dataVisitor)
366394
* @param string $snapshotFileName The absolute path to the file the snapshot file should use.
367395
* This value is, usually, the one produced by another snapshot `snapshotFileName()`
368396
* method.
397+
*
398+
* @return void
369399
*/
370400
public function setSnapshotFileName($snapshotFileName)
371401
{

0 commit comments

Comments
 (0)