Skip to content

Conversation

@martin-georgiev
Copy link
Owner

@martin-georgiev martin-georgiev commented Mar 25, 2025

Changes to type handling

This PR significantly improves how numeric types and booleans are preserved when transforming data between PostgreSQL arrays and PHP. Previously, type information could be lost during transformation, resulting in unexpected behavior when retrieving data.

Numbers handling

Before:

  • Numbers could lose their original type during transformation
  • Float values might be converted to integers (e.g., 1.01)
  • String representation of numbers could be converted to actual numbers

After:

  • Type information is carefully preserved in both directions
  • Integers stay as integers: 11
  • Floats stay as floats: 1.51.5
  • Numeric strings stay as strings: '1.0''1.0'

Examples

Integer/float preservation

// Before
$phpArray = [1, 2.5, '3.0'];
// After transformation back from PostgreSQL:
// [1, 2, 3] (types and precision could be lost)

// After
$phpArray = [1, 2.5, '3.0']; 
// After transformation back from PostgreSQL:
// [1, 2.5, '3.0'] (original types preserved)

Boolean

// Before
$phpArray = [true, false];
// Might be inconsistently handled

// After
$phpArray = [true, false];
// Properly converted to PostgreSQL 'true'/'false' and back
// Returns [true, false] with correct boolean types

Large integers and scientific notation

// Before
$phpArray = ['9223372036854775808', '1.23e5'];
// After transformation:
// Potential loss of precision or conversion to different types

// After
$phpArray = ['9223372036854775808', '1.23e5'];
// After transformation:
// ['9223372036854775808', '1.23e5'] (preserved exactly)

Summary by CodeRabbit

  • Refactor

    • Streamlined the conversion process between PostgreSQL text arrays and PHP arrays to enhance reliability and error handling.
    • Improved integer validation logic for cleaner, more maintainable data processing.
    • Introduced a new utility class for transforming data between PostgreSQL text arrays and PHP arrays.
  • Tests

    • Expanded test coverage to include additional numeric formats such as floats, scientific notation, and very large numbers.
    • Reorganized test cases for clearer behavior definitions and robust transformation validation.
    • Updated return type annotations for better clarity in test case outputs.
    • Added new tests for exception handling and resource cleanup.
    • Introduced a comprehensive test suite for the new ArrayDataTransformer class.

@coderabbitai
Copy link

coderabbitai bot commented Mar 25, 2025

Walkthrough

This pull request refactors code related to array transformations. In the Doctrine DBAL types, a redundant string conversion is removed by storing the string representation in a variable, while in the DataStructure class, conversion methods for PostgreSQL arrays are refactored with added helper methods, improved error handling, and modular logic. A new ArrayDataTransformer class is introduced for handling these transformations. Test cases have been updated accordingly to cover additional numeric formats and mixed content.

Changes

File(s) Change Summary
src/MartinGeorgiev/Doctrine/DBAL/Types/BaseIntegerArray.php
tests/MartinGeorgiev/Doctrine/DBAL/Types/TextArrayTest.php
Refactored integer validation by storing the string conversion once to reduce redundancy. Test cases updated to include float and string elements in array transformation.
src/MartinGeorgiev/Utils/DataStructure.php
tests/MartinGeorgiev/Utils/DataStructureTest.php
Overhauled methods for converting PostgreSQL arrays to PHP arrays and vice versa. Introduced new helper methods (formatValue, isNumericSimple, unescapeString), improved error handling, and expanded tests for various numeric representations.
src/MartinGeorgiev/Doctrine/DBAL/Types/TextArray.php Updated TextArray class to use ArrayDataTransformer for array transformations instead of DataStructure.
src/MartinGeorgiev/Utils/ArrayDataTransformer.php Introduced new ArrayDataTransformer class with methods for transforming PostgreSQL text arrays to PHP arrays and vice versa, including error handling and value formatting.
tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php Added new test class for ArrayDataTransformer with comprehensive tests for transformation methods and error handling.

Sequence Diagram(s)

Possibly related PRs

Poem

In code meadows I hop with glee,
Transforming arrays as light as can be.
Refactored logic, so neat and clear,
A rabbit's cheer rings far and near.
Bugs scurry off while the tests shine bright,
Hoppy code, under the moonlight!
🐇💻✨

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/MartinGeorgiev/Utils/DataStructure.php (1)

