|
| 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 DOMDocument; |
| 11 | +use PHP_CodeSniffer\Sniffs\Sniff; |
| 12 | +use PHP_CodeSniffer\Files\File; |
| 13 | +use SimpleXMLElement; |
| 14 | + |
| 15 | +class ClassReferencesInConfigurationFilesSniff implements Sniff |
| 16 | +{ |
| 17 | + private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference'; |
| 18 | + private const ERROR_CODE_CONFIG = 'IncorrectClassReference'; |
| 19 | + private const ERROR_MESSAGE_MODULE = 'Incorrect format of module reference'; |
| 20 | + private const ERROR_CODE_MODULE = 'IncorrectModuleReference'; |
| 21 | + |
| 22 | + /** |
| 23 | + * @inheritdoc |
| 24 | + */ |
| 25 | + public function register() |
| 26 | + { |
| 27 | + return [ |
| 28 | + T_INLINE_HTML, |
| 29 | + ]; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @inheritdoc |
| 34 | + */ |
| 35 | + public function process(File $phpcsFile, $stackPtr) |
| 36 | + { |
| 37 | + if ($stackPtr > 0) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // We need to format the incoming XML to avoid tags split into several lines. In that case, PHP's DOMElement |
| 42 | + // returns the position of the closing /> as the position of the tag, and we need the position of < |
| 43 | + // instead, as it is the one we compare with $stackPtr later on. |
| 44 | + $xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); |
| 45 | + if ($xml === false) { |
| 46 | + $phpcsFile->addError( |
| 47 | + sprintf( |
| 48 | + "Couldn't parse contents of '%s', check that they are in valid XML format", |
| 49 | + $phpcsFile->getFilename(), |
| 50 | + ), |
| 51 | + $stackPtr, |
| 52 | + self::ERROR_CODE_CONFIG |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + $classes = $this->collectClassesInConfig($xml); |
| 57 | + $this->assertNonFactoryName($phpcsFile, $classes); |
| 58 | + |
| 59 | + $modules = $this->getValuesFromXmlTagAttribute($xml, '//@module', 'module'); |
| 60 | + $this->assertNonFactoryNameModule($phpcsFile, $modules); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Check whether specified class names are right according PSR-1 Standard. |
| 65 | + * |
| 66 | + * @param File $phpcsFile |
| 67 | + * @param array $elements |
| 68 | + */ |
| 69 | + private function assertNonFactoryName(File $phpcsFile, array $elements) |
| 70 | + { |
| 71 | + foreach ($elements as $element) { |
| 72 | + if (stripos($element['value'], 'Magento') === false) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element['value']) !== 1) { |
| 76 | + $phpcsFile->addError( |
| 77 | + self::ERROR_MESSAGE_CONFIG, |
| 78 | + $element['lineNumber'], |
| 79 | + self::ERROR_CODE_CONFIG, |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Check whether specified class names in modules are right according PSR-1 Standard. |
| 87 | + * |
| 88 | + * @param File $phpcsFile |
| 89 | + * @param array $classes |
| 90 | + */ |
| 91 | + private function assertNonFactoryNameModule(File $phpcsFile, array $classes) |
| 92 | + { |
| 93 | + foreach ($classes as $element) { |
| 94 | + if (preg_match('/^([A-Z][A-Za-z\d_]+)+$/', $element['value']) !== 1) { |
| 95 | + $phpcsFile->addError( |
| 96 | + self::ERROR_MESSAGE_MODULE, |
| 97 | + $element['lineNumber'], |
| 98 | + self::ERROR_CODE_MODULE, |
| 99 | + ); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Format the incoming XML to avoid tags split into several lines. |
| 106 | + * |
| 107 | + * @param File $phpcsFile |
| 108 | + * @return false|string |
| 109 | + */ |
| 110 | + private function getFormattedXML(File $phpcsFile) |
| 111 | + { |
| 112 | + $doc = new DomDocument('1.0'); |
| 113 | + $doc->formatOutput = true; |
| 114 | + $doc->loadXML($phpcsFile->getTokensAsString(0, count($phpcsFile->getTokens()))); |
| 115 | + return $doc->saveXML(); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Parse an XML for references to PHP class names in selected tags or attributes |
| 120 | + * |
| 121 | + * @param SimpleXMLElement $xml |
| 122 | + * @return array |
| 123 | + */ |
| 124 | + private function collectClassesInConfig(SimpleXMLElement $xml): array |
| 125 | + { |
| 126 | + $classes = $this->getValuesFromXmlTagContent( |
| 127 | + $xml, |
| 128 | + ' |
| 129 | + /config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)] |
| 130 | + | //model[not(parent::connection)] | //backend_model | //source_model | //price_model |
| 131 | + | //model_token | //writer_model | //clone_model | //frontend_model | //working_model |
| 132 | + | //admin_renderer | //renderer', |
| 133 | + ); |
| 134 | + |
| 135 | + $classes = array_merge( |
| 136 | + $classes, |
| 137 | + $this->getValuesFromXmlTagAttribute( |
| 138 | + $xml, |
| 139 | + '//@backend_model', |
| 140 | + 'backend_model' |
| 141 | + ), |
| 142 | + $this->getValuesFromXmlTagAttribute( |
| 143 | + $xml, |
| 144 | + '/config//preference', |
| 145 | + 'type' |
| 146 | + ), |
| 147 | + $this->getValuesFromXmlTagName( |
| 148 | + $xml, |
| 149 | + '/logging/*/expected_models/* | /logging/*/actions/*/expected_models/*', |
| 150 | + ) |
| 151 | + ); |
| 152 | + |
| 153 | + $classes = array_map( |
| 154 | + function (array $extendedNode) { |
| 155 | + $extendedNode['value'] = explode('::', trim($extendedNode['value']))[0]; |
| 156 | + return $extendedNode; |
| 157 | + }, |
| 158 | + $classes |
| 159 | + ); |
| 160 | + |
| 161 | + return $classes; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Extract value from tag contents which exist in the XML path |
| 166 | + * |
| 167 | + * @param SimpleXMLElement $xml |
| 168 | + * @param string $xPath |
| 169 | + * @return array |
| 170 | + */ |
| 171 | + private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath): array |
| 172 | + { |
| 173 | + $nodes = $xml->xpath($xPath) ?: []; |
| 174 | + return array_map(function ($item) { |
| 175 | + return [ |
| 176 | + 'value' => (string)$item, |
| 177 | + 'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, |
| 178 | + ]; |
| 179 | + }, $nodes); |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Extract value from tag names which exist in the XML path |
| 184 | + * |
| 185 | + * @param SimpleXMLElement $xml |
| 186 | + * @param string $xPath |
| 187 | + * @return array |
| 188 | + */ |
| 189 | + private function getValuesFromXmlTagName(SimpleXMLElement $xml, string $xPath): array |
| 190 | + { |
| 191 | + $nodes = $xml->xpath($xPath) ?: []; |
| 192 | + return array_map(function ($item) { |
| 193 | + return [ |
| 194 | + 'value' => $item->getName(), |
| 195 | + 'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, |
| 196 | + ]; |
| 197 | + }, $nodes); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * Extract value from tag attributes which exist in the XML path |
| 202 | + * |
| 203 | + * @param SimpleXMLElement $xml |
| 204 | + * @param string $xPath |
| 205 | + * @param string $attr |
| 206 | + * @return array |
| 207 | + */ |
| 208 | + private function getValuesFromXmlTagAttribute(SimpleXMLElement $xml, string $xPath, string $attr): array |
| 209 | + { |
| 210 | + $nodes = $xml->xpath($xPath) ?: []; |
| 211 | + return array_map(function ($item) use ($attr) { |
| 212 | + $nodeArray = (array)$item; |
| 213 | + if (isset($nodeArray['@attributes'][$attr])) { |
| 214 | + return [ |
| 215 | + 'value' => $nodeArray['@attributes'][$attr], |
| 216 | + 'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, |
| 217 | + ]; |
| 218 | + } |
| 219 | + }, $nodes); |
| 220 | + } |
| 221 | +} |
0 commit comments