Three men have been charged after police officers were forced to take cover inside a McDonald's restaurant when two patrol cars were shot at and one was also rammed.

Two officers were in their divisional van about 3.15am on Tuesday when it was peppered with shotgun pellets on Sunbury's Horne Street, north-west of Melbourne.

They sought refuge in the nearby McDonald's, but two more officers were also shot at when they arrived for back up and police said a silver sedan then rammed the van before speeding off.

Three men were charged on Tuesday night.