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
54 changes: 54 additions & 0 deletions Magento2/Sniffs/Legacy/DiConfigSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento2\Sniffs\Legacy;

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

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

/**
* @var string[] Associative array containing the obsolete nodes and the message to display when they are found.
*/
private $obsoleteDiNodes = [
'<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.'
];

/**
* @inheritDoc
*/
public function register(): array
{
return [
T_INLINE_HTML
];
}

/**
* @inheritDoc
*/
public function process(File $phpcsFile, $stackPtr)
{
$lineContent = $phpcsFile->getTokensAsString($stackPtr, 1);

foreach ($this->obsoleteDiNodes as $element => $message) {
if (strpos($lineContent, $element) !== false) {
$phpcsFile->addWarning(
$message,
$stackPtr,
self::WARNING_CODE
);
}
}
}
}
33 changes: 33 additions & 0 deletions Magento2/Tests/Legacy/DiConfigUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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 [
12 => 1,
16 => 1,
17 => 1,
18 => 1,
19 => 1
];
}
}
23 changes: 23 additions & 0 deletions Magento2/Tests/Legacy/DiConfigUnitTest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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">
<type>
<arguments>
<argument name="prices" xsi:type="array">
<item key="regular_price" xsi:type="string">Magento\Catalog\Pricing\Price\RegularPrice</item>
</argument>
</arguments>
</type>
<instance>
<array key="params">
<param name="default_value" new_attribute="0">
<value new_attribute="5">scalar5</value>
</param>
</array>
</instance>
</config>
8 changes: 7 additions & 1 deletion Magento2/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<description>Magento Coding Standard</description>

<!-- File extensions to be checked. -->
<arg name="extensions" value="php,phtml,graphqls/GraphQL,less/CSS,html/PHP"/>
<arg name="extensions" value="php,phtml,graphqls/GraphQL,less/CSS,html/PHP,xml"/>

<!-- Severity 10 errors: Critical code issues. -->
<rule ref="Generic.Functions.CallTimePassByReference">
Expand Down Expand Up @@ -223,6 +223,11 @@
<severity>8</severity>
<type>warning</type>
</rule>
<rule ref="Magento2.Legacy.DiConfig">
<include-pattern>*\/di.xml$</include-pattern>
<severity>8</severity>
<type>warning</type>
</rule>

<!-- Severity 7 warnings: General code issues. -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax">
Expand Down Expand Up @@ -258,6 +263,7 @@
<type>warning</type>
</rule>
<rule ref="Generic.PHP.DisallowShortOpenTag">
<exclude-pattern>*\.xml$</exclude-pattern>
<severity>7</severity>
<type>warning</type>
</rule>
Expand Down