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
2 changes: 1 addition & 1 deletion src/xPDO/Om/xPDOGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public static function varExport($var, $indentLevel = 1) {
if ($char !== ' ') break;
$spaces++;
}
$output[] = str_repeat(' ', $indentLevel + 1) . str_repeat(' ', $spaces / 2) . substr($line, ($spaces ? $spaces + 1 : 0));
$output[] = str_repeat(' ', $indentLevel + 1) . str_repeat(' ', intdiv($spaces, 2)) . substr($line, ($spaces ? $spaces + 1 : 0));
}
}
else {
Expand Down
1 change: 1 addition & 0 deletions test/complete.phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<file>./xPDO/Test/Om/xPDOQueryHavingTest.php</file>
<file>./xPDO/Test/Om/xPDOQueryLimitTest.php</file>
<file>./xPDO/Test/Om/xPDOQuerySortByTest.php</file>
<file>./xPDO/Test/Om/xPDOGeneratorTest.php</file>
<file>./xPDO/Test/Cache/xPDOCacheManagerTest.php</file>
<file>./xPDO/Test/Cache/xPDOCacheDbTest.php</file>
<file>./xPDO/Test/Compression/xPDOZipTest.php</file>
Expand Down
1 change: 1 addition & 0 deletions test/mysql.phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<file>./xPDO/Test/Om/xPDOQueryHavingTest.php</file>
<file>./xPDO/Test/Om/xPDOQueryLimitTest.php</file>
<file>./xPDO/Test/Om/xPDOQuerySortByTest.php</file>
<file>./xPDO/Test/Om/xPDOGeneratorTest.php</file>
<file>./xPDO/Test/Cache/xPDOCacheManagerTest.php</file>
<file>./xPDO/Test/Cache/xPDOCacheDbTest.php</file>
<file>./xPDO/Test/Compression/xPDOZipTest.php</file>
Expand Down
1 change: 1 addition & 0 deletions test/pgsql.phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<file>./xPDO/Test/Om/xPDOQueryHavingTest.php</file>
<file>./xPDO/Test/Om/xPDOQueryLimitTest.php</file>
<file>./xPDO/Test/Om/xPDOQuerySortByTest.php</file>
<file>./xPDO/Test/Om/xPDOGeneratorTest.php</file>
<file>./xPDO/Test/Cache/xPDOCacheManagerTest.php</file>
<file>./xPDO/Test/Cache/xPDOCacheDbTest.php</file>
<file>./xPDO/Test/Compression/xPDOZipTest.php</file>
Expand Down
1 change: 1 addition & 0 deletions test/sqlite.phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<file>./xPDO/Test/Om/xPDOQueryHavingTest.php</file>
<file>./xPDO/Test/Om/xPDOQueryLimitTest.php</file>
<file>./xPDO/Test/Om/xPDOQuerySortByTest.php</file>
<file>./xPDO/Test/Om/xPDOGeneratorTest.php</file>
<file>./xPDO/Test/Cache/xPDOCacheManagerTest.php</file>
<file>./xPDO/Test/Cache/xPDOCacheDbTest.php</file>
<file>./xPDO/Test/Compression/xPDOZipTest.php</file>
Expand Down
76 changes: 76 additions & 0 deletions test/xPDO/Test/Om/xPDOGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
/**
* This file is part of the xPDO package.
*
* Copyright (c) Jason Coward <jason@opengeek.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace xPDO\Test\Om;

use PHPUnit\Framework\TestCase;
use xPDO\Om\xPDOGenerator;

/**
* Tests related to xPDOGenerator utility methods.
*
* @package xPDO\Test\Om
*/
class xPDOGeneratorTest extends TestCase
{
/**
* Regression test for PHP 8.1 deprecation: str_repeat() received a float
* when $spaces is odd because $spaces / 2 produces a float.
*
* var_export() of a nested array emits 2-space-indented lines. The space
* counting loop in varExport() uses next() which skips index 0, so a line
* with 2 leading spaces yields $spaces = 1, and 1 / 2 = 0.5 (float).
* The fix replaces $spaces / 2 with intdiv($spaces, 2).
*
* @see xPDOGenerator::varExport()
*/
public function testVarExportNestedArrayOddSpacesProducesNoDeprecation(): void
{
// A nested array guarantees var_export() will emit lines with 2 leading
// spaces (1 level of PHP's native 2-space indentation), which causes
// $spaces = 1 (odd) inside varExport() — the float trigger.
$input = ['key' => ['nested' => 'value']];

$deprecationEmitted = false;
set_error_handler(function (int $errno, string $errstr) use (&$deprecationEmitted): bool {
if ($errno === E_DEPRECATED && str_contains($errstr, 'float')) {
$deprecationEmitted = true;
}
return true;
}, E_DEPRECATED);

$result = xPDOGenerator::varExport($input, 1);

restore_error_handler();

$this->assertFalse(
$deprecationEmitted,
'xPDOGenerator::varExport() emitted a float-to-int deprecation notice — str_repeat() received a float from $spaces / 2.'
);

// Also assert the output is a non-empty string to confirm the method ran.
$this->assertIsString($result);
$this->assertNotEmpty($result);
}

/**
* Verify varExport() produces correctly indented output for a nested array
* when $spaces is odd (the same code path as the float bug).
*/
public function testVarExportNestedArrayReturnsString(): void
{
$input = ['a' => 1, 'b' => ['c' => 2]];
$result = xPDOGenerator::varExport($input, 1);

$this->assertIsString($result);
// The result must start with the array opening token from var_export.
$this->assertStringStartsWith('array (', $result);
}
}
Loading