-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
Fixing Psalm errors #16
Conversation
msmakouz
commented
Sep 15, 2024
•
edited
Loading
edited
Q | A |
---|---|
Bugfix? | ✔️ |
Breaks BC? | ❌ |
New feature? | ❌ |
WalkthroughThe pull request introduces several changes across multiple files, primarily focusing on enhancing type safety and simplifying method calls related to metrics handling. Key modifications include the addition of strict typing declarations, adjustments to class constructors, and the removal of redundant prefixes in method names. These changes collectively aim to streamline code interactions and improve the clarity of the metrics-related functionality within the application. Changes
Possibly related PRs
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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
Outside diff range and nitpick comments (1)
src/MetricsOptions.php (1)
3-4
: Strict typing is a good practice for enhancing type safety.The addition of the strict type declaration is a positive change that can help catch potential bugs related to type mismatches.
Consider enabling strict typing across the codebase for consistency and to fully leverage the benefits of type safety. This may require updating other files to use strict typing and ensuring that function calls and return types are properly typed.
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (13)
- psalm.xml (1 hunks)
- src/AbstractMetrics.php (1 hunks)
- src/Collector.php (1 hunks)
- src/Exception/MetricsException.php (1 hunks)
- src/Metrics.php (6 hunks)
- src/MetricsFactory.php (1 hunks)
- src/MetricsIgnoreResponse.php (6 hunks)
- src/MetricsOptions.php (1 hunks)
- src/RetryMetrics.php (1 hunks)
- src/SuppressExceptionsMetrics.php (1 hunks)
- tests/Unit/MetricsIgnoreResponseTest.php (6 hunks)
- tests/Unit/MetricsTest.php (6 hunks)
- tests/Unit/RetryMetricsTest.php (8 hunks)
Files skipped from review due to trivial changes (2)
- src/SuppressExceptionsMetrics.php
- tests/Unit/RetryMetricsTest.php
Additional comments not posted (34)
src/Exception/MetricsException.php (1)
3-4
: Strict types declaration is a good addition, but verify the impact on the codebase.Adding
declare(strict_types=1);
is a good practice that can help catch type-related bugs early and improve code maintainability. This change aligns with the PR objective of fixing Psalm errors.However, please ensure that:
- All function calls and return statements within this file are updated to use the correct types.
- Code in other files that interacts with
MetricsException
is also updated as needed to avoid introducing new runtime errors due to type mismatches.You can use the following script to check for files that use
MetricsException
:Verification successful
Strict types declaration in MetricsException is safe and beneficial.
The addition of
declare(strict_types=1);
tosrc/Exception/MetricsException.php
is a positive change that aligns with best practices for PHP development. After analyzing the implementation of MetricsException and its usage across the codebase, we can confirm that this change:
- Doesn't introduce any breaking changes to the existing codebase.
- Is consistent with how MetricsException is currently used in both production and test code.
- May help catch potential type-related issues earlier in the development process.
The MetricsException class is simple, extending ServiceException without adding any custom methods or properties. Its usage is consistent throughout the codebase, with production code always providing the expected parameters (message, code, previous exception) when instantiating it.
This change is safe to implement and can contribute to improved code quality and maintainability.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Search for files that use or reference `MetricsException`. # Test: Search for `MetricsException` usage. Expect: List of files that need to be checked for type compatibility. rg --type php -i $'MetricsException'Length of output: 4768
Script:
#!/bin/bash # Description: Examine MetricsException implementation and usage # View the content of MetricsException.php echo "Content of MetricsException.php:" cat src/Exception/MetricsException.php echo -e "\n\nUsage of MetricsException constructor:" rg --type php -C 3 'new MetricsException'Length of output: 5354
src/AbstractMetrics.php (4)
3-3
: Good practice!Adding a strict typing declaration is a good practice to ensure type safety and catch potential bugs early.
11-11
: LGTM!The new property is correctly typed and marked as readonly, which is a good practice.
14-14
: Good catch!Changing the type of the
SERVICE_NAME
constant tonon-empty-string
enhances type safety by ensuring that the constant always has a value.
18-21
: Verify the impact on the application.The constructor ensures that an instance of
RPCInterface
is properly configured with a service prefix upon instantiation, which is a good practice.However, this change may impact how metrics are handled in the broader application context, as the
$rpc
property is now initialized with a specific service prefix.Please ensure that this change does not break any existing functionality related to metrics handling in the application.
src/MetricsFactory.php (1)
3-4
: Great addition of strict typing!The
declare(strict_types=1);
directive is a valuable addition to the file. It enables strict type mode, which enforces type safety by throwing a TypeError if a value does not match the expected type. This can help uncover hidden bugs, prevent issues related to type coercion, and improve overall code robustness.While it may require updating function/method signatures and variable types to align with the stricter type expectations, the long-term benefits of enhanced code quality and maintainability make it a worthwhile change.
src/RetryMetrics.php (1)
3-4
: Strict type declaration added. LGTM!The addition of the strict type declaration at the beginning of the file is a good practice. It enables strict type checking, which can help catch type-related bugs early and promote writing cleaner and more maintainable code.
Benefits of using strict typing:
- Enhances type safety by throwing a TypeError if a value does not match the expected type.
- Prevents runtime errors related to type mismatches.
- Encourages writing code with consistent and explicit type declarations.
Note: Existing code that relies on loose type comparisons or coercion might need to be updated to adhere to the strict typing rules.
src/Metrics.php (6)
15-15
: LGTM!The removal of the "metrics." prefix from the RPC call simplifies the code without affecting the functionality. The use of
\compact
function with a namespace prefix is a good practice for clarity.
24-24
: LGTM!The removal of the "metrics." prefix from the RPC call simplifies the code without affecting the functionality. The use of
\compact
function with a namespace prefix is a good practice for clarity.
33-33
: LGTM!The removal of the "metrics." prefix from the RPC call simplifies the code without affecting the functionality. The use of
\compact
function with a namespace prefix is a good practice for clarity.
42-42
: LGTM!The removal of the "metrics." prefix from the RPC call simplifies the code without affecting the functionality. The use of
\compact
function with a namespace prefix is a good practice for clarity.
51-56
: LGTM!The removal of the "metrics." prefix from the RPC call simplifies the code without affecting the functionality. The suppression of the "duplicate metric" error by checking for a specific error message and returning early is a good practice to handle expected errors gracefully.
68-68
: LGTM!The removal of the "metrics." prefix from the RPC call simplifies the code without affecting the functionality.
src/Collector.php (1)
3-3
: Strict typing declaration enhances code quality.The addition of the
declare(strict_types=1);
directive at the beginning of the file is a positive change that promotes type safety and helps catch potential bugs early. Strict typing ensures that the types of variables match the expected types defined in function signatures, throwing type errors when mismatches occur.This modification aligns with best practices for writing robust and maintainable PHP code. It can prevent subtle bugs related to type coercion and make the codebase more predictable.
src/MetricsIgnoreResponse.php (6)
Line range hint
23-29
: LGTM!The removal of the
metrics.
prefix from the method name is consistent with the PR objective of simplifying the method calls. The method logic is correct, and the implementation is accurate.
Line range hint
32-38
: LGTM!The removal of the
metrics.
prefix from the method name is consistent with the PR objective of simplifying the method calls. The method logic is correct, and the implementation is accurate.
Line range hint
41-47
: LGTM!The removal of the
metrics.
prefix from the method name is consistent with the PR objective of simplifying the method calls. The method logic is correct, and the implementation is accurate.
Line range hint
50-56
: LGTM!The removal of the
metrics.
prefix from the method name is consistent with the PR objective of simplifying the method calls. The method logic is correct, and the implementation is accurate.
Line range hint
59-73
: LGTM!The removal of the
metrics.
prefix from the method name is consistent with the PR objective of simplifying the method calls. The method logic is correct, and the implementation is accurate.
Line range hint
76-82
: LGTM!The removal of the
metrics.
prefix from the method name is consistent with the PR objective of simplifying the method calls. The method logic is correct, and the implementation is accurate.tests/Unit/MetricsIgnoreResponseTest.php (7)
23-26
: LGTM!The mock RPC interface setup looks good. The expectation for the
withServicePrefix
method call with 'metrics' argument is appropriate for the test cases.
34-34
: LGTM!The updated expectation for the
callIgnoreResponse
method call without the 'metrics.' prefix is consistent with the PR objective and looks good.
43-43
: LGTM!The updated expectation for the
callIgnoreResponse
method call without the 'metrics.' prefix is consistent with the PR objective and looks good.
52-52
: LGTM!The updated expectation for the
callIgnoreResponse
method call without the 'metrics.' prefix is consistent with the PR objective and looks good.
61-61
: LGTM!The updated expectation for the
callIgnoreResponse
method call without the 'metrics.' prefix is consistent with the PR objective and looks good.
75-75
: LGTM!The updated expectation for the
call
method call without the 'metrics.' prefix is consistent with the PR objective and looks good.
117-117
: LGTM!The updated expectation for the
call
method call without the 'metrics.' prefix is consistent with the PR objective and looks good.tests/Unit/MetricsTest.php (7)
24-27
: LGTM!The setup expectation for the
withServicePrefix
method is correct and ensures that the service prefix is set correctly for the metrics service.
85-85
: Verify the method name change in the codebase.The expectation for the
call
method has been updated to remove the prefix "metrics." from the method name. This change is correct and aligns with the new service structure or API.Run the following script to verify that the old method name with the prefix is not used in the codebase:
Verification successful
Verification successful: Method name change correctly implemented
The verification process confirms that the removal of the "metrics." prefix from the method name has been correctly and consistently applied across the codebase. The 'Observe' method is now being used without the prefix in various files, including tests, interfaces, and implementations.
- No occurrences of the old "metrics.Observe" pattern were found.
- Multiple instances of the new 'Observe' method usage were identified in relevant files.
This change appears to have been implemented thoroughly and consistently.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the old method name with the prefix "metrics." is not used in the codebase. # Test: Search for the old method name with the prefix. Expect: No occurrences. rg --type php $'metrics\.Observe'Length of output: 34
Script:
#!/bin/bash # Description: Verify the method name change and ripgrep functionality # Test 1: Check if ripgrep is working by searching for a common PHP keyword echo "Test 1: Checking ripgrep functionality" rg --type php "function" # Test 2: Perform a broader search for any "metrics" related method calls echo "Test 2: Searching for metrics-related method calls" rg --type php "metrics\.\w+" # Test 3: Search for the new method name "Observe" echo "Test 3: Searching for the new method name 'Observe'" rg --type php "\bObserve\b"Length of output: 10143
110-110
: Verify the method name change in the codebase.The expectation for the
call
method has been updated to remove the prefix "metrics." from the method name. This change is correct and aligns with the new service structure or API.Run the following script to verify that the old method name with the prefix is not used in the codebase:
Verification successful
Verification successful: 'metrics.' prefix removal is consistent
The removal of the 'metrics.' prefix from the 'Set' method call in the test file is consistent with the rest of the codebase. No occurrences of 'metrics.Set' were found, and all uses of the 'Set' method in metrics-related classes do not include the prefix. This change appears to be part of a broader refactoring to simplify method calls in the metrics system.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the old method name with the prefix "metrics." is not used in the codebase. # Test: Search for the old method name with the prefix. Expect: No occurrences. rg --type php $'metrics\.Set'Length of output: 30
Script:
#!/bin/bash # Description: Verify the method name change and search for potential variations # Test 1: Verify ripgrep is working correctly echo "Test 1: Verifying ripgrep functionality" rg --version # Test 2: Search for any occurrence of 'Set' method in PHP files echo "Test 2: Searching for 'Set' method in PHP files" rg --type php '\bSet\b' # Test 3: Search for 'metrics.Set' in all files echo "Test 3: Searching for 'metrics.Set' in all files" rg 'metrics\.Set' # Test 4: Search for 'metrics' to see if there are any related usages echo "Test 4: Searching for 'metrics' in all files" rg 'metrics' # Test 5: List all PHP files in the repository echo "Test 5: Listing all PHP files" find . -name "*.php"Length of output: 9482
35-35
: Verify the method name change in the codebase.The expectation for the
call
method has been updated to remove the prefix "metrics." from the method name. This change is correct and aligns with the new service structure or API.Run the following script to verify that the old method name with the prefix is not used in the codebase:
Verification successful
Verification confirms the method name change is consistent.
The removal of the "metrics." prefix from the method name in the test file is consistent with the rest of the codebase. No occurrences of the old method name with the prefix were found, and all uses of 'metrics' in the codebase are unrelated to this specific change.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the old method name with the prefix "metrics." is not used in the codebase. # Test: Search for the old method name with the prefix. Expect: No occurrences. rg --type php $'metrics\.Add'Length of output: 30
Script:
#!/bin/bash # Description: Verify the presence of PHP files and check for variations of the method name # Check for PHP files echo "Checking for PHP files:" fd -e php # Verify rg is working by searching for a common PHP keyword echo -e "\nVerifying rg functionality:" rg --type php 'function' # Perform a broader search for potential variations of the method name echo -e "\nSearching for variations of the method name:" rg --type php 'Add.*metrics|metrics.*Add' # Search for any occurrence of 'metrics' in PHP files echo -e "\nSearching for any occurrence of 'metrics' in PHP files:" rg --type php 'metrics'Length of output: 16204
140-140
: Verify the method name change in the codebase.The expectation for the
call
method has been updated to remove the prefix "metrics." from the method name. This change is correct and aligns with the new service structure or API.Run the following script to verify that the old method name with the prefix is not used in the codebase:
Verification successful
Verification confirms the method name change is consistent.
The codebase analysis shows that the removal of the "metrics." prefix from the "Declare" method is consistent throughout the project. No instances of the old "metrics.Declare" usage were found, and the change aligns with the current codebase structure.
- The old method name "metrics.Declare" is not present in any PHP files.
- The "Declare" method is referenced in test files without the "metrics." prefix.
- Other uses of 'metrics' in the codebase are unrelated to this specific method.
This verification confirms that the change in the test file is correct and doesn't introduce inconsistencies with the rest of the codebase.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the old method name with the prefix "metrics." is not used in the codebase. # Test: Search for the old method name with the prefix. Expect: No occurrences. rg --type php $'metrics\.Declare'Length of output: 34
Script:
#!/bin/bash # Description: Verify the method name change and search for potential usages # Test 1: Verify rg functionality by searching for a known PHP function echo "Test 1: Verifying rg functionality" rg --type php 'function' # Test 2: Search for variations of the old method name echo "Test 2: Searching for variations of the old method name" rg --type php -i 'metrics\s*\.\s*declare' # Test 3: Search for the new method name without the prefix echo "Test 3: Searching for the new method name" rg --type php 'Declare\s*\(' # Test 4: Search for any occurrence of 'metrics' to see if it's used in other contexts echo "Test 4: Searching for any occurrence of 'metrics'" rg --type php 'metrics'Length of output: 15952
182-182
: Verify the method name change in the codebase.The expectation for the
call
method has been updated to remove the prefix "metrics." from the method name. This change is correct and aligns with the new service structure or API.Run the following script to verify that the old method name with the prefix is not used in the codebase:
Verification successful
Verified: Method name change is correct and consistent
The removal of the "metrics." prefix from the "Unregister" method name in the test file is correct and consistent with the rest of the codebase. Our thorough search confirmed that:
- The "Unregister" method is used without the "metrics." prefix in all relevant files.
- There are no occurrences of "metrics.Unregister" in the codebase.
This change aligns with the current implementation and maintains consistency across the project.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the old method name with the prefix "metrics." is not used in the codebase. # Test: Search for the old method name with the prefix. Expect: No occurrences. rg --type php $'metrics\.Unregister'Length of output: 37
Script:
#!/bin/bash # Description: Verify ripgrep functionality and search for the new method name # Test 1: Verify ripgrep is working by searching for a common PHP keyword echo "Test 1: Searching for 'function' keyword" rg --type php 'function' # Test 2: Check for the existence of PHP files echo "Test 2: Counting PHP files" fd -e php | wc -l # Test 3: Search for the new method name without the prefix echo "Test 3: Searching for 'Unregister' method" rg --type php 'Unregister' # Test 4: Search for 'metrics' to see if it's used anywhere echo "Test 4: Searching for 'metrics'" rg --type php 'metrics'Length of output: 16712
60-60
: Verify the method name change in the codebase.The expectation for the
call
method has been updated to remove the prefix "metrics." from the method name. This change is correct and aligns with the new service structure or API.Run the following script to verify that the old method name with the prefix is not used in the codebase:
Verification successful
Removal of 'metrics.' prefix is part of a larger refactoring.
The change to remove the 'metrics.' prefix from the 'Sub' method (and other metric methods) is correct and consistent with the rest of the codebase. This modification appears to be part of a larger refactoring effort, likely related to the recent addition of an async metrics implementation (commit 9a7518e). The change has been applied consistently across all metric-related methods (Sub, Add, Observe, Set) and is reflected in the test files as well.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the old method name with the prefix "metrics." is not used in the codebase. # Test: Search for the old method name with the prefix. Expect: No occurrences. rg --type php $'metrics\.Sub'Length of output: 30
Script:
#!/bin/bash # Description: Perform a comprehensive search for method name changes # Search for variations of "metrics.Sub" in all file types echo "Searching for variations of 'metrics.Sub' in all files:" rg -i 'metrics\s*\.\s*sub' # Search for the new method name "Sub" in PHP files echo "Searching for new method name 'Sub' in PHP files:" rg --type php '\bSub\s*\(' # Check for any remaining references to "metrics." that might be related echo "Checking for remaining references to 'metrics.' in PHP files:" rg --type php 'metrics\s*\.\s*\w+'Length of output: 475
Script:
#!/bin/bash # Description: Further investigation of metrics-related code # Check the content of the specific test file echo "Content of tests/Unit/MetricsTest.php:" cat tests/Unit/MetricsTest.php # Search for any metric-related methods or classes echo "Searching for metric-related methods or classes in PHP files:" rg --type php -i '(metric|sub|add|set|observe)' # Check recent commits for major refactoring echo "Recent commits related to metrics or refactoring:" git log --oneline --grep="metric\|refactor" -n 5Length of output: 34043
@@ -13,6 +13,7 @@ | |||
<RedundantCastGivenDocblockType errorLevel="suppress" /> | |||
<RedundantCondition errorLevel="suppress" /> | |||
<DocblockTypeContradiction errorLevel="suppress" /> | |||
<MissingClassConstType errorLevel="suppress" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider the implications of suppressing MissingClassConstType
errors.
Suppressing MissingClassConstType
errors means that Psalm will no longer report issues when class constants are declared without explicit type annotations. While this allows for more flexibility, it may also introduce potential type-related issues that could go undetected.
It's generally recommended to maintain strict type checking to catch potential issues early and improve code quality. Instead of suppressing these errors, consider refactoring the codebase to use typed class constants.
If you need assistance in refactoring the codebase to use typed class constants, I'd be happy to help. We can work together to identify the affected class constants and add appropriate type annotations to ensure better type safety. Let me know if you'd like me to open a GitHub issue to track this refactoring effort.