Skip to content

Commit 3b0e492

Browse files
authoredOct 14, 2021
Merge pull request #99 from magento-commerce/imported-svera-magento-coding-standard-274
[Imported] AC-663 PHPCS classes test
2 parents 47072f0 + c9ca788 commit 3b0e492

7 files changed

+424
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
9+
<system>
10+
<section id="payment" type="text" sortOrder="400" showInDefault="1" showInWebsite="1" showInStore="1">
11+
<group id="checkmo" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
12+
<label>Check / Money Order</label>
13+
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1">
14+
<label>Enabled</label>
15+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
16+
</field>
17+
</group>
18+
<group id="purchaseorder" translate="label" type="text" sortOrder="32" showInDefault="1" showInWebsite="1" showInStore="1">
19+
<label>Purchase Order</label>
20+
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1">
21+
<label>Enabled</label>
22+
<source_model>Magento\CONFIG\Model\Config\Source\Yesno</source_model>
23+
</field>
24+
</group>
25+
<group id="banktransfer" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
26+
<label>Bank Transfer Payment</label>
27+
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1">
28+
<label>Enabled</label>
29+
<source_model>Config\Model\Config\Source\Yesno</source_model>
30+
</field>
31+
<field id="title" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
32+
<label>Title</label>
33+
</field>
34+
<field id="order_status" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" canRestore="1">
35+
<label>New Order Status</label>
36+
<source_model>Sales\MODEL\Config\Source\Order\Status\NewStatus</source_model>
37+
</field>
38+
<field id="allowspecific" translate="label" type="allowspecific" sortOrder="50" showInDefault="1" showInWebsite="1" canRestore="1">
39+
<label>Payment from Applicable Countries</label>
40+
<source_model>MAGENTO\Payment\Model\Config\Source\Allspecificcountries</source_model>
41+
</field>
42+
</group>
43+
</section>
44+
</system>
45+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
9+
<system>
10+
<section id="payment" type="text" sortOrder="400" showInDefault="1" showInWebsite="1" showInStore="1">
11+
<group id="checkmo" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
12+
<label>Check / Money Order</label>
13+
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1">
14+
<label>Enabled</label>
15+
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
16+
</field>
17+
</group>
18+
<group id="purchaseorder" translate="label" type="text" sortOrder="32" showInDefault="1" showInWebsite="1" showInStore="1">
19+
<label>Purchase Order</label>
20+
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1">
21+
<label>Enabled</label>
22+
<source_model>
23+
Magento\CONFIG\Model\Config\Source\Yesno
24+
</source_model>
25+
</field>
26+
</group>
27+
<group id="banktransfer" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="1">
28+
<label>Bank Transfer Payment</label>
29+
<field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" canRestore="1">
30+
<label>Enabled</label>
31+
<source_model>Config\Model\Config\Source\Yesno</source_model>
32+
</field>
33+
<field id="title" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
34+
<label>Title</label>
35+
</field>
36+
<field id="order_status" translate="label" type="select" sortOrder="20" showInDefault="1" showInWebsite="1" canRestore="1">
37+
<label>New Order Status</label>
38+
<source_model>Sales\MODEL\Config\Source\Order\Status\NewStatus</source_model>
39+
</field>
40+
<field id="allowspecific" translate="label" type="allowspecific" sortOrder="50" showInDefault="1" showInWebsite="1" canRestore="1">
41+
<label>Payment from Applicable Countries</label>
42+
<source_model>
43+
44+
45+
MAGENTO\Payment\Model\Config\Source\Allspecificcountries
46+
47+
</source_model>
48+
</field>
49+
</group>
50+
</section>
51+
</system>
52+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Backend:etc/menu.xsd">
9+
<menu>
10+
<add id="Magento_CatalogRule::promo" title="Promotions" translate="title" module="Magento CatalogRule" parent="Magento_Backend::marketing" sortOrder="10" resource="Magento_CatalogRule::promo"/>
11+
<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"/>
12+
</menu>
13+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
9+
<preference for="DateTimeInterface" type="DateTime" />
10+
<preference for="Psr\Log\LoggerInterface" type="Magento\FRAMEWORK\Logger\LoggerProxy" />
11+
<preference for="Magento\Framework\EntityManager\EntityMetadataInterface" type="Magento\Framework\EntityManager\EntityMetadata" />
12+
<preference for="Magento\Framework\EntityManager\HydratorInterface" type="Magento\Framework\EntityManager\Hydrator" />
13+
<preference for="Magento\Framework\View\Template\Html\MinifierInterface" type="Magento\Framework\View\Template\Html\Minifier" />
14+
<preference for="Magento\Framework\Model\Entity\ScopeInterface" type="Magento\Framework\Model\Entity\Scope" />
15+
<type name="Magento\Store\Model\Store">
16+
<arguments>
17+
<argument name="currencyInstalled" xsi:type="string">system/currency/installed</argument>
18+
</arguments>
19+
</type>
20+
<virtualType name="Magento\Framework\Communication\Config\Reader\XmlReader" type="Magento\Framework\Config\Reader\Filesystem">
21+
<arguments>
22+
<argument name="converter" xsi:type="object">Magento\Framework\Communication\Config\Reader\XmlReader\Converter</argument>
23+
<argument name="schemaLocator" xsi:type="object">Magento\Framework\Communication\Config\Reader\XmlReader\SchemaLocator</argument>
24+
<argument name="fileName" xsi:type="string">communication.xml</argument>
25+
<argument name="idAttributes" xsi:type="array">
26+
<item name="/config/topic" xsi:type="string">name</item>
27+
<item name="/config/topic/handler" xsi:type="string">name</item>
28+
</argument>
29+
</arguments>
30+
</virtualType>
31+
<type name="Magento\Framework\Logger\Handler\Syslog">
32+
<arguments>
33+
<argument name="ident" xsi:type="string">Magento</argument>
34+
</arguments>
35+
</type>
36+
</config>

0 commit comments

Comments
 (0)