Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
66 changes: 50 additions & 16 deletions src/FileUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ public static function upgradePhpFiles(): void
$fileStr = str_replace('use Symfony\Component\HttpFoundation\Request;', 'use FacturaScripts\Core\Request;', $fileStr);
$fileStr = str_replace('use Symfony\Component\HttpFoundation\Response;', 'use FacturaScripts\Core\Response;', $fileStr);

if (strpos($fileStr, 'HttpFoundation\RedirectResponse') !== false && strpos($fileStr, 'FacturaScripts\Core\Response') !== false) {
if (strpos($fileStr, 'HttpFoundation\RedirectResponse') !== false && strpos($fileStr, 'use FacturaScripts\Core\Response;') !== false) {
$fileStr = str_replace('use Symfony\Component\HttpFoundation\RedirectResponse;', '', $fileStr);
} else {
$fileStr = str_replace('use Symfony\Component\HttpFoundation\RedirectResponse;', 'use FacturaScripts\Core\Response;', $fileStr);
Expand All @@ -304,7 +304,12 @@ public static function upgradePhpFiles(): void
}

// reemplazamos loadFromCode('', $where) por loadWhere($where)
$fileStr = preg_replace('/->loadFromCode\(\s*[\'\"]\s*\'\s*,\s*([^)]+)\)/', '->loadWhere($1)', $fileStr);
$fileStr = self::replaceMethodCall($fileStr, '->loadFromCode(', function (array $args) {
if (count($args) >= 2 && in_array(trim($args[0]), ["''", '""'], true)) {
return '->loadWhere(' . implode(', ', array_slice($args, 1)) . ')';
}
return '->loadFromCode(' . implode(', ', $args) . ')';
});

// reemplazamos loadFromCode($code) por load($code)
$fileStr = str_replace('->loadFromCode($', '->load($', $fileStr);
Expand Down Expand Up @@ -359,12 +364,12 @@ public static function upgradePhpFiles(): void
$fileStr = preg_replace('/\$this->previousData\[([^\]]+)\]/', '$this->getOriginal($1)', $fileStr);

// reemplazamos llamadas al método all() con 3 parámetros añadiendo el 4º parámetro (50)
// añade ", 50" solo cuando hay exactamente 3 argumentos
$fileStr = preg_replace(
'/(->|::)all\(\s*([^,()]+)\s*,\s*([^,()]+)\s*,\s*([^,()]+)\s*\)/',
'$1all($2, $3, $4, 50)',
$fileStr
);
foreach (['->all(', '::all('] as $allNeedle) {
$fileStr = self::replaceMethodCall($fileStr, $allNeedle, function (array $args) use ($allNeedle) {
$suffix = count($args) === 3 ? ', 50' : '';
return substr($allNeedle, 0, 2) . 'all(' . implode(', ', $args) . $suffix . ')';
});
}

// reemplazamos protected function onChange($field) por protected function onChange(string $field): bool
$fileStr = str_replace('protected function onChange($field)', 'protected function onChange(string $field): bool', $fileStr);
Expand Down Expand Up @@ -622,6 +627,23 @@ private static function convertDataBaseWhereCalls(string $fileStr): string
return $result . substr($fileStr, $pos);
}

private static function replaceMethodCall(string $fileStr, string $needle, callable $fn): string
{
$result = '';
$pos = 0;

while (($start = strpos($fileStr, $needle, $pos)) !== false) {
$result .= substr($fileStr, $pos, $start - $pos);
$openParen = $start + strlen($needle) - 1;
$closeParen = self::findClosingParen($fileStr, $openParen);
$argsStr = substr($fileStr, $openParen + 1, $closeParen - $openParen - 1);
$result .= $fn(self::parseCallArguments($argsStr));
$pos = $closeParen + 1;
}

return $result . substr($fileStr, $pos);
}

/**
* Localiza el paréntesis de cierre balanceado a partir de la posición del paréntesis de apertura.
* Ignora correctamente los paréntesis que aparecen dentro de cadenas de texto.
Expand All @@ -633,12 +655,19 @@ private static function findClosingParen(string $code, int $openAt): int
$inDouble = false;

for ($i = $openAt, $len = strlen($code); $i < $len; $i++) {
$ch = $code[$i];
$prev = $i > 0 ? $code[$i - 1] : '';
$ch = $code[$i];

//contamos el numero de barras consecutivas
$count = 0;
for ($j = $i - 1; $j >= 0 && $code[$j] === '\\'; $j--) {
$count++;
}
// si el número de barras es impar, el carácter actual está escapado
$escaped = ($count % 2) === 1;

if ($ch === "'" && !$inDouble && $prev !== '\\') {
if ($ch === "'" && !$inDouble && !$escaped) {
$inSingle = !$inSingle;
} elseif ($ch === '"' && !$inSingle && $prev !== '\\') {
} elseif ($ch === '"' && !$inSingle && !$escaped) {
$inDouble = !$inDouble;
} elseif (!$inSingle && !$inDouble) {
if ($ch === '(' || $ch === '[') {
Expand Down Expand Up @@ -719,14 +748,19 @@ private static function parseCallArguments(string $str): array
$inDouble = false; // cursor dentro de una cadena con comillas dobles

for ($i = 0, $len = strlen($str); $i < $len; $i++) {
$ch = $str[$i];
$prev = $i > 0 ? $str[$i - 1] : '';
$ch = $str[$i];

$bsCount = 0;
for ($j = $i - 1; $j >= 0 && $str[$j] === '\\'; $j--) {
$bsCount++;
}
$escaped = ($bsCount % 2) === 1;

if ($ch === "'" && !$inDouble && $prev !== '\\') {
if ($ch === "'" && !$inDouble && !$escaped) {
// apertura/cierre de cadena simple; el carácter pertenece al argumento
$inSingle = !$inSingle;
$current .= $ch;
} elseif ($ch === '"' && !$inSingle && $prev !== '\\') {
} elseif ($ch === '"' && !$inSingle && !$escaped) {
// apertura/cierre de cadena doble; el carácter pertenece al argumento
$inDouble = !$inDouble;
$current .= $ch;
Expand Down
234 changes: 234 additions & 0 deletions tests/FileUpdaterUpgradeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,238 @@ public function testUpgradeSalesModSignature(): void
}
}
}

public function testUpgradeNestedCallArguments(): void
{
$originalDir = getcwd();
$sandbox = sys_get_temp_dir() . '/fsmaker_upgrade_' . uniqid('', true);

if (!mkdir($sandbox, 0755, true) && !is_dir($sandbox)) {
$this->fail('Unable to create temporary plugin directory.');
}

try {
chdir($sandbox);
Utils::setSilent(true);
Utils::setFolder($sandbox);

file_put_contents('facturascripts.ini', "[plugin]\nname = TestPlugin\n");
mkdir('Model', 0755, true);

$fixturePath = __DIR__ . '/SampleFiles/NestedParentheses.txt';
$targetPath = $sandbox . '/Model/TestModel.php';
if (!copy($fixturePath, $targetPath)) {
$this->fail('Unable to copy sample file.');
}

FileUpdater::upgradePhpFiles();

$updated = file_get_contents($targetPath);

// loadFromCode con 1 argumento se convierte en loadWhere con array de Where
$this->assertStringNotContainsString('->loadFromCode(', $updated);
$this->assertStringContainsString(
'$this->loadWhere([Where::eq(\'id\', $this->getParentId())])',
$updated
);

// all() con 3 args cuyo último contiene '()' → debe añadir el 4º parámetro (50)
$this->assertStringContainsString(
'self::all([Where::eq(\'active\', 1)], null, $this->getOrder(), 50)',
$updated
);
} finally {
chdir($originalDir);
Utils::setSilent(false);

if (is_dir($sandbox)) {
$items = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($sandbox, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($items as $item) {
$path = $item->getPathname();
$item->isDir() ? rmdir($path) : unlink($path);
}
rmdir($sandbox);
}
}
}

public function testUpgradeDoubleBackslashBefore(): void
{
// número par de backslashes antes de comilla → la comilla cierra el string (no está escapada)
$originalDir = getcwd();
$sandbox = sys_get_temp_dir() . '/fsmaker_upgrade_' . uniqid('', true);

if (!mkdir($sandbox, 0755, true) && !is_dir($sandbox)) {
$this->fail('Unable to create temporary plugin directory.');
}

try {
chdir($sandbox);
Utils::setSilent(true);
Utils::setFolder($sandbox);

file_put_contents('facturascripts.ini', "[plugin]\nname = TestPlugin\n");
mkdir('Model', 0755, true);

$fixturePath = __DIR__ . '/SampleFiles/EscapedQuotes.txt';
$targetPath = $sandbox . '/Model/TestModel.php';
if (!copy($fixturePath, $targetPath)) {
$this->fail('Unable to copy sample file.');
}

FileUpdater::upgradePhpFiles();

$updated = file_get_contents($targetPath);

$this->assertStringNotContainsString('DataBaseWhere', $updated);
$this->assertSame(2, substr_count($updated, 'Where::eq('));

// 'C:\\' → dos backslashes + comilla que cierra: argumento único, no partido
$this->assertStringContainsString(
<<<'EOT'
Where::eq('ruta', 'C:\\')
EOT,
$updated
);

// "valor\\\\fin" → cuatro backslashes (dos literales) + comilla que cierra
$this->assertStringContainsString(
<<<'EOT'
Where::eq('desc', "valor\\\\fin")
EOT,
$updated
);
} finally {
chdir($originalDir);
Utils::setSilent(false);

if (is_dir($sandbox)) {
$items = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($sandbox, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($items as $item) {
$path = $item->getPathname();
$item->isDir() ? rmdir($path) : unlink($path);
}
rmdir($sandbox);
}
}
}

public function testUpgradeFilesIsIdempotent(): void
{

$cases = [
['fixture' => 'DeprecatedDataBaseWhere.txt', 'dir' => 'Model', 'file' => 'TestModel.php'],
['fixture' => 'HttpFoundationBoth.txt', 'dir' => 'Controller', 'file' => 'TestController.php'],
];

foreach ($cases as $case) {
$originalDir = getcwd();
$sandbox = sys_get_temp_dir() . '/fsmaker_upgrade_' . uniqid('', true);

if (!mkdir($sandbox, 0755, true) && !is_dir($sandbox)) {
$this->fail('Unable to create temporary plugin directory.');
}

try {
chdir($sandbox);
Utils::setSilent(true);
Utils::setFolder($sandbox);

file_put_contents('facturascripts.ini', "[plugin]\nname = TestPlugin\n");
mkdir($sandbox . '/' . $case['dir'], 0755, true);

$targetPath = $sandbox . '/' . $case['dir'] . '/' . $case['file'];
if (!copy(__DIR__ . '/SampleFiles/' . $case['fixture'], $targetPath)) {
$this->fail('Unable to copy sample file: ' . $case['fixture']);
}

// primera pasada
FileUpdater::upgradePhpFiles();
$afterFirstPass = file_get_contents($targetPath);

// segunda pasada: debe ser un no-op
FileUpdater::upgradePhpFiles();
$afterSecondPass = file_get_contents($targetPath);

$this->assertSame(
$afterFirstPass,
$afterSecondPass,
'Second upgrade pass must not change already-upgraded file: ' . $case['fixture']
);
} finally {
chdir($originalDir);
Utils::setSilent(false);

if (is_dir($sandbox)) {
$items = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($sandbox, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($items as $item) {
$path = $item->getPathname();
$item->isDir() ? rmdir($path) : unlink($path);
}
rmdir($sandbox);
}
}
}
}

public function testUpgradeRedirectResponse(): void
{
// ResponseFactory contiene el prefijo "Response": sin búsqueda exacta confundiría el import
$originalDir = getcwd();
$sandbox = sys_get_temp_dir() . '/fsmaker_upgrade_' . uniqid('', true);

if (!mkdir($sandbox, 0755, true) && !is_dir($sandbox)) {
$this->fail('Unable to create temporary plugin directory.');
}

try {
chdir($sandbox);
Utils::setSilent(true);
Utils::setFolder($sandbox);

file_put_contents('facturascripts.ini', "[plugin]\nname = TestPlugin\n");
mkdir('Controller', 0755, true);

$fixturePath = __DIR__ . '/SampleFiles/RedirectResponseWithFqcn.txt';
$targetPath = $sandbox . '/Controller/TestController.php';
if (!copy($fixturePath, $targetPath)) {
$this->fail('Unable to copy sample file.');
}

FileUpdater::upgradePhpFiles();

$updated = file_get_contents($targetPath);

// el import de Symfony desaparece y se añade el de FacturaScripts
$this->assertStringNotContainsString('use Symfony\Component\HttpFoundation\RedirectResponse;', $updated);
$this->assertStringContainsString('use FacturaScripts\Core\Response;', $updated);

// ResponseFactory no se toca
$this->assertStringContainsString('use FacturaScripts\Core\ResponseFactory;', $updated);
} finally {
chdir($originalDir);
Utils::setSilent(false);

if (is_dir($sandbox)) {
$items = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($sandbox, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($items as $item) {
$path = $item->getPathname();
$item->isDir() ? rmdir($path) : unlink($path);
}
rmdir($sandbox);
}
}
}
}
20 changes: 20 additions & 0 deletions tests/SampleFiles/EscapedQuotes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace FacturaScripts\Plugins\TestPlugin\Model;

use FacturaScripts\Core\Base\DataBase\DataBaseWhere;

class TestModel
{
public function find(): array
{
// 'C:\\' : dos backslashes seguidos de comilla → la comilla SÍ cierra el string
// (número par de backslashes → ninguno escapa la comilla)
$where = [new DataBaseWhere('ruta', 'C:\\', '=')];

// mismo patrón dentro de double-quoted string
$where[] = new DataBaseWhere('desc', "valor\\\\fin", '=');

return [];
}
}
21 changes: 21 additions & 0 deletions tests/SampleFiles/HttpFoundationBoth.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace FacturaScripts\Plugins\TestPlugin\Controller;

use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;

class TestController
{
public function run(): Response
{
$where = [new DataBaseWhere('active', 1)];

if (empty($where)) {
return new RedirectResponse('/error');
}

return new Response('ok');
}
}
Loading