Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

chore: update to phpstan 2 - #42

Merged
Derek Kaser (dkaser) merged 1 commit into
mainfrom
phpstan
May 18, 2025
Merged

chore: update to phpstan 2#42
Derek Kaser (dkaser) merged 1 commit into
mainfrom
phpstan

Conversation

@dkaser

@dkaser Derek Kaser (dkaser) commented May 18, 2025

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Bug Fixes

    • Improved input validation and error handling across various areas, including log retrieval and API interactions, to prevent runtime errors and ensure robust operation.
    • Enhanced dynamic path resolution for file inclusions, reducing reliance on hardcoded paths and improving compatibility.
  • Refactor

    • Standardized the use of constants and variable naming for better maintainability and clarity.
    • Centralized configuration and localization file loading using dynamic root path detection.
  • Chores

    • Updated development dependencies and static analysis configuration to use newer versions and refined analysis levels.

@coderabbitai

coderabbitai Bot commented May 18, 2025

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The changes update development dependencies, enhance input validation and error handling, refactor file path handling to use dynamic or constant-based paths, and standardize variable naming. Several PHP files now check for required constants or validate inputs before proceeding. Absolute paths are replaced with relative or constant-based paths, improving maintainability and flexibility.

Changes

File(s) Change Summary
composer.json Updated development dependencies: phpstan/phpstan to ^2.1, friendsofphp/php-cs-fixer to ^3.75.
phpstan.neon Changed PHPStan analysis level from max to numeric level 9.
src/usr/local/emhttp/plugins/tailscale/Tailscale_dashboard.page Dynamically determines document root before including page.php; fallback logic for $docroot improved.
src/usr/local/emhttp/plugins/tailscale/include/Pages/Dashboard.php Removed $docroot initialization and absolute path require; no other logic changed.
src/usr/local/emhttp/plugins/tailscale/include/Pages/Lock.php Enforces presence of PLUGIN_ROOT constant; replaces path logic with constant; explicit error if undefined.
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LocalAPI.php Replaces socket path property with class constant; adds input validation; improves JSON encoding and logging.
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Translator.php Renames property/variables for clarity; uses PLUGIN_ROOT constant for paths; checks for constant presence.
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Utils.php Adds input validation and error handling; improves path and type safety in several utility functions.
src/usr/local/emhttp/plugins/tailscale/include/common.php Defines a PLUGIN_ROOT constant; uses dynamic path for including files; replaces hardcoded paths with constant-based paths.
src/usr/local/emhttp/plugins/tailscale/include/data/Config.php
.../Lock.php
.../Status.php
Changes require_once for common.php from absolute to relative path using dirname(dirname(__FILE__)).
src/usr/local/emhttp/plugins/tailscale/include/get_log.php Adds input validation for POST parameters before processing logs; throws exception on invalid input.
src/usr/local/emhttp/plugins/tailscale/include/page.php Adds require_once for common.php inside getPage function to ensure dependency is loaded.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant PHP Script
    participant CommonPHP
    participant PluginRootConstant

    User->>PHP Script: Request page or action
    PHP Script->>PluginRootConstant: Check if PLUGIN_ROOT defined
    alt PLUGIN_ROOT not defined
        PHP Script->>User: Throw RuntimeException
    else PLUGIN_ROOT defined
        PHP Script->>CommonPHP: require_once common.php
        CommonPHP-->>PHP Script: Load dependencies
        PHP Script->>User: Continue processing
    end
Loading

Poem

🐇
Hop, hop! The code is neat,
Paths are nimble, checks complete.
Constants guide us through the night,
No more hardcoded paths in sight!
With careful checks and tidy flow,
The Tailscale plugin’s good to go.
🥕

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate Unit Tests
  • Create PR with Unit Tests
  • Commit Unit Tests in branch phpstan
  • Post Copyable Unit Tests in 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

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

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 generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @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.

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

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.

@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: 0

🧹 Nitpick comments (1)
src/usr/local/emhttp/plugins/tailscale/include/get_log.php (1)

26-28: Good addition of input validation

Adding validation for user input before processing improves security by preventing potential code injection or unexpected behavior. Consider making the error message more specific to help with debugging (e.g., "Invalid log file or max lines parameter").

- throw new InvalidArgumentException("Invalid input");
+ throw new InvalidArgumentException("Invalid input: log must be a string and max must be numeric");
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between efd69cc and 49fee95.

