|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Sniffs\Legacy; |
| 7 | + |
| 8 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 9 | +use PHP_CodeSniffer\Files\File; |
| 10 | + |
| 11 | +/** |
| 12 | + * Detects typical Magento 1.x classes constructions. |
| 13 | + */ |
| 14 | +class MageEntitySniff implements Sniff |
| 15 | +{ |
| 16 | + /** |
| 17 | + * String representation of error. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected $errorMessage = 'Possible Magento 2 design violation. Detected typical Magento 1.x construction "%s".'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Error violation code. |
| 25 | + * |
| 26 | + * @var string |
| 27 | + */ |
| 28 | + protected $errorCode = 'FoundLegacyEntity'; |
| 29 | + |
| 30 | + /** |
| 31 | + * Legacy entity from Magento 1. |
| 32 | + * |
| 33 | + * @var string |
| 34 | + */ |
| 35 | + protected $legacyEntity = 'Mage'; |
| 36 | + |
| 37 | + /** |
| 38 | + * Legacy prefixes from Magento 1. |
| 39 | + * |
| 40 | + * @var array |
| 41 | + */ |
| 42 | + protected $legacyPrefixes = [ |
| 43 | + 'Mage_', |
| 44 | + 'Enterprise_' |
| 45 | + ]; |
| 46 | + |
| 47 | + /** |
| 48 | + * @inheritdoc |
| 49 | + */ |
| 50 | + public function register() |
| 51 | + { |
| 52 | + return [ |
| 53 | + T_DOUBLE_COLON, |
| 54 | + T_NEW |
| 55 | + ]; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * List of tokens for which we should find name before his position. |
| 60 | + * |
| 61 | + * @var array |
| 62 | + */ |
| 63 | + protected $nameBefore = [ |
| 64 | + T_DOUBLE_COLON |
| 65 | + ]; |
| 66 | + |
| 67 | + /** |
| 68 | + * @inheritdoc |
| 69 | + */ |
| 70 | + public function process(File $phpcsFile, $stackPtr) |
| 71 | + { |
| 72 | + $tokens = $phpcsFile->getTokens(); |
| 73 | + if (in_array($tokens[$stackPtr]['code'], $this->nameBefore)) { |
| 74 | + $oldPosition = $stackPtr; |
| 75 | + $stackPtr = $phpcsFile->findPrevious(T_STRING, $stackPtr - 1, null, false, null, true); |
| 76 | + if ($stackPtr === false) { |
| 77 | + return; |
| 78 | + } |
| 79 | + $entityName = $tokens[$stackPtr]['content']; |
| 80 | + $error = [$entityName . $tokens[$oldPosition]['content']]; |
| 81 | + } else { |
| 82 | + $oldPosition = $stackPtr; |
| 83 | + $stackPtr = $phpcsFile->findNext(T_STRING, $oldPosition + 1, null, false, null, true); |
| 84 | + if ($stackPtr === false) { |
| 85 | + return; |
| 86 | + } |
| 87 | + $entityName = $tokens[$stackPtr]['content']; |
| 88 | + $error = [$tokens[$oldPosition]['content'] . ' ' . $entityName]; |
| 89 | + } |
| 90 | + if ($entityName === $this->legacyEntity || $this->isPrefixLegacy($entityName)) { |
| 91 | + $phpcsFile->addError( |
| 92 | + $this->errorMessage, |
| 93 | + $stackPtr, |
| 94 | + $this->errorCode, |
| 95 | + $error |
| 96 | + ); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Method checks if passed string contains legacy prefix from Magento 1. |
| 102 | + * |
| 103 | + * @param string $entityName |
| 104 | + * @return bool |
| 105 | + */ |
| 106 | + private function isPrefixLegacy($entityName) |
| 107 | + { |
| 108 | + foreach ($this->legacyPrefixes as $entity) { |
| 109 | + if (strpos($entityName, $entity) === 0) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + } |
| 113 | + return false; |
| 114 | + } |
| 115 | +} |
0 commit comments