-
Notifications
You must be signed in to change notification settings - Fork 160
AC-663 PHPCS classes test #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 28 commits
56e2de8
c9703a6
5a83385
5b40712
4fe226e
9d542d9
2c5c076
3636e9d
0293560
b91d0d4
717c998
5810c6e
0bc42a6
19bb57b
48a5816
b10df58
64fa0cc
5aa5721
389d7ca
c480100
c0ead7c
7b38922
9a69638
936c28c
fbe2c1c
34169b2
6558e29
36776b8
39267ba
8402f7d
1338211
ee4c7d6
08ed34b
c9ca788
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
declare(strict_types = 1); | ||
|
||
namespace Magento2\Sniffs\Legacy; | ||
|
||
use DOMDocument; | ||
use PHP_CodeSniffer\Sniffs\Sniff; | ||
use PHP_CodeSniffer\Files\File; | ||
use SimpleXMLElement; | ||
|
||
class ClassReferencesInConfigurationFilesSniff implements Sniff | ||
{ | ||
private const ERROR_MESSAGE_CONFIG = 'Incorrect format of PHP class reference'; | ||
private const ERROR_CODE_CONFIG = 'IncorrectClassReference'; | ||
private const ERROR_MESSAGE_MODULE = 'Incorrect format of module reference'; | ||
private const ERROR_CODE_MODULE = 'IncorrectModuleReference'; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function register() | ||
{ | ||
return [ | ||
T_INLINE_HTML, | ||
]; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function process(File $phpcsFile, $stackPtr) | ||
{ | ||
if ($stackPtr > 0) { | ||
return; | ||
} | ||
|
||
// We need to format the incoming XML to avoid tags split into several lines. In that case, PHP's DOMElement | ||
// returns the position of the closing /> as the position of the tag, and we need the position of < | ||
// instead, as it is the one we compare with $stackPtr later on. | ||
$xml = simplexml_load_string($this->getFormattedXML($phpcsFile)); | ||
if ($xml === false) { | ||
$phpcsFile->addError( | ||
sprintf( | ||
"Couldn't parse contents of '%s', check that they are in valid XML format", | ||
$phpcsFile->getFilename(), | ||
), | ||
$stackPtr, | ||
self::ERROR_CODE_CONFIG | ||
); | ||
} | ||
|
||
$classes = $this->collectClassesInConfig($xml); | ||
$this->assertNonFactoryName($phpcsFile, $classes); | ||
|
||
$modules = $this->getValuesFromXmlTagAttribute($xml, '//@module', 'module'); | ||
$this->assertNonFactoryNameModule($phpcsFile, $modules); | ||
|
||
$layouts = $this->collectClassesInLayout($xml); | ||
$this->assertNonFactoryName($phpcsFile, $layouts); | ||
} | ||
|
||
/** | ||
* Check whether specified class names are right according PSR-1 Standard. | ||
* | ||
* @param File $phpcsFile | ||
* @param array $elements | ||
*/ | ||
private function assertNonFactoryName(File $phpcsFile, array $elements) | ||
{ | ||
foreach ($elements as $element) { | ||
if (stripos($element['value'], 'Magento') === false) { | ||
continue; | ||
} | ||
if (preg_match('/^([A-Z][a-z\d\\\\]+)+$/', $element['value']) !== 1) { | ||
$phpcsFile->addError( | ||
self::ERROR_MESSAGE_CONFIG, | ||
$element['lineNumber'], | ||
self::ERROR_CODE_CONFIG, | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Check whether specified class names in modules are right according PSR-1 Standard. | ||
* | ||
* @param File $phpcsFile | ||
* @param array $classes | ||
*/ | ||
private function assertNonFactoryNameModule(File $phpcsFile, array $classes) | ||
{ | ||
foreach ($classes as $element) { | ||
if (preg_match('/^([A-Z][A-Za-z\d_]+)+$/', $element['value']) !== 1) { | ||
$phpcsFile->addError( | ||
self::ERROR_MESSAGE_MODULE, | ||
$element['lineNumber'], | ||
self::ERROR_CODE_MODULE, | ||
); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Format the incoming XML to avoid tags split into several lines. | ||
* | ||
* @param File $phpcsFile | ||
* @return false|string | ||
*/ | ||
private function getFormattedXML(File $phpcsFile) | ||
{ | ||
$doc = new DomDocument('1.0'); | ||
$doc->formatOutput = true; | ||
$doc->loadXML($phpcsFile->getTokensAsString(0, 999999)); | ||
return $doc->saveXML(); | ||
} | ||
|
||
/** | ||
* Parse an XML for references to PHP class names in selected tags or attributes | ||
* | ||
* @param SimpleXMLElement $xml | ||
* @return array | ||
*/ | ||
private function collectClassesInConfig(SimpleXMLElement $xml): array | ||
{ | ||
$classes = $this->getValuesFromXmlTagContent( | ||
$xml, | ||
' | ||
/config//resource_adapter | /config/*[not(name()="sections")]//class[not(ancestor::observers)] | ||
| //model[not(parent::connection)] | //backend_model | //source_model | //price_model | ||
| //model_token | //writer_model | //clone_model | //frontend_model | //working_model | ||
| //admin_renderer | //renderer', | ||
); | ||
$classes = array_merge( | ||
$classes, | ||
$this->getValuesFromXmlTagAttribute( | ||
$xml, | ||
'//@backend_model', | ||
'backend_model' | ||
) | ||
); | ||
$classes = array_merge( | ||
$classes, | ||
$this->getValuesFromXmlTagAttribute( | ||
$xml, | ||
'/config//preference', | ||
'type' | ||
) | ||
); | ||
$classes = array_merge( | ||
$classes, | ||
$this->getValuesFromXmlTagName( | ||
$xml, | ||
'/logging/*/expected_models/* | /logging/*/actions/*/expected_models/*', | ||
) | ||
); | ||
svera marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$classes = array_map( | ||
function (array $extendedNode) { | ||
$extendedNode['value'] = explode('::', trim($extendedNode['value']))[0]; | ||
return $extendedNode; | ||
}, | ||
$classes | ||
); | ||
|
||
return $classes; | ||
} | ||
|
||
/** | ||
* Parse an XML for references to PHP class names in selected tags or attributes | ||
* | ||
* @param SimpleXMLElement $xml | ||
* @return array | ||
*/ | ||
private function collectClassesInLayout(SimpleXMLElement $xml): array | ||
{ | ||
return $this->getValuesFromXmlTagAttribute( | ||
$xml, | ||
'/layout//@module', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sivaschenko I see a lot of occurrences of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this check can be removed as well There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed as talked offline @sivaschenko |
||
'module' | ||
); | ||
} | ||
|
||
/** | ||
* Extract value from tag contents which exist in the XML path | ||
* | ||
* @param SimpleXMLElement $xml | ||
* @param string $xPath | ||
* @return array | ||
*/ | ||
private function getValuesFromXmlTagContent(SimpleXMLElement $xml, string $xPath): array | ||
{ | ||
$nodes = $xml->xpath($xPath) ?: []; | ||
return array_map(function ($item) { | ||
return [ | ||
'value' => (string)$item, | ||
'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, | ||
]; | ||
}, $nodes); | ||
} | ||
|
||
/** | ||
* Extract value from tag names which exist in the XML path | ||
* | ||
* @param SimpleXMLElement $xml | ||
* @param string $xPath | ||
* @return array | ||
*/ | ||
private function getValuesFromXmlTagName(SimpleXMLElement $xml, string $xPath): array | ||
{ | ||
$nodes = $xml->xpath($xPath) ?: []; | ||
return array_map(function ($item) { | ||
return [ | ||
'value' => $item->getName(), | ||
'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, | ||
]; | ||
}, $nodes); | ||
} | ||
|
||
/** | ||
* Extract value from tag attributes which exist in the XML path | ||
* | ||
* @param SimpleXMLElement $xml | ||
* @param string $xPath | ||
* @param string $attr | ||
* @return array | ||
*/ | ||
private function getValuesFromXmlTagAttribute(SimpleXMLElement $xml, string $xPath, string $attr): array | ||
{ | ||
$nodes = $xml->xpath($xPath) ?: []; | ||
return array_map(function ($item) use ($attr) { | ||
$nodeArray = (array)$item; | ||
if (isset($nodeArray['@attributes'][$attr])) { | ||
return [ | ||
'value' => $nodeArray['@attributes'][$attr], | ||
'lineNumber' => dom_import_simplexml($item)->getLineNo()-1, | ||
]; | ||
} | ||
}, $nodes); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> | ||
<system> | ||
<section id="payment" type="text" sortOrder="400" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<group id="checkmo" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Check / Money Order</label> | ||
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1"> | ||
<label>Enabled</label> | ||
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model> | ||
</field> | ||
</group> | ||
<group id="purchaseorder" translate="label" type="text" sortOrder="32" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Purchase Order</label> | ||
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1"> | ||
<label>Enabled</label> | ||
<source_model>Magento\CONFIG\Model\Config\Source\Yesno</source_model> | ||
</field> | ||
</group> | ||
<group id="banktransfer" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Bank Transfer Payment</label> | ||
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1"> | ||
<label>Enabled</label> | ||
<source_model>Config\Model\Config\Source\Yesno</source_model> | ||
</field> | ||
<field id="title" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"> | ||
<label>Title</label> | ||
</field> | ||
<field id="order_status" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" canRestore="1"> | ||
<label>New Order Status</label> | ||
<source_model>Sales\MODEL\Config\Source\Order\Status\NewStatus</source_model> | ||
</field> | ||
<field id="allowspecific" translate="label" type="allowspecific" sortOrder="50" showInDefault="1" showInWebsite="1" canRestore="1"> | ||
<label>Payment from Applicable Countries</label> | ||
<source_model>MAGENTO\Payment\Model\Config\Source\Allspecificcountries</source_model> | ||
</field> | ||
</group> | ||
</section> | ||
</system> | ||
</config> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd"> | ||
<system> | ||
<section id="payment" type="text" sortOrder="400" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<group id="checkmo" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Check / Money Order</label> | ||
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1"> | ||
<label>Enabled</label> | ||
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model> | ||
</field> | ||
</group> | ||
<group id="purchaseorder" translate="label" type="text" sortOrder="32" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Purchase Order</label> | ||
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1"> | ||
<label>Enabled</label> | ||
<source_model> | ||
Magento\CONFIG\Model\Config\Source\Yesno | ||
</source_model> | ||
</field> | ||
</group> | ||
<group id="banktransfer" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1"> | ||
<label>Bank Transfer Payment</label> | ||
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1"> | ||
<label>Enabled</label> | ||
<source_model>Config\Model\Config\Source\Yesno</source_model> | ||
</field> | ||
<field id="title" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1"> | ||
<label>Title</label> | ||
</field> | ||
<field id="order_status" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" canRestore="1"> | ||
<label>New Order Status</label> | ||
<source_model>Sales\MODEL\Config\Source\Order\Status\NewStatus</source_model> | ||
</field> | ||
<field id="allowspecific" translate="label" type="allowspecific" sortOrder="50" showInDefault="1" showInWebsite="1" canRestore="1"> | ||
<label>Payment from Applicable Countries</label> | ||
<source_model> | ||
|
||
|
||
MAGENTO\Payment\Model\Config\Source\Allspecificcountries | ||
|
||
</source_model> | ||
</field> | ||
</group> | ||
</section> | ||
</system> | ||
</config> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd"> | ||
<menu> | ||
<add id="Magento_CatalogRule::promo" title="Promotions" translate="title" module="Magento CatalogRule" parent="Magento_Backend::marketing" sortOrder="10" resource="Magento_CatalogRule::promo"/> | ||
<add id="Magento_CatalogRule::promo_catalog" title="Catalog Price Rule" translate="title" sortOrder="10" module="Magento_CatalogRule" parent="Magento_CatalogRule::promo" action="catalog_rule/promo_catalog/" dependsOnModule="Magento_Catalog" resource="Magento_CatalogRule::promo_catalog"/> | ||
</menu> | ||
</config> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?xml version="1.0"?> | ||
<!-- | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
--> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | ||
<preference for="DateTimeInterface" type="DateTime" /> | ||
<preference for="Psr\Log\LoggerInterface" type="Magento\FRAMEWORK\Logger\LoggerProxy" /> | ||
<preference for="Magento\Framework\EntityManager\EntityMetadataInterface" type="Magento\Framework\EntityManager\EntityMetadata" /> | ||
<preference for="Magento\Framework\EntityManager\HydratorInterface" type="Magento\Framework\EntityManager\Hydrator" /> | ||
<preference for="Magento\Framework\View\Template\Html\MinifierInterface" type="Magento\Framework\View\Template\Html\Minifier" /> | ||
<preference for="Magento\Framework\Model\Entity\ScopeInterface" type="Magento\Framework\Model\Entity\Scope" /> | ||
<type name="Magento\Store\Model\Store"> | ||
<arguments> | ||
<argument name="currencyInstalled" xsi:type="string">system/currency/installed</argument> | ||
</arguments> | ||
</type> | ||
<virtualType name="Magento\Framework\Communication\Config\Reader\XmlReader" type="Magento\Framework\Config\Reader\Filesystem"> | ||
<arguments> | ||
<argument name="converter" xsi:type="object">Magento\Framework\Communication\Config\Reader\XmlReader\Converter</argument> | ||
<argument name="schemaLocator" xsi:type="object">Magento\Framework\Communication\Config\Reader\XmlReader\SchemaLocator</argument> | ||
<argument name="fileName" xsi:type="string">communication.xml</argument> | ||
<argument name="idAttributes" xsi:type="array"> | ||
<item name="/config/topic" xsi:type="string">name</item> | ||
<item name="/config/topic/handler" xsi:type="string">name</item> | ||
</argument> | ||
</arguments> | ||
</virtualType> | ||
<type name="Magento\Framework\Logger\Handler\Syslog"> | ||
<arguments> | ||
<argument name="ident" xsi:type="string">Magento</argument> | ||
</arguments> | ||
</type> | ||
</config> |
Uh oh!
There was an error while loading. Please reload this page.