PHP_CodeSniffer::realpath PHP Method

realpath() public static method

Allows for phar support.
public static realpath ( string $path ) : mixed
$path string The path to use.
return mixed
    public static function realpath($path)
    {
        // Support the path replacement of ~ with the user's home directory.
        if (substr($path, 0, 2) === '~/') {
            $homeDir = getenv('HOME');
            if ($homeDir !== false) {
                $path = $homeDir . substr($path, 1);
            }
        }
        // No extra work needed if this is not a phar file.
        if (self::isPharFile($path) === false) {
            return realpath($path);
        }
        // Before trying to break down the file path,
        // check if it exists first because it will mostly not
        // change after running the below code.
        if (file_exists($path) === true) {
            return $path;
        }
        $phar = Phar::running(false);
        $extra = str_replace('phar://' . $phar, '', $path);
        $path = realpath($phar);
        if ($path === false) {
            return false;
        }
        $path = 'phar://' . $path . $extra;
        if (file_exists($path) === true) {
            return $path;
        }
        return false;
    }

Usage Example

 /**
  * Processes an unknown command line argument.
  *
  * Assumes all unknown arguments are files and folders to check.
  *
  * @param string $arg The command line argument.
  * @param int    $pos The position of the argument on the command line.
  *
  * @return void
  */
 public function processUnknownArgument($arg, $pos)
 {
     $file = PHP_CodeSniffer::realpath($arg);
     if (file_exists($file) === false) {
         if ($this->dieOnUnknownArg === false) {
             return;
         }
         echo 'ERROR: The file "' . $arg . '" does not exist.' . PHP_EOL . PHP_EOL;
         $this->printUsage();
         exit(2);
     } else {
         $this->values['files'][] = $file;
     }
 }
All Usage Examples Of PHP_CodeSniffer::realpath