How to automate manual task replacing legacy MySQL passwords by new ones on WHM/cPanel server and in user's .php files?
This task may be needed in case one migrate from old MySQL to new one. In such a case the mysql passwords are automatically reseted:

The system changed the password for the database user "myzom" to a random string because the original password used an old and insecure format that is incompatible with MySQL version 5.6.17. You must manually change the password for "username" to match the original password in order to ensure that applications that use the credentials will continue to function
leaving files with old outdated passwords, causing sites unable to connect MySQL. More about that here.
So how to as quickly as possible change all the passwords in files?

Here is the process:

1. Go to new server WHM into section SQL Services > Change MySQL User Password
2. Rewrite first mysql username to the following command which you will execute on the server command line:

grep -C 4 -Ri "userhere_name" /home/userhere/public_html

3. From the results of the above command, copy old password to some temporary place, example browser search box.
4. proceed with the password reset on WHM page opened in step 1 and copy also new password.
5. WHM will say: You have successfully changed “mysqlusername”’s MySQL password.
6. Replace old password by new one in files:

for file in $(find /home/userhere/public_html -type f);do sed -i "s|oldpassword|newpassword|g" 2>/dev/null;done

---
Another approach can be to create script to list all mysql users, find .php files that contain that mysql username and also phrase: "localhost" or 'localhost'
manually read and copy old password
Do password generating/reset command for the username
Do search and replace command in files to replace old copied password by new password.

If you find easier way to complete the process, please kindly share it.