-
Notifications
You must be signed in to change notification settings - Fork 16
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
CONTRIB-8920 moodle-cs: Deprecation alerts for capabilities #3
base: main
Are you sure you want to change the base?
Conversation
* Add a simple deprecation warning for capabilities we might want to get rid of.
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.
I didn't dig into the rest of the code, but wanted to highlight the benefit of safer and more efficient strict checks for in_array.
$values = $file->getTokensAsString($starttoken, $endtoken - $starttoken); | ||
foreach ($alldeprecated as $capability) { | ||
if (strpos($values, $capability) !== false) { | ||
if (in_array($capability, $this->capabilitiesWarningList)) { |
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.
if (in_array($capability, $this->capabilitiesWarningList)) { | |
if (in_array($capability, $this->capabilitiesWarningList, true)) { |
* @return bool true if the current methodname is access function. | ||
*/ | ||
protected function is_an_access_function(array $token) { | ||
return in_array($token['content'], $this->accessfunctions); |
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.
return in_array($token['content'], $this->accessfunctions); | |
return in_array($token['content'], $this->accessfunctions, true); |
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.
Hi @jrchamp
Thanks for the quick feedback. I went back to the doc of in_array I have not used this parameter before, so good to know. I could probably have used isset also. I am not sure which one is quicker.
I will modify this once you will have a chance to look at the rest. The global approach needs to be validated and the big picture is: do we really get this the capabilitiesWarningList through rulseset.xml or by reading each access.php.
Thanks,
Laurent
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.
Glad it was useful! I'm just a community member, so HQ will review the rest of the code.
isset() is quicker, but would require array keys instead of array values. ['a' => true, 'b' => true]
Overall, in_array() is probably fine for this use case because O(log(n)) vs O(n) isn't a huge difference when n is small.
Side note: As long as you aren't allowing meaningful null
values, isset($a[$b]) is the faster replacement for array_key_exists($b, $a)
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.
Hi @jrchamp,
Cheers, I will take that into account once the full review is done. As we say "Make it work, Make it better"...
To give you the context will have couple of capabilities in the array so (n) will be small and as per the second in_array, the number of parameters is also very small (2 or 3 max). I knew about the isset better performances, but then again if performance is not the main issue (and here we have only a couple of elements), I prefer in_array as it is more understandable as you read the code.
I have never noticed with the third parameter of is_array thanks for the input and the detailed explanations !
Laurent
Hi @laurentdavid ! Nice one, I imagine we'll be able to introduce this once MDL-55580 is done. Said that, I've a couple of objections/ideas to comment. Haven't looked to the implementation detail but about how the deprecated capabilities are declared (both in core, in the issue, and also here, in ruleset.xml). There is some paralelism with this requirement (be aware of the deprecated caps) and what we do in various places to know the list of components available in Moodle. To get the avalable components in Moodle, we use this: https://github.com/moodlehq/moodle-cs/blob/main/moodle/Util/MoodleUtil.php#L123. Basically it:
And that's the very same approach we have followed with other "guessed" information, needed by some Sniffs to do their work. We also allow So, in the case of the deprecated capabilities... I think we should try something similar:
So, basically, from current patch... I can see some changes may be needed:
Also, completely apart from this issue...
But this point is another story (we will need it for CiBoT and how we run it). So, basically, if you're able to get 1 (and specially 2) working... then this 3rd point can be easily done. The main point is to be able to get the list of capabilties deprecated WITHOUT installing Moodle. If that's achieved... then these 1-2-3 points are really easy! Hope I've explained it clearly, in my brain this is 99% parallel to the "load components" case, that we need for a big number of sniffs (namespaces, packages...). So we need a similar "load deprecated caps" implementation. Just to avoid using different techniques (config settings, ruleset, checkout...) for being able to pass needed information to the sniffs. Ciao :-) |
public $capabilitiesWarningList = []; | ||
|
||
/** | ||
* If we try to check this capability, an error will be shown. | ||
* | ||
* @var array | ||
*/ | ||
public $capabilitiesErrorList = []; |
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.
How are these filled?
* | ||
* @var string[] | ||
*/ | ||
public $accessfunctions = ['has_access', 'has_capability']; |
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.
What about has_any_capability
, has_all_capabilities
, and the require_*
versions?
This is a follow up of MDL-55580. The idea is to add the tooling for codechecker and alert developers of possible
capabilities deprecation.