-
Notifications
You must be signed in to change notification settings - Fork 31
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
Parse types for validity, and smarter checking #120
Conversation
I think it's pretty complete now. It parses all standard PHP and PHPDoc types, and additionally most PHPStan types, aside from conditional return types, which would require look ahead, and global constants and a weird rule about collections, which would require looking up information about classes. It's not very thorough, it'll pass a bunch of stuff that's not valid, but, aside from the previously mentioned omissions, it should give fewer false positives than the checker currently does. |
I did a diff of the output between the current checker and my change on the course directory, and looked at the first few. There were a lot of cases of parameters declared in PHPDoc as nullable, and in PHP itself not as nullable, but given a null default value. This is accepted by the checker currently, but not by my change. Apparently it's valid, but not recommended. Should it be accepted? |
Sorry for the spam, I was keen to get this out the door, but it wasn't really ready. To see how well it's working, I've categorised the differences between what the current checker reports and my change as it is now, when run on the Moodle code base (current master). I've used PHPStan to decide what is and isn't an error. It looks like there's still a few things to fix. Handling non-DNF types is beyond me, though. That's going to have to go in the too-hard basket. I'd like to get feedback on whether the code smell checks are still wanted, and if so, which ones (capitalisation mismatch in type would be difficult for me to add, though). If so, I think it would be good if these could be reported as warnings rather than errors, but I'm not sure how to go about that, any ideas? False Positives No Longer Reported
PHPDoc type includes pass by reference &: 68 Code Smells No Longer Reported Errors No Longer Reported Errors Newly Reported Code Smells Newly Reported False Positives Newly Reported
|
I've handled non-DNF types by checking that they parse correctly, and just giving up on the type checking if it gets too complicated. I think it's good to go now. Here's an analysis of the differences between the current checker and my change, as it is now: False Positives No Longer Reported
PHPDoc type includes pass by reference &: 69 Code Smells No Longer Reported
Case mismatch in type: 47
Total: 113 Errors Newly Reported Code Smells Newly Reported
Technically I don't think these are errors--they're just a valid type with something else immediately after them, but it's indicitive of a mistake. These are caught by checking that there's a space (or punctuation ,;:.) after the type. False Positives Newly Reported |
I've added in type checking for return values and class variables (previously only function parameters were type checked). To prevent too many new false positives, I've also added in scanning class hierarchies (within the current file), checking types aliases in use ... as ... directives, and knowledge of PHP predefined and SPL classes. This has kept new false positives down to 3. There are CI warnings due to a change in CI rules, but most don't relate to code I changed. |
Hi @james-cnz , We are currently actively trying to wind down this plugin and project in favour of our existing moodlehq/moodle-cs PHP_CodeSniffer ruleset. Details of these changes can be found at moodlehq/moodle-cs#30. This whole ruleset is the biggest blocker, but we're hoping to be able to use some off-the-shelf rules to replace it. I don't know what this means for this patch but it will have to change for the phpcs rulesets -- I'm just not sure of details yet. Sorry to be the bearer of bad news, Andrew |
Hi @andrewnicols, |
Hi @andrewnicols, |
Replaced by moodlehq/moodle-cs#123 |
This change parses PHPDoc types to check for validity, and also does (slightly) smarter checking between PHP types and PHPDoc types.
Type checking is based loosely on the PHPStan rule that PHPDoc types should be the same as or narrower than than PHP types (since PHPDoc types are more comprehensive, so can specify types more precisely). It recognises this as correct:
And this as not:
It's not very smart. Although it can parse object details, it doesn't understand them, and just treats all objects as plain objects. It's no PHPStan, but it's a step up from current type checking.
I tried to figure out the failing CI check, but don't understand what it means.