PhpStorm未识别PHPDoc的string[]类型约束,传入数字数组无报错
string[] PHPDoc Annotations Hey there, I see the issue you're facing—PhpStorm isn't flagging the mismatch when you pass an array of integers to a method annotated with @param string[] $emails. Let's get this sorted out:
First, let's confirm your PHPDoc is correctly written (yours looks perfect!):
class Slack { /** * @param string[] $emails */ public static function export($emails) : void {} } Slack::export([1,2,3]); // Should trigger an error but doesn't Slack::export(['1','2','3']); // Correct, no error expected
The problem here is likely that PhpStorm's type compatibility inspection for array elements isn't enabled or is set to a low severity level. Here's how to fix it step by step:
- Open PhpStorm's Settings:
- On Windows/Linux: Go to
File > Settings - On macOS: Go to
PhpStorm > Settings
- On Windows/Linux: Go to
- Navigate to Editor > Inspections > PHP > Type compatibility
- Find the Type mismatch option and check the box next to it
- Set the severity level to Error or Warning (this ensures you'll get a clear visual indicator for the mismatch)
If the above doesn't resolve the issue right away, try clearing PhpStorm's cache—stale cached data can sometimes interfere with proper inspections:
- Go to
File > Invalidate Caches... - Select the Invalidate and Restart option to refresh the IDE's cache and restart PhpStorm
Once you've made these changes, PhpStorm should correctly flag Slack::export([1,2,3]); as a type mismatch, since the array contains integers instead of the expected strings.
内容的提问来源于stack exchange,提问作者Chris

