Factor duplicated code into a single method

[#56447064]
Merge 56447064-code-climate to master

[#56447064]