Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sirbrillig/phpcs-variable-analysis
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v2.0.6
Choose a base ref
...
head repository: sirbrillig/phpcs-variable-analysis
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Loading
Showing with 5,275 additions and 973 deletions.
  1. +59 −0 .circleci/config.yml
  2. +29 −0 .gitattributes
  3. +5 −0 .gitignore
  4. +66 −15 README.md
  5. +17 −15 {VariableAnalysis → }/Tests/BaseTestCase.php
  6. +33 −0 Tests/VariableAnalysisSniff/ArrayAssignmentShortcutTest.php
  7. +60 −0 Tests/VariableAnalysisSniff/ArrowFunctionTest.php
  8. +41 −0 Tests/VariableAnalysisSniff/GlobalScopeTest.php
  9. +66 −0 Tests/VariableAnalysisSniff/IfConditionTest.php
  10. +18 −0 Tests/VariableAnalysisSniff/IssetTest.php
  11. +29 −0 Tests/VariableAnalysisSniff/UnsetTest.php
  12. +74 −0 Tests/VariableAnalysisSniff/UnusedFollowedByRequireTest.php
  13. +1,038 −0 Tests/VariableAnalysisSniff/VariableAnalysisTest.php
  14. +48 −0 Tests/VariableAnalysisSniff/VariableArgumentListTest.php
  15. +18 −0 Tests/VariableAnalysisSniff/WhileLoopTest.php
  16. +15 −0 Tests/VariableAnalysisSniff/fixtures/AnonymousClassFixture.php
  17. +43 −0 Tests/VariableAnalysisSniff/fixtures/AnonymousClassWithPropertiesFixture.php
  18. +30 −0 Tests/VariableAnalysisSniff/fixtures/ArrayAssignmentShortcutFixture.php
  19. +73 −0 Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php
  20. +56 −0 Tests/VariableAnalysisSniff/fixtures/AssignmentByReferenceFixture.php
  21. +5 −1 ...ableAnalysis/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/ClassReferenceFixture.php
  22. +110 −0 Tests/VariableAnalysisSniff/fixtures/ClassWithMembersFixture.php
  23. +3 −3 {VariableAnalysis/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/CompactFixture.php
  24. +50 −0 Tests/VariableAnalysisSniff/fixtures/DestructuringFixture.php
  25. +42 −0 ...nalysis/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/FunctionWithClosureFixture.php
  26. +4 −0 ...is/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/FunctionWithDefaultParamFixture.php
  27. +17 −1 ...nalysis/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/FunctionWithForeachFixture.php
  28. +73 −0 Tests/VariableAnalysisSniff/fixtures/FunctionWithGlobalVarFixture.php
  29. +169 −0 Tests/VariableAnalysisSniff/fixtures/FunctionWithIfConditionFixture.php
  30. +1 −1 ...is/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/FunctionWithInlineAssignFixture.php
  31. +138 −0 Tests/VariableAnalysisSniff/fixtures/FunctionWithInlineIfConditionFixture.php
  32. 0 .../Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/FunctionWithRedeclarationsFixture.php
  33. +24 −4 ...lysis/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/FunctionWithReferenceFixture.php
  34. +1 −1 ...alysis/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/FunctionWithTryCatchFixture.php
  35. +93 −0 Tests/VariableAnalysisSniff/fixtures/FunctionWithUnusedParamsFixture.php
  36. +8 −0 Tests/VariableAnalysisSniff/fixtures/FunctionWithUseReferenceFixture.php
  37. +11 −1 ...is/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/FunctionWithVariableCallFixture.php
  38. +74 −0 Tests/VariableAnalysisSniff/fixtures/FunctionWithWhileLoopFixture.php
  39. 0 ...alysis/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/FunctionWithoutParamFixture.php
  40. +4 −0 Tests/VariableAnalysisSniff/fixtures/FunctionsOutsideClassFixture.php
  41. +31 −0 Tests/VariableAnalysisSniff/fixtures/GetDefinedVarsFixture.php
  42. +11 −0 Tests/VariableAnalysisSniff/fixtures/GlobalScopeFixture.php
  43. 0 {VariableAnalysis/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/HeredocFixture.php
  44. +37 −0 Tests/VariableAnalysisSniff/fixtures/IssetFixture.php
  45. +21 −0 Tests/VariableAnalysisSniff/fixtures/PregReplaceFixture.php
  46. +67 −0 Tests/VariableAnalysisSniff/fixtures/ThisWithinNestedClosedScopeFixture.php
  47. 0 {VariableAnalysis/Tests/CodeAnalysis → Tests/VariableAnalysisSniff}/fixtures/TraitFixture.php
  48. +8 −4 ...es/ClassWithMembersFixture.php → Tests/VariableAnalysisSniff/fixtures/TraitWithMembersFixture.php
  49. +11 −0 Tests/VariableAnalysisSniff/fixtures/TraitWithPropertiesFixture.php
  50. +12 −0 Tests/VariableAnalysisSniff/fixtures/UnsetFixture.php
  51. +39 −0 Tests/VariableAnalysisSniff/fixtures/UnusedAfterUsedFixture.php
  52. +24 −0 Tests/VariableAnalysisSniff/fixtures/UnusedFollowedByRequireFixture.php
  53. +36 −0 Tests/VariableAnalysisSniff/fixtures/UnusedForeachFixture.php
  54. +22 −0 Tests/VariableAnalysisSniff/fixtures/UnusedVarWithValueChangeFixture.php
  55. +45 −0 Tests/VariableAnalysisSniff/fixtures/VariableArgumentListFixture.php
  56. +56 −0 Tests/VariableAnalysisSniff/fixtures/VariableVariablesFixture.php
  57. +3 −0 Tests/bootstrap.php
  58. +21 −12 VariableAnalysis/Lib/Constants.php
  59. +821 −92 VariableAnalysis/Lib/Helpers.php
  60. +22 −6 VariableAnalysis/Lib/ScopeInfo.php
  61. +11 −0 VariableAnalysis/Lib/ScopeType.php
  62. +71 −6 VariableAnalysis/Lib/VariableInfo.php
  63. +1,135 −220 VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php
  64. +0 −400 VariableAnalysis/Tests/CodeAnalysis/VariableAnalysisTest.php
  65. +0 −55 VariableAnalysis/Tests/CodeAnalysis/composer.lock
  66. +0 −23 VariableAnalysis/Tests/CodeAnalysis/fixtures/FunctionWithGlobalVarFixture.php
  67. +0 −13 VariableAnalysis/Tests/CodeAnalysis/fixtures/FunctionsOutsideClassFixture.php
  68. +0 −3 VariableAnalysis/Tests/bootstrap.php
  69. +4 −11 VariableAnalysis/ruleset.xml
  70. +5 −58 VariableAnalysis/ruleset.xml.example
  71. +0 −6 circle.yml
  72. +47 −0 composer.circleci.json
  73. +19 −6 composer.json
  74. +21 −6 phpcs.xml → phpcs.xml.dist
  75. +11 −0 phpstan.neon.dist
  76. +9 −0 phpunit.circleci.xml
  77. +0 −10 phpunit.xml
  78. +10 −0 phpunit.xml.dist
59 changes: 59 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
version: 2
jobs:
build_php7.4:
docker:
- image: circleci/php:7.4.6
steps:
- checkout
- run: COMPOSER=composer.json composer install
- run: COMPOSER=composer.json composer test
- run: COMPOSER=composer.json composer phpstan
- run: COMPOSER=composer.json composer lint
build_php7.3:
docker:
- image: circleci/php:7.3.1
steps:
- checkout
- run: COMPOSER=composer.json composer install
- run: COMPOSER=composer.json composer test
- run: COMPOSER=composer.json composer phpstan
- run: COMPOSER=composer.json composer lint
build_php5.6:
docker:
- image: circleci/php:5.6.40-zts-stretch-node-browsers-legacy
steps:
- checkout
- run: COMPOSER=composer.circleci.json composer install
- run: COMPOSER=composer.circleci.json composer test
build_php5.4:
docker:
- image: lavoweb/php-5.4
steps:
- run:
name: PHP version check
command: php --version
- run:
name: Install system packages
command: apt-get update && apt-get -y install git
- checkout
- run:
name: Install composer
command: |
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === trim(file_get_contents('https://composer.github.io/installer.sig'))) { echo 'Installer verified'; } else { echo 'Installer invalid'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"
- run:
name: Install dependencies
command: COMPOSER=composer.circleci.json php composer.phar install
- run:
name: Run tests
command: COMPOSER=composer.circleci.json php composer.phar test
workflows:
version: 2
build_php_versions:
jobs:
- build_php5.4
- build_php5.6
- build_php7.3
- build_php7.4
29 changes: 29 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#
# Exclude these files from release archives.
# This will also make them unavailable when using Composer with `--prefer-dist`.
# If you develop for this repo using Composer, use `--prefer-source`.
# https://www.reddit.com/r/PHP/comments/2jzp6k/i_dont_need_your_tests_in_my_production
# https://blog.madewithlove.be/post/gitattributes/
#
/.gitattributes export-ignore
/.gitignore export-ignore
/composer.circleci.json export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/phpunit.circleci.xml export-ignore
/.circleci/ export-ignore
/Tests/ export-ignore

#
# Auto detect text files and perform LF normalization
# http://davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/
#
* text=auto

#
# The above will handle all files NOT found below
#
*.md text
*.php text
*.inc text
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/composer.lock
/vendor/
phpunit.xml
.phpunit.result.cache
.phpcs.xml
phpcs.xml
tags
81 changes: 66 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -4,17 +4,23 @@

Plugin for PHP_CodeSniffer static analysis tool that adds analysis of problematic variable use.

* Performs static analysis of variable use.
* Warns on use of undefined variables.
* Warns if variables are set or declared but never used within that scope.
* Warns if variables are redeclared within same scope.
* Warns if $this, self::$static_member, static::$static_member is used outside class scope.
**Please note that this README is for VariableAnalysis v3. For documentation about v2, [see this page](https://github.com/sirbrillig/phpcs-variable-analysis/blob/2.x-legacy/README.md).**

- Warns if variables are used without being defined. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable`)
- Warns if variables are used for an array push shortcut without being defined. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedArrayVariable`)
- Warns if variables are used inside `unset()` without being defined. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedUnsetVariable`)
- Warns if variables are set or declared but never used. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable`)
- Warns if function parameters are declared but never used. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedParameter`)
- Warns if function parameters are declared but never used before other parameters that are used. (Sniff code: `VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedParameterBeforeUsed`)
- Warns if `$this`, `self::$static_member`, `static::$static_member` is used outside class scope. (Sniff codes: `VariableAnalysis.CodeAnalysis.VariableAnalysis.SelfOutsideClass` or `VariableAnalysis.CodeAnalysis.VariableAnalysis.StaticOutsideClass`)

## Installation

### Requirements

VariableAnalysis requires PHP 5.4 or higher and [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) version **3.0.2** or higher.
VariableAnalysis requires PHP 5.4 or higher and [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) version 3.5.0 or higher.

It also requires [PHPCSUtils](https://phpcsutils.com/) which must be installed as a PHPCS standard. If you are using composer, this will be done automatically (see below).

### With PHPCS Composer Installer

@@ -42,24 +48,55 @@ It should just work after that!

### Standalone

1. Install PHP_CodeSniffer by following its [installation instructions](https://github.com/squizlabs/PHP_CodeSniffer#installation) (via Composer, Phar file, PEAR, or Git checkout).
1. Install PHP_CodeSniffer (PHPCS) by following its [installation instructions](https://github.com/squizlabs/PHP_CodeSniffer#installation) (via Composer, Phar file, PEAR, or Git checkout).

Do ensure that PHP_CodeSniffer's version matches our [requirements](#requirements)
Do ensure that PHP_CodeSniffer's version matches our [requirements](#requirements).

2. Clone the repository:
2. Install PHPCSUtils (required by this sniff). Download either the zip or tar.gz file from [the PHPCSUtils latest release page](https://github.com/PHPCSStandards/PHPCSUtils/releases/latest). Expand the file and rename the resulting directory to `phpcsutils`. Move the directory to a place where you'd like to keep all your PHPCS standards.

git clone -b master https://github.com/sirbrillig/VariableAnalysis.git VariableAnalysis
3. Install VariableAnalysis. Download either the zip or tar.gz file from [the VariableAnalysis latest release page](https://github.com/sirbrillig/phpcs-variable-analysis/releases/latest). Expand the file and rename the resulting directory to `phpcs-variable-analysis`. Move the directory to a place where you'd like to keep all your PHPCS standards.

3. Add its path to the PHP_CodeSniffer configuration:
4. Add the paths of the newly installed standards to the [PHP_CodeSniffer installed_paths configuration](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-installed-standard-paths). The following command should append the new standards to your existing standards (be sure to supply the actual paths to the directories you created above).

phpcs --config-set installed_paths /path/to/VariableAnalysis
phpcs --config-set installed_paths "$(phpcs --config-show|grep installed_paths|awk '{ print $2 }'),/path/to/phpcsutils,/path/to/phpcs-variable-analysis"

If you already have installed paths, [use a comma to separate them](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options#setting-the-installed-standard-paths).
If you do not have any other standards installed, you can do this more easily (again, be sure to supply the actual paths):

phpcs --config-set installed_paths /path/to/phpcsutils,/path/to/phpcs-variable-analysis

## Customization

There's a variety of options to customize the behaviour of VariableAnalysis, take
a look at the included ruleset.xml.example for commented examples of a configuration.
There's a variety of options to customize the behaviour of VariableAnalysis, take a look at the included ruleset.xml.example for commented examples of a configuration.

The available options are as follows:

- `allowUnusedFunctionParameters` (bool, default `false`): if set to true, function arguments will never be marked as unused.
- `allowUnusedCaughtExceptions` (bool, default `true`): if set to true, caught Exception variables will never be marked as unused.
- `allowUnusedParametersBeforeUsed` (bool, default `true`): if set to true, unused function arguments will be ignored if they are followed by used function arguments.
- `allowUnusedVariablesBeforeRequire` (bool, default `false`): if set to true, variables defined before a `require`, `require_once`, `include`, or `include_once` will not be marked as unused. They may be intended for the required file.
- `allowUndefinedVariablesInFileScope` (bool, default `false`): if set to true, undefined variables in the file's top-level scope will never be marked as undefined.
- `validUnusedVariableNames` (string, default `null`): a space-separated list of names of placeholder variables that you want to ignore from unused variable warnings. For example, to ignore the variables `$junk` and `$unused`, this could be set to `'junk unused'`.
- `ignoreUnusedRegexp` (string, default `null`): a PHP regexp string (note that this requires explicit delimiters) for variables that you want to ignore from unused variable warnings. For example, to ignore the variables `$_junk` and `$_unused`, this could be set to `'/^_/'`.
- `validUndefinedVariableNames` (string, default `null`): a space-separated list of names of placeholder variables that you want to ignore from undefined variable warnings. For example, to ignore the variables `$post` and `$undefined`, this could be set to `'post undefined'`. This can be used in combination with `validUndefinedVariableRegexp`.
- `validUndefinedVariableRegexp` (string, default `null`): a PHP regexp string (note that this requires explicit delimiters) for variables that you want to ignore from undefined variable warnings. For example, to ignore the variables `$post` and `$undefined`, this could be set to `'/^(post|undefined)$/'`. This can be used in combination with `validUndefinedVariableNames`.
- `allowUnusedForeachVariables` (bool, default `true`): if set to true, unused values from the `key => value` syntax in a `foreach` loop will never be marked as unused.
- `sitePassByRefFunctions` (string, default `null`): a list of custom functions which pass in variables to be initialized by reference (eg `preg_match()`) and therefore should not require those variables to be defined ahead of time. The list is space separated and each entry is of the form `functionName:1,2`. The function name comes first followed by a colon and a comma-separated list of argument numbers (starting from 1) which should be considered variable definitions. The special value `...` in the arguments list will cause all arguments after the last number to be considered variable definitions.
- `allowWordPressPassByRefFunctions` (bool, default `false`): if set to true, a list of common WordPress pass-by-reference functions will be added to the list of PHP ones so that passing undefined variables to these functions (to be initialized by reference) will be allowed.

To set these these options, you must use XML in your ruleset. For details, see the [phpcs customizable sniff properties page](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties). Here is an example that ignores all variables that start with an underscore:

```xml
<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis">
<properties>
<property name="ignoreUnusedRegexp" value="/^_/"/>
</properties>
</rule>
```

## See Also

- [ImportDetection](https://github.com/sirbrillig/phpcs-import-detection): A set of phpcs sniffs to look for unused or unimported symbols.
- [phpcs-changed](https://github.com/sirbrillig/phpcs-changed): Run phpcs on files, but only report warnings/errors from lines which were changed.

## Original

@@ -69,9 +106,23 @@ This was forked from the excellent work in https://github.com/illusori/PHP_Codes

Please open issues or PRs on this repository.

Any changes should be accompanied by tests and should pass linting and static analysis. Please use phpdoc (rather than actual types) for declaring types since this must run in PHP 5.4.

To run tests, make sure composer is installed, then run:

```
composer install # you only need to do this once
composer test
```

To run linting, use:

```
composer lint
```

To run static analysis, use:

```
composer phpstan
```
32 changes: 17 additions & 15 deletions VariableAnalysis/Tests/BaseTestCase.php → Tests/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -7,12 +7,18 @@
use PHP_CodeSniffer\Config;

class BaseTestCase extends TestCase {
public function prepareLocalFileForSniffs($sniffFiles, string $fixtureFile): LocalFile {
$config = new Config();
$ruleset = new Ruleset($config);
if (! is_array($sniffFiles)) {
$sniffFiles = [$sniffFiles];
}
const STANDARD_NAME = 'VariableAnalysis';

public function prepareLocalFileForSniffs($fixtureFile) {
$sniffFile = __DIR__ . '/../VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php';

$config = new Config();
$config->cache = false;
$config->standards = [self::STANDARD_NAME];
$config->ignored = [];

$sniffFiles = [realpath($sniffFile)];
$ruleset = new Ruleset($config);
$ruleset->registerSniffs($sniffFiles, [], []);
$ruleset->populateTokenListeners();
if (! file_exists($fixtureFile)) {
@@ -21,25 +27,21 @@ public function prepareLocalFileForSniffs($sniffFiles, string $fixtureFile): Loc
return new LocalFile($fixtureFile, $ruleset, $config);
}

public function getLineNumbersFromMessages(array $messages): array {
public function getLineNumbersFromMessages(array $messages) {
$lines = array_keys($messages);
sort($lines);
return $lines;
}

public function getWarningLineNumbersFromFile(LocalFile $phpcsFile): array {
public function getWarningLineNumbersFromFile(LocalFile $phpcsFile) {
return $this->getLineNumbersFromMessages($phpcsFile->getWarnings());
}

public function getErrorLineNumbersFromFile(LocalFile $phpcsFile): array {
public function getErrorLineNumbersFromFile(LocalFile $phpcsFile) {
return $this->getLineNumbersFromMessages($phpcsFile->getErrors());
}

public function getSniffFiles(): array {
return [__DIR__ . '/../../VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php'];
}

public function getFixture(string $fixtureFilename): string {
return __DIR__ . '/CodeAnalysis/fixtures/' . $fixtureFilename;
public function getFixture($fixtureFilename) {
return realpath(__DIR__ . '/VariableAnalysisSniff/fixtures/' . $fixtureFilename);
}
}
33 changes: 33 additions & 0 deletions Tests/VariableAnalysisSniff/ArrayAssignmentShortcutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace VariableAnalysis\Tests\VariableAnalysisSniff;

use VariableAnalysis\Tests\BaseTestCase;

class ArrayAssignmentShortcutTest extends BaseTestCase {
public function testArrayAssignmentReportsCorrectLines() {
$fixtureFile = $this->getFixture('ArrayAssignmentShortcutFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->process();

$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
21,
27,
28,
29,
];
$this->assertEquals($expectedWarnings, $lines);
}

public function testArrayAssignmentHasCorrectSniffCodes() {
$fixtureFile = $this->getFixture('ArrayAssignmentShortcutFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->process();

$warnings = $phpcsFile->getWarnings();
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedArrayVariable', $warnings[21][5][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedArrayVariable', $warnings[27][5][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable', $warnings[28][5][0]['source']);
$this->assertEquals('VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable', $warnings[29][10][0]['source']);
}
}
60 changes: 60 additions & 0 deletions Tests/VariableAnalysisSniff/ArrowFunctionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
namespace VariableAnalysis\Tests\VariableAnalysisSniff;

use VariableAnalysis\Tests\BaseTestCase;

class ArrowFunctionTest extends BaseTestCase {
public function testArrowFunctions() {
$fixtureFile = $this->getFixture('ArrowFunctionFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'true'
);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
9,
14,
19,
24,
30,
34,
51,
57,
61,
67,
71,
];
$this->assertEquals($expectedWarnings, $lines);
}

public function testArrowFunctionsWithoutUnusedBeforeUsed() {
$fixtureFile = $this->getFixture('ArrowFunctionFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'false'
);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
9,
14,
19,
24,
30,
34,
39,
51,
57,
61,
63,
67,
71,
];
$this->assertEquals($expectedWarnings, $lines);
}
}
Loading