|
| 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 Magento2\Sniffs\Legacy; |
| 9 | + |
| 10 | +use PHP_CodeSniffer\Files\File; |
| 11 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 12 | + |
| 13 | +class PhtmlTemplateSniff implements Sniff |
| 14 | +{ |
| 15 | + private const WARNING_CODE = 'PhtmlTemplateObsolete'; |
| 16 | + |
| 17 | + private const OBSOLETE_REGEX_IN_SPECIFIC_PHTML_TEMPLATES = [ |
| 18 | + '/(["\'])jquery\/ui\1/' => 'Please do not use "jquery/ui" library in templates. Use needed jquery ' . |
| 19 | + 'ui widget instead.', |
| 20 | + '/data-mage-init=(?:\'|")(?!\s*{\s*"[^"]+")/' => 'Please do not initialize JS component in php. Do ' . |
| 21 | + 'it in template.', |
| 22 | + '@x-magento-init.>(?!\s*+{\s*"[^"]+"\s*:\s*{\s*"[\w/-]+")@i' => 'Please do not initialize JS component ' . |
| 23 | + 'in php. Do it in template.', |
| 24 | + ]; |
| 25 | + |
| 26 | + /** |
| 27 | + * @inheritdoc |
| 28 | + */ |
| 29 | + public function register(): array |
| 30 | + { |
| 31 | + return [ |
| 32 | + T_OBJECT_OPERATOR, |
| 33 | + T_INLINE_HTML, |
| 34 | + T_HEREDOC |
| 35 | + ]; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritdoc |
| 40 | + */ |
| 41 | + public function process(File $phpcsFile, $stackPtr) |
| 42 | + { |
| 43 | + $tokens = $phpcsFile->getTokens(); |
| 44 | + if ($tokens[$stackPtr]['code'] === T_OBJECT_OPERATOR) { |
| 45 | + $this->checkBlockVariable($phpcsFile, $stackPtr, $tokens); |
| 46 | + $this->checkThisVariable($phpcsFile, $stackPtr, $tokens); |
| 47 | + } |
| 48 | + if ($tokens[$stackPtr]['code'] === T_INLINE_HTML || $tokens[$stackPtr]['code'] === T_HEREDOC) { |
| 49 | + $this->checkHtml($phpcsFile, $stackPtr); |
| 50 | + |
| 51 | + $file = $phpcsFile->getFilename(); |
| 52 | + |
| 53 | + if (strpos($file, '/view/frontend/templates/') !== false |
| 54 | + || strpos($file, '/view/base/templates/') !== false |
| 55 | + ) { |
| 56 | + $this->checkHtmlSpecificFiles($phpcsFile, $stackPtr); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Check access to protected and private members of Block |
| 63 | + * |
| 64 | + * @param File $phpcsFile |
| 65 | + * @param int $stackPtr |
| 66 | + * @param array $tokens |
| 67 | + */ |
| 68 | + private function checkBlockVariable(File $phpcsFile, int $stackPtr, array $tokens): void |
| 69 | + { |
| 70 | + $varPos = $phpcsFile->findPrevious(T_VARIABLE, $stackPtr - 1); |
| 71 | + if ($tokens[$varPos]['content'] !== '$block') { |
| 72 | + return; |
| 73 | + } |
| 74 | + $stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1); |
| 75 | + if (strpos($tokens[$stringPos]['content'], '_') === 0) { |
| 76 | + $phpcsFile->addWarning( |
| 77 | + 'Access to protected and private members of Block class is ' . |
| 78 | + 'obsolete in phtml templates. Use only public members.', |
| 79 | + $stringPos, |
| 80 | + self::WARNING_CODE |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Check access to members and methods of Block class through $this |
| 87 | + * |
| 88 | + * @param File $phpcsFile |
| 89 | + * @param int $stackPtr |
| 90 | + * @param array $tokens |
| 91 | + */ |
| 92 | + private function checkThisVariable(File $phpcsFile, int $stackPtr, array $tokens): void |
| 93 | + { |
| 94 | + $varPos = $phpcsFile->findPrevious(T_VARIABLE, $stackPtr - 1); |
| 95 | + if ($tokens[$varPos]['content'] !== '$this') { |
| 96 | + return; |
| 97 | + } |
| 98 | + $stringPos = $phpcsFile->findNext(T_STRING, $stackPtr + 1); |
| 99 | + if (strpos($tokens[$stringPos]['content'], 'helper') === false) { |
| 100 | + $phpcsFile->addWarning( |
| 101 | + 'Access to members and methods of Block class through $this is ' . |
| 102 | + 'obsolete in phtml templates. Use only $block instead of $this.', |
| 103 | + $stringPos, |
| 104 | + self::WARNING_CODE |
| 105 | + ); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Check use of "text/javascript" type |
| 111 | + * |
| 112 | + * @param File $phpcsFile |
| 113 | + * @param int $stackPtr |
| 114 | + */ |
| 115 | + private function checkHtml(File $phpcsFile, int $stackPtr): void |
| 116 | + { |
| 117 | + $content = $phpcsFile->getTokensAsString($stackPtr, 1); |
| 118 | + |
| 119 | + if (preg_match('/type="text\/javascript"/', $content)) { |
| 120 | + $phpcsFile->addWarning( |
| 121 | + 'Please do not use "text/javascript" type attribute.', |
| 122 | + $stackPtr, |
| 123 | + self::WARNING_CODE |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Check of some obsoletes uses in specific files |
| 130 | + * |
| 131 | + * @param File $phpcsFile |
| 132 | + * @param int $stackPtr |
| 133 | + */ |
| 134 | + private function checkHtmlSpecificFiles(File $phpcsFile, int $stackPtr): void |
| 135 | + { |
| 136 | + $content = $phpcsFile->getTokensAsString($stackPtr, 1); |
| 137 | + |
| 138 | + foreach (self::OBSOLETE_REGEX_IN_SPECIFIC_PHTML_TEMPLATES as $obsoleteRegex => $errorMessage) { |
| 139 | + if (preg_match($obsoleteRegex, $content)) { |
| 140 | + $phpcsFile->addWarning( |
| 141 | + $errorMessage, |
| 142 | + $stackPtr, |
| 143 | + self::WARNING_CODE |
| 144 | + ); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments