| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * \PHPCompatibility\AbstractFunctionCallParameterSniff. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @category PHP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package  PHPCompatibility | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author   Juliette Reinders Folmer <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace PHPCompatibility; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PHPCompatibility\Sniff; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * \PHPCompatibility\AbstractFunctionCallParameterSniff. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Abstract class to use as a base for examining the parameter values passed to function calls. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @category PHP | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @package  PHPCompatibility | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author   Juliette Reinders Folmer <[email protected]> | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  | abstract class AbstractFunctionCallParameterSniff extends Sniff | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * Is the sniff looking for a function call or a method call ? | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * Note: the child class may need to do additional checks to make sure that | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * the method called is of the right class/object. | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * Checking that is outside of the scope of this abstract sniff. | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @var bool False (default) if the sniff is looking for function calls. | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      *           True if the sniff is looking for method calls. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     protected $isMethod = false; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * Functions the sniff is looking for. Should be defined in the child class. | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @var array The only requirement for this array is that the top level | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      *            array keys are the names of the functions you're looking for. | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      *            Other than that, the array can have arbitrary content | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      *            depending on your needs. | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     protected $targetFunctions = array(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * List of tokens which when they preceed the $stackPtr indicate that this | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * is not a function call. | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |      * @var array | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     private $ignoreTokens = array( | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         T_DOUBLE_COLON    => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         T_OBJECT_OPERATOR => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         T_FUNCTION        => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         T_NEW             => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         T_CONST           => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         T_USE             => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * Returns an array of tokens this test wants to listen for. | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function register() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         // Handle case-insensitivity of function names. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->targetFunctions = $this->arrayKeysToLowercase($this->targetFunctions); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         return array(T_STRING); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      * Processes this test, when one of its tokens is encountered. | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * @param \PHP_CodeSniffer_File $phpcsFile The file being scanned. | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * @param int                   $stackPtr  The position of the current token in | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      *                                         the stack passed in $tokens. | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     public function process(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         if ($this->bowOutEarly() === true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $tokens     = $phpcsFile->getTokens(); | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         $function   = $tokens[$stackPtr]['content']; | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         $functionLc = strtolower($function); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         if (isset($this->targetFunctions[$functionLc]) === false) { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             return; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         $prevNonEmpty = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |         if ($this->isMethod === true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             if ($tokens[$prevNonEmpty]['code'] !== T_DOUBLE_COLON | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                 && $tokens[$prevNonEmpty]['code'] !== T_OBJECT_OPERATOR | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 // Not a call to a PHP method. | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 return; | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             if (isset($this->ignoreTokens[$tokens[$prevNonEmpty]['code']]) === true) { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                 // Not a call to a PHP function. | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                 return; | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             if ($tokens[$prevNonEmpty]['code'] === T_NS_SEPARATOR | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |                 && $tokens[$prevNonEmpty - 1]['code'] === T_STRING | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |             ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                 // Namespaced function. | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                 return; | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         $parameters = $this->getFunctionCallParameters($phpcsFile, $stackPtr); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |         if (empty($parameters)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             return $this->processNoParameters($phpcsFile, $stackPtr, $function); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             return $this->processParameters($phpcsFile, $stackPtr, $function, $parameters); | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |      * Do a version check to determine if this sniff needs to run at all. | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |      * If the check done in a child class is not specific to one PHP version, | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * this function should return `false`. | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |      * @return bool | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 141 |  |  |     abstract protected function bowOutEarly(); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |      * Process the parameters of a matched function. | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |      * This method has to be made concrete in child classes. | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |      * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned. | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |      * @param int                   $stackPtr     The position of the current token in the stack. | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |      * @param string                $functionName The token content (function name) which was matched. | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |      * @param array                 $parameters   Array with information about the parameters. | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |      * @return int|void Integer stack pointer to skip forward or void to continue | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |      *                  normal file processing. | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |      */ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  |     abstract public function processParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName, $parameters); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  |      * Process the function if no parameters were found. | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  |      * Defaults to doing nothing. Can be overloaded in child classes to handle functions | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |      * were parameters are expected, but none found. | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |      * @param \PHP_CodeSniffer_File $phpcsFile    The file being scanned. | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  |      * @param int                   $stackPtr     The position of the current token in the stack. | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |      * @param string                $functionName The token content (function name) which was matched. | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 170 |  |  |      * @return int|void Integer stack pointer to skip forward or void to continue | 
            
                                                                        
                            
            
                                    
            
            
                | 171 |  |  |      *                  normal file processing. | 
            
                                                                        
                            
            
                                    
            
            
                | 172 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |     public function processNoParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $functionName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |         return; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 177 |  |  | }//end class | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |  |