Unused language string detection

Unused language string detection

by Mike Churchward -
Number of replies: 5
Picture of Core developers Picture of Plugin developers Picture of Testers

Is there a tool in Moodle (or elsewhere) to flag defined language strings that are not actually used? I've been doing some massive refactoring of plugins and I'm sure such unused strings exist, and it would be good to remove them.

Average of ratings: -
In reply to Mike Churchward

Re: Unused language string detection

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Ages ago I hacked together something like this. It was never a polished tool, and I don't think I have the code any more.

The basic concept was to use CodeChecker. At the time, I had recently worked on adding some custom rules for that, and so I know  that in the 'process' method of a rule (e.g. https://github.com/moodlehq/moodle-local_codechecker/blob/master/pear/PHP/CodeSniffer/Standards/Generic/Sniffs/Functions/CallTimePassByReferenceSniff.php#L56) you get the PHP_CodeSniffer_File data structure representing the parsed PHP code for each file it processes. Therefore, it is quite easy to find calls to get_string (or, the lang string constructor) and dig out the arguments.

The tricky bit is where the arguments are variables, rather than just a literal string, but still, you can list those, and investigate them further by hand.

Then you need to look at code like https://github.com/moodlehq/moodle-local_codechecker/blob/master/index.php#L67 for invoking PHP_CodeSniffer with a given custom ruleset (which in this case would be just the one sniff), and doing stuff with the results.

In reply to Tim Hunt

Re: Unused language string detection

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

Thanks Tim.

I was hoping something already existed, but using codechecker is likely the best way forward. I played with creating sniffs a couple of years ago. I'll have to brush up on that again.

In reply to Mike Churchward

Re: Unused language string detection

by Mike Churchward -
Picture of Core developers Picture of Plugin developers Picture of Testers

I know the plugins database does some string checking. I can't remember if it identifies missing or unused. Hoping David Mudrack can see this and chime in.


In reply to Mike Churchward

Re: Unused language string detection

by Michael Aherne -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

I've often wished there was something that would do this the other way round, i.e. find get_string() calls where the string doesn't exist.

In reply to Mike Churchward

Re: Unused language string detection

by David Mudrák -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Plugins guardians Picture of Testers Picture of Translators

Many years ago, I had a simple grep-based script that was used for cleaning up strings for Moodle 2.0. The relevant issue is MDL-16769. The script is available at https://github.com/moodlehq/moodle-local_amos/blob/master/bin/detect.sh and may be used as an inspiration for a custom solution. Please feel encouraged to share eventual improvements.

Average of ratings: Useful (3)