Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 28 additions & 14 deletions Sniffs/CodeAnalysis/VariableAnalysisSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ class Generic_Sniffs_CodeAnalysis_VariableAnalysisSniff implements PHP_CodeSniff
'yaz_hits' => array(2),
'yaz_scan_result' => array(2),
'yaz_wait' => array(1),
);
);

/**
* Allows an install to extend the list of known pass-by-reference functions
Expand All @@ -356,6 +356,12 @@ class Generic_Sniffs_CodeAnalysis_VariableAnalysisSniff implements PHP_CodeSniff
*/
public $validUnusedVariableNames = null;

/**
* A list of names of variables that you want to ignore from
* undefined variable warnings
*/
public $validUndefinedVariableNames = null;

/**
* Returns an array of tokens this test wants to listen for.
*
Expand All @@ -373,6 +379,12 @@ public function register() {
$this->validUnusedVariableNames =
preg_split('/\s+/', trim($this->validUnusedVariableNames));
}

if (!empty($this->validUndefinedVariableNames)) {
$this->validUndefinedVariableNames =
preg_split('/\s+/', trim($this->validUndefinedVariableNames));
}

return array(
T_VARIABLE,
T_DOUBLE_QUOTED_STRING,
Expand Down Expand Up @@ -429,7 +441,7 @@ function scopeKey($currScope) {
$currScope = 'file';
}
return ($this->currentFile ? $this->currentFile->getFilename() : 'unknown file') .
':' . $currScope;
':' . $currScope;
}

// Warning: this is an autovivifying get
Expand Down Expand Up @@ -487,8 +499,8 @@ function markVariableDeclaration($varName, $scopeType, $typeHint, $stackPtr, $cu
VariableInfo::$scopeTypeDescriptions[$varInfo->scopeType],
"\${$varName}",
VariableInfo::$scopeTypeDescriptions[$scopeType],
)
);
)
);
}
}
$varInfo->scopeType = $scopeType;
Expand Down Expand Up @@ -534,9 +546,11 @@ function markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $cu

if ($this->isVariableUndefined($varName, $stackPtr, $currScope) === true) {
// We haven't been defined by this point.
$phpcsFile->addWarning("Variable %s is undefined.", $stackPtr,
'UndefinedVariable',
array("\${$varName}"));
if (!$this->validUndefinedVariableNames || !in_array($varName, $this->validUndefinedVariableNames)) {
$phpcsFile->addWarning("Variable %s is undefined.", $stackPtr,
'UndefinedVariable',
array("\${$varName}"));
}
}
return true;
}
Expand Down Expand Up @@ -746,7 +760,7 @@ protected function checkForFunctionPrototype(
$openPtr - 1, null, true, null, true);
if (($functionPtr !== false) &&
(($tokens[$functionPtr]['code'] === T_FUNCTION) ||
($tokens[$functionPtr]['code'] === T_CLOSURE))) {
($tokens[$functionPtr]['code'] === T_CLOSURE))) {
// TODO: typeHint
$this->markVariableDeclaration($varName, 'param', null, $stackPtr, $functionPtr);
// Are we pass-by-reference?
Expand Down Expand Up @@ -871,7 +885,7 @@ protected function checkForSuperGlobal(
'_ENV',
'argv',
'argc',
))) {
))) {
return true;
}

Expand Down Expand Up @@ -1047,7 +1061,7 @@ protected function checkForStaticDeclaration(
T_DOUBLE_COLON,
T_START_HEREDOC, T_HEREDOC, T_END_HEREDOC,
T_START_NOWDOC, T_NOWDOC, T_END_NOWDOC,
),
),
$stackPtr - 1, null, true, null, true);
if (($staticPtr === false) || ($tokens[$staticPtr]['code'] !== T_STATIC)) {
//if ($varName == 'static4') {
Expand Down Expand Up @@ -1477,8 +1491,8 @@ protected function processScopeClose(
array(
VariableInfo::$scopeTypeDescriptions[$varInfo->scopeType],
"\${$varInfo->name}",
)
);
)
);
}
if (isset($varInfo->firstInitialized)) {
$phpcsFile->addWarning(
Expand All @@ -1488,8 +1502,8 @@ protected function processScopeClose(
array(
VariableInfo::$scopeTypeDescriptions[$varInfo->scopeType],
"\${$varInfo->name}",
)
);
)
);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "ingatlancom/variable-analysis",
"description": "Provides PHP CodeSniffer sniffs to find unused and undefined variables.",
"require": {
"php": ">=5.3.0",
"squizlabs/php_codesniffer": ">=1.5.3"
},
"license": "BSD-2-Clause"
}
10 changes: 10 additions & 0 deletions ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
junk
dummy
"/>
<!--
Include the following property if you want to have a whitelist of variable names
that are defined in all php files and that you don't want to provoke an undefined
variable warning for.
Format is "variableName" without a leading $ with whitespace delimiting names.
-->
<property name="validUndefinedVariableNames" value="
junk
dummy
"/>
</properties>
</rule>

Expand Down