@@ -511,48 +511,83 @@ protected function checkForStaticMember(File $phpcsFile, $stackPtr, $varName, $c
511
511
$ tokens = $ phpcsFile ->getTokens ();
512
512
$ token = $ tokens [$ stackPtr ];
513
513
514
- // Are we a static member?
515
514
$ doubleColonPtr = $ stackPtr - 1 ;
516
515
if ($ tokens [$ doubleColonPtr ]['code ' ] !== T_DOUBLE_COLON ) {
517
516
return false ;
518
517
}
519
518
$ classNamePtr = $ stackPtr - 2 ;
520
- if (($ tokens [$ classNamePtr ]['code ' ] !== T_STRING )
521
- && ($ tokens [$ classNamePtr ]['code ' ] !== T_SELF )
522
- && ($ tokens [$ classNamePtr ]['code ' ] !== T_STATIC )) {
519
+ $ staticReferences = [
520
+ T_STRING ,
521
+ T_SELF ,
522
+ T_STATIC ,
523
+ ];
524
+ if (! in_array ($ tokens [$ classNamePtr ]['code ' ], $ staticReferences , true )) {
523
525
return false ;
524
526
}
527
+ return true ;
528
+ }
525
529
530
+ protected function checkForStaticOutsideClass (File $ phpcsFile , $ stackPtr , $ varName , $ currScope ) {
526
531
// Are we refering to self:: outside a class?
527
532
// TODO: not sure this is our business or should be some other sniff.
528
- if (($ tokens [$ classNamePtr ]['code ' ] === T_SELF ) || ($ tokens [$ classNamePtr ]['code ' ] === T_STATIC )) {
529
- if ($ tokens [$ classNamePtr ]['code ' ] === T_SELF ) {
530
- $ err_class = 'SelfOutsideClass ' ;
531
- $ err_desc = 'self:: ' ;
532
- } else {
533
- $ err_class = 'StaticOutsideClass ' ;
534
- $ err_desc = 'static:: ' ;
533
+
534
+ $ tokens = $ phpcsFile ->getTokens ();
535
+ $ token = $ tokens [$ stackPtr ];
536
+
537
+ $ doubleColonPtr = $ stackPtr - 1 ;
538
+ if ($ tokens [$ doubleColonPtr ]['code ' ] !== T_DOUBLE_COLON ) {
539
+ return false ;
540
+ }
541
+ $ classNamePtr = $ stackPtr - 2 ;
542
+ $ code = $ tokens [$ classNamePtr ]['code ' ];
543
+ $ staticReferences = [
544
+ T_SELF ,
545
+ T_STATIC ,
546
+ ];
547
+ if (! in_array ($ code , $ staticReferences , true )) {
548
+ return false ;
549
+ }
550
+ $ errorClass = $ code === T_SELF ? 'SelfOutsideClass ' : 'StaticOutsideClass ' ;
551
+ $ staticRefType = $ code === T_SELF ? 'self:: ' : 'static:: ' ;
552
+ if (!empty ($ token ['conditions ' ])) {
553
+ if ($ this ->areAnyConditionsAClosure ($ phpcsFile , $ token ['conditions ' ])) {
554
+ $ phpcsFile ->addError ("Use of {$ staticRefType }%s inside closure. " , $ stackPtr , $ errorClass , ["\${$ varName }" ]);
555
+ return true ;
535
556
}
536
- if (!empty ($ token ['conditions ' ])) {
537
- foreach (array_reverse ($ token ['conditions ' ], true ) as $ scopePtr => $ scopeCode ) {
538
- // self within a closure is invalid
539
- // Note: have to fetch code from $tokens, T_CLOSURE isn't set for conditions codes.
540
- if ($ tokens [$ scopePtr ]['code ' ] === T_CLOSURE ) {
541
- $ phpcsFile ->addError ("Use of {$ err_desc }%s inside closure. " , $ stackPtr , $ err_class , ["\${$ varName }" ]);
542
- return true ;
543
- }
544
- if ($ scopeCode === T_CLASS ) {
545
- return true ;
546
- }
547
- }
557
+ if ($ this ->areAnyConditionsAClass ($ token ['conditions ' ])) {
558
+ return true ;
548
559
}
549
- $ phpcsFile ->addError ("Use of {$ err_desc }%s outside class definition. " , $ stackPtr , $ err_class , ["\${$ varName }" ]);
550
- return true ;
551
560
}
552
-
561
+ $ phpcsFile ->addError (
562
+ "Use of {$ staticRefType }%s outside class definition. " ,
563
+ $ stackPtr ,
564
+ $ errorClass ,
565
+ ["\${$ varName }" ]
566
+ );
553
567
return true ;
554
568
}
555
569
570
+ protected function areAnyConditionsAClosure ($ phpcsFile , $ conditions ) {
571
+ // self within a closure is invalid
572
+ $ tokens = $ phpcsFile ->getTokens ();
573
+ foreach (array_reverse ($ conditions , true ) as $ scopePtr => $ scopeCode ) {
574
+ // Note: have to fetch code from $tokens, T_CLOSURE isn't set for conditions codes.
575
+ if ($ tokens [$ scopePtr ]['code ' ] === T_CLOSURE ) {
576
+ return true ;
577
+ }
578
+ }
579
+ return false ;
580
+ }
581
+
582
+ protected function areAnyConditionsAClass ($ conditions ) {
583
+ foreach (array_reverse ($ conditions , true ) as $ scopePtr => $ scopeCode ) {
584
+ if ($ scopeCode === T_CLASS ) {
585
+ return true ;
586
+ }
587
+ }
588
+ return false ;
589
+ }
590
+
556
591
protected function checkForAssignment (File $ phpcsFile , $ stackPtr , $ varName , $ currScope ) {
557
592
$ tokens = $ phpcsFile ->getTokens ();
558
593
$ token = $ tokens [$ stackPtr ];
@@ -773,18 +808,6 @@ protected function checkForSymbolicObjectProperty(File $phpcsFile, $stackPtr, $v
773
808
return true ;
774
809
}
775
810
776
- /**
777
- * Called to process class member vars.
778
- *
779
- * @param File $phpcsFile The PHP_CodeSniffer file where this token was found.
780
- * @param int $stackPtr The position where the token was found.
781
- */
782
- protected function processMemberVar (File $ phpcsFile , $ stackPtr ) {
783
- $ tokens = $ phpcsFile ->getTokens ();
784
- $ token = $ tokens [$ stackPtr ];
785
- // TODO: don't care for now
786
- }
787
-
788
811
/**
789
812
* Called to process normal member vars.
790
813
*
@@ -846,6 +869,11 @@ protected function processVariable(File $phpcsFile, $stackPtr) {
846
869
return ;
847
870
}
848
871
872
+ // Check for static members used outside a class
873
+ if ($ this ->checkForStaticOutsideClass ($ phpcsFile , $ stackPtr , $ varName , $ currScope )) {
874
+ return ;
875
+ }
876
+
849
877
// $var part of class::$var static member
850
878
if ($ this ->checkForStaticMember ($ phpcsFile , $ stackPtr , $ varName , $ currScope )) {
851
879
return ;
@@ -1015,27 +1043,27 @@ protected function processScopeCloseForVariable($phpcsFile, $varInfo) {
1015
1043
// of "unused variable" warnings.
1016
1044
return ;
1017
1045
}
1018
- if (isset ($ varInfo ->firstDeclared )) {
1046
+ $ stackPtr = $ this ->getStackPtrIfVariableIsUnused ($ varInfo );
1047
+ if ($ stackPtr ) {
1019
1048
$ phpcsFile ->addWarning (
1020
1049
"Unused %s %s. " ,
1021
- $ varInfo -> firstDeclared ,
1050
+ $ stackPtr ,
1022
1051
'UnusedVariable ' ,
1023
1052
[
1024
1053
VariableInfo::$ scopeTypeDescriptions [$ varInfo ->scopeType ],
1025
1054
"\${$ varInfo ->name }" ,
1026
1055
]
1027
1056
);
1028
1057
}
1058
+ }
1059
+
1060
+ protected function getStackPtrIfVariableIsUnused ($ varInfo ) {
1061
+ if (isset ($ varInfo ->firstDeclared )) {
1062
+ return $ varInfo ->firstDeclared ;
1063
+ }
1029
1064
if (isset ($ varInfo ->firstInitialized )) {
1030
- $ phpcsFile ->addWarning (
1031
- "Unused %s %s. " ,
1032
- $ varInfo ->firstInitialized ,
1033
- 'UnusedVariable ' ,
1034
- [
1035
- VariableInfo::$ scopeTypeDescriptions [$ varInfo ->scopeType ],
1036
- "\${$ varInfo ->name }" ,
1037
- ]
1038
- );
1065
+ return $ varInfo ->firstInitialized ;
1039
1066
}
1067
+ return null ;
1040
1068
}
1041
1069
}
0 commit comments