|
| 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\Sniffs\Sniff; |
| 11 | +use PHP_CodeSniffer\Files\File; |
| 12 | + |
| 13 | +/** |
| 14 | + * Tests to find usage of restricted code |
| 15 | + */ |
| 16 | +class RestrictedCodeSniff implements Sniff |
| 17 | +{ |
| 18 | + private const ERROR_MESSAGE = "Class '%s' is restricted in %s. Suggested replacement: %s"; |
| 19 | + private const ERROR_CODE = "restrictedClass"; |
| 20 | + |
| 21 | + /** |
| 22 | + * List of fixtures that contain restricted classes and should not be tested |
| 23 | + * |
| 24 | + * @var array |
| 25 | + */ |
| 26 | + private $fixtureFiles = []; |
| 27 | + |
| 28 | + /** |
| 29 | + * Restricted classes |
| 30 | + * |
| 31 | + * @var array |
| 32 | + */ |
| 33 | + private $classes = []; |
| 34 | + |
| 35 | + /** |
| 36 | + * RestrictedCodeSniff constructor. |
| 37 | + */ |
| 38 | + public function __construct() |
| 39 | + { |
| 40 | + $this->loadData('restricted_classes*.php'); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @inheritdoc |
| 45 | + */ |
| 46 | + public function register() |
| 47 | + { |
| 48 | + return [ |
| 49 | + T_STRING, |
| 50 | + T_CONSTANT_ENCAPSED_STRING |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @inheritdoc |
| 56 | + */ |
| 57 | + public function process(File $phpcsFile, $stackPtr) |
| 58 | + { |
| 59 | + // phpcs:ignore |
| 60 | + if (array_key_exists(basename($phpcsFile->getFilename()), $this->fixtureFiles)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $tokens = $phpcsFile->getTokens(); |
| 65 | + $token = $tokens[$stackPtr]['content']; |
| 66 | + if (array_key_exists($token, $this->classes)) { |
| 67 | + if ($this->isExcluded($token, $phpcsFile)) { |
| 68 | + return; |
| 69 | + } |
| 70 | + $phpcsFile->addError( |
| 71 | + sprintf( |
| 72 | + self::ERROR_MESSAGE, |
| 73 | + $token, |
| 74 | + $phpcsFile->getFilename(), |
| 75 | + $this->classes[$token]['replacement'] |
| 76 | + ), |
| 77 | + $stackPtr, |
| 78 | + self::ERROR_CODE, |
| 79 | + ); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Checks if currently parsed file should be excluded from analysis |
| 85 | + * |
| 86 | + * @param string $token |
| 87 | + * @param File $phpcsFile |
| 88 | + * @return bool |
| 89 | + */ |
| 90 | + private function isExcluded(string $token, File $phpcsFile): bool |
| 91 | + { |
| 92 | + if (in_array($phpcsFile->getFilename(), $this->fixtureFiles)) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + foreach ($this->classes[$token]['exclude'] as $exclude) { |
| 96 | + if (strpos($phpcsFile->getFilename(), $exclude) !== false) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Loads and merges data from fixtures |
| 105 | + * |
| 106 | + * @param string $filePattern |
| 107 | + * @return void |
| 108 | + */ |
| 109 | + private function loadData(string $filePattern) |
| 110 | + { |
| 111 | + // phpcs:ignore |
| 112 | + foreach (glob(__DIR__ . '/_files/' . $filePattern) as $file) { |
| 113 | + $relativePath = str_replace( |
| 114 | + '\\', |
| 115 | + '/', |
| 116 | + str_replace(__DIR__ . DIRECTORY_SEPARATOR, '', $file) |
| 117 | + ); |
| 118 | + array_push($this->fixtureFiles, $relativePath); |
| 119 | + $this->classes = array_merge_recursive($this->classes, $this->readList($file)); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Isolate including a file into a method to reduce scope |
| 125 | + * |
| 126 | + * @param string $file |
| 127 | + * @return array |
| 128 | + */ |
| 129 | + private function readList($file) |
| 130 | + { |
| 131 | + // phpcs:ignore |
| 132 | + return include $file; |
| 133 | + } |
| 134 | +} |
0 commit comments