|
| 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 Magento2\Sniffs\Less\TokenizerSymbolsInterface; |
| 11 | +use PHP_CodeSniffer\Files\File; |
| 12 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 13 | + |
| 14 | +class LicenseSniff implements Sniff |
| 15 | +{ |
| 16 | + private const WARNING_CODE = 'FoundLegacyTextInCopyright'; |
| 17 | + |
| 18 | + public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS, 'PHP']; |
| 19 | + |
| 20 | + /** |
| 21 | + * @inheritdoc |
| 22 | + */ |
| 23 | + public function register() |
| 24 | + { |
| 25 | + return [ |
| 26 | + T_DOC_COMMENT_STRING, |
| 27 | + T_INLINE_HTML |
| 28 | + ]; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @inheritDoc |
| 33 | + */ |
| 34 | + public function process(File $phpcsFile, $stackPtr) |
| 35 | + { |
| 36 | + $tokens = $phpcsFile->getTokens(); |
| 37 | + $content = null; |
| 38 | + |
| 39 | + if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_STRING) { |
| 40 | + $content = $tokens[$stackPtr]['content']; |
| 41 | + } |
| 42 | + if ($tokens[$stackPtr]['code'] === T_INLINE_HTML) { |
| 43 | + $content = $phpcsFile->getTokensAsString($stackPtr, 1); |
| 44 | + } |
| 45 | + if ($content != null){ |
| 46 | + $this->checkLicense($content, $stackPtr, $phpcsFile); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @param string $content |
| 52 | + * @param int $stackPtr |
| 53 | + * @param File $phpcsFile |
| 54 | + */ |
| 55 | + private function checkLicense(string $content, int $stackPtr, File $phpcsFile): void |
| 56 | + { |
| 57 | + $commentContent = $content; |
| 58 | + if (stripos($commentContent, 'copyright') === false) { |
| 59 | + return; |
| 60 | + } |
| 61 | + foreach (['Irubin Consulting Inc', 'DBA Varien', 'Magento Inc'] as $legacyText) { |
| 62 | + if (stripos($commentContent, $legacyText) !== false) { |
| 63 | + $phpcsFile->addWarning( |
| 64 | + sprintf("The copyright license contains legacy text: %s.", $legacyText), |
| 65 | + $stackPtr, |
| 66 | + self::WARNING_CODE |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments