[Debug] fixed CS

This commit is contained in:
Fabien Potencier 2013-07-23 20:05:03 +02:00
parent 667194574d
commit a0b1585d3c
2 changed files with 49 additions and 76 deletions

View File

@ -162,7 +162,7 @@ class ErrorHandler
$this->handleFatalError($error);
}
public function handleFatalError($error)
private function handleFatalError($error)
{
// get current exception handler
$exceptionHandler = set_exception_handler(function() {});
@ -173,35 +173,35 @@ class ErrorHandler
$message = sprintf('%s: %s in %s line %d', $level, $error['message'], $error['file'], $error['line']);
$exception = new FatalErrorException($message, 0, $error['type'], $error['file'], $error['line']);
if ($this->handleUndefinedFunctionError($exceptionHandler[0], $error, $exception)) {
return;
if ($ex = $this->handleUndefinedFunctionError($error, $exception)) {
return $exceptionHandler[0]->handle($ex);
}
if ($this->handleClassNotFoundError($exceptionHandler[0], $error, $exception)) {
return;
if ($ex = $this->handleClassNotFoundError($error, $exception)) {
return $exceptionHandler[0]->handle($ex);
}
$exceptionHandler[0]->handle($exception);
}
}
private function handleUndefinedFunctionError($exceptionHandler, $error, $exception)
private function handleUndefinedFunctionError($error, $exception)
{
$messageLen = strlen($error['message']);
$notFoundSuffix = "()";
$notFoundSuffix = '()';
$notFoundSuffixLen = strlen($notFoundSuffix);
if ($notFoundSuffixLen > $messageLen) {
return false;
return;
}
if (0 !== substr_compare($error['message'], $notFoundSuffix, -$notFoundSuffixLen)) {
return false;
return;
}
$prefix = "Call to undefined function ";
$prefix = 'Call to undefined function ';
$prefixLen = strlen($prefix);
if (0 !== strpos($error['message'], $prefix)) {
return false;
return;
}
$fullyQualifiedFunctionName = substr($error['message'], $prefixLen, -$notFoundSuffixLen);
@ -209,7 +209,7 @@ class ErrorHandler
$functionName = substr($fullyQualifiedFunctionName, $namespaceSeparatorIndex + 1);
$namespacePrefix = substr($fullyQualifiedFunctionName, 0, $namespaceSeparatorIndex);
$message = sprintf(
"Attempted to call function '%s' from namespace '%s' in %s line %d.",
'Attempted to call function "%s" from namespace "%s" in %s line %d.',
$functionName,
$namespacePrefix,
$error['file'],
@ -218,7 +218,7 @@ class ErrorHandler
} else {
$functionName = $fullyQualifiedFunctionName;
$message = sprintf(
"Attempted to call function '%s' from the global namespace in %s line %d.",
'Attempted to call function "%s" from the global namespace in %s line %d.',
$functionName,
$error['file'],
$error['line']
@ -241,34 +241,29 @@ class ErrorHandler
}
if ($candidates) {
$message .= " Did you mean to call: " . implode(", ", array_map(function ($val) {
return "'".$val."'";
}, $candidates)). "?";
$message .= ' Did you mean to call: '.implode(', ', array_map(function ($val) {
return '"'.$val.'"';
}, $candidates)).'?';
}
$exceptionHandler->handle(new UndefinedFunctionException(
$message,
$exception
));
return true;
return new UndefinedFunctionException($message, $exception);
}
private function handleClassNotFoundError($exceptionHandler, $error, $exception)
private function handleClassNotFoundError($error, $exception)
{
$messageLen = strlen($error['message']);
$notFoundSuffix = "' not found";
$notFoundSuffix = '" not found';
$notFoundSuffixLen = strlen($notFoundSuffix);
if ($notFoundSuffixLen > $messageLen) {
return false;
return;
}
if (0 !== substr_compare($error['message'], $notFoundSuffix, -$notFoundSuffixLen)) {
return false;
return;
}
foreach (array("class", "interface", "trait") as $typeName) {
$prefix = ucfirst($typeName)." '";
foreach (array('class', 'interface', 'trait') as $typeName) {
$prefix = ucfirst($typeName).' "';
$prefixLen = strlen($prefix);
if (0 !== strpos($error['message'], $prefix)) {
continue;
@ -279,7 +274,7 @@ class ErrorHandler
$className = substr($fullyQualifiedClassName, $namespaceSeparatorIndex + 1);
$namespacePrefix = substr($fullyQualifiedClassName, 0, $namespaceSeparatorIndex);
$message = sprintf(
"Attempted to load %s '%s' from namespace '%s' in %s line %d. Do you need to 'use' it from another namespace?",
'Attempted to load %s "%s" from namespace "%s" in %s line %d. Do you need to "use" it from another namespace?',
$typeName,
$className,
$namespacePrefix,
@ -289,7 +284,7 @@ class ErrorHandler
} else {
$className = $fullyQualifiedClassName;
$message = sprintf(
"Attempted to load %s '%s' from the global namespace in %s line %d. Did you forget a use statement for this %s?",
'Attempted to load %s "%s" from the global namespace in %s line %d. Did you forget a use statement for this %s?',
$typeName,
$className,
$error['file'],
@ -299,18 +294,10 @@ class ErrorHandler
}
if (isset($this->classNameToUseStatementSuggestions[$className])) {
$message .= sprintf(
" Perhaps you need to add 'use %s' at the top of this file?",
$this->classNameToUseStatementSuggestions[$className]
);
$message .= sprintf(' Perhaps you need to add "use %s" at the top of this file?', $this->classNameToUseStatementSuggestions[$className]);
}
$exceptionHandler->handle(new ClassNotFoundException(
$message,
$exception
));
return true;
return new ClassNotFoundException($message, $exception);
}
}
}

View File

@ -101,54 +101,36 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
'type' => 1,
'line' => 12,
'file' => 'foo.php',
'message' => "Class 'WhizBangFactory' not found",
'message' => 'Class "WhizBangFactory" not found',
),
"Attempted to load class 'WhizBangFactory' from the global namespace in foo.php line 12. Did you forget a use statement for this class?",
'Attempted to load class "WhizBangFactory" from the global namespace in foo.php line 12. Did you forget a use statement for this class?',
),
array(
array(
'type' => 1,
'line' => 12,
'file' => 'foo.php',
'message' => "Class 'Foo\\Bar\\WhizBangFactory' not found",
'message' => 'Class "Foo\\Bar\\WhizBangFactory" not found',
),
"Attempted to load class 'WhizBangFactory' from namespace 'Foo\\Bar' in foo.php line 12. Do you need to 'use' it from another namespace?",
'Attempted to load class "WhizBangFactory" from namespace "Foo\\Bar" in foo.php line 12. Do you need to "use" it from another namespace?',
),
array(
array(
'type' => 1,
'line' => 12,
'file' => 'foo.php',
'message' => "Class 'Request' not found",
'message' => 'Class "Request" not found',
),
"Attempted to load class 'Request' from the global namespace in foo.php line 12. Did you forget a use statement for this class? Perhaps you need to add 'use Symfony\\Component\\HttpFoundation\\Request' at the top of this file?",
'Attempted to load class "Request" from the global namespace in foo.php line 12. Did you forget a use statement for this class? Perhaps you need to add "use Symfony\\Component\\HttpFoundation\\Request" at the top of this file?',
),
array(
array(
'type' => 1,
'line' => 12,
'file' => 'foo.php',
'message' => "Class 'Foo\\Bar\\Request' not found",
'message' => 'Class "Foo\\Bar\\Request" not found',
),
"Attempted to load class 'Request' from namespace 'Foo\\Bar' in foo.php line 12. Do you need to 'use' it from another namespace? Perhaps you need to add 'use Symfony\\Component\\HttpFoundation\\Request' at the top of this file?",
),
array(
array(
'type' => 1,
'line' => 12,
'file' => 'foo.php',
'message' => "Class 'Response' not found",
),
"Attempted to load class 'Response' from the global namespace in foo.php line 12. Did you forget a use statement for this class? Perhaps you need to add 'use Symfony\\Component\\HttpFoundation\\Response' at the top of this file?",
),
array(
array(
'type' => 1,
'line' => 12,
'file' => 'foo.php',
'message' => "Class 'Foo\\Bar\\Response' not found",
),
"Attempted to load class 'Response' from namespace 'Foo\\Bar' in foo.php line 12. Do you need to 'use' it from another namespace? Perhaps you need to add 'use Symfony\\Component\\HttpFoundation\\Response' at the top of this file?",
'Attempted to load class "Request" from namespace "Foo\\Bar" in foo.php line 12. Do you need to "use" it from another namespace? Perhaps you need to add "use Symfony\\Component\\HttpFoundation\\Request" at the top of this file?',
),
);
}
@ -159,11 +141,13 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testClassNotFound($error, $translatedMessage)
{
$handler = ErrorHandler::register(3);
$m = new \ReflectionMethod($handler, 'handleFatalError');
$m->setAccessible(true);
$exceptionHandler = new MockExceptionHandler;
set_exception_handler(array($exceptionHandler, 'handle'));
$handler->handleFatalError($error);
$m->invoke($handler, $error);
$this->assertNotNull($exceptionHandler->e);
$this->assertSame($translatedMessage, $exceptionHandler->e->getMessage());
@ -183,36 +167,36 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
'type' => 1,
'line' => 12,
'file' => 'foo.php',
'message' => "Call to undefined function test_namespaced_function()",
'message' => 'Call to undefined function test_namespaced_function()',
),
"Attempted to call function 'test_namespaced_function' from the global namespace in foo.php line 12. Did you mean to call: '\\symfony\\component\\debug\\tests\\test_namespaced_function'?",
'Attempted to call function "test_namespaced_function" from the global namespace in foo.php line 12. Did you mean to call: "\\symfony\\component\\debug\\tests\\test_namespaced_function"?',
),
array(
array(
'type' => 1,
'line' => 12,
'file' => 'foo.php',
'message' => "Call to undefined function Foo\\Bar\\Baz\\test_namespaced_function()",
'message' => 'Call to undefined function Foo\\Bar\\Baz\\test_namespaced_function()',
),
"Attempted to call function 'test_namespaced_function' from namespace 'Foo\\Bar\\Baz' in foo.php line 12. Did you mean to call: '\\symfony\\component\\debug\\tests\\test_namespaced_function'?",
'Attempted to call function "test_namespaced_function" from namespace "Foo\\Bar\\Baz" in foo.php line 12. Did you mean to call: "\\symfony\\component\\debug\\tests\\test_namespaced_function"?',
),
array(
array(
'type' => 1,
'line' => 12,
'file' => 'foo.php',
'message' => "Call to undefined function foo()",
'message' => 'Call to undefined function foo()',
),
"Attempted to call function 'foo' from the global namespace in foo.php line 12.",
'Attempted to call function "foo" from the global namespace in foo.php line 12.',
),
array(
array(
'type' => 1,
'line' => 12,
'file' => 'foo.php',
'message' => "Call to undefined function Foo\\Bar\\Baz\\foo()",
'message' => 'Call to undefined function Foo\\Bar\\Baz\\foo()',
),
"Attempted to call function 'foo' from namespace 'Foo\Bar\Baz' in foo.php line 12.",
'Attempted to call function "foo" from namespace "Foo\Bar\Baz" in foo.php line 12.',
),
);
}
@ -223,11 +207,13 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testUndefinedFunction($error, $translatedMessage)
{
$handler = ErrorHandler::register(3);
$m = new \ReflectionMethod($handler, 'handleFatalError');
$m->setAccessible(true);
$exceptionHandler = new MockExceptionHandler;
set_exception_handler(array($exceptionHandler, 'handle'));
$handler->handleFatalError($error);
$m->invoke($handler, $error);
$this->assertNotNull($exceptionHandler->e);
$this->assertSame($translatedMessage, $exceptionHandler->e->getMessage());