Skip to content

Commit 3804f23

Browse files
committed
MC-40411: SVC false-positive: removing the leading slash from a class name in di.xml
1 parent c9485d2 commit 3804f23

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

src/Analyzer/DiXml/VirtualTypeAnalyzer.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,31 @@ private function triggerNodeChange(VirtualType $nodeBefore, VirtualType $nodeAft
125125

126126
foreach ($bcFieldBefore as $fieldName => $valueBefore) {
127127
$valueAfter = $bcFieldAfter[$fieldName];
128-
if ($valueBefore !== $valueAfter) {
128+
$changed = false;
129+
switch ($fieldName) {
130+
case 'type':
131+
$changed = $this->isTypeChanged($valueBefore, $valueAfter);
132+
break;
133+
default:
134+
$changed = $valueBefore !== $valueAfter;
135+
break;
136+
}
137+
if ($changed) {
129138
$operation = new VirtualTypeChanged($beforeFilePath, $fieldName);
130139
$this->report->add('di', $operation);
131140
}
132141
}
133142
}
143+
144+
/**
145+
* Trim leading backslashes and than compare types
146+
*
147+
* @param $typeBefore
148+
* @param $typeAfter
149+
* @return bool
150+
*/
151+
private function isTypeChanged($typeBefore, $typeAfter): bool
152+
{
153+
return ltrim(trim($typeBefore), "\\") !== ltrim(trim($typeAfter), "\\");
154+
}
134155
}

tests/Unit/Console/Command/CompareSourceCommandDiXmlTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,14 @@ public function changesDataProvider()
103103
],
104104
'Major change is detected.',
105105
],
106+
'removing-leading-slash-from-type' => [
107+
$pathToFixtures . '/removing-leading-slash-from-type/source-code-before',
108+
$pathToFixtures . '/removing-leading-slash-from-type/source-code-after',
109+
[
110+
'#Suggested semantic versioning change: NONE#',
111+
],
112+
'Patch change is detected.',
113+
],
106114
];
107115
}
108116
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
<virtualType name="testVirtualType" type="Path\To\Type">
10+
<arguments>
11+
<argument name="type" xsi:type="string">schema</argument>
12+
</arguments>
13+
</virtualType>
14+
15+
<preference for="Path\To\Interface" type="Path\To\Type" />
16+
17+
<type name="Path\To\Type">
18+
<arguments>
19+
<argument name="argumentName" xsi:type="array">
20+
<item name="itemName" xsi:type="const">Path\To\Type\Item</item>
21+
</argument>
22+
</arguments>
23+
</type>
24+
25+
<type name="Path\To\Type">
26+
<plugin name="pluginName" type="Path\To\Type\Plugin"/>
27+
</type>
28+
</config>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
<virtualType name="testVirtualType" type="\Path\To\Type">
10+
<arguments>
11+
<argument name="type" xsi:type="string">schema</argument>
12+
</arguments>
13+
</virtualType>
14+
15+
<preference for="\Path\To\Interface" type="\Path\To\Type" />
16+
17+
<type name="\Path\To\Type">
18+
<arguments>
19+
<argument name="argumentName" xsi:type="array">
20+
<item name="itemName" xsi:type="const">\Path\To\Type\Item</item>
21+
</argument>
22+
</arguments>
23+
</type>
24+
25+
<type name="\Path\To\Type">
26+
<plugin name="pluginName" type="\Path\To\Type\Plugin"/>
27+
</type>
28+
</config>

0 commit comments

Comments
 (0)