Skip to content

fix: Enhance property reflection in UserPropertiesClassReflectionExtension to support identity class resolution and improve type inference for user component properties. #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 8, 2025

Conversation

terabytesoftw
Copy link
Member

@terabytesoftw terabytesoftw commented Jun 8, 2025

Q A
Is bugfix? ✔️
New feature?
Breaks BC?

Summary by CodeRabbit

  • New Features
    • Improved handling and type inference for user identity-related properties, including support for multiple special properties on the user component.
    • Added methods to retrieve detailed component configuration by ID or class name.
  • Bug Fixes
    • Fixed property reflection for user component properties to support accurate resolution of the identity class.
  • Tests
    • Added new tests and stub classes to verify correct behavior of user identity property handling and component definitions.
  • Documentation
    • Updated changelog entries for clarity and accuracy.

…ension` to support identity class resolution and improve type inference for user component properties.
@terabytesoftw terabytesoftw added the bug Something isn't working label Jun 8, 2025
Copy link

coderabbitai bot commented Jun 8, 2025

"""

Walkthrough

The updates introduce enhanced handling and testing of Yii user component property reflection and configuration. Service map functionality is extended to provide raw component definitions by ID or class. The reflection extension now supports multiple user properties and improved type inference. Test fixtures and stubs are updated to support these changes, and relevant documentation is adjusted.

Changes

File(s) Change Summary
CHANGELOG.md Updated wording for bug fixes and added a new entry describing improved property reflection and type inference.
src/ServiceMap.php Added storage and retrieval of component configuration arrays; new methods for accessing definitions by ID/class.
src/reflection/UserPropertiesClassReflectionExtension.php Enhanced property reflection for Yii User component, supporting multiple properties and improved type inference.
tests/ServiceMapTest.php Added assertions for component definition retrieval by ID and class, and for non-existent classes.
tests/fixture/config.php Added a 'user' component with class and identityClass properties; imported stub User class.
tests/stub/MyController.php Added retrieval of user ID property in controller action for test coverage.
tests/stub/User.php Introduced a stub User class implementing IdentityInterface for testing purposes.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test Case
    participant ServiceMap as ServiceMap
    participant Reflection as UserPropertiesClassReflectionExtension

    Test->>ServiceMap: getComponentDefinitionById('user')
    ServiceMap-->>Test: returns component config array

    Test->>ServiceMap: getComponentDefinitionByClassName('yii\\web\\User')
    ServiceMap-->>Test: returns component config array

    Reflection->>ServiceMap: getComponentDefinitionById('user')
    ServiceMap-->>Reflection: returns user component config

    Reflection->>Reflection: getIdentityClass() extracts 'identityClass'
    Reflection-->>Test: returns property reflection with inferred type
Loading

Possibly related PRs

Poem

In the warren of code where the user hops in,
New stubs and reflections let testing begin.
With maps of components and IDs to find,
The rabbit ensures no bugs are left behind.
Now user identity’s clear as the sky—
🐇 Code leaps ahead, let’s give it a try!
"""


📜 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 d5cf413 and 9fa35ae.

📒 Files selected for processing (1)
  • src/reflection/UserPropertiesClassReflectionExtension.php (4 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/reflection/UserPropertiesClassReflectionExtension.php
⏰ Context from checks skipped due to timeout of 90000ms (18)
  • GitHub Check: phpunit-compatibility / PHP 8.1-ubuntu-latest
  • GitHub Check: phpunit-compatibility / PHP 8.2-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.4-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.3-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.3-ubuntu-latest
  • GitHub Check: phpunit-compatibility / PHP 8.2-ubuntu-latest
  • GitHub Check: phpunit-compatibility / PHP 8.1-windows-latest
  • GitHub Check: phpunit / PHP 8.2-windows-latest
  • GitHub Check: phpunit / PHP 8.3-windows-latest
  • GitHub Check: phpunit / PHP 8.1-windows-latest
  • GitHub Check: phpunit / PHP 8.4-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.1-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.4-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.2-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.3-windows-latest
  • GitHub Check: phpunit / PHP 8.1-windows-latest
  • GitHub Check: phpunit / PHP 8.2-windows-latest
  • GitHub Check: composer-require-checker / PHP 8.4-ubuntu-latest
✨ 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.
    • 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 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.

Copy link

codecov bot commented Jun 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (c390b9b) to head (9fa35ae).
Report is 1 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##                main       #34   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity        53        60    +7     
===========================================
  Files              1         1           
  Lines             79        91   +12     
===========================================
+ Hits              79        91   +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@terabytesoftw terabytesoftw changed the title fix: Enhance property reflection in UserPropertiesClassReflectionExtension to support identity class resolution and improve type inference for user component properties. fix: Enhance property reflection in UserPropertiesClassReflectionExtension to support identity class resolution and improve type inference for user component properties. Jun 8, 2025
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: 4

🧹 Nitpick comments (3)
CHANGELOG.md (1)

9-9: Minor style improvement: Use hyphen in compound adjective.

Consider using "bleeding-edge" instead of "bleeding edge" when it modifies the following noun.

-- Enh #29: Enable strict rules and bleeding edge analysis, and update `README.md` with strict configuration examples (@terabytesoftw)
+- Enh #29: Enable strict rules and bleeding-edge analysis, and update `README.md` with strict configuration examples (@terabytesoftw)
🧰 Tools
🪛 LanguageTool

[uncategorized] ~9-~9: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ...ftw) - Enh #29: Enable strict rules and bleeding edge analysis, and update README.md with s...

(EN_COMPOUND_ADJECTIVE_INTERNAL)

src/ServiceMap.php (2)

79-85: Docblock type too broad – tighten to nested array.

$componentsDefinitions always stores an array for each component ID, never a scalar.
Refine the PHPStan type to improve inference.

- * @phpstan-var array<string, mixed>
+ * @phpstan-var array<string, array<array-key, mixed>>

149-155: Minor: redundant is_array() guard.

$componentsDefinitions[$id] is written exclusively with an array value in processComponents; the extra is_array() check is unnecessary noise.

Not critical – consider during clean-up.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c390b9b and a75dcd9.

📒 Files selected for processing (7)
  • CHANGELOG.md (1 hunks)
  • src/ServiceMap.php (7 hunks)
  • src/reflection/UserPropertiesClassReflectionExtension.php (4 hunks)
  • tests/ServiceMapTest.php (2 hunks)
  • tests/fixture/config.php (2 hunks)
  • tests/stub/MyController.php (1 hunks)
  • tests/stub/User.php (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/fixture/config.php (1)
tests/stub/User.php (1)
  • User (10-36)
🪛 LanguageTool
CHANGELOG.md

[uncategorized] ~9-~9: If this is a compound adjective that modifies the following noun, use a hyphen.
Context: ...ftw) - Enh #29: Enable strict rules and bleeding edge analysis, and update README.md with s...

(EN_COMPOUND_ADJECTIVE_INTERNAL)

⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: phpunit-compatibility / PHP 8.3-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.1-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.2-ubuntu-latest
  • GitHub Check: phpunit-compatibility / PHP 8.4-windows-latest
🔇 Additional comments (7)
tests/stub/MyController.php (1)

94-94: LGTM! Good test coverage for the new user property reflection.

The addition of $id = Yii::$app->user->id; appropriately tests the enhanced property reflection for the user component's ID property, complementing the existing user component usage patterns in the test controller.

tests/fixture/config.php (2)

6-6: LGTM! Proper import for the User stub class.

The import correctly references the User stub class needed for the identity class configuration.


17-20: LGTM! Well-structured user component configuration.

The user component configuration correctly sets up the identity class that enables the enhanced property reflection functionality. The configuration follows Yii2 conventions and properly references the stub User class.

CHANGELOG.md (2)

7-7: LGTM! Correct categorization of bug fixes.

The changes from "Enh" to "Bug" properly categorize these entries as bug fixes rather than enhancements.

Also applies to: 10-10


11-11: LGTM! Accurate documentation of the property reflection enhancement.

The changelog entry clearly describes the UserPropertiesClassReflectionExtension improvements for identity class resolution and type inference.

tests/ServiceMapTest.php (2)

15-15: LGTM! Appropriate import for User class.

The import correctly references the User class needed for the component definition tests.


115-132: LGTM! Comprehensive test coverage for component definition retrieval.

The new assertions thoroughly test the component definition functionality:

  • Positive case: Retrieving definition for existing yii\web\User class
  • Negative case: Handling non-existent component class
  • ID-based retrieval: Getting definition by component ID 'user'

The expected values correctly match the configuration in tests/fixture/config.php, ensuring the ServiceMap properly exposes component definitions including identityClass.

…ssReflectionExtension` and update `User` class method signatures to remove type hints.
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

♻️ Duplicate comments (3)
src/reflection/UserPropertiesClassReflectionExtension.php (3)

143-143: ⚠️ Potential issue

Method call to non-existent isSubclassOfClass().

This issue was flagged in previous reviews but remains unresolved. ClassReflection exposes isSubclassOf(string $className), not isSubclassOfClass(). This will cause a fatal error.

Apply this diff to fix the method call:

-$classReflection->isSubclassOfClass($this->reflectionProvider->getClass(User::class)) === false
+$classReflection->isSubclassOf(User::class) === false

148-151: ⚠️ Potential issue

hasProperty() returns true for any name when identity class exists.

This issue was flagged in previous reviews but remains unresolved. The current logic ignores $propertyName and returns true for any property name as long as an identityClass is configured, causing getProperty() to later throw MissingPropertyFromReflectionException.

Apply this diff to properly check property names:

-return
-    $this->getIdentityClass() !== null ||
-    $this->serviceMap->getComponentClassById($propertyName) !== null;
+return in_array($propertyName, ['id', 'identity', 'isGuest'], true)
+        ? $this->getIdentityClass() !== null
+        : $this->serviceMap->getComponentClassById($propertyName) !== null;

85-119: 🛠️ Refactor suggestion

Potential type inconsistency for id property - missing null possibility.

