Passed
Push — main ( 2c37f6...88fc45 )
by Sammy
01:19
created

FileSystem.class.php (1 issue)

Severity
1
<?php
2
3
namespace HexMakina\LocalFS;
4
5
class FileSystem
6
{
7
8
  private $root_path = null;
9
10
  function __construct($root_path) // create a filesystem to handle directory work
11
  {
12
    $this->root_path = $root_path;
13
  }
14
15
  public function path_to($filename)
16
  {
17
    return $this->root_path.'/'.$filename;
18
  }
19
20
  public function filenames($regex=null) : array
21
  {
22
		if(!file_exists($this->root_path) && mkdir($this->root_path) === false)
23
			return [];
24
25
    $filenames = self::preg_scandir($this->root_path, $regex);// ID_SEQUENCENUMBER.ext
26
    if(!is_null($filenames))
27
      sort($filenames);
28
29
    return $filenames;
30
  }
31
32
  public function filepathes($regex=null) : array
33
  {
34
    $filenames = $this->filenames($regex);
35
    $filepathes = [];
36
    foreach($filenames as $filename)
37
      $filepathes[] = $this->path_to($filename);
38
39
    return $filepathes;
40
  }
41
42
43
  // kontrolas ĉu la dosiero aŭ dosierujo ekzistas
44
  public static function exists($src_path){     return file_exists($src_path);}
45
  public static function is_file($src_path){    return is_file($src_path);}
46
  public static function is_dir($src_path){     return is_dir($src_path);}
47
  public static function is_link($src_path){    return is_link($src_path);}
48
49
50
  public static function resolve_symlink($path)
51
  {
52
    if(is_link($path))
53
    {
54
      $res = readlink($path);
55
      if($res === false)
56
        throw new \Exception('readlink failed on '.$path);
57
58
      $path = $res;
59
    }
60
    return $path;
61
  }
62
63
  public static function copy($src_path, $dst_path)
64
  {
65
    if(self::exists($src_path) && self::is_file($src_path))
66
    {
67
      $dst = new FilePath($dst_path);
68
      if(self::exists($dst->dir()) && self::is_dir($dst->dir()))
69
        return copy($src_path, $dst_path); // Returns TRUE on success or FALSE on failure.
70
71
      // vd(__FUNCTION__.'ERR: self::exists('.$dst->dir().') && self::is_dir('.$dst->dir().')');
72
    }
73
    // vd(__FUNCTION__."ERR: self::exists($src_path) && self::is_file($src_path)")
74
    return false;
75
  }
76
77
  public static function move($src_path, $dst_path)
78
  {
79
    if(self::exists($src_path) && self::is_file($src_path))
80
    {
81
      $dst = new FilePath($dst_path, true);
0 ignored issues
show
The call to HexMakina\LocalFS\FilePath::__construct() has too many arguments starting with true. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

81
      $dst = /** @scrutinizer ignore-call */ new FilePath($dst_path, true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
82
      if(self::exists($dst->dir()) && self::is_dir($dst->dir()))
83
      {
84
        return rename($src_path, $dst_path); // Returns TRUE on success or FALSE on failure.
85
      }
86
87
      // vd(__FUNCTION__.' ERR: self::exists('.$dst->dir().') && self::is_dir('.$dst->dir().')');
88
    }
89
    // vd(__FUNCTION__."ERR: self::exists($src_path) && self::is_file($src_path)")
90
91
    return false;
92
  }
93
94
95
  public static function make_dir($dst_path, $permission=0777, $recursive=true) : bool
96
  {
97
    return mkdir($dst_path, $permission, $recursive);
98
  }
99
100
  public static function remove($src_path, $follow_link=false)
101
  {
102
    $ret = false;
103
    if(self::exists($src_path))
104
    {
105
      if(self::is_link($src_path) && $follow_link === true)
106
        $ret = self::remove(readlink($src_path));
107
      elseif(self::is_file($src_path))
108
        $ret = unlink($src_path); // Returns TRUE on success or FALSE on failure.
109
      else if(self::is_dir($src_path))
110
        $ret = rmdir($src_path); // Returns TRUE on success or FALSE on failure.
111
    }
112
    return $ret;
113
  }
114
115
  public static function preg_scandir($dir_path, $regex=null)
116
  {
117
    if(!self::exists($dir_path) || !self::is_dir($dir_path))
118
      return null;
119
120
    if(($filenames = scandir($dir_path, SCANDIR_SORT_ASCENDING)) !== false)
121
      return is_null($regex)? $filenames : preg_grep($regex, $filenames);
122
123
    throw new \Exception("directory path '$dir_path' cannot be scanned");
124
  }
125
126
  // TODO
127
  // readdir() - Read entry from directory handle
128
  // chdir() - Change directory
129
  // dir() - Return an instance of the Directory class
130
  // opendir() - Open directory handle
131
132
  public static function server_info()
133
  {
134
    return 'PHP '.PHP_VERSION.' | OS '.PHP_OS_FAMILY.' ('.PHP_OS.') | SAPI '.PHP_SAPI.' | GENESIS '.date('Y-m-d h:i:s');
135
  }
136
}
137