⛔ Files ignored due to path filters (1)
  • composer.lock is excluded by !**/*.lock
📒 Files selected for processing (14)
  • composer.json (1 hunks)
  • phpstan.neon (1 hunks)
  • src/usr/local/emhttp/plugins/tailscale/Tailscale_dashboard.page (1 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/Pages/Dashboard.php (0 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/Pages/Lock.php (1 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LocalAPI.php (1 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Translator.php (1 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Utils.php (4 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/common.php (1 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/data/Config.php (1 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/data/Lock.php (1 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/data/Status.php (1 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/get_log.php (1 hunks)
  • src/usr/local/emhttp/plugins/tailscale/include/page.php (1 hunks)
💤 Files with no reviewable changes (1)
  • src/usr/local/emhttp/plugins/tailscale/include/Pages/Dashboard.php
🧰 Additional context used
🧬 Code Graph Analysis (1)
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Translator.php (1)
src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Info.php (1)
  • tr (45-48)
🔇 Additional comments (23)
phpstan.neon (1)

2-2: Set PHPStan analysis level to 9
Switching from the "max" keyword to the numeric level 9 aligns with PHPStan 2’s configuration. Please run your full static analysis (vendor/bin/phpstan analyse) to confirm no new errors are introduced, and update any ignoreErrors or baseline files if needed.

composer.json (1)

3-4: Upgrade dev dependencies for static analysis and code style
The phpstan/phpstan constraint is bumped to ^2.1 and friendsofphp/php-cs-fixer to ^3.75. After running composer update --dev, verify that your CI pipelines and local checks pass, review each tool’s changelog for breaking changes, and adjust any custom configurations accordingly.

src/usr/local/emhttp/plugins/tailscale/include/page.php (1)

11-11: Improved file inclusion using relative path

Using dirname(__FILE__) ensures that the common.php file is included relative to the current file location rather than using an absolute path. This change improves maintainability as it will work regardless of the installation directory.

src/usr/local/emhttp/plugins/tailscale/include/data/Status.php (1)

6-6: Improved file inclusion using relative path

Using dirname(dirname(__FILE__)) to navigate up two directory levels from the current file is the correct approach for including common.php. This change improves maintainability by removing hardcoded absolute paths.

src/usr/local/emhttp/plugins/tailscale/Tailscale_dashboard.page (1)

7-9: Improved document root handling

The code now dynamically determines the document root with proper fallback mechanisms. This approach is more robust than using hardcoded paths, as it can adapt to different server configurations while maintaining backward compatibility.

src/usr/local/emhttp/plugins/tailscale/include/data/Config.php (1)

6-6: Good improvement to path handling

Replacing the hardcoded absolute path with a dynamically constructed relative path makes the code more portable and less dependent on fixed directory structures.

src/usr/local/emhttp/plugins/tailscale/include/data/Lock.php (1)

6-6: Good improvement to path handling

Replacing the hardcoded absolute path with a dynamically constructed relative path improves maintainability and portability.

src/usr/local/emhttp/plugins/tailscale/include/common.php (2)

5-5: Good addition of centralized constant

Defining the PLUGIN_ROOT constant within the namespace centralizes the plugin root directory path, which can be reused throughout the codebase.


7-7: Improved path resolution using constant

Using the newly defined PLUGIN_ROOT constant in the glob pattern ensures consistent path resolution and improves maintainability.

src/usr/local/emhttp/plugins/tailscale/include/Pages/Lock.php (2)

13-15: Good runtime validation of required constant

Adding a runtime check for the PLUGIN_ROOT constant with a clear exception message improves error handling and helps with debugging.


23-23: Consistent use of centralized constant

Replacing hardcoded paths with the PLUGIN_ROOT constant across all require statements ensures consistency and makes path management more maintainable.

Also applies to: 26-26, 29-29, 32-32

src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Utils.php (4)

25-33: Good addition of input validation!

Adding validation for the URL parameter and JSON encoding outcome improves the robustness of the code. This change prevents silent failures and provides clear error messages when issues occur.


136-138: Good defensive coding practice.

Similar to the changes in send_usage(), adding URL validation here ensures the method fails early with a clear error message rather than proceeding with an invalid URL.


181-185: Improved document root handling.

This change appropriately handles the document root by:

  1. Using $_SERVER['DOCUMENT_ROOT'] as the primary source
  2. Providing a fallback value if it's not available
  3. Adding type checking to ensure $docroot is a string

This is more robust than relying on a global variable.


229-229: Enhanced type safety.

Adding a type check for $_SERVER['PHP_SELF'] before using it with basename() prevents potential errors if the server variable isn't set or isn't a string.

src/usr/local/emhttp/plugins/tailscale/include/Tailscale/LocalAPI.php (4)

14-14: Good use of class constant.

Converting tailscaleSocket from a property to a class constant is appropriate since this is a fixed value that shouldn't change during the object's lifetime.


18-26: Improved input validation and error handling.

Similar to the changes in Utils.php, adding validation for the URL parameter and JSON encoding outcome enhances the robustness of the code. This makes the code more reliable by ensuring valid inputs and providing clear error messages.


33-33: Consistent use of class constant.

Updated to use the class constant syntax for tailscaleSocket, maintaining consistency with the earlier change to make it a constant.


39-40: Performance improvement by avoiding redundant JSON encoding.

Now the JSON is encoded once at the beginning and the encoded string is reused, which is more efficient and consistent than encoding multiple times.

Also applies to: 46-47

src/usr/local/emhttp/plugins/tailscale/include/Tailscale/Translator.php (4)

7-8: Improved property naming and type documentation.

The property was renamed from $tailscale_lang to the more concise $lang, and a PHPDoc type annotation was added to improve type documentation. This makes the code more readable and helps with static analysis.


14-16: Good dependency verification.

This runtime check ensures that the required PLUGIN_ROOT constant is defined. This is a good practice as it makes the code fail early with a clear error message if a dependency is missing, rather than failing later with a potentially less clear error.


20-22: Better path management using constants.

Replacing hardcoded paths with the PLUGIN_ROOT constant improves maintainability and reduces the risk of path-related errors if the plugin structure changes.

Also applies to: 23-26


28-37: Consistent variable naming throughout the class.

The variable renaming from $tailscale_lang to $lang is consistently applied throughout the class, including in the recursive flattening logic, property assignment, and the tr() method. This consistency makes the code easier to understand and maintain.

Also applies to: 40-40, 45-45

@coderabbitai

coderabbitai Bot commented May 18, 2025

Copy link
Copy Markdown

Warning

Docstrings generation is only available on the Pro tier.

@dkaser
Derek Kaser (dkaser) merged commit 3e29970 into main May 18, 2025
@dkaser
Derek Kaser (dkaser) deleted the phpstan branch May 18, 2025 03:39
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant