Skip to content

Fix str_repeat() float argument in xPDOGenerator::varExport()#274

Open
opengeek wants to merge 1 commit intomodxcms:3.xfrom
opengeek:fix/strrepeat-float-varexport
Open

Fix str_repeat() float argument in xPDOGenerator::varExport()#274
opengeek wants to merge 1 commit intomodxcms:3.xfrom
opengeek:fix/strrepeat-float-varexport

Conversation

@opengeek
Copy link
Copy Markdown
Member

What changed and why

xPDOGenerator::varExport() used integer division via / to compute the indent level passed to str_repeat(). When the internal $spaces counter is odd (which happens at any nesting depth where var_export() produces a line with an odd number of leading spaces), the expression $spaces / 2 yields a float. PHP 8.1 introduced a deprecation notice for implicit float-to-int casts, so every nested array exported through the generator emitted:

Deprecated: Implicit conversion from float 0.5 to int loses precision

The fix replaces $spaces / 2 with intdiv($spaces, 2), which performs integer division without producing a float. intdiv() has been available since PHP 7.0, well within the project minimum.

Files and methods changed

  • src/xPDO/Om/xPDOGenerator.phpvarExport() (static), line 129: $spaces / 2 replaced with intdiv($spaces, 2).
  • test/xPDO/Test/Om/xPDOGeneratorTest.php — new test file (2 test methods).
  • test/sqlite.phpunit.xml, test/mysql.phpunit.xml, test/pgsql.phpunit.xml, test/complete.phpunit.xml — new test file registered in the Complete testsuite block of each config.

Cross-driver impact

xPDOGenerator is a code-generation utility invoked during schema-to-model compilation (i.e., when a developer runs xPDO::loadPackage() or the generator CLI to produce PHP class files from an XML schema). It is not involved at query time or in the ORM runtime path. The deprecation therefore surfaces during development and build workflows, not in production request handling. All four driver configs (MySQL, PostgreSQL, SQLite, complete) were updated because the generator is driver-agnostic — the affected method has no driver-specific branches.

Test coverage

New file: test/xPDO/Test/Om/xPDOGeneratorTest.php

  • testVarExportNestedArrayOddSpacesProducesNoDeprecation — installs an E_DEPRECATED error handler before calling varExport() with a two-level nested array (which guarantees an odd $spaces value), then asserts no float deprecation message was captured. This test failed before the fix and passes after.
  • testVarExportNestedArrayReturnsString — asserts that varExport() returns a non-empty string beginning with array ( for the same nested input, confirming output correctness is not affected by the fix.

Full suite result after fix (sqlite config): Tests: 432, Assertions: 599, Skipped: 1 — zero failures.

Breaking change assessment

This is a pure internal implementation fix. varExport() is a protected static method with no public API contract. The method signature is unchanged. Return values are identical — intdiv($n, 2) produces the same integer result as (int)($n / 2) for all non-negative integers. No callers outside the class are affected. This change is safe to include in a patch release.

Contributors

No external contributor prompted this fix. It was identified internally during 3.x release preparation while auditing PHP 8.1 deprecation notices across the codebase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants