Skip to content

Conversation

@jaxwilko
Copy link
Member

@jaxwilko jaxwilko commented Oct 15, 2025

This PR adds support for including a NL prior to the return statement if one was there previously.

This should fix #10

Summary by CodeRabbit

  • New Features

    • Support for imported constants and enum values in array configuration output.
    • Support for expressions used in imported values when rendering arrays.
  • Bug Fixes

    • Preserve blank line before return statements for readable formatting.
    • Ensure a trailing return block is added after include statements.
  • Tests

    • Added coverage and fixtures for complex/compact imports, updating imports, expression handling, and include-formatting.

@coderabbitai
Copy link

coderabbitai bot commented Oct 15, 2025

Walkthrough

Adds a protected pStmt_Return method to ArrayPrinter that inspects tokens before return statements to preserve blank lines after imports; inserts the method before array rendering. Adds tests and fixtures covering imported constants, enum value rendering, expressions, and include+return formatting.

Changes

Cohort / File(s) Summary
Printer logic
src/Printer/ArrayPrinter.php
Adds protected function pStmt_Return(Stmt\Return_ $node): string which reads parser tokens around a return node to detect and preserve a double-newline prefix; minor formatting whitespace adjustment.
Array file tests
tests/ArrayFileTest.php
Adds tests: testConfigImportComplex, testConfigImportCompact, testConfigImportsUpdating, testConfigExpression, testIncludeFormatting validating import rendering, enum value access, constant updates, expression rendering, and include+return formatting.
Array import fixtures
tests/fixtures/array/import-compact.php, tests/fixtures/array/import-complex.php
New fixture files adding use Symfony\Component\HttpFoundation\Response and use Example\EnumExample and returning arrays referencing Response::HTTP_OK and EnumExample::Value->value.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Dev as Developer
  participant AF as ArrayFile
  participant Parser as PHP Parser
  participant TS as TokenStream
  participant Printer as ArrayPrinter

  Dev->>AF: set(...) and write()
  AF->>Parser: parse file -> AST + tokens
  AF->>Printer: request printing of AST
  Printer->>TS: query tokens at node.startTokenPos and previous positions
  TS-->>Printer: token sequence (including newlines/whitespace)
  alt double newline detected before return
    Printer->>Printer: prepend newline prefix to return rendering
  end
  Printer-->>AF: formatted PHP with preserved blank line and rendered return
  AF-->>Dev: file written
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

A nibble, a sniff, a careful line,
I hop between use and return so fine.
Enums and constants placed with care,
Tokens checked — a blank line stays there.
Thump-thump! The config is snug and fair. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Out of Scope Changes Check ❓ Inconclusive The core functional change—the pStmt_Return method—is directly in scope for addressing the newline preservation requirement from issue #10. However, the PR introduces several new test methods and fixtures related to import functionality (testConfigImportComplex, testConfigImportCompact, testConfigImportsUpdating, testConfigExpression) and corresponding fixture files that extend beyond the specific newline preservation issue. While these tests may serve as supporting infrastructure for the new method, the extent to which they represent additional functionality or are necessary for validating the primary fix cannot be fully determined from the provided summaries alone. To clarify scope, review the import-related test methods to determine whether they are testing the integration of the new pStmt_Return method with existing import functionality or whether they represent separate feature enhancements beyond issue #10. If these tests are supplementary to the primary fix, ensure their purpose is documented. If they address separate functionality, consider whether they should be in a separate PR or if the PR objectives should be updated to reflect the broader scope.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "Added support for return having a nl prefix if one was there previously" accurately describes the main functional change in the PR, which is the addition of the pStmt_Return method in ArrayPrinter that preserves newline prefixes before return statements. The title is specific, concise, and directly related to the core objective of fixing issue #10 by maintaining blank lines between use statements and return statements during config file updates.
Linked Issues Check ✅ Passed The code changes directly address the requirement from issue #10 by implementing the pStmt_Return method in ArrayPrinter that detects and preserves newline prefixes before return statements when they existed previously. The method retrieves tokens from the parser and computes a prefix newline if preceding tokens indicate a double newline before non-whitespace content, which aligns with the stated objective to preserve blank lines between use statements and return statements during config file updates. The addition of test cases (particularly testIncludeFormatting) and fixtures validates that the newline preservation functionality works correctly in the config update workflow.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/nl-before-return

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8f9b9b6 and 64a935c.

📒 Files selected for processing (1)
  • src/Printer/ArrayPrinter.php (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: windows-latest / PHP 8.3
  • GitHub Check: windows-latest / PHP 8.2
  • GitHub Check: windows-latest / PHP 8.1
  • GitHub Check: Code Analysis
🔇 Additional comments (1)
src/Printer/ArrayPrinter.php (1)

29-29: LGTM!

The extra blank line maintains consistent spacing around constant declarations.


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

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot]

This comment was marked as resolved.

@LukeTowers

This comment was marked as resolved.

@coderabbitai

This comment was marked as resolved.

coderabbitai[bot]

This comment was marked as resolved.

@jaxwilko jaxwilko merged commit 6c9dcb1 into main Oct 15, 2025
21 checks passed
@jaxwilko jaxwilko deleted the fix/nl-before-return branch October 15, 2025 22:16
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.

The space after the use is removed after the config change.

3 participants