@@ -49,36 +49,6 @@ class FunctionCommentSniff implements Sniff
49
49
'TRUEFALSE ' => 'bool ' ,
50
50
];
51
51
52
- /**
53
- * An array of variable types for param/var we will check.
54
- *
55
- * @var array<string>
56
- */
57
- public $ allowedTypes = [
58
- 'array ' ,
59
- 'array-key ' ,
60
- 'bool ' ,
61
- 'callable ' ,
62
- 'double ' ,
63
- 'float ' ,
64
- 'int ' ,
65
- 'positive-int ' ,
66
- 'negative-int ' ,
67
- 'iterable ' ,
68
- 'mixed ' ,
69
- 'object ' ,
70
- 'resource ' ,
71
- 'callable ' ,
72
- 'true ' ,
73
- 'false ' ,
74
- 'null ' ,
75
- 'scalar ' ,
76
- 'stdClass ' ,
77
- '\stdClass ' ,
78
- 'string ' ,
79
- 'void ' ,
80
- ];
81
-
82
52
83
53
/**
84
54
* Returns an array of tokens this test wants to listen for.
@@ -756,72 +726,6 @@ protected function processParams(File $phpcsFile, $stackPtr, $commentStart)
756
726
}
757
727
}//end if
758
728
759
- $ suggestedName = '' ;
760
- $ typeName = '' ;
761
- if (count ($ typeNames ) === 1 ) {
762
- $ typeName = $ param ['type ' ];
763
- $ suggestedName = static ::suggestType ($ typeName );
764
- }
765
-
766
- // This runs only if there is only one type name and the type name
767
- // is not one of the disallowed type names.
768
- if (count ($ typeNames ) === 1 && $ typeName === $ suggestedName ) {
769
- // Check type hint for array and custom type.
770
- $ suggestedTypeHint = '' ;
771
- if (strpos ($ suggestedName , 'array ' ) !== false && $ suggestedName !== 'array-key ' ) {
772
- $ suggestedTypeHint = 'array ' ;
773
- } else if (strpos ($ suggestedName , 'callable ' ) !== false ) {
774
- $ suggestedTypeHint = 'callable ' ;
775
- } else if (substr ($ suggestedName , -2 ) === '[] ' ) {
776
- $ suggestedTypeHint = 'array ' ;
777
- } else if ($ suggestedName === 'object ' ) {
778
- $ suggestedTypeHint = '' ;
779
- } else if (in_array ($ typeName , $ this ->allowedTypes ) === false ) {
780
- $ suggestedTypeHint = $ suggestedName ;
781
- }
782
-
783
- if ($ suggestedTypeHint !== '' && isset ($ realParams [$ checkPos ]) === true ) {
784
- $ typeHint = $ realParams [$ checkPos ]['type_hint ' ];
785
- // Primitive type hints are allowed to be omitted.
786
- if ($ typeHint === '' && in_array ($ suggestedTypeHint , $ this ->allowedTypes ) === false ) {
787
- $ error = 'Type hint "%s" missing for %s ' ;
788
- $ data = [
789
- $ suggestedTypeHint ,
790
- $ param ['var ' ],
791
- ];
792
- $ phpcsFile ->addError ($ error , $ stackPtr , 'TypeHintMissing ' , $ data );
793
- } else if ($ typeHint !== $ suggestedTypeHint && $ typeHint !== '' ) {
794
- // The type hint could be fully namespaced, so we check
795
- // for the part after the last "\".
796
- $ nameParts = explode ('\\' , $ suggestedTypeHint );
797
- $ lastPart = end ($ nameParts );
798
- if ($ lastPart !== $ typeHint && $ this ->isAliasedType ($ typeHint , $ suggestedTypeHint , $ phpcsFile ) === false ) {
799
- $ error = 'Expected type hint "%s"; found "%s" for %s ' ;
800
- $ data = [
801
- $ lastPart ,
802
- $ typeHint ,
803
- $ param ['var ' ],
804
- ];
805
- $ phpcsFile ->addError ($ error , $ stackPtr , 'IncorrectTypeHint ' , $ data );
806
- }
807
- }//end if
808
- } else if ($ suggestedTypeHint === ''
809
- && isset ($ realParams [$ checkPos ]) === true
810
- ) {
811
- $ typeHint = $ realParams [$ checkPos ]['type_hint ' ];
812
- if ($ typeHint !== ''
813
- && in_array ($ typeHint , $ this ->allowedTypes ) === false
814
- ) {
815
- $ error = 'Unknown type hint "%s" found for %s ' ;
816
- $ data = [
817
- $ typeHint ,
818
- $ param ['var ' ],
819
- ];
820
- $ phpcsFile ->addError ($ error , $ stackPtr , 'InvalidTypeHint ' , $ data );
821
- }
822
- }//end if
823
- }//end if
824
-
825
729
// Check number of spaces after the type.
826
730
$ spaces = 1 ;
827
731
if ($ param ['type_space ' ] !== $ spaces ) {
@@ -1025,73 +929,6 @@ public static function suggestType($type)
1025
929
}//end suggestType()
1026
930
1027
931
1028
- /**
1029
- * Checks if a used type hint is an alias defined by a "use" statement.
1030
- *
1031
- * @param string $typeHint The type hint used.
1032
- * @param string $suggestedTypeHint The fully qualified type to
1033
- * check against.
1034
- * @param \PHP_CodeSniffer\Files\File $phpcsFile The file being checked.
1035
- *
1036
- * @return boolean
1037
- */
1038
- protected function isAliasedType ($ typeHint , $ suggestedTypeHint , File $ phpcsFile )
1039
- {
1040
- $ tokens = $ phpcsFile ->getTokens ();
1041
-
1042
- // Iterate over all "use" statements in the file.
1043
- $ usePtr = 0 ;
1044
- while ($ usePtr !== false ) {
1045
- $ usePtr = $ phpcsFile ->findNext (T_USE , ($ usePtr + 1 ));
1046
- if ($ usePtr === false ) {
1047
- return false ;
1048
- }
1049
-
1050
- // Only check use statements in the global scope.
1051
- if (empty ($ tokens [$ usePtr ]['conditions ' ]) === false ) {
1052
- continue ;
1053
- }
1054
-
1055
- // Now comes the original class name, possibly with namespace
1056
- // backslashes.
1057
- $ originalClass = $ phpcsFile ->findNext (Tokens::$ emptyTokens , ($ usePtr + 1 ), null , true );
1058
- if ($ originalClass === false || ($ tokens [$ originalClass ]['code ' ] !== T_STRING
1059
- && $ tokens [$ originalClass ]['code ' ] !== T_NS_SEPARATOR )
1060
- ) {
1061
- continue ;
1062
- }
1063
-
1064
- $ originalClassName = '' ;
1065
- while (in_array ($ tokens [$ originalClass ]['code ' ], [T_STRING , T_NS_SEPARATOR ]) === true ) {
1066
- $ originalClassName .= $ tokens [$ originalClass ]['content ' ];
1067
- $ originalClass ++;
1068
- }
1069
-
1070
- if (ltrim ($ originalClassName , '\\' ) !== ltrim ($ suggestedTypeHint , '\\' )) {
1071
- continue ;
1072
- }
1073
-
1074
- // Now comes the "as" keyword signaling an alias name for the class.
1075
- $ asPtr = $ phpcsFile ->findNext (Tokens::$ emptyTokens , ($ originalClass + 1 ), null , true );
1076
- if ($ asPtr === false || $ tokens [$ asPtr ]['code ' ] !== T_AS ) {
1077
- continue ;
1078
- }
1079
-
1080
- // Now comes the name the class is aliased to.
1081
- $ aliasPtr = $ phpcsFile ->findNext (Tokens::$ emptyTokens , ($ asPtr + 1 ), null , true );
1082
- if ($ aliasPtr === false || $ tokens [$ aliasPtr ]['code ' ] !== T_STRING
1083
- || $ tokens [$ aliasPtr ]['content ' ] !== $ typeHint
1084
- ) {
1085
- continue ;
1086
- }
1087
-
1088
- // We found a use statement that aliases the used type hint!
1089
- return true ;
1090
- }//end while
1091
-
1092
- }//end isAliasedType()
1093
-
1094
-
1095
932
/**
1096
933
* Determines if a comment line is part of an @code/@endcode example.
1097
934
*
0 commit comments