PHILADELPHIA — A federal judge ruled Wednesday that the Trump administration cannot cut off grants to Philadelphia over the way the city deals with immigrants who are in the country illegally.

In his ruling, Judge Michael Baylson of the Eastern District of Pennsylvania said the conditions placed on the city by the federal government in order to receive the funding are unconstitutional, "arbitrary and capricious." He also wrote that Philadelphia's policies are reasonable and appropriate.

Philadelphia has said that as a so-called sanctuary city, it will turn over immigrants to Immigration and Customs Enforcement agents only if they have a warrant signed by a judge. The city has been fighting federal efforts to block funding as a result of the policy.

Philadelphia Mayor Jim Kenney, a Democrat, called President Donald Trump a "bully" and he hailed the judge's ruling.

"It is a ruling that prevents a White House run by a bully from bullying Philadelphia into changing its policies," Kenney said at a news conference. "It is a ruling that should make clear to Attorney General [Jeff] Sessions that federal grant dollars cannot be used for political shakedowns."

Department of Justice spokesman Devin O'Malley said the department maintains it used proper authority to attach conditions to the public safety grants.

"Today's opinion from the district court in Philadelphia is a victory for criminal aliens in Philadelphia, who can continue to commit crimes in the City knowing that its leadership will protect them from federal immigration officers whose job it is to hold them accountable and remove them from the country," O'Malley wrote in an emailed statement.

Several sanctuary cities have opted to limit cooperation with government enforcement of immigration law. The Justice Department has threatened to cut off millions of dollars in federal grants to cities if they don't meet certain criteria for cooperating with immigration officials. Philadelphia's attorneys have argued that the move is unconstitutional and that it harms residents by withholding money earmarked to help buy kits to counteract opioid overdoses.

Sessions has said that cities that don't help enforce immigration law are endangering public safety, especially when it comes to sharing information about immigrants who have been accused of crimes.

A federal appeals court in April sided with Chicago in a similar dispute.

Kenney, Philadelphia’s mayor, said that Philadelphia city policies do not violate any federal laws, that the city works with federal authorities and that the city's policies do not prevent Immigration and Customs Enforcement from doing its job.

He said that immigrants are "the single greatest reason that Philadelphia has reversed five years of population loss," and that police depend on information from immigrants to solve crimes without fears that that those with information will be asked about their immigration status.

Kenney also said that the issue hits close to home. "If this was Irish immigration now, I might be in a detention center somewhere, or my kids might have been taken away from me," he said.

"When we came here, the same know-nothings and nativists of the day which exist, are Trump people today, were saying the same things about us: We were criminals, we were good for nothings, we were lowlifes, we were, you know, bad people and we should go back where we came from," Kenney said.

"It's the same fear, anger and vitriol that my folks experienced generations ago, that Mexicans, and Guatemalans and Hondurans and others are experiencing now — and Africans, and people who are Muslim," he said.