Skip to content

Create phpcs static check for DiConfigTest #233

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

Merged
merged 12 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Magento2/Sniffs/Legacy/DiConfigSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Legacy;

use DOMDocument;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Sniffs\Sniff;

class DiConfigSniff implements Sniff
{
private const WARNING_CODE = 'FoundObsoleteAttribute';
private const ERROR_CODE = 'WrongXML';

private $xpathObsoleteElems = [
Copy link
Member

@sivaschenko sivaschenko Aug 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would renamve this property as it's not xpath anymore, also it would be good to add a phpdoc

Suggested change
private $xpathObsoleteElems = [
private $obsoleteDiNodes = [

'param',
'instance',
'array',
'item[@key]',
'value'
];

private $messages = [
'param' => 'The <param> node is obsolete. Instead, use the <argument name="..." xsi:type="...">',
'instance' => 'The <instance> node is obsolete. Instead, use the <argument name="..." xsi:type="object">',
'array' => 'The <array> node is obsolete. Instead, use the <argument name="..." xsi:type="array">',
'item[@key]' => 'The <item key="..."> node is obsolete. Instead, use the <item name="..." xsi:type="...">',
'value' => 'The <value> node is obsolete. Instead, provide the actual value as a text literal.'
];

public function register(): array
{
return [
T_INLINE_HTML
];
}

public function process(File $phpcsFile, $stackPtr): int
{
$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
);
}

foreach ($this->xpathObsoleteElems as $obsoleteElem) {
$found = $xml->xpath($obsoleteElem);
if ($found === true) {
$phpcsFile->addWarning(
$this->messages[$obsoleteElem],
$stackPtr,
self::WARNING_CODE
);
}
}
}

/**
* 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();
}
}
34 changes: 34 additions & 0 deletions Magento2/Tests/Legacy/DiConfigUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento2\Tests\Legacy;

use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;

class DiConfigUnitTest extends AbstractSniffUnitTest
{
/**
* @inheritdoc
*/
public function getErrorList(): array
{
return [];
}

/**
* @inheritdoc
*/
public function getWarningList(): array
{
return [
9 => 1,
10 => 1,
11 => 1,
12 => 1,
13 => 1,
15 => 1
];
}
}
19 changes: 19 additions & 0 deletions Magento2/Tests/Legacy/DiConfigUnitTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<!--
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<root>
<instance>
<param name="param1" new_attribute="2">
<array key="amounts">
<item key="item1" new_attribute="3">
<value new_attribute="4">scalar20</value>
</item>
<item>50.00</item>
</array>
</param>
</instance>
</root>
5 changes: 5 additions & 0 deletions Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.DiConfig">
<severity>8</severity>
<type>warning</type>
<include-pattern>*\/DiConfigUnitTest.xml$</include-pattern>
</rule>

<!-- Severity 7 warnings: General code issues. -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax">
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"require": {
"php": ">=7.3",
"squizlabs/php_codesniffer": "^3.6",
"webonyx/graphql-php": "^14.9"
"webonyx/graphql-php": "^14.9",
"ext-simplexml": "*",
"ext-dom": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.5.8"
Expand Down