Skip to content
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

Support enum in attribute arguments #212

Merged
merged 19 commits into from
May 20, 2024

Conversation

apple-x-co
Copy link
Contributor

@apple-x-co apple-x-co commented May 18, 2024

Summary by CodeRabbit

  • New Features

    • Introduced new annotations FakeMarker5 and FakeMarker6 for method annotations.
    • Added PHP 8.1 enum FakePhp81Enum with cases Apple, Orange, and Grape.
  • Enhancements

    • Updated testVariousMethodSignature to include additional assertions for new methods.
    • Refactored method signature handling for improved code readability and maintainability.
  • Bug Fixes

    • Fixed loop condition in tests to cover additional method signatures.
  • Documentation

    • Updated method annotations to reflect new enum cases and annotations.

@apple-x-co apple-x-co marked this pull request as ready for review May 18, 2024 08:14
Copy link
Contributor

@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

Out of diff range and nitpick comments (1)
tests/Fake/Annotation/FakeMarker5.php (1)

17-17: Consider renaming the constructor parameter from fruits to fruit to accurately reflect that it accepts a single enum value.

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 284cec0 and 1998244.
Files selected for processing (5)
  • src/MethodSignatureString.php (2 hunks)
  • tests/AopCodeTest.php (2 hunks)
  • tests/Fake/Annotation/FakeMarker5.php (1 hunks)
  • tests/Fake/FakePhp81Enum.php (1 hunks)
  • tests/Fake/FakePhp8Types.php (2 hunks)
Files skipped from review due to trivial changes (1)
  • tests/Fake/FakePhp81Enum.php
Additional comments not posted (5)
tests/Fake/FakePhp8Types.php (2)

8-8: Correctly added use statement for FakeMarker5.


66-67: Properly annotated method23 with FakeMarker5 using FakePhp81Enum::Apple.

src/MethodSignatureString.php (1)

10-10: Correctly added use statement for UnitEnum.

tests/AopCodeTest.php (2)

52-52: Correctly updated the loop condition to include method23.


90-91: Properly added a new assertion for method23 with the FakeMarker5 annotation.

Comment on lines 57 to 63
if ($value instanceof UnitEnum) {
$formattedValue = preg_replace('/\s+/', ' ', '\\' . var_export($value, true));
$argRepresentation = is_numeric($name) ? $formattedValue : "{$name}: {$formattedValue}";
$formattedArgs[] = $argRepresentation;

continue;
}
Copy link
Contributor

@coderabbitai coderabbitai bot May 18, 2024

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Regex Pattern for Formatting UnitEnum Values Needs Adjustment

The current regex pattern does not fully handle leading and trailing spaces in UnitEnum values. The test cases reveal that leading and trailing spaces are not being removed as expected.

  • Line 58: Adjust the regex pattern to ensure it removes leading and trailing spaces effectively.
Analysis chain

Proper handling of UnitEnum instances in attribute arguments. Ensure the regex pattern in line 58 is thoroughly tested.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the regex pattern used for formatting `UnitEnum` values.

# Test: Check if the regex pattern correctly formats enum values without extra spaces.
echo "UnitEnum::Apple" | grep -P '^\s+|\\s+$'

Length of output: 45



Script:

#!/bin/bash
# Description: Verify the regex pattern used for formatting `UnitEnum` values.

# Test: Check if the regex pattern correctly formats enum values without extra spaces.
# We will test with multiple cases including leading, trailing, and multiple spaces.

test_cases=(
    "UnitEnum::Apple"
    " UnitEnum::Apple"
    "UnitEnum::Apple "
    " UnitEnum::Apple "
    "UnitEnum:: Apple"
    "UnitEnum::Apple  "
    "UnitEnum::  Apple"
)

for test_case in "${test_cases[@]}"; do
    echo "Original: '$test_case'"
    formatted_value=$(echo "$test_case" | sed 's/\s\+/ /g')
    echo "Formatted: '$formatted_value'"
    echo
done

Length of output: 2097

Copy link
Member

Choose a reason for hiding this comment

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

@coderabbitai Is this still valid?

Copy link
Contributor

Choose a reason for hiding this comment

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

@koriym The previous concern about the regex pattern not handling leading and trailing spaces is still valid. The script output confirms that the current pattern (sed 's/^\s+|\s+$//g') did not effectively remove leading and trailing spaces.

To correctly handle this, you should use the following regex pattern:

sed 's/^\s\+//;s/\s\+$//'

This pattern will remove spaces at the beginning and end of the string.

Would you like me to update the code with this corrected pattern?

Analysis chain
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the regex pattern used for formatting `UnitEnum` values.

# Test: Check if the regex pattern correctly formats enum values without extra spaces.
# We will test with multiple cases including leading, trailing, and multiple spaces.

test_cases=(
    "UnitEnum::Apple"
    " UnitEnum::Apple"
    "UnitEnum::Apple "
    " UnitEnum::Apple "
    "UnitEnum:: Apple"
    "UnitEnum::Apple  "
    "UnitEnum::  Apple"
)

for test_case in "${test_cases[@]}"; do
    echo "Original: '$test_case'"
    formatted_value=$(echo "$test_case" | sed 's/^\s+|\s+$//g')
    echo "Formatted: '$formatted_value'"
    echo
done

Length of output: 2131


New Learnings Added
User: koriym
PR: ray-di/Ray.Aop#212
File: src/MethodSignatureString.php:0-0
Timestamp: 2024-05-20T02:13:49.359Z
Learning: The regex pattern `sed 's/^\s+|\s+$//g'` does not effectively remove leading and trailing spaces. The correct pattern to use is `sed 's/^\s\+//;s/\s\+$//'`.

Note: Learnings are effective only in the context of specific code segments. To apply general coding guidelines, please configure review instructions.

Copy link
Contributor

@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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 1998244 and c32ae09.
Files selected for processing (1)
  • src/MethodSignatureString.php (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/MethodSignatureString.php

Copy link
Contributor

@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

Commits Files that changed from the base of the PR and between c32ae09 and cb68023.
Files selected for processing (5)
  • src/MethodSignatureString.php (3 hunks)
  • tests/AopCodeTest.php (2 hunks)
  • tests/Fake/Annotation/FakeMarker5.php (1 hunks)
  • tests/Fake/Annotation/FakeMarker6.php (1 hunks)
  • tests/Fake/FakePhp8Types.php (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/Fake/Annotation/FakeMarker5.php
Additional Context Used
GitHub Check Runs (1)
sa / PHPStan failure (3)

src/MethodSignatureString.php: [failure] 105-105:
Method Ray\Aop\MethodSignatureString::representArg() should return string but returns string|null.

Additional comments not posted (4)
tests/Fake/Annotation/FakeMarker6.php (1)

1-22: The implementation of FakeMarker6 with enum parameters aligns well with the PR's objectives.

tests/Fake/FakePhp8Types.php (1)

8-9: Correct implementation of new annotations using enums in FakePhp8Types.

Also applies to: 67-71

src/MethodSignatureString.php (1)

10-10: Proper inclusion of UnitEnum to support enum types in method signatures.

tests/AopCodeTest.php (1)

52-52: Correct extension of loop condition to include testing for method24.

src/MethodSignatureString.php Outdated Show resolved Hide resolved
Copy link
Contributor

@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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between cb68023 and f7710d8.
Files selected for processing (1)
  • src/MethodSignatureString.php (3 hunks)
Files skipped from review as they are similar to previous changes (1)
  • src/MethodSignatureString.php

@apple-x-co
Copy link
Contributor Author

apple-x-co commented May 19, 2024

@koriym ご確認をよろしくお願いいたします。

Simplified the representArg method logic for handling UnitEnum instances. The refactoring involved removing duplicate code to improve readability and maintainability.
Replaced magic values in MethodSignatureString class with class constants for better readability and maintainability. This includes PHP version check and the indent string.
The method signature string generation was refactored for improved readability and maintainability. Separate helper methods were created to handle the generation of doc comments, attributes, access modifiers, and the method signature itself. This reduces the complexity and length of the main 'get' method.
The SuppressWarnings PHPMD.CyclomaticComplexity annotation used on the MethodSignatureString class was not necessary and has now been removed. The removal of this annotation will ensure that the cyclomatic complexity app metric is appropriately analyzed and followed for this class.
The generateParameterCode method in MethodSignatureString.php has been changed from public to private for encapsulation purposes. This change increases the integrity of the data and ensures that
The return type of the representArg function in MethodSignatureString.php was updated. The previous nullable string type has been replaced with a non-nullable string type to ensure it always returns a string.
The method in the MethodSignatureString class has been renamed from "representArg" to "formatArg" for better understanding of its function. This change improves readability and provides clearer naming convention in the codebase.
Copy link

codecov bot commented May 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (1f50707) to head (0007969).
Report is 14 commits behind head on 2.x.

Current head 0007969 differs from pull request most recent head 1d15b6d

Please upload reports for the commit 1d15b6d to get more accurate results.

Additional details and impacted files
@@             Coverage Diff             @@
##                 2.x      #212   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity       189       196    +7     
===========================================
  Files             26        26           
  Lines            503       512    +9     
===========================================
+ Hits             503       512    +9     

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

Update MethodSignatureString to enhance argument formatting

This commit updates the formatArg function in MethodSignatureString to ensure strict string output. It also removes unnecessary string casting in the foreach loop that processes argument lists. The changes provide a more consistent and reliable argument format for use in function signatures.
Copy link
Contributor

@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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between f7710d8 and 0007969.
Files selected for processing (1)
  • src/MethodSignatureString.php (3 hunks)
Additional comments not posted (10)
src/MethodSignatureString.php (10)

10-10: Added use UnitEnum; to support enum types in attribute arguments.


25-28: Introduced constants to manage PHP version differences and indentation.


35-35: Utilization of constants for determining nullable string based on PHP version.


45-50: Refactored get method to use helper methods for building method signatures.


54-59: Method getDocComment simplifies the extraction of doc comments.


62-84: Enhanced attribute handling in addAttributes method, including support for PHP 8.


87-92: Method addAccessModifiers correctly adds method access modifiers to the signature.


95-107: Method addMethodSignature constructs the method signature using parameter and return type information.


113-119: Introduced formatArg method to handle UnitEnum values differently, ensuring proper string representation.


122-122: generateParameterCode method efficiently constructs parameter strings with type, default value, and other attributes.

@koriym
Copy link
Member

koriym commented May 19, 2024

@apple-x-co 少しリファクタリングするつもりが大きなものになってしまいました><
その代わり、PHPMD.CyclomaticComplexity警告抑制を取り除くことができました。

@ray-di ray-di deleted a comment from coderabbitai bot May 19, 2024
@koriym koriym requested a review from NaokiTsuchiya May 20, 2024 01:19
@koriym
Copy link
Member

koriym commented May 20, 2024

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented May 20, 2024

Actions Performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 284cec0 and 74dd135.
Files selected for processing (6)
  • src/MethodSignatureString.php (3 hunks)
  • tests/AopCodeTest.php (2 hunks)
  • tests/Fake/Annotation/FakeMarker5.php (1 hunks)
  • tests/Fake/Annotation/FakeMarker6.php (1 hunks)
  • tests/Fake/FakePhp81Enum.php (1 hunks)
  • tests/Fake/FakePhp8Types.php (2 hunks)
Files skipped from review as they are similar to previous changes (6)
  • src/MethodSignatureString.php
  • tests/AopCodeTest.php
  • tests/Fake/Annotation/FakeMarker5.php
  • tests/Fake/Annotation/FakeMarker6.php
  • tests/Fake/FakePhp81Enum.php
  • tests/Fake/FakePhp8Types.php

Copy link
Contributor

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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 74dd135 and 632473a.
Files selected for processing (2)
  • src/MethodSignatureString.php (2 hunks)
  • tests/AopCodeTest.php (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/AopCodeTest.php
Additional Context Used
GitHub Check Runs (2)
sa / Psalm failure (7)

src/MethodSignatureString.php: [failure] 68-68:
ImpureMethodCall: Cannot call a possibly-mutating method ReflectionMethod::getAttributes from a mutation-free context


src/MethodSignatureString.php: [failure] 69-69:
ImpureMethodCall: Cannot call a possibly-mutating method ReflectionAttribute::getArguments from a mutation-free context


src/MethodSignatureString.php: [failure] 103-103:
ImpureMethodCall: Cannot call a possibly-mutating method Ray\Aop\TypeString::__invoke from a mutation-free context


src/MethodSignatureString.php: [failure] 123-123:
ImpureMethodCall: Cannot call a possibly-mutating method Ray\Aop\TypeString::__invoke from a mutation-free context

cs / Coding Standards failure (3)

src/MethodSignatureString.php: [failure] 23-23:
Found multi-line doc comment with single line content, use one-line doc comment instead.

Additional comments not posted (4)
src/MethodSignatureString.php (4)

28-31: Constants for PHP version and nullable strings have been added.

These constants improve readability and maintainability by centralizing version-specific behavior.


38-38: Ensure the constructor's logic correctly handles PHP version comparisons.

Verification successful

The constructor logic in MethodSignatureString correctly handles PHP version comparisons by setting $nullableStr based on the provided $phpVersion. The comparison logic is straightforward and appears to be implemented correctly.

  • The constructor checks if $phpVersion is greater than or equal to self::PHP_VERSION_8.
  • If true, it sets $nullableStr to self::NULLABLE_PHP8.
  • Otherwise, it sets $nullableStr to self::NULLABLE_PHP7.

This logic ensures that the correct nullable string is used based on the PHP version.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the PHP version comparison logic correctly initializes `nullableStr`.

# Test: Check if the nullable string is correctly set based on PHP version.
php -r 'echo (80000 >= 80000 ? "null|" : "?");'

Length of output: 120



Script:

#!/bin/bash
# Description: Verify the constructor's logic for handling PHP version comparisons in MethodSignatureString.

# Locate the MethodSignatureString class and its constructor
ast-grep --lang php --pattern $'class MethodSignatureString {
  $$$
  public function __construct($_) {
    $$$
  }
  $$$
}'

Length of output: 232



Script:

#!/bin/bash
# Description: Verify the constructor's logic for handling PHP version comparisons in MethodSignatureString.

# Locate the MethodSignatureString class and its constructor
rg 'class MethodSignatureString' -A 20 | rg 'public function __construct'

Length of output: 154



Script:

#!/bin/bash
# Description: Extract the full constructor implementation of MethodSignatureString to verify PHP version comparison logic.

# Extract the constructor implementation from MethodSignatureString class
rg 'public function __construct' -A 20 src/MethodSignatureString.php

Length of output: 802


44-50: Refactored get method to use helper methods for building method signatures.

This refactoring enhances modularity and readability.


112-118: Handle UnitEnum values in attribute arguments.

The method formatArg correctly formats enum values, enhancing the flexibility of attribute annotations.

Removed unnecessary spaces in array definitions in tests and updated the method of handling whitespace in the MethodSignatureString. This change enhances code readability by ensuring consistent formatting standards across the codebase.
Copy link
Contributor

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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 74dd135 and b609f43.
Files selected for processing (2)
  • src/MethodSignatureString.php (2 hunks)
  • tests/AopCodeTest.php (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • tests/AopCodeTest.php
Additional Context Used
GitHub Check Runs (1)
sa / Psalm failure (7)

src/MethodSignatureString.php: [failure] 66-66:
ImpureMethodCall: Cannot call a possibly-mutating method ReflectionMethod::getAttributes from a mutation-free context


src/MethodSignatureString.php: [failure] 67-67:
ImpureMethodCall: Cannot call a possibly-mutating method ReflectionAttribute::getArguments from a mutation-free context


src/MethodSignatureString.php: [failure] 101-101:
ImpureMethodCall: Cannot call a possibly-mutating method Ray\Aop\TypeString::__invoke from a mutation-free context


src/MethodSignatureString.php: [failure] 121-121:
ImpureMethodCall: Cannot call a possibly-mutating method Ray\Aop\TypeString::__invoke from a mutation-free context

Additional comments not posted (6)
src/MethodSignatureString.php (6)

26-29: Constants PHP_VERSION_8, NULLABLE_PHP8, NULLABLE_PHP7, and INDENT have been added to manage PHP version checks and formatting. This is a good use of constants to avoid magic numbers and strings throughout the code.


36-36: The constructor now uses a ternary operation to set nullableStr based on the PHP version. This is a clear and concise way to handle version-dependent features.


42-48: Refactoring of the get method to use helper methods for building the method signature is a good practice as it improves modularity and readability.


51-56: The method getDocComment simplifies the extraction of the doc comment from a method. Using a ternary operator to check if the comment is a string before appending is a clean approach.


84-89: The addAccessModifiers method effectively gathers and appends access modifiers to the signature. This method is straightforward and correctly implemented.


119-119: The generateParameterCode method effectively constructs parameter strings for the method signature. It handles types, default values, and other parameter attributes correctly.

src/MethodSignatureString.php Outdated Show resolved Hide resolved
src/MethodSignatureString.php Show resolved Hide resolved
src/MethodSignatureString.php Show resolved Hide resolved
The method attribute fetching process has been refactored to improve readability and maintainability. The method attributes are now fetched once and stored in a variable before being iterated over, rather than fetching them directly during the iteration. The 'psalm-suppress MixedAssignment' annotation has been replaced by a 'var mixed' comment, providing easier understanding of the variable's expected value format.
@ray-di ray-di deleted a comment from coderabbitai bot May 20, 2024
@koriym koriym merged commit 41e5af6 into ray-di:2.x May 20, 2024
19 checks passed
@apple-x-co apple-x-co deleted the enum-in-attribute-args branch May 20, 2024 07:51
@ray-di ray-di deleted a comment from coderabbitai bot May 20, 2024
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.

3 participants