The id property is typed as IntegerType | StringType but according to Yii's IdentityInterface::getId(), it can also return null. This could cause type errors in static analysis when the user is not authenticated.

Apply this diff to include null in the union type:

 if ($propertyName === 'id') {
     return new ComponentPropertyReflection(
         new DummyPropertyReflection($propertyName),
-        TypeCombinator::union(new IntegerType(), new StringType()),
+        TypeCombinator::addNull(TypeCombinator::union(new IntegerType(), new StringType())),
         $classReflection,
     );
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a75dcd9 and 928cb07.

📒 Files selected for processing (2)
  • src/reflection/UserPropertiesClassReflectionExtension.php (4 hunks)
  • tests/stub/User.php (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/stub/User.php
⏰ Context from checks skipped due to timeout of 90000ms (18)
  • GitHub Check: phpstan / PHP 8.4-ubuntu-latest
  • GitHub Check: composer-require-checker / PHP 8.4-ubuntu-latest
  • GitHub Check: phpunit-compatibility / PHP 8.2-ubuntu-latest
  • GitHub Check: phpunit-compatibility / PHP 8.3-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.1-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.2-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.4-windows-latest
  • GitHub Check: phpunit / PHP 8.4-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.3-ubuntu-latest
  • GitHub Check: phpunit / PHP 8.1-windows-latest
  • GitHub Check: phpunit / PHP 8.1-ubuntu-latest
  • GitHub Check: phpunit / PHP 8.3-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.3-windows-latest
  • GitHub Check: phpunit / PHP 8.3-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.2-windows-latest
  • GitHub Check: phpunit-compatibility / PHP 8.1-windows-latest
  • GitHub Check: phpunit / PHP 8.2-windows-latest
  • GitHub Check: phpunit / PHP 8.1-windows-latest
🔇 Additional comments (3)
src/reflection/UserPropertiesClassReflectionExtension.php (3)

161-172: Approve the new getIdentityClass() method implementation.

The implementation correctly extracts the identity class from the user component configuration and provides appropriate null handling. The method encapsulates the logic well and improves code readability.


16-22: Approve the additional type imports.

The new type imports (BooleanType, IntegerType, ObjectType, StringType, TypeCombinator) are correctly added to support the enhanced property type inference functionality.


64-64: Approve the ReflectionProvider constructor parameter addition.

The addition of ReflectionProvider to the constructor is necessary for the enhanced subclass checking functionality and follows proper dependency injection patterns.

… to use `ConstantBooleanType` for 'isGuest' property.
…Extension` for improved readability and maintainability.
…ionExtension` for improved clarity and performance.
@terabytesoftw terabytesoftw requested a review from Copilot June 8, 2025 14:47
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes property reflection issues in the UserPropertiesClassReflectionExtension by enhancing the handling of user identity-related properties, such as "id", "identity", and "isGuest". It updates test stubs and component definitions to verify accurate type inference and reflection.

  • Updated test stubs and configuration to support user identity resolution.
  • Refined UserPropertiesClassReflectionExtension to handle special properties based on identityClass.
  • Adjusted ServiceMap to properly extract and store component definitions.

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/stub/User.php Introduces a stub for a User model implementing IdentityInterface.
tests/stub/MyController.php Adds an assignment for user id; consider checking its usage.
tests/fixture/config.php Adds user component configuration with identityClass resolution.
tests/ServiceMapTest.php Adds tests to validate the resolution of component definitions.
src/reflection/UserPropertiesClassReflectionExtension.php Enhances property reflection for special user component properties.
src/ServiceMap.php Updates component definitions handling for improved static analysis.
CHANGELOG.md Updates changelog to reflect fixes and improvements in user property reflection.
Comments suppressed due to low confidence (1)

src/reflection/UserPropertiesClassReflectionExtension.php:143

  • The hasProperty method determines the existence of 'id' and 'isGuest' based solely on getIdentityClass() being non-null, yet getProperty always returns a reflection for these properties. Consider updating hasProperty to unconditionally return true for 'id' and 'isGuest' to ensure consistent behavior.
return in_array($propertyName, ['id', 'identity', 'isGuest'], true) ? $this->getIdentityClass() !== null : $this->serviceMap->getComponentClassById($propertyName) !== null;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant