Skip to content

Make CircleParser more robust - #819

Merged
malberts merged 2 commits into
masterfrom
circles
Aug 12, 2025
Merged

Make CircleParser more robust#819
malberts merged 2 commits into
masterfrom
circles

Conversation

@JeroenDeDauw

@JeroenDeDauw JeroenDeDauw commented Aug 12, 2025

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes

    • Prevents fatal errors when an invalid, missing, or non‑positive circle radius is provided; radius now defaults to 1 for safer map rendering.
  • Documentation

    • Updated release notes under Maps 12.0.0 to mention the circle radius fix.
  • Tests

    • Added tests verifying default radius behavior and handling of invalid or negative values.

@coderabbitai

coderabbitai Bot commented Aug 12, 2025

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

Added a release note. Refactored CircleParser to delegate circle construction and enforce radius defaulting to 1 for missing/invalid/non-positive inputs. Updated LegacyModel\Circle with typed properties, moved radius validation into setter, and adjusted method return types. Tests were extended to cover new radius behaviors.

Changes

Cohort / File(s) Summary of Changes
Documentation
RELEASE-NOTES.md
Added Maps 12.0.0 note: "Fixed fatal error when providing an invalid radius for circles."
Parser refactor and radius validation
src/WikitextParsers/CircleParser.php
Replaced inline circle construction with private buildCircle(string $circleWikitext): Circle and extractRadius(array $circleData): float; radius now defaults to 1 for missing, non-positive, or invalid values; parse() delegates to helpers. No public API changes.
Legacy model typing & validation
src/LegacyModel/Circle.php
Introduced typed private properties (LatLongValue $circleCentre, float $circleRadius); moved radius > 0 validation into setCircleRadius(float $circleRadius): void which throws on invalid input; updated setCircleCentre(...): void signature.
Tests for CircleParser
tests/Integration/Parsers/CircleParserTest.php
Added newCircleParser() helper; updated tests to use it; added tests asserting radius == 1.0 for missing, negative, and non-numeric radii.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

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

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

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 2ec710d and c17bfeb.

📒 Files selected for processing (1)
  • src/LegacyModel/Circle.php (2 hunks)
🔇 Additional comments (2)
src/LegacyModel/Circle.php (2)

19-20: Typed properties for centre and radius: LGTM

Good move to strict typing here. This tightens invariants and avoids accidental null/uninitialized state. Constructor routing through setters ensures validation runs consistently.


44-46: No internal string/null usages detected for Circle setters

A repo-wide search revealed only one instantiation of Circle—and it passes the correct types:

  • src/WikitextParsers/CircleParser.php:77
    return new Circle(
    $this->stringToLatLongValue($circleData[0]), ← LatLongValue
    $this->extractRadius($circleData) ← float (cast in extractRadius)
    );

No calls to setCircleRadius() or setCircleCentre() pass a raw string or null. Thus, there’s no internal BC break.

Recommendation: Communicate this new strict typing to any external consumers (e.g., in release notes or an upgrade guide) so they can adjust numeric-string or null arguments into floats and LatLongValue objects.

Comment on lines +52 to +56
public function setCircleRadius( float $circleRadius ): void {
if ( $circleRadius <= 0 ) {
throw new InvalidArgumentException( '$circleRadius must be greater than zero, got "' . $circleRadius . '"' );
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

NaN and Infinity bypass validation — treat non-finite values as invalid

Comparison with NaN returns false, so NaN currently slips through and gets assigned. Infinity should also be rejected. Tighten the guard to only allow finite, positive values.

Apply this diff:

-    if ( $circleRadius <= 0 ) {
-        throw new InvalidArgumentException( '$circleRadius must be greater than zero, got "' . $circleRadius . '"' );
-    }
+    if ( $circleRadius <= 0 || is_nan( $circleRadius ) || is_infinite( $circleRadius ) ) {
+        throw new InvalidArgumentException(
+            sprintf( '$circleRadius must be a finite number greater than zero, got %s', var_export( $circleRadius, true ) )
+        );
+    }

Optionally, add a small unit test to assert the setter rejects NaN/INF (and that the parser never produces them).

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
public function setCircleRadius( float $circleRadius ): void {
if ( $circleRadius <= 0 ) {
throw new InvalidArgumentException( '$circleRadius must be greater than zero, got "' . $circleRadius . '"' );
}
public function setCircleRadius( float $circleRadius ): void {
- if ( $circleRadius <= 0 ) {
- throw new InvalidArgumentException( '$circleRadius must be greater than zero, got "' . $circleRadius . '"' );
- }
+ if (
+ $circleRadius <= 0
+ || is_nan( $circleRadius )
+ || is_infinite( $circleRadius )
+ ) {
+ throw new InvalidArgumentException(
+ sprintf(
+ '$circleRadius must be a finite number greater than zero, got %s',
+ var_export( $circleRadius, true )
+ )
+ );
+ }
🤖 Prompt for AI Agents
In src/LegacyModel/Circle.php around lines 52 to 56, the setter currently only
checks $circleRadius <= 0 which allows NaN and Infinity to pass; update the
guard to reject non-finite values too by verifying the value is finite and
greater than zero (e.g. use PHP's is_finite($circleRadius) and $circleRadius >
0), and throw the same InvalidArgumentException when the check fails; optionally
add a unit test asserting the setter rejects NAN, INF and -INF (and that any
parser code does not produce them).

@malberts
malberts merged commit 1afab38 into master Aug 12, 2025
5 of 16 checks passed
@malberts
malberts deleted the circles branch August 12, 2025 15:36
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