Skip to content

Commit 482a58e

Browse files
authored
Merge pull request #282 from uuf6429/chore/phpstan-level-5
Fixes for PHPStan level 5
2 parents 2ba6668 + 30f3d6b commit 482a58e

28 files changed

+391
-404
lines changed

bin/cucumber_changelog

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ if (!$changelog = file_get_contents(__DIR__ . '/../vendor/cucumber/cucumber/gher
1818
exit(1);
1919
}
2020

21-
if (!preg_match('/(?<changes>##\s+\['.preg_quote($version).'\].*?)^##\s/ms', $changelog, $matches)) {
21+
if (!preg_match('/(?<changes>##\s+\['.preg_quote($version, '/').'\].*?)^##\s/ms', $changelog, $matches)) {
2222
echo "Error: Could not find version $version in changelog\n";
2323
exit(1);
2424
}

bin/update_cucumber

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
#!/usr/bin/env php
22
<?php
33

4-
if (PHP_VERSION_ID < 70400) {
5-
echo "Updater requires PHP 7.4";
6-
exit(1);
7-
}
8-
94
$composerFile = __DIR__ . '/../composer.json';
105
$composerConfig = file_get_contents($composerFile);
116
foreach (json_decode($composerConfig, true, 512, JSON_THROW_ON_ERROR)['repositories'] as $repository)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
"autoload-dev": {
4343
"psr-4": {
44-
"Tests\\Behat\\": "tests/Behat/"
44+
"Tests\\": "tests/"
4545
}
4646
},
4747

phpstan.dist.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 3
2+
level: 5
33
paths:
44
- src
55
- tests

src/Behat/Gherkin/Filter/LineFilter.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@
2222
*/
2323
class LineFilter implements FilterInterface
2424
{
25+
/**
26+
* @var int
27+
*/
2528
protected $filterLine;
2629

2730
/**
2831
* Initializes filter.
2932
*
30-
* @param string $filterLine Line of the scenario to filter on
33+
* @param int|numeric-string $filterLine Line of the scenario to filter on
3134
*/
3235
public function __construct($filterLine)
3336
{
34-
$this->filterLine = intval($filterLine);
37+
$this->filterLine = (int) $filterLine;
3538
}
3639

3740
/**

src/Behat/Gherkin/Filter/LineRangeFilter.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,20 @@
2222
*/
2323
class LineRangeFilter implements FilterInterface
2424
{
25+
/**
26+
* @var int
27+
*/
2528
protected $filterMinLine;
29+
/**
30+
* @var int
31+
*/
2632
protected $filterMaxLine;
2733

2834
/**
2935
* Initializes filter.
3036
*
31-
* @param string $filterMinLine Minimum line of a scenario to filter on
32-
* @param string $filterMaxLine Maximum line of a scenario to filter on
37+
* @param int|numeric-string $filterMinLine Minimum line of a scenario to filter on
38+
* @param int|numeric-string|'*' $filterMaxLine Maximum line of a scenario to filter on
3339
*/
3440
public function __construct($filterMinLine, $filterMaxLine)
3541
{

src/Behat/Gherkin/Lexer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function skipPredictedToken()
145145
* Constructs token with specified parameters.
146146
*
147147
* @param string $type Token type
148-
* @param string $value Token value
148+
* @param int|string|null $value Token value
149149
*
150150
* @return array
151151
*/

src/Behat/Gherkin/Node/FeatureNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ public function getLine()
251251
/**
252252
* Returns whether the file path is an absolute path.
253253
*
254-
* @param string $file A file path
254+
* @param string|null $file A file path
255255
*
256256
* @return bool
257257
*

src/Behat/Gherkin/Parser.php

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
* $featuresArray = $parser->parse('/path/to/feature.feature');
3232
*
3333
* @author Konstantin Kudryashov <ever.zet@gmail.com>
34+
*
35+
* @phpstan-type TParsedExpressionResult FeatureNode|BackgroundNode|ScenarioNode|OutlineNode|ExampleTableNode|TableNode|PyStringNode|StepNode|string
3436
*/
3537
class Parser
3638
{
@@ -83,7 +85,7 @@ public function parse($input, $file = null)
8385
while ('EOS' !== ($predicted = $this->predictTokenType())) {
8486
$node = $this->parseExpression();
8587

86-
if ($node === null || $node === "\n") {
88+
if ($node === "\n") {
8789
continue;
8890
}
8991

@@ -178,7 +180,7 @@ protected function predictTokenType()
178180
/**
179181
* Parses current expression & returns Node.
180182
*
181-
* @return string|FeatureNode|BackgroundNode|ScenarioNode|OutlineNode|TableNode|StepNode
183+
* @phpstan-return TParsedExpressionResult
182184
*
183185
* @throws ParserException
184186
*/
@@ -262,14 +264,12 @@ protected function parseFeature()
262264
));
263265
}
264266

265-
if (!$node instanceof ScenarioNode) {
266-
throw new ParserException(sprintf(
267-
'Expected Scenario, Outline or Background, but got %s on line: %d%s',
268-
$node->getNodeType(),
269-
$node->getLine(),
270-
$this->file ? ' in file: ' . $this->file : ''
271-
));
272-
}
267+
throw new ParserException(sprintf(
268+
'Expected Scenario, Outline or Background, but got %s on line: %d%s',
269+
$node->getNodeType(),
270+
$node->getLine(),
271+
$this->file ? ' in file: ' . $this->file : ''
272+
));
273273
}
274274

275275
return new FeatureNode(
@@ -337,14 +337,12 @@ protected function parseBackground()
337337
));
338338
}
339339

340-
if (!$node instanceof StepNode) {
341-
throw new ParserException(sprintf(
342-
'Expected Step, but got %s on line: %d%s',
343-
$node->getNodeType(),
344-
$node->getLine(),
345-
$this->file ? ' in file: ' . $this->file : ''
346-
));
347-
}
340+
throw new ParserException(sprintf(
341+
'Expected Step, but got %s on line: %d%s',
342+
$node->getNodeType(),
343+
$node->getLine(),
344+
$this->file ? ' in file: ' . $this->file : ''
345+
));
348346
}
349347

350348
return new BackgroundNode(rtrim($title) ?: null, $steps, $keyword, $line);
@@ -396,14 +394,12 @@ protected function parseScenario()
396394
));
397395
}
398396

