| @@ 17-130 (lines=114) @@ | ||
| 14 | * @package PHPCompatibility |
|
| 15 | * @author Wim Godden <[email protected]> |
|
| 16 | */ |
|
| 17 | class PHPCompatibility_Sniffs_PHP_NewOperatorsSniff extends PHPCompatibility_Sniff |
|
| 18 | { |
|
| 19 | ||
| 20 | /** |
|
| 21 | * A list of new operators |
|
| 22 | * |
|
| 23 | * The array lists : version number with false (not present) or true (present). |
|
| 24 | * If's sufficient to list the first version where the keyword appears. |
|
| 25 | * |
|
| 26 | * @var array(string => array(string => int|string|null)) |
|
| 27 | */ |
|
| 28 | protected $newOperators = array ( |
|
| 29 | 'T_SPACESHIP' => array( |
|
| 30 | '5.6' => false, |
|
| 31 | '7.0' => true, |
|
| 32 | 'description' => 'Spaceship operator' |
|
| 33 | ), |
|
| 34 | 'float' => array( |
|
| 35 | '5.6' => false, |
|
| 36 | '7.0' => true, |
|
| 37 | 'description' => 'float return type' |
|
| 38 | ), |
|
| 39 | 'bool' => array( |
|
| 40 | '5.6' => false, |
|
| 41 | '7.0' => true, |
|
| 42 | 'description' => 'bool return type' |
|
| 43 | ), |
|
| 44 | 'string' => array( |
|
| 45 | '5.6' => false, |
|
| 46 | '7.0' => true, |
|
| 47 | 'description' => 'string return type' |
|
| 48 | ), |
|
| 49 | ); |
|
| 50 | ||
| 51 | ||
| 52 | /** |
|
| 53 | * If true, an error will be thrown; otherwise a warning. |
|
| 54 | * |
|
| 55 | * @var bool |
|
| 56 | */ |
|
| 57 | protected $error = false; |
|
| 58 | ||
| 59 | ||
| 60 | /** |
|
| 61 | * Returns an array of tokens this test wants to listen for. |
|
| 62 | * |
|
| 63 | * @return array |
|
| 64 | */ |
|
| 65 | public function register() |
|
| 66 | { |
|
| 67 | return array(T_RETURN_TYPE); |
|
| 68 | }//end register() |
|
| 69 | ||
| 70 | ||
| 71 | /** |
|
| 72 | * Processes this test, when one of its tokens is encountered. |
|
| 73 | * |
|
| 74 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 75 | * @param int $stackPtr The position of the current token in |
|
| 76 | * the stack passed in $tokens. |
|
| 77 | * |
|
| 78 | * @return void |
|
| 79 | */ |
|
| 80 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 81 | { |
|
| 82 | $tokens = $phpcsFile->getTokens(); |
|
| 83 | ||
| 84 | if (in_array($tokens[$stackPtr]['content'], array_keys($this->newTypes))) { |
|
| 85 | $this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content']); |
|
| 86 | } |
|
| 87 | }//end process() |
|
| 88 | ||
| 89 | ||
| 90 | /** |
|
| 91 | * Generates the error or wanrning for this sniff. |
|
| 92 | * |
|
| 93 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 94 | * @param int $stackPtr The position of the function |
|
| 95 | * in the token array. |
|
| 96 | * @param string $typeName The type. |
|
| 97 | * @param string $pattern The pattern used for the match. |
|
| 98 | * |
|
| 99 | * @return void |
|
| 100 | */ |
|
| 101 | protected function addError($phpcsFile, $stackPtr, $typeName, $pattern=null) |
|
| 102 | { |
|
| 103 | if ($pattern === null) { |
|
| 104 | $pattern = $typeName; |
|
| 105 | } |
|
| 106 | ||
| 107 | $error = ''; |
|
| 108 | ||
| 109 | $this->error = false; |
|
| 110 | foreach ($this->newTypes[$pattern] as $version => $present) { |
|
| 111 | if ($this->supportsBelow($version)) { |
|
| 112 | if ($present === false) { |
|
| 113 | $this->error = true; |
|
| 114 | $error .= 'not present in PHP version ' . $version . ' or earlier'; |
|
| 115 | } |
|
| 116 | } |
|
| 117 | } |
|
| 118 | if (strlen($error) > 0) { |
|
| 119 | $error = $this->newTypes[$typeName]['description'] . ' is ' . $error; |
|
| 120 | ||
| 121 | if ($this->error === true) { |
|
| 122 | $phpcsFile->addError($error, $stackPtr); |
|
| 123 | } else { |
|
| 124 | $phpcsFile->addWarning($error, $stackPtr); |
|
| 125 | } |
|
| 126 | } |
|
| 127 | ||
| 128 | }//end addError() |
|
| 129 | ||
| 130 | }//end class |
|
| 131 | ||
| @@ 17-130 (lines=114) @@ | ||
| 14 | * @package PHPCompatibility |
|
| 15 | * @author Wim Godden <[email protected]> |
|
| 16 | */ |
|
| 17 | class PHPCompatibility_Sniffs_PHP_NewScalarReturnTypeDeclarationsSniff extends PHPCompatibility_Sniff |
|
| 18 | { |
|
| 19 | ||
| 20 | /** |
|
| 21 | * A list of new types |
|
| 22 | * |
|
| 23 | * The array lists : version number with false (not present) or true (present). |
|
| 24 | * If's sufficient to list the first version where the keyword appears. |
|
| 25 | * |
|
| 26 | * @var array(string => array(string => int|string|null)) |
|
| 27 | */ |
|
| 28 | protected $newTypes = array ( |
|
| 29 | 'int' => array( |
|
| 30 | '5.6' => false, |
|
| 31 | '7.0' => true, |
|
| 32 | 'description' => 'int return type' |
|
| 33 | ), |
|
| 34 | 'float' => array( |
|
| 35 | '5.6' => false, |
|
| 36 | '7.0' => true, |
|
| 37 | 'description' => 'float return type' |
|
| 38 | ), |
|
| 39 | 'bool' => array( |
|
| 40 | '5.6' => false, |
|
| 41 | '7.0' => true, |
|
| 42 | 'description' => 'bool return type' |
|
| 43 | ), |
|
| 44 | 'string' => array( |
|
| 45 | '5.6' => false, |
|
| 46 | '7.0' => true, |
|
| 47 | 'description' => 'string return type' |
|
| 48 | ), |
|
| 49 | ); |
|
| 50 | ||
| 51 | ||
| 52 | /** |
|
| 53 | * If true, an error will be thrown; otherwise a warning. |
|
| 54 | * |
|
| 55 | * @var bool |
|
| 56 | */ |
|
| 57 | protected $error = false; |
|
| 58 | ||
| 59 | ||
| 60 | /** |
|
| 61 | * Returns an array of tokens this test wants to listen for. |
|
| 62 | * |
|
| 63 | * @return array |
|
| 64 | */ |
|
| 65 | public function register() |
|
| 66 | { |
|
| 67 | return array(T_RETURN_TYPE); |
|
| 68 | }//end register() |
|
| 69 | ||
| 70 | ||
| 71 | /** |
|
| 72 | * Processes this test, when one of its tokens is encountered. |
|
| 73 | * |
|
| 74 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 75 | * @param int $stackPtr The position of the current token in |
|
| 76 | * the stack passed in $tokens. |
|
| 77 | * |
|
| 78 | * @return void |
|
| 79 | */ |
|
| 80 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
|
| 81 | { |
|
| 82 | $tokens = $phpcsFile->getTokens(); |
|
| 83 | ||
| 84 | if (in_array($tokens[$stackPtr]['content'], array_keys($this->newTypes))) { |
|
| 85 | $this->addError($phpcsFile, $stackPtr, $tokens[$stackPtr]['content']); |
|
| 86 | } |
|
| 87 | }//end process() |
|
| 88 | ||
| 89 | ||
| 90 | /** |
|
| 91 | * Generates the error or wanrning for this sniff. |
|
| 92 | * |
|
| 93 | * @param PHP_CodeSniffer_File $phpcsFile The file being scanned. |
|
| 94 | * @param int $stackPtr The position of the function |
|
| 95 | * in the token array. |
|
| 96 | * @param string $typeName The type. |
|
| 97 | * @param string $pattern The pattern used for the match. |
|
| 98 | * |
|
| 99 | * @return void |
|
| 100 | */ |
|
| 101 | protected function addError($phpcsFile, $stackPtr, $typeName, $pattern=null) |
|
| 102 | { |
|
| 103 | if ($pattern === null) { |
|
| 104 | $pattern = $typeName; |
|
| 105 | } |
|
| 106 | ||
| 107 | $error = ''; |
|
| 108 | ||
| 109 | $this->error = false; |
|
| 110 | foreach ($this->newTypes[$pattern] as $version => $present) { |
|
| 111 | if ($this->supportsBelow($version)) { |
|
| 112 | if ($present === false) { |
|
| 113 | $this->error = true; |
|
| 114 | $error .= 'not present in PHP version ' . $version . ' or earlier'; |
|
| 115 | } |
|
| 116 | } |
|
| 117 | } |
|
| 118 | if (strlen($error) > 0) { |
|
| 119 | $error = $this->newTypes[$typeName]['description'] . ' is ' . $error; |
|
| 120 | ||
| 121 | if ($this->error === true) { |
|
| 122 | $phpcsFile->addError($error, $stackPtr); |
|
| 123 | } else { |
|
| 124 | $phpcsFile->addWarning($error, $stackPtr); |
|
| 125 | } |
|
| 126 | } |
|
| 127 | ||
| 128 | }//end addError() |
|
| 129 | ||
| 130 | }//end class |
|
| 131 | ||