Skip to content

Commit e016902

Browse files
Merge pull request #281 from uuf6429/chore/phpstan-level-3
Update to PHPStan level 3; various fixes
2 parents bd74622 + ac70642 commit e016902

File tree

12 files changed

+123
-94
lines changed

12 files changed

+123
-94
lines changed

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: 1
2+
level: 3
33
paths:
44
- src
55
- tests

src/Behat/Gherkin/Loader/AbstractFileLoader.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected function findRelativePath($path)
5050
*
5151
* @param string $path Relative path
5252
*
53-
* @return string
53+
* @return false|string
5454
*/
5555
protected function findAbsolutePath($path)
5656
{
@@ -69,4 +69,17 @@ protected function findAbsolutePath($path)
6969

7070
return false;
7171
}
72+
73+
/**
74+
* @throws \RuntimeException
75+
*/
76+
final protected function getAbsolutePath(string $path): string
77+
{
78+
$resolvedPath = $this->findAbsolutePath($path);
79+
if ($resolvedPath === false) {
80+
throw new \RuntimeException("Unable to locate absolute path of \"$path\"");
81+
}
82+
83+
return $resolvedPath;
84+
}
7285
}

src/Behat/Gherkin/Loader/DirectoryLoader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public function __construct(Gherkin $gherkin)
4444
public function supports($resource)
4545
{
4646
return is_string($resource)
47-
&& is_dir($this->findAbsolutePath($resource));
47+
&& ($path = $this->findAbsolutePath($resource)) !== false
48+
&& is_dir($path);
4849
}
4950