399-
if (!$node instanceof StepNode) {
400-
throw new ParserException(sprintf(
401-
'Expected Step, but got %s on line: %d%s',
402-
$node->getNodeType(),
403-
$node->getLine(),
404-
$this->file ? ' in file: ' . $this->file : ''
405-
));
406-
}
397+
throw new ParserException(sprintf(
398+
'Expected Step, but got %s on line: %d%s',
399+
$node->getNodeType(),
400+
$node->getLine(),
401+
$this->file ? ' in file: ' . $this->file : ''
402+
));
407403
}
408404

409405
array_pop($this->passedNodesStack);
@@ -472,14 +468,12 @@ protected function parseOutline()
472468
));
473469
}
474470

475-
if (!$node instanceof StepNode) {
476-
throw new ParserException(sprintf(
477-
'Expected Step or Examples table, but got %s on line: %d%s',
478-
$node->getNodeType(),
479-
$node->getLine(),
480-
$this->file ? ' in file: ' . $this->file : ''
481-
));
482-
}
471+
throw new ParserException(sprintf(
472+
'Expected Step or Examples table, but got %s on line: %d%s',
473+
$node->getNodeType(),
474+
$node->getLine(),
475+
$this->file ? ' in file: ' . $this->file : ''
476+
));
483477
}
484478

485479
if (count($examples) === 0) {
@@ -589,7 +583,7 @@ protected function parsePyString()
589583
/**
590584
* Parses tags.
591585
*
592-
* @return BackgroundNode|FeatureNode|OutlineNode|ScenarioNode|StepNode|TableNode|string
586+
* @phpstan-return TParsedExpressionResult
593587
*/
594588
protected function parseTags()
595589
{
@@ -677,7 +671,7 @@ protected function parseNewline()
677671
/**
678672
* Parses language block and updates lexer configuration based on it.
679673
*
680-
* @return BackgroundNode|FeatureNode|OutlineNode|ScenarioNode|StepNode|TableNode|string
674+
* @phpstan-return TParsedExpressionResult
681675
*
682676
* @throws ParserException
683677
*/

tests/Behat/Gherkin/Cache/FileCacheTest.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,18 @@
1919

2020
class FileCacheTest extends TestCase
2121
{
22-
private $path;
23-
private $cache;
22+
private string $path;
23+
private FileCache $cache;
24+
25+
protected function setUp(): void
26+
{
27+
$this->cache = new FileCache($this->path = sys_get_temp_dir() . uniqid('/gherkin-test'));
28+
}
29+
30+
protected function tearDown(): void
31+
{
32+
(new Filesystem())->remove($this->path);
33+
}
2434

2535
public function testIsFreshWhenThereIsNoFile(): void
2636
{
@@ -29,7 +39,7 @@ public function testIsFreshWhenThereIsNoFile(): void
2939

3040
public function testIsFreshOnFreshFile(): void
3141
{
32-
$feature = new FeatureNode(null, null, [], null, [], null, null, null, null);
42+
$feature = new FeatureNode(null, null, [], null, [], '', '', null, 1);
3343

3444
$this->cache->write('some_path', $feature);
3545

@@ -38,7 +48,7 @@ public function testIsFreshOnFreshFile(): void
3848

3949
public function testIsFreshOnOutdated(): void
4050
{
41-
$feature = new FeatureNode(null, null, [], null, [], null, null, null, null);
51+
$feature = new FeatureNode(null, null, [], null, [], '', '', null, 1);
4252

4353
$this->cache->write('some_path', $feature);
4454

@@ -47,8 +57,8 @@ public function testIsFreshOnOutdated(): void
4757

4858
public function testCacheAndRead(): void
4959
{
50-
$scenarios = [new ScenarioNode('Some scenario', [], [], null, null)];
51-
$feature = new FeatureNode('Some feature', 'some description', [], null, $scenarios, null, null, null, null);
60+
$scenarios = [new ScenarioNode('Some scenario', [], [], '', 1)];
61+
$feature = new FeatureNode('Some feature', 'some description', [], null, $scenarios, '', '', null, 1);
5262

5363
$this->cache->write('some_feature', $feature);
5464
$featureRead = $this->cache->read('some_feature');
@@ -61,7 +71,7 @@ public function testBrokenCacheRead(): void
6171
// First, write a valid cache and find the file that was written
6272
$this->cache->write(
6373
'broken_feature',
64-
new FeatureNode(null, null, [], null, [], null, null, null, null),
74+
new FeatureNode(null, null, [], null, [], '', '', null, 1),
6575
);
6676
$files = glob($this->path . '/**/*.feature.cache');
6777
$this->assertCount(1, $files, 'Cache should have written a single file');
@@ -84,14 +94,4 @@ public function testUnwriteableCacheDir(): void
8494
new FileCache('/dev/null/gherkin-test');
8595
}
8696
}
87-
88-
protected function setUp(): void
89-
{
90-
$this->cache = new FileCache($this->path = sys_get_temp_dir() . uniqid('/gherkin-test'));
91-
}
92-
93-
protected function tearDown(): void
94-
{
95-
(new Filesystem())->remove($this->path);
96-
}
9797
}

0 commit comments

Comments
 (0)