Skip to content

Conversation

@Gnative
Copy link

@Gnative Gnative commented Aug 12, 2025

Models on Model::create() will return the dirty attribute, not data from the database.

This change prevents a $value that is already a Geometry from being passed to the parser and causing an error.

{
    "message": "Cannot use object of type Clickbar\\Magellan\\Data\\Geometries\\MultiPoint as array",
    "exception": "Error",
    "file": "/Users/Gnative/Development/Sites/Contours/source/http/contours/vendor/clickbar/laravel-magellan/src/IO/Parser/WKB/Scanner.php",
    "line": 17,
    "trace": [
        {
            "file": "/Users/Gnative/Development/Sites/Contours/source/http/contours/vendor/clickbar/laravel-magellan/src/IO/Parser/WKB/WKBParser.php",
            "line": 28,
            "function": "__construct",
            "class": "Clickbar\\Magellan\\IO\\Parser\\WKB\\Scanner",
            "type": "->"
        },
                {
            "file": "/Users/Gnative/Development/Sites/Contours/source/http/contours/vendor/clickbar/laravel-magellan/src/Cast/GeometryCast.php",
            "line": 52,
            "function": "parse",
            "class": "Clickbar\\Magellan\\IO\\Parser\\WKB\\WKBParser",
            "type": "->"
        },
        {
            "file": "/Users/Gnative/Development/Sites/Contours/source/http/contours/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php",
            "line": 886,
            "function": "get",
            "class": "Clickbar\\Magellan\\Cast\\GeometryCast",
            "type": "->"
        },
    ]
}

Summary by CodeRabbit

  • Bug Fixes
    • Correctly recognizes and accepts pre-constructed geometry objects without re-parsing, preventing rare type/validation errors and reducing overhead when reusing geometry instances. Null and string/binary input handling unchanged; no public API changes or integration action required. Improves stability and performance in workflows that pass existing geometry values.

@coderabbitai
Copy link

coderabbitai bot commented Aug 12, 2025

📝 Walkthrough

Walkthrough

Adds an early-return to GeometryCast::get: if the input is already a Geometry instance, assert its type and return it immediately; otherwise existing null handling and WKT/WKB parsing remain unchanged. No public API or set() behavior changes. (≤50 words)

Changes

Cohort / File(s) Summary of Changes
Geometry cast handling
src/Cast/GeometryCast.php
Added early exit: when $value is an instance of Geometry, assert its geometry type and return it directly, bypassing WKB/WKT parsing; other branches unchanged.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant GeometryCast
  participant Parser as WKT/WKB Parser

  Caller->>GeometryCast: get(value)
  alt value instanceof Geometry
    GeometryCast-->>Caller: assertGeometryType(value) then return value
  else
    GeometryCast->>Parser: parse(value)
    Parser-->>GeometryCast: Geometry
    GeometryCast-->>Caller: return parsed Geometry (after type assertion)
  end
Loading

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Possibly related PRs

Suggested reviewers

  • saibotk

Poem

A hop, a skip — no parse today,
Geometry sits; I’ll clear the way.
Assert, return, the work is done,
Faster paths beneath the sun.
Rabbit cheers — one small change, one run. 🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e50ed42 and cd477c7.

📒 Files selected for processing (1)
  • src/Cast/GeometryCast.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/Cast/GeometryCast.php (2)
src/Data/Geometries/Geometry.php (1)
  • Geometry (13-73)
tests/Models/Geometry.php (1)
  • Geometry (10-24)

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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/Cast/GeometryCast.php (2)

41-46: Document thrown exception in get()/set() PHPDoc

Both methods can now throw \InvalidArgumentException when an already-instantiated Geometry doesn’t match the configured cast type. Please reflect this in the docblocks.

 /**
  * Cast the given value.
  *
  * @param  Model  $model
+ * @throws \InvalidArgumentException If the provided Geometry does not match the configured cast type
  */
 public function get($model, string $key, mixed $value, array $attributes)
 /**
  * Prepare the given value for storage.
  *
  * @param  Model  $model
+ * @throws \InvalidArgumentException If the provided Geometry does not match the configured cast type
  */
 public function set($model, string $key, mixed $value, array $attributes)

Also applies to: 70-75


51-54: Add regression tests for subtype mismatch and pass-through

Recommend adding tests to cover:

  • Dirty attribute contains a wrong Geometry subtype for a specific cast (expect InvalidArgumentException).
  • Dirty attribute contains the correct subtype (expect the same instance to be returned, no parsing).

I can draft these tests against an example model (e.g., point field cast to Point::class) if helpful.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cd477c7 and 4e9dc70.

📒 Files selected for processing (1)
  • src/Cast/GeometryCast.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/Cast/GeometryCast.php (2)
src/Data/Geometries/Geometry.php (1)
  • Geometry (13-73)
tests/Models/Geometry.php (1)
  • Geometry (10-24)
🔇 Additional comments (1)
src/Cast/GeometryCast.php (1)

51-54: Good fix: assert subtype before early-returning Geometry

Early return now enforces the caster’s target subtype via assertGeometryType() before returning, which both fixes the parsing error and preserves the cast contract. Looks correct and minimal.

@saibotk
Copy link
Member

saibotk commented Aug 12, 2025

Hey! Thanks for the contribution, would you mind adding a test case for this?
If you feel like that is too hard, i can also try to do so when you can provide me with some minimal example code to reproduce the issue.
Otherwise, please also add a note to the changelog and run composer fix to fix the styling issues :)

Thanks!

@Gnative
Copy link
Author

Gnative commented Aug 12, 2025

Ok silly me. I was working off 2.0.0 not the main branch and the last pull request #148 fixed this problem.
I just migrated from mstaack/laravel-postgis and it was fairly smooth going.

Sorry to waste your time and thank you for the package.

@Gnative Gnative closed this Aug 12, 2025
@saibotk
Copy link
Member

saibotk commented Aug 13, 2025

No worries!

Thanks! That reminds me to do a release :)

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