Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6e973e6
Upgrade PHPCS requirement to 3.5
sirbrillig May 16, 2020
c62ea4a
Upgrade PHPCS requirement in README to 3.5
sirbrillig May 16, 2020
f9a2832
Add basic tests for arrow functions
sirbrillig May 16, 2020
895b177
Add tests for variable argument lists
sirbrillig Jul 6, 2020
7ec29a4
Make debug function variadic and work for objects
sirbrillig May 16, 2020
2f38fa7
Add ScopeType constants
sirbrillig May 16, 2020
6d401f1
Remove unused variables in checkForSuperGlobal
sirbrillig May 16, 2020
1dc004c
Add description to processVariable
sirbrillig Jul 5, 2020
4382eb8
Refactor scope detection to handle arrow functions
sirbrillig May 17, 2020
f4e7a67
Allow ellipses in argument lists
sirbrillig Jul 6, 2020
b7d3566
Only use T_FN if it exists
sirbrillig Jul 6, 2020
bf5a3ad
Don't assume token still exists when looking for closer
sirbrillig Jul 6, 2020
77a01e2
Remove null coalesce
sirbrillig Jul 6, 2020
4d3786e
Remove unused functions
sirbrillig Jul 6, 2020
401b59e
Move isVariableANumericVariable to Helpers
sirbrillig Jul 6, 2020
7e782f3
Use isArrowFunction rather than T_FN in getContainingArrowFunctionIndex
sirbrillig Jul 7, 2020
0bd2b14
Use isArrowFunction in getFunctionIndexForFunctionArgument for T_FN
sirbrillig Jul 7, 2020
f8ef8ed
Replace T_FN with isArrowFunction in getStartOfTokenScope
sirbrillig Jul 7, 2020
39ddb52
Replace T_FN in process
sirbrillig Jul 7, 2020
2a59db1
Stop using scope_condition for scope conditions in process
sirbrillig Jul 7, 2020
e9fce57
Stop using scope_condition in findVariableScopeExceptArrowFunctions
sirbrillig Jul 7, 2020
79dec4c
Replace scope_opener/closer with getArrowFunctionOpenClose
sirbrillig Jul 7, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Plugin for PHP_CodeSniffer static analysis tool that adds analysis of problemati

### Requirements

VariableAnalysis requires PHP 5.6 or higher and [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) version 3.1.0 or higher.
VariableAnalysis requires PHP 5.6 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).

Expand Down
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);
}
}
48 changes: 48 additions & 0 deletions Tests/VariableAnalysisSniff/VariableArgumentListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace VariableAnalysis\Tests\VariableAnalysisSniff;

use VariableAnalysis\Tests\BaseTestCase;

class VariableArgumentListTest extends BaseTestCase {
public function testVariableArgumentList() {
$fixtureFile = $this->getFixture('VariableArgumentListFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'true'
);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
6,
15,
23,
33,
38,
];
$this->assertEquals($expectedWarnings, $lines);
}

public function testVariableArgumentListWithoutUnusedBeforeUsed() {
$fixtureFile = $this->getFixture('VariableArgumentListFixture.php');
$phpcsFile = $this->prepareLocalFileForSniffs($fixtureFile);
$phpcsFile->ruleset->setSniffProperty(
'VariableAnalysis\Sniffs\CodeAnalysis\VariableAnalysisSniff',
'allowUnusedParametersBeforeUsed',
'false'
);
$phpcsFile->process();
$lines = $this->getWarningLineNumbersFromFile($phpcsFile);
$expectedWarnings = [
6,
15,
19,
23,
33,
38,
43,
];
$this->assertEquals($expectedWarnings, $lines);
}
}
73 changes: 73 additions & 0 deletions Tests/VariableAnalysisSniff/fixtures/ArrowFunctionFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

function arrowFunctionAsVariableWithNoWarnings($subject) {
$arrowFunc = fn($foo) => $foo . $subject;
echo $arrowFunc('hello');
}

function arrowFunctionAsVariableWithUndefinedInside($subject) {
$arrowFunc = fn($foo) => $foo . $bar . $subject; // undefined variable $bar
echo $arrowFunc('hello');
}

function arrowFunctionAsVariableWithUndefinedInClosure() {
$arrowFunc = fn($foo) => $foo . $subject; // undefined variable $subject
echo $arrowFunc('hello');
}

function arrowFunctionAsVariableWithUnusedInside($subject) {
$arrowFunc = fn($foo) => $subject; // unused variable $foo
echo $arrowFunc('hello');
}

function unusedArrowFunctionVariable($subject) {
$arrowFunc = fn($foo) => $foo . $subject; // unused variable $arrowFunc
}

function arrowFunctionAsVariableUsingOutsideArrow($subject) {
$arrowFunc = fn($foo) => $foo . $subject;
echo $arrowFunc('hello');
echo $foo; // undefined variable $foo
}

function arrowFunctionAsVariableWithUnusedInsideAfterUsed($subject) {
$arrowFunc = fn($foo, $bar) => $foo . $subject; // unused variable $bar
echo $arrowFunc('hello');
}

function arrowFunctionAsVariableWithUsedInsideAfterUnused($subject) {
$arrowFunc = fn($foo, $bar) => $bar . $subject; // unused variable $foo (but before used)
echo $arrowFunc('hello');
}

function arrowFunctionAsExpressionWithNoWarnings() {
$posts = [];
$ids = array_map(fn($post) => $post->id, $posts);
echo $ids;
}

function arrowFunctionAsExpressionWithUndefinedVariableInside() {
$posts = [];
$ids = array_map(fn($post) => $post->id . $foo, $posts); // undefined variable $foo
echo $ids;
}

function arrowFunctionAsExpressionWithUnusedVariableInside($subject) {
$posts = [];
$ids = array_map(fn($post) => $subject, $posts); // unused variable $post
echo $ids;
}

function arrowFunctionAsExpressionWithUsedAfterUnused($subject) { // unused variable $subject
$posts = [];
$ids = array_map(fn($foo, $post) => $post->id, $posts); // unused variable $foo (but before used)
echo $ids;
}

function arrowFunctionAsExpressionWithUnusedVariableOutsideArrow($subject) { //unused variable $subject
$posts = [];
$ids = array_map(fn($post) => $post->id, $posts);
echo $ids;
echo $post; // undefined variable $post;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
function functionWithVariableArgumentsAlone(...$rest) {
echo $rest[0];
}

function functionWithVariableArgumentsAloneUnused(...$rest) { // unused variable $rest
echo "Hello";
}

function functionWithVariableArgumentsAfterOther($first, ...$rest) {
echo $first;
echo $rest[0];
}

function functionWithVariableArgumentsUnusedAfterOther($first, ...$rest) { // unused variable $rest
echo $first;
}

function functionWithVariableArgumentsAfterOtherUnused($first, ...$rest) { // unused variable $first (but before used)
echo $rest[0];
}

function functionWithVariableArgumentsUnusedAfterOtherUnused($first, ...$rest) { // unused variable $rest and unused variable $first
echo $first;
}

function functionWithArrowVariableArgumentsAlone($subject) {
$arrowFunc = fn(...$foo) => $foo[0] . $subject;
echo $arrowFunc('hello');
}

function functionWithArrowVariableArgumentsAloneUnused($subject) {
$arrowFunc = fn(...$foo) => $subject; // unused variable $foo
echo $arrowFunc('hello');
}

function functionWithArrowVariableArgumentsUnusedAfterOther() {
$arrowFunc = fn($first, ...$foo) => $first; // unused variable $foo
echo $arrowFunc('hello');
}

function functionWithArrowVariableArgumentsAfterOtherUnused() {
$arrowFunc = fn($first, ...$foo) => $foo[0]; // unused variable $first (but before used)
echo $arrowFunc('hello');
}
Loading