|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\MagentoCloud\Test\Sniffs\Directives; |
| 9 | + |
| 10 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 11 | +use PHP_CodeSniffer\Files\File; |
| 12 | + |
| 13 | +/** |
| 14 | + * Sniffer to check if the strict_types declaration is included and add it if not. |
| 15 | + * |
| 16 | + * Class StrictTypesSniff |
| 17 | + */ |
| 18 | +class StrictTypesSniff implements Sniff |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Flag to keep track of whether the file has been fixed. |
| 22 | + * |
| 23 | + * @var boolean |
| 24 | + */ |
| 25 | + private $fixed = false; |
| 26 | + |
| 27 | + /** |
| 28 | + * @return array |
| 29 | + */ |
| 30 | + public function register() |
| 31 | + { |
| 32 | + return [ |
| 33 | + T_OPEN_TAG |
| 34 | + ]; |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * @param File $phpcsFile |
| 39 | + * @param int $stackPtr |
| 40 | + * @return int|void |
| 41 | + */ |
| 42 | + public function process(File $phpcsFile, $stackPtr) |
| 43 | + { |
| 44 | + // Get all tokens. |
| 45 | + $tokens = $phpcsFile->getTokens(); |
| 46 | + |
| 47 | + // Tokens to look for. |
| 48 | + $findTokens = [ |
| 49 | + T_DECLARE, |
| 50 | + T_NAMESPACE, |
| 51 | + T_CLASS |
| 52 | + ]; |
| 53 | + |
| 54 | + // Find the first occurrence of the tokens to look for. |
| 55 | + $position = $phpcsFile->findNext($findTokens, $stackPtr); |
| 56 | + |
| 57 | + if ($position === false) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // If the first token found is not T_DECLARE, then the file does not include a strict_types declaration. |
| 62 | + if ($tokens[$position]['code'] !== T_DECLARE) { |
| 63 | + // Fix and set the boolean flag to true. |
| 64 | + $this->fix($phpcsFile, $position); |
| 65 | + } |
| 66 | + |
| 67 | + // If the file includes a declare directive, and the file has not already been fixed, scan specifically |
| 68 | + // for strict_types and fix as needed. |
| 69 | + if (!$this->fixed) { |
| 70 | + if (!$this->scan($phpcsFile, $tokens, $position)) { |
| 71 | + $this->fix($phpcsFile, $position); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Fixer to add the strict_types declaration. |
| 78 | + * |
| 79 | + * @param File $phpcsFile |
| 80 | + * @param int $position |
| 81 | + */ |
| 82 | + private function fix(File $phpcsFile, int $position) : void |
| 83 | + { |
| 84 | + // Get the fixer. |
| 85 | + $fixer = $phpcsFile->fixer; |
| 86 | + // Record the error. |
| 87 | + $phpcsFile->addFixableError("Missing strict_types declaration", $position, self::class); |
| 88 | + // Prepend content at the given position. |
| 89 | + $fixer->addContentBefore($position, "declare(strict_types=1);\n\n"); |
| 90 | + // Set flag. |
| 91 | + $this->fixed = true; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Recursive method to scan declare statements for strict_types. |
| 96 | + * |
| 97 | + * @param File $phpcsFile |
| 98 | + * @param array $tokens |
| 99 | + * @param int $position |
| 100 | + * @return bool |
| 101 | + */ |
| 102 | + private function scan(File $phpcsFile, array $tokens, int $position) : bool |
| 103 | + { |
| 104 | + // Exit statement, if the beginning of the file has been reached. |
| 105 | + if ($tokens[$position]['code'] === T_OPEN_TAG || $position === 0) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + if (!$phpcsFile->findNext([T_STRING], $position)) { |
| 110 | + // If there isn't a T_STRING token for the declare directive, continue scan. |
| 111 | + return $this->scan($phpcsFile, $tokens, $phpcsFile->findPrevious([T_DECLARE], $position - 1)); |
| 112 | + } else { |
| 113 | + // Checking specifically for strict_types. |
| 114 | + $temp = $phpcsFile->findNext([T_STRING], $position); |
| 115 | + if ($tokens[$temp]['content'] === 'strict_types') { |
| 116 | + // Return true as strict_types directive has been found. |
| 117 | + return true; |
| 118 | + } else { |
| 119 | + // Continue scan if strict_types hasn't been found. |
| 120 | + return $this->scan($phpcsFile, $tokens, $phpcsFile->findPrevious([T_DECLARE], $position - 1)); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments