Skip to content

Commit 4f36541

Browse files
authored
Add debug output (#78)
* Add Helpers::debug function * Add debug logging for all main parse decisions
1 parent f72e025 commit 4f36541

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

VariableAnalysis/Lib/Helpers.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,13 @@ public static function getStackPtrIfVariableIsUnused(VariableInfo $varInfo) {
222222
}
223223
return null;
224224
}
225+
226+
public static function debug($message) {
227+
if (! defined('PHP_CODESNIFFER_VERBOSITY')) {
228+
return;
229+
}
230+
if (PHP_CODESNIFFER_VERBOSITY > 3) {
231+
echo PHP_EOL . "VariableAnalysisSniff: DEBUG: $message" . PHP_EOL;
232+
}
233+
}
225234
}

VariableAnalysis/Sniffs/CodeAnalysis/VariableAnalysisSniff.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,10 @@ protected function processVariable(File $phpcsFile, $stackPtr) {
750750
$token = $tokens[$stackPtr];
751751

752752
$varName = Helpers::normalizeVarName($token['content']);
753+
Helpers::debug('examining token ' . $varName);
753754
$currScope = Helpers::findVariableScope($phpcsFile, $stackPtr);
754755
if ($currScope === false) {
756+
Helpers::debug('no scope found');
755757
return;
756758
}
757759

@@ -777,80 +779,96 @@ protected function processVariable(File $phpcsFile, $stackPtr) {
777779

778780
// Are we a $object->$property type symbolic reference?
779781
if ($this->checkForSymbolicObjectProperty($phpcsFile, $stackPtr, $varName, $currScope)) {
782+
Helpers::debug('found symbolic object property');
780783
return;
781784
}
782785

783786
// Are we a function or closure parameter?
784787
if ($this->checkForFunctionPrototype($phpcsFile, $stackPtr, $varName, $currScope)) {
788+
Helpers::debug('found function prototype');
785789
return;
786790
}
787791

788792
// Are we a catch parameter?
789793
if ($this->checkForCatchBlock($phpcsFile, $stackPtr, $varName, $currScope)) {
794+
Helpers::debug('found catch block');
790795
return;
791796
}
792797

793798
// Are we $this within a class?
794799
if ($this->checkForThisWithinClass($phpcsFile, $stackPtr, $varName, $currScope)) {
800+
Helpers::debug('found this usage within a class');
795801
return;
796802
}
797803

798804
// Are we a $GLOBALS, $_REQUEST, etc superglobal?
799805
if ($this->checkForSuperGlobal($phpcsFile, $stackPtr, $varName, $currScope)) {
806+
Helpers::debug('found superglobal');
800807
return;
801808
}
802809

803810
// Check for static members used outside a class
804811
if ($this->checkForStaticOutsideClass($phpcsFile, $stackPtr, $varName, $currScope)) {
812+
Helpers::debug('found static usage outside of class');
805813
return;
806814
}
807815

808816
// $var part of class::$var static member
809817
if ($this->checkForStaticMember($phpcsFile, $stackPtr, $varName, $currScope)) {
818+
Helpers::debug('found static member');
810819
return;
811820
}
812821

813822
// Is the next non-whitespace an assignment?
814823
if ($this->checkForAssignment($phpcsFile, $stackPtr, $varName, $currScope)) {
824+
Helpers::debug('found assignment');
815825
return;
816826
}
817827

818828
// OK, are we within a list (...) = construct?
819829
if ($this->checkForListAssignment($phpcsFile, $stackPtr, $varName, $currScope)) {
830+
Helpers::debug('found list assignment');
820831
return;
821832
}
822833

823834
// OK, are we within a [...] = construct?
824835
if ($this->checkForListShorthandAssignment($phpcsFile, $stackPtr, $varName, $currScope)) {
836+
Helpers::debug('found list shorthand assignment');
825837
return;
826838
}
827839

828840
// Are we a global declaration?
829841
if ($this->checkForGlobalDeclaration($phpcsFile, $stackPtr, $varName, $currScope)) {
842+
Helpers::debug('found global declaration');
830843
return;
831844
}
832845

833846
// Are we a static declaration?
834847
if ($this->checkForStaticDeclaration($phpcsFile, $stackPtr, $varName, $currScope)) {
848+
Helpers::debug('found static declaration');
835849
return;
836850
}
837851

838852
// Are we a foreach loopvar?
839853
if ($this->checkForForeachLoopVar($phpcsFile, $stackPtr, $varName, $currScope)) {
854+
Helpers::debug('found foreach loop variable');
840855
return;
841856
}
842857

843858
// Are we pass-by-reference to known pass-by-reference function?
844859
if ($this->checkForPassByReferenceFunctionCall($phpcsFile, $stackPtr, $varName, $currScope)) {
860+
Helpers::debug('found pass by reference');
845861
return;
846862
}
847863

848864
// Are we a numeric variable used for constructs like preg_replace?
849865
if ($this->checkForNumericVariable($varName)) {
866+
Helpers::debug('found numeric variable');
850867
return;
851868
}
852869

853870
// OK, we don't appear to be a write to the var, assume we're a read.
871+
Helpers::debug('looks like a variable read');
854872
$this->markVariableReadAndWarnIfUndefined($phpcsFile, $varName, $stackPtr, $currScope);
855873
}
856874

0 commit comments

Comments
 (0)