Skip to content

Commit 8dd6436

Browse files
committed
Merge remote-tracking branch 'origin/AC-9619' into Hammer-SVC-False-Positive-Build
2 parents dfabb3e + 9ab6e5d commit 8dd6436

File tree

2 files changed

+156
-2
lines changed

2 files changed

+156
-2
lines changed

src/Analyzer/DiXml/VirtualTypeAnalyzer.php

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\SemanticVersionChecker\Node\VirtualType;
1414
use Magento\SemanticVersionChecker\Operation\DiXml\VirtualTypeChanged;
1515
use Magento\SemanticVersionChecker\Operation\DiXml\VirtualTypeRemoved;
16+
use Magento\SemanticVersionChecker\Operation\DiXml\VirtualTypeToTypeChanged;
1617
use Magento\SemanticVersionChecker\Registry\XmlRegistry;
1718
use PHPSemVerChecker\Registry\Registry;
1819
use PHPSemVerChecker\Report\Report;
@@ -60,7 +61,7 @@ public function analyze($registryBefore, $registryAfter)
6061
foreach ($moduleNodes as $name => $nodeBefore) {
6162
// search nodesAfter the by name
6263
$nodeAfter = $nodesAfter[$moduleName][$name] ?? false;
63-
64+
$fileAfter = $registryAfter->mapping[XmlRegistry::NODES_KEY][$moduleName];
6465
if ($nodeAfter !== false && $nodeBefore !== $nodeAfter) {
6566
/* @var VirtualType $nodeAfter */
6667
$this->triggerNodeChange($nodeBefore, $nodeAfter, $fileBefore);
@@ -78,14 +79,68 @@ public function analyze($registryBefore, $registryAfter)
7879
}
7980
}
8081

81-
$operation = new VirtualTypeRemoved($fileBefore, $name);
82+
$finalPath = $this->convertClassNameToFilePath($fileAfter, $name, '.php');
83+
84+
if (file_exists($finalPath)) {
85+
$operation = new VirtualTypeToTypeChanged($fileBefore, $name);
86+
} else {
87+
$operation = new VirtualTypeRemoved($fileBefore, $name);
88+
}
8289
$this->report->add('di', $operation);
8390
}
8491
}
8592

8693
return $this->report;
8794
}
8895

96+
/**
97+
* Method to convert class name to file path
98+
*
99+
* @param string $filePath
100+
* @param string $className
101+
* @param string $extraString
102+
* @return string
103+
*/
104+
private function convertClassNameToFilePath($filePath, $className, $extraString = ''): string
105+
{
106+
// Split the initial file path to get the base directory.
107+
$parts = explode('/', $filePath);
108+
$classParts = explode('\\', $className);
109+
110+
// Find the common part between the file path and class name.
111+
$baseDirParts = [];
112+
foreach ($parts as $part) {
113+
$baseDirParts[] = $part;
114+
115+
if (in_array($part, $classParts)) {
116+
break;
117+
}
118+
}
119+
120+
// Reconstruct the base directory path.
121+
$baseDir = implode('/', $baseDirParts);
122+
123+
// Replace namespace separators with directory separators in the class name.
124+
$classFilePath = str_replace('\\', '/', $className);
125+
126+
$position = strpos($classFilePath, "/");
127+
128+
if ($position !== false) {
129+
$classFilePath = substr($classFilePath, $position);
130+
}
131+
132+
// Combine the base directory and class file path.
133+
$fullPath = rtrim($baseDir, '/') . $classFilePath;
134+
135+
136+
// Append the extra string if provided.
137+
if ($extraString) {
138+
$fullPath .= $extraString;
139+
}
140+
return $fullPath;
141+
}
142+
143+
89144
/**
90145
* Return a filtered node list from type {@link VirtualType}
91146
*
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Magento\SemanticVersionChecker\Operation\DiXml;
11+
12+
use PHPSemVerChecker\Operation\Operation;
13+
use PHPSemVerChecker\SemanticVersioning\Level;
14+
15+
/**
16+
* When a virtual type was changed.
17+
*/
18+
class VirtualTypeToTypeChanged extends Operation
19+
{
20+
/**
21+
* Error codes.
22+
*
23+
* @var array
24+
*/
25+
protected $code = 'M201';
26+
27+
/**
28+
* Change level.
29+
*
30+
* @var int
31+
*/
32+
protected $level = Level::PATCH;
33+
34+
/**
35+
* Operation message.
36+
*
37+
* @var string
38+
*/
39+
protected $reason = 'Virtual Type was changed to type';
40+
/**
41+
* File path before changes.
42+
*
43+
* @var string
44+
*/
45+
protected $fileBefore;
46+
47+
/**
48+
* Property context before changes.
49+
*
50+
* @var \PhpParser\Node\Stmt
51+
*/
52+
protected $contextBefore;
53+
54+
/**
55+
* Property before changes.
56+
*
57+
* @var \PhpParser\Node\Stmt\Property
58+
*/
59+
protected $propertyBefore;
60+
61+
/**
62+
* @param string $fileBefore
63+
* @param string $target
64+
*/
65+
public function __construct($fileBefore, $target)
66+
{
67+
$this->fileBefore = $fileBefore;
68+
$this->target = $target;
69+
}
70+
71+
/**
72+
* Returns file path before changes.
73+
*
74+
* @return string
75+
*/
76+
public function getLocation(): string
77+
{
78+
return $this->fileBefore;
79+
}
80+
81+
/**
82+
* Returns line position of existed property.
83+
*
84+
* @return int
85+
*/
86+
public function getLine(): int
87+
{
88+
return 0;
89+
}
90+
/**
91+
* Get level.
92+
*
93+
* @return mixed
94+
*/
95+
public function getLevel(): int
96+
{
97+
return $this->level;
98+
}
99+
}

0 commit comments

Comments
 (0)