|
| 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 | + /** |
| 17 | + * A list of tokenizers this sniff supports. |
| 18 | + * |
| 19 | + * @var array |
| 20 | + */ |
| 21 | + public $supportedTokenizers = [TokenizerSymbolsInterface::TOKENIZER_CSS, 'PHP']; |
| 22 | + |
| 23 | + private const WARNING_CODE = 'FoundLegacyTextInCopyright'; |
| 24 | + |
| 25 | + private const LEGACY_TEXTS = ['Irubin Consulting Inc', 'DBA Varien', 'Magento Inc']; |
| 26 | + |
| 27 | + /** |
| 28 | + * @inheritdoc |
| 29 | + */ |
| 30 | + public function register() |
| 31 | + { |
| 32 | + return [ |
| 33 | + T_DOC_COMMENT_STRING, |
| 34 | + T_INLINE_HTML |
| 35 | + ]; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @inheritDoc |
| 40 | + */ |
| 41 | + public function process(File $phpcsFile, $stackPtr) |
| 42 | + { |
| 43 | + $tokens = $phpcsFile->getTokens(); |
| 44 | + $content = null; |
| 45 | + |
| 46 | + if ($tokens[$stackPtr]['code'] === T_DOC_COMMENT_STRING) { |
| 47 | + $content = $tokens[$stackPtr]['content']; |
| 48 | + } |
| 49 | + if ($tokens[$stackPtr]['code'] === T_INLINE_HTML) { |
| 50 | + $content = $phpcsFile->getTokensAsString($stackPtr, 1); |
| 51 | + } |
| 52 | + if ($content != null) { |
| 53 | + $this->checkLicense($content, $stackPtr, $phpcsFile); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Check that the copyright license does not contain legacy text |
| 59 | + * |
| 60 | + * @param string $content |
| 61 | + * @param int $stackPtr |
| 62 | + * @param File $phpcsFile |
| 63 | + */ |
| 64 | + private function checkLicense(string $content, int $stackPtr, File $phpcsFile): void |
| 65 | + { |
| 66 | + $commentContent = $content; |
| 67 | + if (stripos($commentContent, 'copyright') === false) { |
| 68 | + return; |
| 69 | + } |
| 70 | + foreach (self::LEGACY_TEXTS as $legacyText) { |
| 71 | + if (stripos($commentContent, $legacyText) !== false) { |
| 72 | + $phpcsFile->addWarning( |
| 73 | + sprintf("The copyright license contains legacy text: %s.", $legacyText), |
| 74 | + $stackPtr, |
| 75 | + self::WARNING_CODE |
| 76 | + ); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments