Skip to content

Commit

Permalink
Update to PHPStan level 3; various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
uuf6429 committed Jan 31, 2025
1 parent bd74622 commit 79688b6
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 84 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
],

"require": {
"php": "8.1.* || 8.2.* || 8.3.* || 8.4.*",
"php": "^8.1 || ^8.2 || ^8.3 || ^8.4",
"composer-runtime-api": "^2.2"
},

Expand Down
2 changes: 1 addition & 1 deletion phpstan.dist.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 1
level: 3
paths:
- src
- tests
12 changes: 4 additions & 8 deletions src/Behat/Gherkin/Node/OutlineNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class OutlineNode implements ScenarioInterface
*/
private $steps;
/**
* @var ExampleTableNode|ExampleTableNode[]
* @var array<array-key, ExampleTableNode>
*/
private $tables;
/**
Expand All @@ -52,7 +52,7 @@ class OutlineNode implements ScenarioInterface
* @param string|null $title
* @param string[] $tags
* @param StepNode[] $steps
* @param ExampleTableNode|ExampleTableNode[] $tables
* @param ExampleTableNode|array<array-key, ExampleTableNode> $tables
* @param string $keyword
* @param int $line
*/
Expand All @@ -69,11 +69,7 @@ public function __construct(
$this->steps = $steps;
$this->keyword = $keyword;
$this->line = $line;
if (!is_array($tables)) {
$this->tables = [$tables];
} else {
$this->tables = $tables;
}
$this->tables = is_array($tables) ? $tables : [$tables];
}

/**
Expand Down Expand Up @@ -197,7 +193,7 @@ public function getExamples()
/**
* Returns examples tables array for the outline.
*
* @return ExampleTableNode[]
* @return array<array-key, ExampleTableNode>
*/
public function getExampleTables()
{
Expand Down
10 changes: 4 additions & 6 deletions src/Behat/Gherkin/Node/TableNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
*/
class TableNode implements ArgumentInterface, IteratorAggregate
{
private array $table;

/**
* @var array
*/
private $table;
/**
* @var int
* @var array<array-key, int>
*/
private $maxLineLength = [];
private array $maxLineLength = [];

/**
* Initializes table.
Expand Down
13 changes: 8 additions & 5 deletions tests/Behat/Gherkin/Filter/LineFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class LineFilterTest extends FilterTestCase
{
public function testIsFeatureMatchFilter()
public function testIsFeatureMatchFilter(): void
{
$feature = new FeatureNode(null, null, [], null, [], null, null, null, 1);

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

public function testIsScenarioMatchFilter()
public function testIsScenarioMatchFilter(): void
{
$scenario = new ScenarioNode(null, [], [], null, 2);

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

public function testFilterFeatureScenario()
public function testFilterFeatureScenario(): void
{
$filter = new LineFilter(2);
$feature = $filter->filterFeature($this->getParsedFeature());
Expand All @@ -71,19 +71,20 @@ public function testFilterFeatureScenario()
$this->assertCount(0, $scenarios = $feature->getScenarios());
}

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

$filter = new LineFilter(20);
$feature = $filter->filterFeature($this->getParsedFeature());
$this->assertCount(1, $scenarios = $feature->getScenarios());
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
$exampleTableNodes = $scenarios[0]->getExampleTables();
$this->assertCount(1, $exampleTableNodes);
$this->assertCount(2, $exampleTableNodes[0]->getRows());
Expand All @@ -97,6 +98,7 @@ public function testFilterFeatureOutline()
$feature = $filter->filterFeature($this->getParsedFeature());
$this->assertCount(1, $scenarios = $feature->getScenarios());
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
$exampleTableNodes = $scenarios[0]->getExampleTables();
$this->assertCount(1, $exampleTableNodes);
$this->assertCount(2, $exampleTableNodes[0]->getRows());
Expand All @@ -110,6 +112,7 @@ public function testFilterFeatureOutline()
$feature = $filter->filterFeature($this->getParsedFeature());
$this->assertCount(1, $scenarios = $feature->getScenarios());
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
$this->assertCount(1, $scenarios[0]->getExampleTable()->getRows());
$this->assertSame([['action', 'outcome']], $scenarios[0]->getExampleTable()->getRows());
}
Expand Down
28 changes: 14 additions & 14 deletions tests/Behat/Gherkin/Filter/LineRangeFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\OutlineNode;
use Behat\Gherkin\Node\ScenarioNode;
use PHPUnit\Framework\Attributes\DataProvider;

class LineRangeFilterTest extends FilterTestCase
{
public function featureLineRangeProvider()
public static function featureLineRangeProvider(): iterable
{
return [
['1', '1', true],
Expand All @@ -29,18 +30,16 @@ public function featureLineRangeProvider()
];
}

/**
* @dataProvider featureLineRangeProvider
*/
public function testIsFeatureMatchFilter($filterMinLine, $filterMaxLine, $expected)
#[DataProvider('featureLineRangeProvider')]
public function testIsFeatureMatchFilter(string $filterMinLine, string $filterMaxLine, bool $expected): void
{
$feature = new FeatureNode(null, null, [], null, [], null, null, null, 1);

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

public function scenarioLineRangeProvider()
public static function scenarioLineRangeProvider(): iterable
{
return [
['1', '2', 1],
Expand All @@ -55,22 +54,20 @@ public function scenarioLineRangeProvider()
];
}

/**
* @dataProvider scenarioLineRangeProvider
*/
public function testIsScenarioMatchFilter($filterMinLine, $filterMaxLine, $expectedNumberOfMatches)
#[DataProvider('scenarioLineRangeProvider')]
public function testIsScenarioMatchFilter(string $filterMinLine, string $filterMaxLine, int $expectedNumberOfMatches): void
{
$scenario = new ScenarioNode(null, [], [], null, 2);
$outline = new OutlineNode(null, [], [], [new ExampleTableNode([], null)], null, 3);

$filter = new LineRangeFilter($filterMinLine, $filterMaxLine);
$this->assertEquals(
$expectedNumberOfMatches,
intval($filter->isScenarioMatch($scenario)) + intval($filter->isScenarioMatch($outline))
(int) $filter->isScenarioMatch($scenario) + (int) $filter->isScenarioMatch($outline)
);
}

public function testFilterFeatureScenario()
public function testFilterFeatureScenario(): void
{
$filter = new LineRangeFilter(1, 3);
$feature = $filter->filterFeature($this->getParsedFeature());
Expand All @@ -87,19 +84,20 @@ public function testFilterFeatureScenario()
$this->assertCount(0, $scenarios = $feature->getScenarios());
}

public function testFilterFeatureOutline()
public function testFilterFeatureOutline(): void
{
$filter = new LineRangeFilter(12, 14);
$feature = $filter->filterFeature($this->getParsedFeature());
/* @var OutlineNode[] $scenarios */
$this->assertCount(1, $scenarios = $feature->getScenarios());
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
$this->assertFalse($scenarios[0]->hasExamples());

$filter = new LineRangeFilter(16, 21);
$feature = $filter->filterFeature($this->getParsedFeature());
$this->assertCount(1, $scenarios = $feature->getScenarios());
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
$exampleTableNodes = $scenarios[0]->getExampleTables();
$this->assertCount(1, $exampleTableNodes);
$this->assertCount(3, $exampleTableNodes[0]->getRows());
Expand All @@ -114,6 +112,7 @@ public function testFilterFeatureOutline()
$feature = $filter->filterFeature($this->getParsedFeature());
$this->assertCount(1, $scenarios = $feature->getScenarios());
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
$exampleTableNodes = $scenarios[0]->getExampleTables();
$this->assertCount(2, $exampleTableNodes);

Expand All @@ -137,6 +136,7 @@ public function testFilterFeatureOutline()
$feature = $filter->filterFeature($this->getParsedFeature());
$this->assertCount(1, $scenarios = $feature->getScenarios());
$this->assertSame('Scenario#3', $scenarios[0]->getTitle());
$this->assertInstanceOf(OutlineNode::class, $scenarios[0]);
$exampleTableNodes = $scenarios[0]->getExampleTables();
$this->assertCount(1, $exampleTableNodes);
$this->assertCount(2, $exampleTableNodes[0]->getRows());
Expand Down
30 changes: 15 additions & 15 deletions tests/Behat/Gherkin/Filter/TagFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class TagFilterTest extends TestCase
{
public function testFilterFeature()
public function testFilterFeature(): void
{
$feature = new FeatureNode(null, null, ['wip'], null, [], null, null, null, 1);
$filter = new TagFilter('@wip');
Expand All @@ -46,7 +46,7 @@ public function testFilterFeature()
$this->assertSame([$matchedScenario], $filteredFeature->getScenarios());
}

public function testIsFeatureMatchFilter()
public function testIsFeatureMatchFilter(): void
{
$feature = new FeatureNode(null, null, [], null, [], null, null, null, 1);

Expand Down Expand Up @@ -95,7 +95,7 @@ public function testIsFeatureMatchFilter()
$this->assertTrue($filter->isFeatureMatch($feature));
}

public function testIsScenarioMatchFilter()
public function testIsScenarioMatchFilter(): void
{
$feature = new FeatureNode(null, null, ['feature-tag'], null, [], null, null, null, 1);
$scenario = new ScenarioNode(null, [], [], null, 2);
Expand Down Expand Up @@ -189,7 +189,7 @@ public function testIsScenarioMatchFilter()
$this->assertFalse($tagFilter->isScenarioMatch($feature, $scenario), 'Tags from different examples tables');
}

public function testFilterFeatureWithTaggedExamples()
public function testFilterFeatureWithTaggedExamples(): void
{
$exampleTableNode1 = new ExampleTableNode([], null, ['etag1', 'etag2']);
$exampleTableNode2 = new ExampleTableNode([], null, ['etag2', 'etag3']);
Expand All @@ -207,13 +207,13 @@ public function testFilterFeatureWithTaggedExamples()
$tagFilter = new TagFilter('@etag1');
$matched = $tagFilter->filterFeature($feature);
$scenarioInterfaces = $matched->getScenarios();
/* @noinspection PhpUndefinedMethodInspection */
$this->assertInstanceOf(OutlineNode::class, $scenarioInterfaces[0]);
$this->assertEquals([$exampleTableNode1], $scenarioInterfaces[0]->getExampleTables());

$tagFilter = new TagFilter('~@etag3');
$matched = $tagFilter->filterFeature($feature);
$scenarioInterfaces = $matched->getScenarios();
/* @noinspection PhpUndefinedMethodInspection */
$this->assertInstanceOf(OutlineNode::class, $scenarioInterfaces[0]);
$this->assertEquals([$exampleTableNode1], $scenarioInterfaces[0]->getExampleTables());

$tagFilter = new TagFilter('@wip');
Expand All @@ -224,13 +224,13 @@ public function testFilterFeatureWithTaggedExamples()
$tagFilter = new TagFilter('@wip&&@etag3');
$matched = $tagFilter->filterFeature($feature);
$scenarioInterfaces = $matched->getScenarios();
/* @noinspection PhpUndefinedMethodInspection */
$this->assertInstanceOf(OutlineNode::class, $scenarioInterfaces[0]);
$this->assertEquals([$exampleTableNode2], $scenarioInterfaces[0]->getExampleTables());

$tagFilter = new TagFilter('@feature-tag&&@etag1&&@wip');
$matched = $tagFilter->filterFeature($feature);
$scenarioInterfaces = $matched->getScenarios();
/* @noinspection PhpUndefinedMethodInspection */
$this->assertInstanceOf(OutlineNode::class, $scenarioInterfaces[0]);
$this->assertEquals([$exampleTableNode1], $scenarioInterfaces[0]->getExampleTables());

$tagFilter = new TagFilter('@feature-tag&&~@etag11111&&@wip');
Expand All @@ -241,7 +241,7 @@ public function testFilterFeatureWithTaggedExamples()
$tagFilter = new TagFilter('@feature-tag&&~@etag1&&@wip');
$matched = $tagFilter->filterFeature($feature);
$scenarioInterfaces = $matched->getScenarios();
/* @noinspection PhpUndefinedMethodInspection */
$this->assertInstanceOf(OutlineNode::class, $scenarioInterfaces[0]);
$this->assertEquals([$exampleTableNode2], $scenarioInterfaces[0]->getExampleTables());

$tagFilter = new TagFilter('@feature-tag&&@etag2');
Expand Down Expand Up @@ -272,13 +272,13 @@ public function testFilterFeatureWithTaggedExamples()
$matched = $tagFilter->filterFeature($feature);
$scenarioInterfaces = $matched->getScenarios();
$this->assertCount(2, $scenarioInterfaces);
/* @noinspection PhpUndefinedMethodInspection */
$this->assertInstanceOf(OutlineNode::class, $scenarioInterfaces[0]);
$this->assertEquals([$exampleTableNode2], $scenarioInterfaces[0]->getExampleTables());
/* @noinspection PhpUndefinedMethodInspection */
$this->assertInstanceOf(OutlineNode::class, $scenarioInterfaces[1]);
$this->assertEquals([$exampleTableNode3], $scenarioInterfaces[1]->getExampleTables());
}

public function testFilterWithWhitespaceIsDeprecated()
public function testFilterWithWhitespaceIsDeprecated(): void
{
$this->expectDeprecationError();

Expand All @@ -291,7 +291,7 @@ public function testFilterWithWhitespaceIsDeprecated()
$this->assertEquals([$scenario], $scenarios);
}

public function testTagFilterThatIsAllWhitespaceIsIgnored()
public function testTagFilterThatIsAllWhitespaceIsIgnored(): void
{
$feature = new FeatureNode(null, null, [], null, [], null, null, null, 1);
$tagFilter = new TagFilter('');
Expand All @@ -300,7 +300,7 @@ public function testTagFilterThatIsAllWhitespaceIsIgnored()
$this->assertTrue($result);
}

private function expectDeprecationError()
private function expectDeprecationError(): void
{
set_error_handler(
static function ($errno, $errstr) {
Expand All @@ -309,6 +309,6 @@ static function ($errno, $errstr) {
},
E_ALL
);
$this->expectException('Exception');
$this->expectException(Exception::class);
}
}
Loading

0 comments on commit 79688b6

Please sign in to comment.