5051
/**
@@ -56,8 +57,7 @@ public function supports($resource)
5657
*/
5758
public function load($resource)
5859
{
59-
$path = $this->findAbsolutePath($resource);
60-
60+
$path = $this->getAbsolutePath($resource);
6161
$iterator = new RecursiveIteratorIterator(
6262
new RecursiveDirectoryIterator($path, RecursiveDirectoryIterator::SKIP_DOTS)
6363
);

src/Behat/Gherkin/Loader/GherkinFileLoader.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,9 @@ public function setCache(CacheInterface $cache)
5656
public function supports($resource)
5757
{
5858
return is_string($resource)
59-
&& is_file($absolute = $this->findAbsolutePath($resource))
60-
&& pathinfo($absolute, PATHINFO_EXTENSION) === 'feature';
59+
&& ($path = $this->findAbsolutePath($resource)) !== false
60+
&& is_file($path)
61+
&& pathinfo($path, PATHINFO_EXTENSION) === 'feature';
6162
}
6263

6364
/**
@@ -69,8 +70,7 @@ public function supports($resource)
6970
*/
7071
public function load($resource)
7172
{
72-
$path = $this->findAbsolutePath($resource);
73-
73+
$path = $this->getAbsolutePath($resource);
7474
if ($this->cache) {
7575
if ($this->cache->isFresh($path, filemtime($path))) {
7676
$feature = $this->cache->read($path);

src/Behat/Gherkin/Loader/YamlFileLoader.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ public function __construct()
3737
public function supports($resource)
3838
{
3939
return is_string($resource)
40-
&& is_file($absolute = $this->findAbsolutePath($resource))
41-
&& pathinfo($absolute, PATHINFO_EXTENSION) === 'yml';
40+
&& ($path = $this->findAbsolutePath($resource)) !== false
41+
&& is_file($path)
42+
&& pathinfo($path, PATHINFO_EXTENSION) === 'yml';
4243
}
4344

4445
/**
@@ -50,7 +51,7 @@ public function supports($resource)
5051
*/
5152
public function load($resource)
5253
{
53-
$path = $this->findAbsolutePath($resource);
54+
$path = $this->getAbsolutePath($resource);
5455
$hash = Yaml::parse(file_get_contents($path));
5556

5657
$features = $this->loader->load($hash);

src/Behat/Gherkin/Node/OutlineNode.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class OutlineNode implements ScenarioInterface
3030
*/
3131
private $steps;
3232
/**
33-
* @var ExampleTableNode|ExampleTableNode[]
33+
* @var array<array-key, ExampleTableNode>
3434
*/
3535
private $tables;
3636
/**
@@ -52,7 +52,7 @@ class OutlineNode implements ScenarioInterface
5252
* @param string|null $title
5353
* @param string[] $tags
5454
* @param StepNode[] $steps
55-
* @param ExampleTableNode|ExampleTableNode[] $tables
55+
* @param ExampleTableNode|array<array-key, ExampleTableNode> $tables
5656
* @param string $keyword
5757
* @param int $line
5858
*/
@@ -69,11 +69,7 @@ public function __construct(
6969
$this->steps = $steps;
7070
$this->keyword = $keyword;
7171
$this->line = $line;
72-
if (!is_array($tables)) {
73-
$this->tables = [$tables];
74-
} else {
75-
$this->tables = $tables;
76-
}
72+
$this->tables = is_array($tables) ? $tables : [$tables];
7773
}
7874

7975
/**
@@ -197,7 +193,7 @@ public function getExamples()
197193
/**
198194
* Returns examples tables array for the outline.
199195
*
200-
* @return ExampleTableNode[]
196+
* @return array<array-key, ExampleTableNode>
201197
*/
202198
public function getExampleTables()
203199
{

src/Behat/Gherkin/Node/TableNode.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
*/
2424
class TableNode implements ArgumentInterface, IteratorAggregate
2525
{
26+
private array $table;
27+
2628
/**
27-
* @var array
28-
*/
29-
private $table;
30-
/**
31-
* @var int
29+
* @var array<array-key, int>
3230
*/
33-
private $maxLineLength = [];
31+
private array $maxLineLength = [];
3432

3533
/**
3634
* Initializes table.

tests/Behat/Gherkin/Filter/LineFilterTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
class LineFilterTest extends FilterTestCase
2020
{
21-
public function testIsFeatureMatchFilter()
21+
public function testIsFeatureMatchFilter(): void
2222
{
2323
$feature = new FeatureNode(null, null, [], null, [], null, null, null, 1);
2424

@@ -32,7 +32,7 @@ public function testIsFeatureMatchFilter()
3232
$this->assertFalse($filter->isFeatureMatch($feature));
3333
}
3434

35-
public function testIsScenarioMatchFilter()
35+
public function testIsScenarioMatchFilter(): void
3636
{
3737
$scenario = new ScenarioNode(null, [], [], null, 2);
3838

@@ -54,7 +54,7 @@ public function testIsScenarioMatchFilter()
5454
$this->assertTrue($filter->isScenarioMatch($outline));
5555
}
5656

57-
public function testFilterFeatureScenario()
57+
public function testFilterFeatureScenario(): void
5858
{
5959
$filter = new LineFilter(2);
6060
$feature = $filter->filterFeature($this->getParsedFeature());
@@ -71,19 +71,20 @@ public function testFilterFeatureScenario()
7171
$this->assertCount(0, $scenarios = $feature->getScenarios());
7272
}
7373

74-
public function testFilterFeatureOutline()
74+
public function testFilterFeatureOutline(): void
7575
{
7676
$filter = new LineFilter(13);
7777
$feature = $filter->filterFeature($this->getParsedFeature());
78-
/* @var OutlineNode[] $scenarios */
7978
$this->assertCount(1, $scenarios = $feature->getScenarios());
8079
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
80+
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
8181
$this->assertCount(4, $scenarios[0]->getExampleTable()->getRows());
8282

8383
$filter = new LineFilter(20);
8484
$feature = $filter->filterFeature($this->getParsedFeature());
8585
$this->assertCount(1, $scenarios = $feature->getScenarios());
8686
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
87+
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
8788
$exampleTableNodes = $scenarios[0]->getExampleTables();
8889
$this->assertCount(1, $exampleTableNodes);
8990
$this->assertCount(2, $exampleTableNodes[0]->getRows());
@@ -97,6 +98,7 @@ public function testFilterFeatureOutline()
9798
$feature = $filter->filterFeature($this->getParsedFeature());
9899
$this->assertCount(1, $scenarios = $feature->getScenarios());
99100
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
101+
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
100102
$exampleTableNodes = $scenarios[0]->getExampleTables();
101103
$this->assertCount(1, $exampleTableNodes);
102104
$this->assertCount(2, $exampleTableNodes[0]->getRows());
@@ -110,6 +112,7 @@ public function testFilterFeatureOutline()
110112
$feature = $filter->filterFeature($this->getParsedFeature());
111113
$this->assertCount(1, $scenarios = $feature->getScenarios());
112114
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
115+
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
113116
$this->assertCount(1, $scenarios[0]->getExampleTable()->getRows());
114117
$this->assertSame([['action', 'outcome']], $scenarios[0]->getExampleTable()->getRows());
115118
}

tests/Behat/Gherkin/Filter/LineRangeFilterTest.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515
use Behat\Gherkin\Node\FeatureNode;
1616
use Behat\Gherkin\Node\OutlineNode;
1717
use Behat\Gherkin\Node\ScenarioNode;
18+
use PHPUnit\Framework\Attributes\DataProvider;
1819

1920
class LineRangeFilterTest extends FilterTestCase
2021
{
21-
public function featureLineRangeProvider()
22+
public static function featureLineRangeProvider(): iterable
2223
{
2324
return [
2425
['1', '1', true],
@@ -29,18 +30,16 @@ public function featureLineRangeProvider()
2930
];
3031
}
3132

32-
/**
33-
* @dataProvider featureLineRangeProvider
34-
*/
35-
public function testIsFeatureMatchFilter($filterMinLine, $filterMaxLine, $expected)
33+
#[DataProvider('featureLineRangeProvider')]
34+
public function testIsFeatureMatchFilter(string $filterMinLine, string $filterMaxLine, bool $expected): void
3635
{
3736
$feature = new FeatureNode(null, null, [], null, [], null, null, null, 1);
3837

3938
$filter = new LineRangeFilter($filterMinLine, $filterMaxLine);
4039
$this->assertSame($expected, $filter->isFeatureMatch($feature));
4140
}
4241

43-
public function scenarioLineRangeProvider()
42+
public static function scenarioLineRangeProvider(): iterable
4443
{
4544
return [
4645
['1', '2', 1],
@@ -55,22 +54,20 @@ public function scenarioLineRangeProvider()
5554
];
5655
}
5756

58-
/**
59-
* @dataProvider scenarioLineRangeProvider
60-
*/
61-
public function testIsScenarioMatchFilter($filterMinLine, $filterMaxLine, $expectedNumberOfMatches)
57+
#[DataProvider('scenarioLineRangeProvider')]
58+
public function testIsScenarioMatchFilter(string $filterMinLine, string $filterMaxLine, int $expectedNumberOfMatches): void
6259
{
6360
$scenario = new ScenarioNode(null, [], [], null, 2);
6461
$outline = new OutlineNode(null, [], [], [new ExampleTableNode([], null)], null, 3);
6562

6663
$filter = new LineRangeFilter($filterMinLine, $filterMaxLine);
6764
$this->assertEquals(
6865
$expectedNumberOfMatches,
69-
intval($filter->isScenarioMatch($scenario)) + intval($filter->isScenarioMatch($outline))
66+
(int) $filter->isScenarioMatch($scenario) + (int) $filter->isScenarioMatch($outline)
7067
);
7168
}
7269

73-
public function testFilterFeatureScenario()
70+
public function testFilterFeatureScenario(): void
7471
{
7572
$filter = new LineRangeFilter(1, 3);
7673
$feature = $filter->filterFeature($this->getParsedFeature());
@@ -87,19 +84,20 @@ public function testFilterFeatureScenario()
8784
$this->assertCount(0, $scenarios = $feature->getScenarios());
8885
}
8986

90-
public function testFilterFeatureOutline()
87+
public function testFilterFeatureOutline(): void
9188
{
9289
$filter = new LineRangeFilter(12, 14);
9390
$feature = $filter->filterFeature($this->getParsedFeature());
94-
/* @var OutlineNode[] $scenarios */
9591
$this->assertCount(1, $scenarios = $feature->getScenarios());
9692
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
93+
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
9794
$this->assertFalse($scenarios[0]->hasExamples());
9895

9996
$filter = new LineRangeFilter(16, 21);
10097
$feature = $filter->filterFeature($this->getParsedFeature());
10198
$this->assertCount(1, $scenarios = $feature->getScenarios());
10299
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
100+
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
103101
$exampleTableNodes = $scenarios[0]->getExampleTables();
104102
$this->assertCount(1, $exampleTableNodes);
105103
$this->assertCount(3, $exampleTableNodes[0]->getRows());
@@ -114,6 +112,7 @@ public function testFilterFeatureOutline()
114112
$feature = $filter->filterFeature($this->getParsedFeature());
115113
$this->assertCount(1, $scenarios = $feature->getScenarios());
116114
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
115+
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
117116
$exampleTableNodes = $scenarios[0]->getExampleTables();
118117
$this->assertCount(2, $exampleTableNodes);
119118

@@ -137,6 +136,7 @@ public function testFilterFeatureOutline()
137136
$feature = $filter->filterFeature($this->getParsedFeature());
138137
$this->assertCount(1, $scenarios = $feature->getScenarios());
139138
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
139+
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
140140
$exampleTableNodes = $scenarios[0]->getExampleTables();
141141
$this->assertCount(1, $exampleTableNodes);
142142
$this->assertCount(2, $exampleTableNodes[0]->getRows());

0 commit comments

Comments
 (0)