1
0
Fork 0

Merge pull request #4886 from curry684/issue-4885

Ensure exception is thrown when classmaps are requested for corrupted files
pull/4851/head^2
Jordi Boggiano 2016-02-06 15:53:14 +00:00
commit 9a57de574d
2 changed files with 19 additions and 12 deletions

View File

@ -122,18 +122,25 @@ class ClassMapGenerator
$extraTypes .= '|enum'; $extraTypes .= '|enum';
} }
try { // Use @ here instead of Silencer to actively suppress 'unhelpful' output
$contents = Silencer::call('php_strip_whitespace', $path); // @link https://github.com/composer/composer/pull/4886
if (!$contents) { $contents = @php_strip_whitespace($path);
if (!file_exists($path)) { if (!$contents) {
throw new \Exception('File does not exist'); if (!file_exists($path)) {
} $message = 'File at "%s" does not exist, check your classmap definitions';
if (!is_readable($path)) { } elseif (!is_readable($path)) {
throw new \Exception('File is not readable'); $message = 'File at "%s" is not readable, check its permissions';
} } elseif ('' === trim(file_get_contents($path))) {
// The input file was really empty and thus contains no classes
return array();
} else {
$message = 'File at "%s" could not be parsed as PHP, it may be binary or corrupted';
} }
} catch (\Exception $e) { $error = error_get_last();
throw new \RuntimeException('Could not scan for classes inside '.$path.": \n".$e->getMessage(), 0, $e); if (isset($error['message'])) {
$message .= PHP_EOL . 'The following message may be helpful:' . PHP_EOL . $error['message'];
}
throw new \RuntimeException(sprintf($message, $path));
} }
// return early if there is no chance of matching anything in this file // return early if there is no chance of matching anything in this file

View File

@ -113,7 +113,7 @@ class ClassMapGeneratorTest extends TestCase
/** /**
* @expectedException \RuntimeException * @expectedException \RuntimeException
* @expectedExceptionMessage Could not scan for classes inside * @expectedExceptionMessage does not exist
*/ */
public function testFindClassesThrowsWhenFileDoesNotExist() public function testFindClassesThrowsWhenFileDoesNotExist()
{ {