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

Replace VariableAnalysis Sniff with VariableAnalysis package #449

Closed
GaryJones opened this issue Oct 31, 2019 · 5 comments · Fixed by #450
Closed

Replace VariableAnalysis Sniff with VariableAnalysis package #449

GaryJones opened this issue Oct 31, 2019 · 5 comments · Fixed by #450

Comments

@GaryJones
Copy link
Contributor

Follow-on to #236.

We currently have a VariableAnalysis sniff, but it has some issues. It currently provides the following violations:

  • WordPressVIPMinimum.Variables.VariableAnalysis.VariableRedeclaration: Warning, severity 5, "Redeclaration of %s %s as %s."
  • WordPressVIPMinimum.Variables.VariableAnalysis.UndefinedVariable: Warning, severity 5, "Variable %s is undefined."
  • WordPressVIPMinimum.Variables.VariableAnalysis.UnusedVariable: Warning, severity 5, "Unused %s %s."
  • WordPressVIPMinimum.Variables.VariableAnalysis.SelfInsideClosure: Error, severity 5, "Use of {$err_desc}%s inside closure."
  • WordPressVIPMinimum.Variables.VariableAnalysis.SelfOutsideClass: Error, severity 5, "Use of {$err_desc}%s outside class definition."
  • WordPressVIPMinimum.Variables.VariableAnalysis.StaticInsideClosure: Error, severity 5, "Use of {$err_desc}%s inside closure."
  • WordPressVIPMinimum.Variables.VariableAnalysis.StaticOutsideClass: Error, severity 5, "Use of {$err_desc}%s outside class definition."

In #236, it was decided to replace this with https://github.com/sirbrillig/phpcs-variable-analysis (cc @sirbrillig) - this provides the following violations:

  • VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration: Warning, severity 5, "Redeclaration of %s %s as %s."
  • VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable: Warning, severity 5, "Variable %s is undefined."
  • VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable: Warning, severity 5, "Unused %s %s."
  • VariableAnalysis.CodeAnalysis.VariableAnalysis.SelfOutsideClass: Error, severity 5, "Use of self::%s outside class definition."
  • VariableAnalysis.CodeAnalysis.VariableAnalysis.StaticOutsideClass: Error, severity 5, "Use of static::%s outside class definition."

There is no SelfInsideClosure or StaticInsideClosure violations, as these appear to be allowed now in modern versions of PHP. The rest of the detection process is more maintained, and handles PHP 7 code better. Switching to it would remove some common false positives.

We could also take this opportunity is taken to silence the UnusedVariable violation. It's severity is already dropped down to 1 for WordPress-VIP-Go, which is the same as what the bot runs at (though this may change in the future). From a VIP perspective, unused variables are not important.

Undefined variables, however, will be flagged in PHP 8, so we should maintain this violation (WordPress-VIP-Go sets it as severity 3), even if the message is changed to highlight this incompatibility with PHP 8.

False Positives

The following snippet is often highlighted by the existing sniff as $this being undefined when in a closure in a class. That should no longer be flagged by the new package.

class MyClass {
	function my_function() {
		return function() {
			$this->my_callback();
		};
	}

	function my_callback() {}
}

The old sniff should be kept and marked as deprecated, to avoid breaking compatibility with custom rulesets, until the next major release of VIPCS when it can be removed.

@GaryJones GaryJones added this to the 2.1 milestone Oct 31, 2019
@GaryJones GaryJones self-assigned this Oct 31, 2019
GaryJones added a commit that referenced this issue Oct 31, 2019
Introduces a new dependency of `sirbrillig/phpcs-variable-analysis` which is a more maintained version of the existing `VariableAnalysis` sniff in VIPCS.

The new sniff is added to the `WordPressVIPMinimum` ruleset. `WordPress-VIP-Go` ruleset inherits this, and maintains its dropping of the severity for `UnusedVariable` and `UndefinedVariable` violations.

