Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for PHPStan level 5 #282

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

uuf6429
Copy link
Contributor

@uuf6429 uuf6429 commented Feb 6, 2025

No description provided.

@@ -41,7 +41,7 @@

"autoload-dev": {
"psr-4": {
"Tests\\Behat\\": "tests/Behat/"
"Tests\\": "tests/"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helps:

  1. IDEs (e.g. PhpStorm or IDEA) correctly detect the tests directory as the "tests root"
  2. It decrease the wtf factor if someone adds test classes elsewhere which wouldn't load otherwise
  3. It may not be the best, performance-wise, but I think it's not so critical seeing as it only impacts tests (i.e. composer --dev).

protected $filterLine;

/**
* Initializes filter.
*
* @param string $filterLine Line of the scenario to filter on
* @param int|numeric-string $filterLine Line of the scenario to filter on
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing a non-numeric-string is Doing It Wrong and deserves a PHPStan slap. ;)

@@ -83,7 +85,7 @@ public function parse($input, $file = null)
while ('EOS' !== ($predicted = $this->predictTokenType())) {
$node = $this->parseExpression();

if ($node === null || $node === "\n") {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked and couldn't find evidence of any parser method returning a null (in addition, the PHPDocs never mentioned nulls either, hence the PHPStan warning).

@@ -262,14 +264,12 @@ protected function parseFeature()
));
}

if (!$node instanceof ScenarioNode) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition and the other cases below were flagged as impossible by PHPStan because:

  1. there is already an earlier branch unconditionally handling such instance
  2. $node is never overwritten

* @param string $filterMinLine Minimum line of a scenario to filter on
* @param string $filterMaxLine Maximum line of a scenario to filter on
* @param int|numeric-string $filterMinLine Minimum line of a scenario to filter on
* @param int|numeric-string|'*' $filterMaxLine Maximum line of a scenario to filter on
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified that PHPStan handles this well: https://phpstan.org/r/bd2527e3-519d-491e-9eb6-7208761748dd

@@ -31,6 +31,8 @@
* $featuresArray = $parser->parse('/path/to/feature.feature');
*
* @author Konstantin Kudryashov <[email protected]>
*
* @phpstan-type TParsedExpressionResult FeatureNode|BackgroundNode|ScenarioNode|OutlineNode|ExampleTableNode|TableNode|PyStringNode|StepNode|string|"\n"|''
Copy link
Contributor Author

@uuf6429 uuf6429 Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified that "\n" is the right way to go: https://phpstan.org/r/8283cfec-bff9-4375-be68-a046ec433b47

Note: even though string includes '' and "\n" I decided to mention them separately nevertheless.. hopefully it would be helpful.

{
if (!extension_loaded('xdebug')) {
$this->markTestSkipped('xdebug extension must be enabled.');
}
$defaultPHPSetting = 256;
$this->iniSet('xdebug.max_nesting_level', $defaultPHPSetting);
ini_set('xdebug.max_nesting_level', $defaultPHPSetting);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->iniSet was resetting the state at the end of the test. This is not done anymore.

Copy link
Contributor Author

@uuf6429 uuf6429 Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof I'm not sure what you meant. Did you mean that is dead/redundant code?

Note that that method has been deprecated. Internally, it seems it "just" calls ini_set (and updates PHPUnit's ini state).

Since this concerns tests, and tests seem to be running fine, I think this may be a non-issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I see that the GH workflow doesn't seem to run with xdebug, I'll try it later on at home and see what happens (since this seems to relate exclusively to xdebug).

PS: We should probably integrate codecov into this project. 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@uuf6429 this "updating PHPUnit's ini state" is precisely the part that will restore them on teardown

Copy link
Contributor Author

@uuf6429 uuf6429 Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see what you mean now, something like:

ini_set(<new value>);
try {
    <the test>
} finally {
    ini_set(<old value>);
}

I'll fix it later on today, thanks for pointing it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I tried two approaches: 4f02b77 and e238904.
Let me know if you prefer the first one, and I'll revert the last commit.

@uuf6429 uuf6429 requested a review from stof February 11, 2025 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants