For several months earlier this year, the publicly traded oil refiner stocks could do no wrong. Companies including Phillips 66 (PSX -0.66%), Marathon Petroleum (MPC -0.26%), HollyFrontier (HFC), and Valero Energy (VLO -0.32%) saw their profits (and by extension, stock prices) rise as they benefited from wide operating spreads.

Fast forward to today, and the situation is vastly different for the nation's biggest oil refiners. Narrowing spreads and margins are putting a serious dent in refiner profitability. Furthermore, a cascade of analyst downgrades is raining on the industry. As a result, should investors avoid oil refiners entirely?

Downgrades across the board amid worsening fundamentals
Analysts with Oppenheimer recently downgraded refiners including, but not limited to, Phillips 66, Marathon Petroleum, HollyFrontier, and Valero. This is squarely the result of a deteriorating earnings outlook, due to a number of factors such as lower spreads, higher crude acquisition costs, and therefore lower margins. The Oppenheimer downgrade is only the latest in a series of analyst downgrades for the sector in recent weeks.

Judging by results from refiners themselves, it seems that there's plenty of merit for pessimistic outlooks. Phillips 66 saw its second-quarter adjusted earnings fall by a full third. Weakness was apparent in its refining business, its biggest segment, where profits fell by nearly half.

Marathon's margins narrowed and served as the primary culprit for its own lackluster performance. Marathon Petroleum realized a gross margin of $11.13 per barrel in the second quarter of 2012, which fell to $6.18 per barrel in the second quarter of 2013. This resulted in a 23% drop in quarterly earnings, year over year.

HollyFrontier and Valero are performing more or less in-line with the industry, both struggling to deal with falling margins. HollyFrontier reported a 46% drop in second-quarter profits, and it's not hard to see why. Refinery gross margins were $20.28 per produced barrel, down 26% from $27.43 per barrel realized in the same quarter one year ago. Furthermore, expenses per barrel clocked in at $6.09, up 21% from $5 per barrel year over year.

Valero's second quarter profits fell by 43% as it realized lower production volumes and was adversely affected by higher maintenance expenses at four of the company's major refining facilities.

With all this going on, most investors would be understandably tempted to believe there's simply no light at the end of the tunnel for refiners and, consequently, no reason to invest.

Is there any reason for optimism?
Of course, commodities are cyclical, and there's always the chance that oil prices will soon become favorable to refiners once again. Plus, by taking cues from management, we can infer a certain sense of optimism among the companies themselves. For instance, each of these stocks has ratcheted up the amount of cash they're deploying to shareholders, even amid broad analyst pessimism.

Phillips 66, for example, recently upped its dividend by 25%, and since being spun off in 2012, the company has nearly doubled its dividend in that time. This represents the third dividend increase since the spinoff.

Marathon Petroleum is busy giving its investors a double-dose of cash returns. The company recently increased its dividend by 20%, raising its payout for the third time in the last two years. In addition, Marathon Petroleum also authorized an additional $2 billion for share buybacks, on top of the remaining $1.34 billion it had in its existing share repurchase program.

HollyFrontier is also throwing cash at shareholders to boost investor morale in a tough operating climate. Along with its quarterly results, the company announced both a special dividend of $0.50 per share in addition to its regular common stock dividend of $0.30 per share.

Valero bumped up its own payout by 12.5% recently, marking the second time this year the company has increased its dividend. Chairman and CEO Bill Klesse made specific mention to the fact that the company increased its dividend twice because management maintains a positive outlook for Valero, even in a tough time for refiners.

The Foolish bottom line
Deteriorating fundamentals are rarely an encouraging sign, and a heap of analyst downgrades only makes an investment decision that much tougher. There's no denying the fact that this is a hard time for oil refiners, as margins are being squeezed. At the same time, an old investing adage remains true: the best time to buy is usually when nobody else wants to.

While it's impossible to know for sure whether the operating environment for refiners will improve sooner rather than later, I have confidence that these well-run businesses will prosper once their margins become favorable once again. Plus, the increasing amount of cash they're heaping on their shareholders is a great sign that management teams of each of these companies are optimistic about what the future holds.