Classes List

frdl\Lint\Php

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 
 100 
 101 
 102 
 103 
 104 
 105 
 106 
 107 
 108 
 109 
 110 
 111 
 112 
 113 
 114 
 115 
 116 
 117 
 118 
 119 
 120 
 121 
 122 
 123 
 124 
 125 
 126 
 127 
 128 
 129 
 130 
 131 
 132 
 133 
 134 
 135 
 136 
 137 
 138 
 139 
 140 
 141 
 142 
 143 
 144 
 145 
 146 
 147 
 148 
 149 
 150 
 151 
 152 
 153 
 154 
 155 
 156 
 157 
 158 
 159 
 160 
<?php


namespace frdl\Lint;

class Php
{

 protected $cacheDir = null;

 public function __construct($cacheDir = null){
    $this->cacheDir = $cacheDir;
 }


  public function setCacheDir($cacheDir = null){
  $this->cacheDir = $cacheDir;
  return $this;
 }


 public function getCacheDir(){
 if(null!==$this->cacheDir && !empty($this->cacheDir)){
    return $this->cacheDir;
 }
 
   if(!isset($_ENV['FRDL_HPS_CACHE_DIR']))$_ENV['FRDL_HPS_CACHE_DIR']=getenv('FRDL_HPS_CACHE_DIR');  

  $this->cacheDir = 
(  isset($_ENV['FRDL_HPS_CACHE_DIR']) && !empty($_ENV['FRDL_HPS_CACHE_DIR']))
  ? $_ENV['FRDL_HPS_CACHE_DIR'
                   : sys_get_temp_dir() . \DIRECTORY_SEPARATOR . get_current_user(). \DIRECTORY_SEPARATOR . 'cache' . \DIRECTORY_SEPARATOR ;   
  
 $this->cacheDir = rtrim($this->cacheDir'\\/'). \DIRECTORY_SEPARATOR.'lint';
 
 if(!is_dir($this->cacheDir)){
mkdir($this->cacheDir0755true); 
 }
 
 
  return $this->cacheDir;
 }

 public function lintString($source){
 $tmpfname = tempnam($this->getCacheDir(), 'frdl_lint_php');
 file_put_contents($tmpfname$source);
 $valid = $this->checkSyntax($tmpfnamefalse);
 unlink($tmpfname);
 return $valid;
 }

 public function lintFile($fileName$checkIncludes = true){    
// $o = new self;
// $o->setCacheDir($o->getCacheDir());
 return call_user_func_array([$this'checkSyntax'], [$fileName$checkIncludes]);
 }

 public static function lintStringStatic($source){
 $o = new self;
 $tmpfname = tempnam($o->getCacheDir(), 'frdl_lint_php');
 file_put_contents($tmpfname$source);
 $valid = $o->checkSyntax($tmpfnamefalse);
 unlink($tmpfname);
 return $valid;
 }

 public static function lintFileStatic($fileName$checkIncludes = true){    
 $o = new self;
 $o->setCacheDir($o->getCacheDir());
 return call_user_func_array([$o'checkSyntax'], [$fileName$checkIncludes]);
 }   


 public static function __callStatic($name$arguments){
 $o = new self;
 return call_user_func_array([$o$name], $arguments);
 }


 public function checkSyntax($fileName$checkIncludes = false){
        // If it is not a file or we can't read it throw an exception
      // if(!is_file($fileName) || !is_readable($fileName))
  if(!file_exists($fileName))
            throw new \Exception("Cannot read file ".$fileName);
       
        // Sort out the formatting of the filename
       $fileName = realpath($fileName);
       
        // Get the shell output from the syntax check command
        $output = shell_exec(sprintf('%s -l "%s"',  (new \webfan\hps\patch\PhpBinFinder())->find(), $fileName));
       
        // Try to find the parse error text and chop it off
        $syntaxError = preg_replace("/Errors parsing.*$/"""$output, -1$count);
       
        // If the error text above was matched, throw an exception containing the syntax error
        if($count > 0)
            //throw new \Exception(trim($syntaxError));
return 'Errors parsing '.print_r([$output$count],true);
       
        // If we are going to check the files includes
        if($checkIncludes)
        {
            foreach($this->getIncludes($fileNameas $include)
            {
                // Check the syntax for each include
$tCheck = $this->checkSyntax($include$checkIncludes);
               if(true!==$tCheck){
 return $tCheck;   
   }
            }
        }
 
  return true;
    }
   
   public function getIncludes($fileName)
    {
        // NOTE that any file coming into this function has already passed the syntax check, so
        // we can assume things like proper line terminations
           
        $includes = array();
        // Get the directory name of the file so we can prepend it to relative paths
        $dir = dirname($fileName);
       
        // Split the contents of $fileName about requires and includes
        // We need to slice off the first element since that is the text up to the first include/require
        $requireSplit = array_slice(preg_split('/require|include/i'file_get_contents($fileName)), 1);
       
        // For each match
        foreach($requireSplit as $string)
        {
            // Substring up to the end of the first line, i.e. the line that the require is on
            $string = substr($string0strpos($string";"));
           
            // If the line contains a reference to a variable, then we cannot analyse it
            // so skip this iteration
            if(strpos($string"$"!== false)
                continue;
           
            // Split the string about single and double quotes
            $quoteSplit = preg_split('/[\'"]/'$string);
           
            // The value of the include is the second element of the array
            // Putting this in an if statement enforces the presence of '' or "" somewhere in the include
            // includes with any kind of run-time variable in have been excluded earlier
            // this just leaves includes with constants in, which we can't do much about
            if($include = $quoteSplit[1])
            {
                // If the path is not absolute, add the dir and separator
                // Then call realpath to chop out extra separators
                if(strpos($include':'=== FALSE)
                    $include = realpath($dir.\DIRECTORY_SEPARATOR.$include);
           
                array_push($includes$include);
            }
        }
       
        return $includes;
    }

}