109-124: Numeric detection logic

The approach of stripping 'e' for scientific notation works for simpler exponent forms like 1.23e4. However, consider extending coverage to handle plus signs (e.g., 1.23e+4) if needed.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6332bc6 and f8b99a4.

📒 Files selected for processing (4)
  • src/MartinGeorgiev/Doctrine/DBAL/Types/BaseIntegerArray.php (1 hunks)
  • src/MartinGeorgiev/Utils/DataStructure.php (1 hunks)
  • tests/MartinGeorgiev/Doctrine/DBAL/Types/TextArrayTest.php (2 hunks)
  • tests/MartinGeorgiev/Utils/DataStructureTest.php (2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
src/MartinGeorgiev/Doctrine/DBAL/Types/BaseIntegerArray.php (3)
src/MartinGeorgiev/Doctrine/DBAL/Types/BigIntArray.php (2)
  • getMinValue (22-25)
  • getMaxValue (27-30)
src/MartinGeorgiev/Doctrine/DBAL/Types/IntegerArray.php (2)
  • getMinValue (22-25)
  • getMaxValue (27-30)
src/MartinGeorgiev/Doctrine/DBAL/Types/SmallIntArray.php (2)
  • getMinValue (22-25)
  • getMaxValue (27-30)
🪛 GitHub Check: PHP 8.4 + Doctrine ORM 2.18 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 2.18 + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM latest + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 3.0 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.14 + Doctrine Lexer 1.2
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM latest + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 2.18 + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM latest + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM latest + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM latest + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 2.14 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 3.0 + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 2.18 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM latest + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 2.18 + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 2.14 + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 2.14 + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 2.14 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 3.0 + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 3.0 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 2.18 + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM latest + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM latest + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 2.18 + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 3.0 + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 3.0 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 2.18 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 3.0 + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 2.14 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 2.14 + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 2.18 + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM latest + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 3.0 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.18 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM latest + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.14 + Doctrine Lexer latest
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.18 + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM latest + Doctrine Lexer 3.0
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM latest + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.14 + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.18 + Doctrine Lexer 2.1
src/MartinGeorgiev/Utils/DataStructure.php

[failure] 88-88:
Cannot cast mixed to string.


[failure] 49-49:
Parameter #2 $array of function array_map expects array, mixed given.

🔇 Additional comments (21)
tests/MartinGeorgiev/Doctrine/DBAL/Types/TextArrayTest.php (2)

77-78: Increased coverage with mixed numeric data

Adding both a float (3.4) and a string float ('5.6') helps ensure transformations handle different numeric representations. Great addition to test coverage!


89-89: Consistency with new numeric items

The updated PostgreSQL value string correctly reflects the addition of 3.4 and "5.6". This maintains consistency between the test input and expected output.

src/MartinGeorgiev/Doctrine/DBAL/Types/BaseIntegerArray.php (1)

50-53: Improved readability by extracting string value

Defining $stringValue and conducting the numeric checks against it promotes clarity. This helps avoid repeated casts and ensures the code is more maintainable.

tests/MartinGeorgiev/Utils/DataStructureTest.php (12)

47-55: Preserving string integers

Enforcing the distinction between string integers and numeric integers ensures correct data typing. This test scenario nicely highlights that behavior.


56-62: Checking numeric integers

This case validates integer conversions without quotes. Good coverage for purely numeric inputs.


65-65: Testing decimal numbers as strings

Preserving decimals as strings tests the difference from true floats. This clarifies behavior for textual float representations.

Also applies to: 72-73


74-80: Validation of decimal float handling

Including these decimal floats validates that they are recognized as numeric and not quoted.


83-83: Documenting special characters

Renaming or clarifying this data set helps in reading the test scenario. It's important to validate transformation with special characters.


93-93: Covering empty string edge cases

This test ensures that empty strings do not get misinterpreted as null values.


100-100: Validating empty array handling

Confirming the mapping of an empty PHP array to {} in Postgres is crucial for single-dimension arrays.


104-107: Handling simple scientific notation in strings

Testing scientific notation ensures the transformation logic can handle exponent forms. This is a valuable addition to numeric coverage.


108-111: Extending coverage to negative exponents

Negative exponents can be trickier to parse, so it's great to see them tested here.


112-115: Distinguishing whole floats from integers

Demonstrating how floats with trailing zeros are stored helps confirm the precise behavior for numeric transformations.


116-123: Testing extremely large integers

This scenario highlights how values outside PHP’s integer range are preserved as strings, preventing overflow or unexpected behavior.


124-135: Comprehensive test for numeric format variations

Combining floats, strings, scientific notation, and large integers in one array solidifies confidence in the transformation logic across multiple numeric representations.

src/MartinGeorgiev/Utils/DataStructure.php (6)

16-16: Introduction of descriptive constants

Using class constants for '{}' and 'null' clarifies intent and eliminates magic strings across the code.

Also applies to: 18-18


26-29: Refined logic for single-dimension PostgreSQL arrays

  1. Checking for empty or 'null' inputs ensures an empty array is returned appropriately.
  2. Early detection of multi-dimensional arrays via str_contains works for simple bracket scanning.
  3. Converting to JSON and then decoding is a concise approach, though it depends on valid quoting in the input.
  4. Mapping strings with unescapeString() finalizes the array items neatly.

Overall, a neat and maintainable solution!

Also applies to: 32-34, 36-38, 40-40, 42-45, 47-50


59-61: Simplifying the PHP array to Postgres text array transformation

  1. Returning '{}' for empty arrays aligns with PostgreSQL conventions.
  2. Rejecting nested arrays at an early stage enforces single-dimension usage.
  3. Mapping values with formatValue() centralizes the escaping logic, promoting consistency.

Also applies to: 63-65, 67-70


72-74: Clear docblock

Well-documented intent for the newly introduced formatValue method. This ensures developers understand its usage constraints.


75-107: Centralized value formatting for PostgreSQL arrays

This method handles null, numeric, or string inputs with distinct logic, ensuring consistent quoting and escaping. It's a solid improvement for maintainability and clarity.

🧰 Tools
🪛 GitHub Check: PHP 8.4 + Doctrine ORM 2.18 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 2.18 + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM latest + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 3.0 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.14 + Doctrine Lexer 1.2

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM latest + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 2.18 + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM latest + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM latest + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM latest + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 2.14 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 3.0 + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 2.18 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM latest + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 2.18 + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 2.14 + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 2.14 + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 2.14 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.4 + Doctrine ORM 3.0 + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 3.0 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.3 + Doctrine ORM 2.18 + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM latest + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM latest + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 2.18 + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 3.0 + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 3.0 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 2.18 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 3.0 + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 2.14 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 2.14 + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM 2.18 + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.2 + Doctrine ORM latest + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 3.0 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.18 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM latest + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.14 + Doctrine Lexer latest

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.18 + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM latest + Doctrine Lexer 3.0

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM latest + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.14 + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.

🪛 GitHub Check: PHP 8.1 + Doctrine ORM 2.18 + Doctrine Lexer 2.1

[failure] 88-88:
Cannot cast mixed to string.


126-129: String unescaping with placeholders

Replacing quotes and backslashes with placeholders, then restoring them, effectively avoids collisions with partial replacements. Clever technique for cleaning up escaped strings.

Also applies to: 131-142

coderabbitai[bot]
coderabbitai bot previously approved these changes Mar 25, 2025
@coveralls
Copy link

coveralls commented Mar 25, 2025

Coverage Status

coverage: 94.913% (-0.03%) from 94.944%
when pulling cd1ee09 on float-arrays
into 6332bc6 on main.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f8b99a4 and 02f4207.

📒 Files selected for processing (2)
  • src/MartinGeorgiev/Utils/DataStructure.php (1 hunks)
  • tests/MartinGeorgiev/Utils/DataStructureTest.php (2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
tests/MartinGeorgiev/Utils/DataStructureTest.php (9)
tests/MartinGeorgiev/Doctrine/DBAL/Types/TextArrayTest.php (1)
  • provideValidTransformations (62-93)
tests/MartinGeorgiev/Doctrine/DBAL/Types/JsonbArrayTest.php (1)
  • provideValidTransformations (60-91)
tests/MartinGeorgiev/Doctrine/DBAL/Types/JsonbTest.php (1)
  • provideValidTransformations (62-104)
tests/MartinGeorgiev/Doctrine/DBAL/Types/BaseArrayTest.php (1)
  • provideValidTransformations (65-81)
tests/MartinGeorgiev/Doctrine/DBAL/Types/BigIntArrayTest.php (1)
  • provideValidTransformations (37-49)
tests/MartinGeorgiev/Doctrine/DBAL/Types/IntegerArrayTest.php (1)
  • provideValidTransformations (40-52)
tests/MartinGeorgiev/Doctrine/DBAL/Types/BooleanArrayTest.php (1)
  • provideValidTransformations (71-90)
tests/MartinGeorgiev/Doctrine/DBAL/Types/SmallIntArrayTest.php (1)
  • provideValidTransformations (37-49)
tests/MartinGeorgiev/Doctrine/DBAL/Types/BaseIntegerArrayTestCase.php (1)
  • provideValidTransformations (66-66)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: PHP 8.4 + Doctrine ORM 2.18 + Doctrine Lexer 2.1
  • GitHub Check: PHP 8.3 + Doctrine ORM 3.0 + Doctrine Lexer 3.0
  • GitHub Check: PHP 8.3 + Doctrine ORM 2.14 + Doctrine Lexer latest
  • GitHub Check: sloth
🔇 Additional comments (16)
tests/MartinGeorgiev/Utils/DataStructureTest.php (11)

37-37: Improved type annotation clarity

The return type annotation has been updated to be more specific, using a strongly-typed array shape instead of a generic array. This provides better type safety and makes the expected return structure clearer.


42-50: LGTM: Clear test case naming with appropriate assertions

The test case has been renamed from a simple indexed array to a descriptive name "simple integer strings as strings are preserved as strings", making the test's purpose immediately clear. The expected PostgreSQL value properly includes quotes to preserve the string type.


51-59: LGTM: Integer type preservation test

This test case properly verifies that numeric integers in PHP are correctly transformed to unquoted values in PostgreSQL arrays.


60-68: LGTM: String decimal preservation test

Good test case that ensures decimal strings maintain their string type during transformation, with proper quoting in the PostgreSQL representation.


69-77: LGTM: Numeric float preservation test

This test case properly verifies that PHP float values are correctly transformed to unquoted decimal values in PostgreSQL arrays.


78-87: LGTM: Comprehensive mixed content test

The test case effectively validates handling of diverse content including special characters, escaping, and numeric values.


88-94: LGTM: Edge cases correctly handled

These test cases correctly verify the handling of empty strings and empty arrays, which are important edge cases.

Also applies to: 95-98


99-102: Good addition of scientific notation test cases

The addition of test cases for scientific notation with both positive and negative exponents is valuable. These cases ensure that scientific notation is preserved as strings during transformation, which is critical for numeric precision.

Also applies to: 103-106


107-110: LGTM: Whole floats preservation test

This test case correctly verifies that floating-point numbers with only zeros after the decimal point maintain their string representation, preserving the decimal nature of the values.


111-118: Important test case for large integers

Excellent addition of a test case for integers that exceed PHP_INT_MAX. This ensures that extremely large integers are correctly preserved as strings, preventing potential data loss during transformation.


119-130: Comprehensive mixed numeric formats test

This is a valuable test case that covers a wide range of numeric representations in a single array, including regular floats, float strings, scientific notation, whole floats, and large integers. It ensures that the type preservation works correctly across all these formats.

src/MartinGeorgiev/Utils/DataStructure.php (5)

16-19: Good use of constants for PostgreSQL values

Using constants for PostgreSQL empty arrays and null values improves code readability and maintainability by centralizing these special values in one place.


26-52: Improved PostgreSQL to PHP array transformation implementation

The rewritten method provides several improvements:

  1. Better handling of edge cases (empty strings, null values)
  2. Clear validation for unsupported multi-dimensional arrays
  3. Using JSON decoding with JSON_BIGINT_AS_STRING ensures large integers are preserved
  4. Improved error handling with detailed error messages
  5. Modular approach with helper methods

This implementation properly preserves numeric types while ensuring compatibility with PostgreSQL arrays.


60-75: Cleaner PHP to PostgreSQL array transformation

The implementation has been simplified and improved by:

  1. Using constants for better readability
  2. Proper validation of input
  3. Using array_map with a dedicated formatting method for better code organization
  4. Clear separation of concerns between transformation and value formatting

This refactoring makes the code more maintainable while ensuring type preservation.


77-133: Well-structured formatValue helper method

This new helper method is comprehensive and handles:

  1. Null values
  2. Numeric types (preserving type information)
  3. Booleans
  4. Objects with __toString implementation
  5. Other types with appropriate fallbacks
  6. Special handling for empty strings
  7. Type-checking with assertions
  8. Proper escaping of values

The implementationis thorough and accounts for various edge cases, making the type preservation robust.


135-150: Effective numeric type detection helper

The isNumericSimple method provides efficient handling of:

  1. Fast path for obvious non-numeric strings
  2. Scientific notation detection
  3. Proper numeric validation

This helper ensures accurate identification of numeric strings, which is crucial for maintaining type consistency.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
src/MartinGeorgiev/Utils/ArrayDataTransformer.php (2)

38-44: Consider more robust array parsing approach

The current implementation uses JSON functions by wrapping the PostgreSQL array content in square brackets. While the test cases cover various scenarios, this approach could be fragile for certain edge cases.

Consider implementing a more robust parsing method specific to PostgreSQL array format, or adding more detailed error messages to help diagnose parsing issues.


150-163: Clever string unescaping, but consider edge cases

The unescaping method uses temporary placeholders (___QUOTE___ and ___DBLBACK___) which works well but could cause issues if these exact strings appear in the original data.

While unlikely, consider using even more unique placeholders or a different approach to avoid potential conflicts with input data.

tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php (1)

1-250: Consider adding tests for additional edge cases

While the test coverage is excellent, consider adding tests for:

  1. Arrays containing null values (different from null arrays)
  2. Objects without __toString() method
  3. Edge cases with floating point precision
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1b8dd5f and d487a4a.

📒 Files selected for processing (4)
  • src/MartinGeorgiev/Doctrine/DBAL/Types/TextArray.php (3 hunks)
  • src/MartinGeorgiev/Utils/ArrayDataTransformer.php (1 hunks)
  • tests/MartinGeorgiev/Doctrine/DBAL/Types/TextArrayTest.php (1 hunks)
  • tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/MartinGeorgiev/Doctrine/DBAL/Types/TextArrayTest.php
🧰 Additional context used
🧬 Code Definitions (2)
src/MartinGeorgiev/Utils/ArrayDataTransformer.php (1)
tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php (1)
  • __toString (137-140)
tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php (2)
src/MartinGeorgiev/Utils/ArrayDataTransformer.php (3)
  • ArrayDataTransformer (12-164)
  • transformPHPArrayToPostgresTextArray (56-73)
  • transformPostgresTextArrayToPHPArray (22-50)
tests/MartinGeorgiev/Doctrine/DBAL/Types/TextArrayTest.php (3)
  • can_transform_from_php_value (41-44)
  • can_transform_to_php_value (51-54)
  • provideValidTransformations (62-93)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: PHP 8.4 + Doctrine ORM 2.18 + Doctrine Lexer latest
  • GitHub Check: PHP 8.2 + Doctrine ORM 3.0 + Doctrine Lexer 3.0
  • GitHub Check: PHP 8.2 + Doctrine ORM latest + Doctrine Lexer 2.1
  • GitHub Check: sloth
🔇 Additional comments (13)
src/MartinGeorgiev/Doctrine/DBAL/Types/TextArray.php (3)

7-8: Import update looks good!

Appropriate replacement of the DataStructure import with the new specialized ArrayDataTransformer class.


45-45: Method replacement looks good!

Properly refactored to use the new ArrayDataTransformer::transformPHPArrayToPostgresTextArray method.


68-68: Method replacement looks good!

Properly refactored to use the new ArrayDataTransformer::transformPostgresTextArrayToPHPArray method.

src/MartinGeorgiev/Utils/ArrayDataTransformer.php (6)

12-17: Well-structured class with clear constants!

Good choice of clear constant names for PostgreSQL empty array and null values.


22-50: Good implementation with proper error handling!

The transformPostgresTextArrayToPHPArray method has good handling of edge cases and error conditions:

  • Empty/null strings return empty arrays
  • Multi-dimensional arrays are rejected with a clear exception
  • JSON parsing errors are caught with informative messages
  • String values are properly unescaped

56-73: Good implementation of array transformation!

The transformPHPArrayToPostgresTextArray method properly:

  • Handles empty arrays
  • Rejects multi-dimensional arrays
  • Maps values through a detailed formatting function
  • Creates the correct PostgreSQL array format

78-131: Excellent value formatting with comprehensive type handling!

The formatValue method handles all data types thoroughly:

  • Nulls are converted to PostgreSQL NULL
  • Numbers (int/float) are handled natively
  • Booleans are formatted correctly
  • Objects with __toString() are handled properly
  • Resources are handled gracefully
  • String escaping is comprehensive

95-110: Good object handling with fallback for non-string-convertible objects

The handling of objects is well-done, with special handling for objects implementing __toString() and a fallback for other objects.


133-148: Clever numeric detection with performance optimization!

The isNumericSimple method has a fast path for obvious cases and special handling for scientific notation.

tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php (4)

10-35: Well-structured test class with thorough testing approach!

The test class properly tests transformation in both directions (PHP to PostgreSQL and vice versa), using a comprehensive data provider.


39-153: Excellent test data provider with comprehensive test cases!

The provideValidTransformations method covers a wide range of scenarios:

  • Different numeric formats (integers, floats, scientific notation)
  • Preservation of string vs. numeric types
  • Special characters and escaping
  • Empty strings and arrays
  • Large integers beyond PHP_INT_MAX
  • Unicode characters
  • Objects with __toString

221-234: Good test for numeric string type preservation!

This test ensures that different numeric representations maintain their original type after a round-trip transformation, which is a key goal of this PR.


239-248: Good resource handling with proper cleanup!

The test properly creates, uses, and closes a resource, ensuring no resource leaks.

@martin-georgiev martin-georgiev changed the title feat!: preserve the type of floats and integers when transforming back and forth between PostgreSQL and PHP feat!: preserve the type of booleans, floats and integers when transforming back and forth between PostgreSQL and PHP Mar 26, 2025
@martin-georgiev martin-georgiev marked this pull request as ready for review March 26, 2025 02:38
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/MartinGeorgiev/Utils/ArrayDataTransformer.php (2)

139-154: Consider preserving the original value in isNumericSimple.

The function modifies the $value parameter when handling scientific notation, which could lead to unexpected behavior if the function is extended in the future.

 private static function isNumericSimple(string $value): bool
 {
     // Fast path for obvious numeric strings
     if ($value === '' || $value[0] === '"') {
         return false;
     }

     // Handle scientific notation
     $lower = \strtolower($value);
     if (\str_contains($lower, 'e')) {
-        $value = \str_replace('e', '', $lower);
+        $numericCheck = \str_replace('e', '', $lower);
+        return \is_numeric($numericCheck);
     }

     // Use built-in numeric check
     return \is_numeric($value);
 }

118-118: Be aware that assertions may be disabled in production.

While the assert statement provides a good runtime type check during development, assertions might be disabled in production environments. Consider adding a fallback validation if this check is critical.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d487a4a and cd1ee09.

📒 Files selected for processing (3)
  • src/MartinGeorgiev/Utils/ArrayDataTransformer.php (1 hunks)
  • src/MartinGeorgiev/Utils/Exception/InvalidArrayFormatException.php (1 hunks)
  • tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php
🧰 Additional context used
🧬 Code Definitions (1)
src/MartinGeorgiev/Utils/ArrayDataTransformer.php (2)
src/MartinGeorgiev/Utils/Exception/InvalidArrayFormatException.php (3)
  • InvalidArrayFormatException (7-23)
  • multiDimensionalArrayNotSupported (9-12)
  • invalidFormat (14-22)
tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php (1)
  • __toString (138-141)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: PHP 8.4 + Doctrine ORM 3.0 + Doctrine Lexer latest
  • GitHub Check: PHP 8.4 + Doctrine ORM latest + Doctrine Lexer latest
  • GitHub Check: PHP 8.4 + Doctrine ORM 2.14 + Doctrine Lexer 2.1
  • GitHub Check: PHP 8.2 + Doctrine ORM 2.18 + Doctrine Lexer 3.0
  • GitHub Check: sloth
🔇 Additional comments (5)
src/MartinGeorgiev/Utils/Exception/InvalidArrayFormatException.php (1)

7-23: Well-designed exception class with clear static factory methods.

This new exception class is well-structured and follows good practices. The static factory methods provide clear, specific error messages for different invalid array scenarios.

src/MartinGeorgiev/Utils/ArrayDataTransformer.php (4)

26-54: Comprehensive transformation from PostgreSQL text array to PHP array.

The implementation handles edge cases well (empty strings, null values, empty arrays) and properly throws exceptions for unsupported formats. The JSON-based approach for parsing is elegant.


62-79: Solid implementation of PHP array to PostgreSQL text array conversion.

The method properly handles empty arrays and prevents multi-dimensional arrays. The formatting approach ensures proper escaping and type preservation.


84-137: Thorough value formatting with proper type handling.

The method handles different data types appropriately, preserving numeric types and properly escaping strings. Good handling of objects with __toString() method.


156-169:

✅ Verification successful

Verify that placeholder strings won't conflict with actual content.

The method uses ___QUOTE___ and ___DBLBACK___ as temporary placeholders during the unescaping process. While unlikely, there's a small risk these could conflict with actual string content.


🏁 Script executed:

#!/bin/bash
# Check if any string literals in the codebase already contain the placeholder strings

echo "Checking for potential placeholder conflicts..."
rg -l "___QUOTE___" --type php
rg -l "___DBLBACK___" --type php

Length of output: 268


Final Verification: No External Conflicts Found

The search confirms that both placeholder strings (___QUOTE___ and ___DBLBACK___) appear only within src/MartinGeorgiev/Utils/ArrayDataTransformer.php, specifically in the unescapeString method. There’s no evidence of these strings being present in other parts of the codebase, so the likelihood of conflict with actual content is minimal.

@coderabbitai
Copy link

coderabbitai bot commented Mar 26, 2025

Tip

For best results, initiate chat on the files or code changes.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Look at the old DataStructure implementation
echo "--- Old Implementation (DataStructure.php) ---"
cat src/MartinGeorgiev/Utils/DataStructure.php

echo "\n\n--- New Implementation (ArrayDataTransformer.php) ---"
cat src/MartinGeorgiev/Utils/ArrayDataTransformer.php

echo "\n\n--- Test cases demonstrating the behavior ---"
cat tests/MartinGeorgiev/Utils/DataStructureTest.php
cat tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php

Length of output: 17182


@martin-georgiev Here's a summary of how these changes affect the handling of booleans and numbers that you can add to the PR description:
## Changes to type handling

This PR significantly improves how numeric types and booleans are preserved when transforming data between PostgreSQL arrays and PHP. Previously, type information could be lost during transformation, resulting in unexpected behavior when retrieving data.

### Numbers handling

**Before:**
- Numbers could lose their original type during transformation
- Float values might be converted to integers (e.g., `1.0``1`)
- String representation of numbers could be converted to actual numbers

**After:**
- Type information is carefully preserved in both directions
- Integers stay as integers: `1``1`
- Floats stay as floats: `1.5``1.5`
- Numeric strings stay as strings: `'1.0'``'1.0'`

### Examples

#### Integer/float preservation

```php
// Before
$phpArray = [1, 2.5, '3.0'];
// After transformation back from PostgreSQL:
// [1, 2, 3] (types and precision could be lost)

// After
$phpArray = [1, 2.5, '3.0']; 
// After transformation back from PostgreSQL:
// [1, 2.5, '3.0'] (original types preserved)

Boolean handling

// Before
$phpArray = [true, false];
// Might be inconsistently handled

// After
$phpArray = [true, false];
// Properly converted to PostgreSQL 'true'/'false' and back
// Returns [true, false] with correct boolean types

Large integers and scientific notation

// Before
$phpArray = ['9223372036854775808', '1.23e5'];
// After transformation:
// Potential loss of precision or conversion to different types

// After
$phpArray = ['9223372036854775808', '1.23e5'];
// After transformation:
// ['9223372036854775808', '1.23e5'] (preserved exactly)

Benefits

  • More reliable data integrity between PHP and PostgreSQL
  • Predictable behavior when working with mixed numeric types
  • Precise representation of large integers and scientific notation
  • Proper preservation of boolean values

--- <details>
<summary>✏️ Learnings added</summary>

Learnt from: martin-georgiev
URL: #304

Timestamp: 2025-03-26T02:46:11.769Z
Learning: The PR "preserve the type of floats and integers when transforming back and forth between PostgreSQL and PHP" improves type handling by ensuring that integers remain integers, floats remain floats, numeric strings stay as strings, and booleans are properly converted through the transformation process.


> Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure [review instructions](https://docs.coderabbit.ai/guides/review-instructions). You can manage existing learnings in the [app](https://app.coderabbit.ai/learnings).

</details>



<!-- <agentComponentType>chat-answer</agentComponentType> -->

<!-- <rawResChunk><answer><details>
<summary>🧩 Analysis chain</summary>

🏁 Script executed:

#!/bin/bash

Look at the old DataStructure implementation

echo "--- Old Implementation (DataStructure.php) ---"
cat src/MartinGeorgiev/Utils/DataStructure.php

echo "\n\n--- New Implementation (ArrayDataTransformer.php) ---"
cat src/MartinGeorgiev/Utils/ArrayDataTransformer.php

echo "\n\n--- Test cases demonstrating the behavior ---"
cat tests/MartinGeorgiev/Utils/DataStructureTest.php
cat tests/MartinGeorgiev/Utils/ArrayDataTransformerTest.php


Length of output: 17182

---



</details>

@martin-georgiev Here's a summary of how these changes affect the handling of booleans and numbers that you can add to the PR description:

```markdown
## Changes to type handling

This PR significantly improves how numeric types and booleans are preserved when transforming data between PostgreSQL arrays and PHP. Previously, type information could be lost during transformation, resulting in unexpected behavior when retrieving data.

### Numbers handling

**Before:**
- Numbers could lose their original type during transformation
- Float values might be converted to integers (e.g., `1.0` → `1`)
- String representation of numbers could be converted to actual numbers

**After:**
- Type information is carefully preserved in both directions
- Integers stay as integers: `1` → `1`
- Floats stay as floats: `1.5` → `1.5`
- Numeric strings stay as strings: `'1.0'` → `'1.0'`

### Examples

#### Integer/float preservation

```php
// Before
$phpArray = [1, 2.5, '3.0'];
// After transformation back from PostgreSQL:
// [1, 2, 3] (types and precision could be lost)

// After
$phpArray = [1, 2.5, '3.0']; 
// After transformation back from PostgreSQL:
// [1, 2.5, '3.0'] (original types preserved)

Boolean handling

// Before
$phpArray = [true, false];
// Might be inconsistently handled

// After
$phpArray = [true, false];
// Properly converted to PostgreSQL 'true'/'false' and back
// Returns [true, false] with correct boolean types

Large integers and scientific notation

// Before
$phpArray = ['9223372036854775808', '1.23e5'];
// After transformation:
// Potential loss of precision or conversion to different types

// After
$phpArray = ['9223372036854775808', '1.23e5'];
// After transformation:
// ['9223372036854775808', '1.23e5'] (preserved exactly)

Benefits

  • More reliable data integrity between PHP and PostgreSQL
  • Predictable behavior when working with mixed numeric types
  • Precise representation of large integers and scientific notation
  • Proper preservation of boolean values

---

<details>
<summary>✏️ Learnings added</summary>

Learnt from: martin-georgiev
URL: #304

Timestamp: 2025-03-26T02:46:11.769Z
Learning: The PR "preserve the type of floats and integers when transforming back and forth between PostgreSQL and PHP" improves type handling by ensuring that integers remain integers, floats remain floats, numeric strings stay as strings, and booleans are properly converted through the transformation process.


> Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure [review instructions](https://docs.coderabbit.ai/guides/review-instructions). You can manage existing learnings in the [app](https://app.coderabbit.ai/learnings).

</details></answer></rawResChunk> -->

@martin-georgiev martin-georgiev merged commit c584099 into main Mar 26, 2025
43 of 47 checks passed
@martin-georgiev martin-georgiev deleted the float-arrays branch March 26, 2025 02:51
@martin-georgiev martin-georgiev changed the title feat!: preserve the type of booleans, floats and integers when transforming back and forth between PostgreSQL and PHP feat!: preserve the type of booleans, floats, scientific notations and integers when transforming back and forth between PostgreSQL and PHP Mar 26, 2025
@github-actions github-actions bot mentioned this pull request Mar 26, 2025
@github-actions github-actions bot mentioned this pull request Mar 30, 2025
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.

3 participants