The ruleset tests have set the previously undefined variables (or changed some variable names), so that violations on each line are still specific to the thing being tested, and doesn't include extra violations due to the use of throwaway variables.

One of the reasons for changing was the previously false positive with respect to the use of `$this` inside closures inside class methods. A ruleset test has been added to cover this, and it correctly does not get flagged in the ruleset test.

The old `VariableAnalysis` has been marked as deprecated, until it can be removed in the next major release of VIPCS. This will stop any breakage of setups which reference `WordPressVIPMinimum.Variables.VariableAnalysis` in a custom ruleset. If called, it throws a warning, and then passes control to the new VariableAnalysis sniff. The deprecation notice is excluded if the sniff is not explicitly included, and the duplicate violation messages are also silenced.

The previously used `VariableAnalaysisHelper.php` has been removed completely, as no-one should have been referencing that directly.

Fixes #449.
@simonwheatley
Copy link

Noted in JIRA as [GH-3323]

GaryJones added a commit that referenced this issue Oct 31, 2019
Introduces a new dependency of `sirbrillig/phpcs-variable-analysis` which is a more maintained version of the existing `VariableAnalysis` sniff in VIPCS.

The new sniff is added to the `WordPressVIPMinimum` ruleset. `WordPress-VIP-Go` ruleset inherits this, and maintains its dropping of the severity for `UnusedVariable` and `UndefinedVariable` violations.

The ruleset tests have set the previously undefined variables (or changed some variable names), so that violations on each line are still specific to the thing being tested, and doesn't include extra violations due to the use of throwaway variables.

One of the reasons for changing was the previously false positive with respect to the use of `$this` inside closures inside class methods. A ruleset test has been added to cover this, and it correctly does not get flagged in the ruleset test.

The old `VariableAnalysis` has been marked as deprecated, until it can be removed in the next major release of VIPCS. This will stop any breakage of setups which reference `WordPressVIPMinimum.Variables.VariableAnalysis` in a custom ruleset. If called, it throws a warning, and then passes control to the new VariableAnalysis sniff. The deprecation notice is excluded if the sniff is not explicitly included, and the duplicate violation messages are also silenced.

The previously used `VariableAnalaysisHelper.php` has been removed completely, as no-one should have been referencing that directly.

Fixes #449.
@sirbrillig
Copy link
Member

sirbrillig commented Nov 1, 2019

Let me know if there's anything I can do to help!

FWIW, VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration was part of the forked package, but I don't know if it actually works; I have no tests for it and I've never seen it show up in practice. I've informally considered it deprecated and I don't list it in the documentation.

@GaryJones
Copy link
Contributor Author

I think variable redeclaration might come up for something like list( $foo, $foo ) = [ 'a', 'b' ];.

@mikeyarce
Copy link
Member

Came across another false positive:

$test[] = [
  'post_id' => 1,
  'isbn'    => 1,
  'status'  => 1,
  'success' => 1 ? 'true' : 'false',
];

Warning: Variable $test is undefined (WordPressVIPMinimum.Variables.VariableAnalysis.UndefinedVariable).

@sirbrillig
Copy link
Member

I know you're not using my sniff yet, but FWIW that's a tricky one, since technically $test is undefined in that case. Even though PHP allows you to create variables when they're being used, but it's hard to know in a sniff if those shortcuts are intentional or accidental. See the conversation here: sirbrillig/phpcs-variable-analysis#98 (comment)

GaryJones added a commit that referenced this issue Apr 17, 2020
Introduces a new dependency of `sirbrillig/phpcs-variable-analysis` which is a more maintained version of the existing `VariableAnalysis` sniff in VIPCS.

The new sniff is added to the `WordPressVIPMinimum` ruleset. `WordPress-VIP-Go` ruleset inherits this, and maintains its dropping of the severity for `UnusedVariable` and `UndefinedVariable` violations.

The ruleset tests have set the previously undefined variables (or changed some variable names), so that violations on each line are still specific to the thing being tested, and doesn't include extra violations due to the use of throwaway variables.

One of the reasons for changing was the previously false positive with respect to the use of `$this` inside closures inside class methods. A ruleset test has been added to cover this, and it correctly does not get flagged in the ruleset test.

The old `VariableAnalysis` has been marked as deprecated, until it can be removed in the next major release of VIPCS. This will stop any breakage of setups which reference `WordPressVIPMinimum.Variables.VariableAnalysis` in a custom ruleset. If called, it throws a warning, and then passes control to the new VariableAnalysis sniff. The deprecation notice is excluded if the sniff is not explicitly included, and the duplicate violation messages are also silenced.

The previously used `VariableAnalaysisHelper.php` has been removed completely, as no-one should have been referencing that directly.

Fixes #449.
GaryJones added a commit that referenced this issue Jul 11, 2020
Introduces a new dependency of `sirbrillig/phpcs-variable-analysis` which is a more maintained version of the existing `VariableAnalysis` sniff in VIPCS.

The new sniff is added to the `WordPressVIPMinimum` ruleset. `WordPress-VIP-Go` ruleset inherits this, and maintains its dropping of the severity for `UnusedVariable` and `UndefinedVariable` violations.

The ruleset tests have set the previously undefined variables (or changed some variable names), so that violations on each line are still specific to the thing being tested, and doesn't include extra violations due to the use of throwaway variables.

One of the reasons for changing was the previously false positive with respect to the use of `$this` inside closures inside class methods. A ruleset test has been added to cover this, and it correctly does not get flagged in the ruleset test.

The old `VariableAnalysis` has been marked as deprecated, until it can be removed in the next major release of VIPCS. This will stop any breakage of setups which reference `WordPressVIPMinimum.Variables.VariableAnalysis` in a custom ruleset. If called, it throws a warning, and then passes control to the new VariableAnalysis sniff. The deprecation notice is excluded if the sniff is not explicitly included, and the duplicate violation messages are also silenced.

The previously used `VariableAnalaysisHelper.php` has been removed completely, as no-one should have been referencing that directly.

Fixes #449.
GaryJones added a commit that referenced this issue Jul 11, 2020
Introduces a new dependency of `sirbrillig/phpcs-variable-analysis` which is a more maintained version of the existing `VariableAnalysis` sniff in VIPCS.

The new sniff is added to the `WordPressVIPMinimum` ruleset. `WordPress-VIP-Go` ruleset inherits this, and maintains its dropping of the severity for `UnusedVariable` and `UndefinedVariable` violations.

The ruleset tests have set the previously undefined variables (or changed some variable names), so that violations on each line are still specific to the thing being tested, and doesn't include extra violations due to the use of throwaway variables.

One of the reasons for changing was the previously false positive with respect to the use of `$this` inside closures inside class methods. A ruleset test has been added to cover this, and it correctly does not get flagged in the ruleset test.

The old `VariableAnalysis` has been marked as deprecated, until it can be removed in the next major release of VIPCS. This will stop any breakage of setups which reference `WordPressVIPMinimum.Variables.VariableAnalysis` in a custom ruleset. If called, it throws a warning, and then passes control to the new VariableAnalysis sniff. The deprecation notice is excluded if the sniff is not explicitly included, and the duplicate violation messages are also silenced.

The previously used `VariableAnalaysisHelper.php` has been removed completely, as no-one should have been referencing that directly.

Fixes #449.
GaryJones added a commit that referenced this issue Jul 11, 2020
Introduces a new dependency of `sirbrillig/phpcs-variable-analysis` which is a more maintained version of the existing `VariableAnalysis` sniff in VIPCS.

The new sniff is added to the `WordPressVIPMinimum` ruleset. `WordPress-VIP-Go` ruleset inherits this, and maintains its dropping of the severity for `UnusedVariable` and `UndefinedVariable` violations.

The ruleset tests have set the previously undefined variables (or changed some variable names), so that violations on each line are still specific to the thing being tested, and doesn't include extra violations due to the use of throwaway variables.

One of the reasons for changing was the previously false positive with respect to the use of `$this` inside closures inside class methods. A ruleset test has been added to cover this, and it correctly does not get flagged in the ruleset test.

The old `VariableAnalysis` has been marked as deprecated, until it can be removed in the next major release of VIPCS. This will stop any breakage of setups which reference `WordPressVIPMinimum.Variables.VariableAnalysis` in a custom ruleset. If called, it throws a warning, and then passes control to the new VariableAnalysis sniff. The deprecation notice is excluded if the sniff is not explicitly included, and the duplicate violation messages are also silenced.

The previously used `VariableAnalaysisHelper.php` has been removed completely, as no-one should have been referencing that directly.

Fixes #449.
GaryJones added a commit that referenced this issue Jul 13, 2020
Introduces a new dependency of `sirbrillig/phpcs-variable-analysis` which is a more maintained version of the existing `VariableAnalysis` sniff in VIPCS.

Requires ^2.8.3, since 2.6.3 – 2.8.2 had PHP 5.6 as the minimum version, but 2.8.3 changes it down to PHP 5.4 as the minimum.

The new sniff is added to the `WordPressVIPMinimum` ruleset. `WordPress-VIP-Go` ruleset inherits this, and maintains its dropping of the severity for `UnusedVariable` and `UndefinedVariable` violations.

The ruleset tests have set the previously undefined variables (or changed some variable names), so that violations on each line are still specific to the thing being tested, and doesn't include extra violations due to the use of throwaway variables.

One of the reasons for changing was the previously false positive with respect to the use of `$this` inside closures inside class methods. A ruleset test has been added to cover this, and it correctly does not get flagged in the ruleset test.

The old `VariableAnalysis` has been marked as deprecated, until it can be removed in the next major release of VIPCS. This will stop any breakage of setups which reference `WordPressVIPMinimum.Variables.VariableAnalysis` in a custom ruleset. If called, it throws a warning, and then passes control to the new VariableAnalysis sniff. The deprecation notice is excluded if the sniff is not explicitly included, and the duplicate violation messages are also silenced.

The previously used `VariableAnalaysisHelper.php` has been removed completely, as no-one should have been referencing that directly.

Fixes #449.
GaryJones added a commit that referenced this issue Jul 13, 2020
Introduces a new dependency of `sirbrillig/phpcs-variable-analysis` which is a more maintained version of the existing `VariableAnalysis` sniff in VIPCS.

Requires ^2.8.3, since 2.6.3 – 2.8.2 had PHP 5.6 as the minimum version, but 2.8.3 changes it down to PHP 5.4 as the minimum.

The new sniff is added to the `WordPressVIPMinimum` ruleset. `WordPress-VIP-Go` ruleset inherits this, and maintains its dropping of the severity for `UnusedVariable` and `UndefinedVariable` violations.

The ruleset tests have set the previously undefined variables (or changed some variable names), so that violations on each line are still specific to the thing being tested, and doesn't include extra violations due to the use of throwaway variables.

One of the reasons for changing was the previously false positive with respect to the use of `$this` inside closures inside class methods. A ruleset test has been added to cover this, and it correctly does not get flagged in the ruleset test.

The old `VariableAnalysis` has been marked as deprecated, until it can be removed in the next major release of VIPCS. This will stop any breakage of setups which reference `WordPressVIPMinimum.Variables.VariableAnalysis` in a custom ruleset. If called, it throws a warning, and then passes control to the new VariableAnalysis sniff. The deprecation notice is excluded if the sniff is not explicitly included, and the duplicate violation messages are also silenced.

The previously used `VariableAnalaysisHelper.php` has been removed completely, as no-one should have been referencing that directly.

Fixes #449.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants