Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Check syntax and other validity aspects of PHP source code.
- *
- * @param string $string A string of PHP source code without an open tag
- * @return boolean TRUE if code appears valid, FALSE otherwise
- */
- function ___check($string)
- {
- $source = '<?php ' . $string;
- $syntax_check = shell_exec(sprintf(
- 'echo %s | php -l',
- escapeshellarg($source)
- ));
- if (stripos($syntax_check, 'No syntax errors') === FALSE) {
- return FALSE;
- }
- foreach (($tokens = token_get_all($source)) as $i => $token) {
- if (is_array($token)) {
- if ($token[0] == T_STRING && $tokens[$i + 1] == '(') {
- $t = $token[1];
- $previous_token = is_array($tokens[$i - 1])
- ? $tokens[$i - 1][0]
- : $tokens[$i - 1];
- switch ($previous_token) {
- // Safe Cases
- case T_FUNCTION:
- case T_OBJECT_OPERATOR:
- break;
- case T_PAAMAYIM_NEKUDOTAYIM:
- $t = $tokens[$i - 2][1];
- $method = $token[1];
- case T_NEW:
- if (!class_exists($t)) {
- echo sprintf(
- 'Error: Class %s does not exist.%s',
- $t
- ) . PHP_EOL;
- return FALSE;
- }
- // Handle method_exists check for static methods
- if (isset($method) && !method_exists($t, $method)) {
- echo sprintf(
- 'Error: Method %s() does not exist on %s.',
- $method,
- $t
- ) . PHP_EOL;
- return FALSE;
- }
- break;
- default:
- if (!function_exists($t)) {
- echo sprintf(
- 'Error: Function %s() does not exist.',
- $t
- ) . PHP_EOL;
- return FALSE;
- }
- break;
- }
- }
- }
- }
- return TRUE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement