General developer forum

Unused language string detection

 
Picture of Mike Churchward
Unused language string detection
Core developersParticularly helpful MoodlersPlugin developersPlugins guardiansTesters

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: -
Tim at Lone Pine Koala Sanctuary
Re: Unused language string detection
Core developersDocumentation writersParticularly helpful MoodlersPlugin 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.

 
Average of ratings: -
Picture of Mike Churchward
Re: Unused language string detection
Core developersParticularly helpful MoodlersPlugin developersPlugins guardiansTesters

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.

 
Average of ratings: -
Picture of Mike Churchward
Re: Unused language string detection
Core developersParticularly helpful MoodlersPlugin developersPlugins guardiansTesters

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.


 
Average of ratings: -
Picture of Michael Aherne
Re: Unused language string detection
Core developersParticularly helpful MoodlersPlugin 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.

 
Average of ratings: -
Picture of David Mudrák
Re: Unused language string detection
Core developersDocumentation writersMoodle HQParticularly helpful MoodlersPlugin developersPlugins guardiansTestersTranslators

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)