Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.73 KB | None | 0 0
  1.     /**
  2.      * Check syntax and other validity aspects of PHP source code.
  3.      *
  4.      * @param string $string A string of PHP source code without an open tag
  5.      * @return boolean TRUE if code appears valid, FALSE otherwise
  6.      */
  7.     function ___check($string)
  8.     {
  9.  
  10.         $source      = '<?php ' . $string;
  11.  
  12.         $syntax_check = shell_exec(sprintf(
  13.             'echo %s | php -l',
  14.             escapeshellarg($source)
  15.         ));
  16.  
  17.         if (stripos($syntax_check, 'No syntax errors') === FALSE) {
  18.             return FALSE;
  19.         }
  20.  
  21.  
  22.         foreach (($tokens = token_get_all($source)) as $i => $token) {
  23.  
  24.             if (is_array($token)) {
  25.  
  26.                 if ($token[0] == T_STRING && $tokens[$i + 1] == '(') {
  27.  
  28.                     $t = $token[1];
  29.  
  30.                     $previous_token = is_array($tokens[$i - 1])
  31.                         ? $tokens[$i - 1][0]
  32.                         : $tokens[$i - 1];
  33.  
  34.                     switch ($previous_token) {
  35.  
  36.                         // Safe Cases
  37.  
  38.                         case T_FUNCTION:
  39.                         case T_OBJECT_OPERATOR:
  40.                             break;
  41.  
  42.                         case T_PAAMAYIM_NEKUDOTAYIM:
  43.  
  44.                             $t      = $tokens[$i - 2][1];
  45.                             $method = $token[1];
  46.  
  47.                         case T_NEW:
  48.  
  49.                             if (!class_exists($t)) {
  50.                                 echo sprintf(
  51.                                     'Error:  Class %s does not exist.%s',
  52.                                     $t
  53.                                 ) . PHP_EOL;
  54.                                 return FALSE;
  55.                             }
  56.  
  57.                             // Handle method_exists check for static methods
  58.  
  59.                             if (isset($method) && !method_exists($t, $method)) {
  60.                                 echo sprintf(
  61.                                     'Error:  Method %s() does not exist on %s.',
  62.                                     $method,
  63.                                     $t
  64.                                 ) . PHP_EOL;
  65.                                 return FALSE;
  66.                             }
  67.  
  68.                             break;
  69.  
  70.                         default:
  71.                             if (!function_exists($t)) {
  72.                                 echo sprintf(
  73.                                     'Error:  Function %s() does not exist.',
  74.                                     $t
  75.                                 ) . PHP_EOL;
  76.                                 return FALSE;
  77.                             }
  78.                             break;
  79.                     }
  80.                 }
  81.             }
  82.         }
  83.  
  84.         return TRUE;
  85.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement