These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Robo\Common; |
||
3 | |||
4 | trait ResourceExistenceChecker |
||
5 | { |
||
6 | /** |
||
7 | * Checks if the given input is a file or folder. |
||
8 | * |
||
9 | * @param string|string[] $resources |
||
10 | * @param string $type "file", "dir", "fileAndDir" |
||
11 | * |
||
12 | * @return bool True if no errors were encountered otherwise false. |
||
13 | */ |
||
14 | protected function checkResources($resources, $type = 'fileAndDir') |
||
15 | { |
||
16 | if (!in_array($type, ['file', 'dir', 'fileAndDir'])) { |
||
17 | throw new \InvalidArgumentException(sprintf('Invalid resource check of type "%s" used!', $type)); |
||
18 | } |
||
19 | if (is_string($resources)) { |
||
20 | $resources = [$resources]; |
||
21 | } |
||
22 | $success = true; |
||
23 | foreach ($resources as $resource) { |
||
24 | $glob = glob($resource); |
||
25 | if ($glob === false) { |
||
26 | $this->printTaskError(sprintf('Invalid glob "%s"!', $resource), $this); |
||
0 ignored issues
–
show
|
|||
27 | $success = false; |
||
28 | continue; |
||
29 | } |
||
30 | foreach ($glob as $resource) { |
||
31 | if (!$this->checkResource($resource, $type)) { |
||
32 | $success = false; |
||
33 | } |
||
34 | } |
||
35 | } |
||
36 | return $success; |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * Checks a single resource, file or directory. |
||
41 | * |
||
42 | * It will print an error as well on the console. |
||
43 | * |
||
44 | * @param string $resource File or folder. |
||
45 | * @param string $type "file", "dir", "fileAndDir" |
||
46 | * |
||
47 | * @return bool |
||
48 | */ |
||
49 | protected function checkResource($resource, $type) |
||
50 | { |
||
51 | switch ($type) { |
||
52 | case 'file': |
||
53 | if (!$this->isFile($resource)) { |
||
54 | $this->printTaskError(sprintf('File "%s" does not exist!', $resource), $this); |
||
0 ignored issues
–
show
It seems like
printTaskError() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the
Loading history...
|
|||
55 | return false; |
||
56 | } |
||
57 | return true; |
||
58 | case 'dir': |
||
59 | if (!$this->isDir($resource)) { |
||
60 | $this->printTaskError(sprintf('Directory "%s" does not exist!', $resource), $this); |
||
0 ignored issues
–
show
It seems like
printTaskError() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the
Loading history...
|
|||
61 | return false; |
||
62 | } |
||
63 | return true; |
||
64 | case 'fileAndDir': |
||
65 | if (!$this->isDir($resource) && !$this->isFile($resource)) { |
||
66 | $this->printTaskError(sprintf('File or directory "%s" does not exist!', $resource), $this); |
||
0 ignored issues
–
show
It seems like
printTaskError() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the
Loading history...
|
|||
67 | return false; |
||
68 | } |
||
69 | return true; |
||
70 | } |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Convenience method to check the often uses "source => target" file / folder arrays. |
||
75 | * |
||
76 | * @param string|array $resources |
||
77 | */ |
||
78 | protected function checkSourceAndTargetResource($resources) |
||
79 | { |
||
80 | if (is_string($resources)) { |
||
81 | $resources = [$resources]; |
||
82 | } |
||
83 | $sources = []; |
||
84 | $targets = []; |
||
85 | foreach ($resources as $source => $target) { |
||
86 | $sources[] = $source; |
||
87 | $target[] = $target; |
||
88 | } |
||
89 | $this->checkResources($sources); |
||
90 | $this->checkResources($targets); |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Wrapper method around phps is_dir() |
||
95 | * |
||
96 | * @param string $directory |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | protected function isDir($directory) |
||
101 | { |
||
102 | return is_dir($directory); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Wrapper method around phps file_exists() |
||
107 | * |
||
108 | * @param string $file |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | protected function isFile($file) |
||
113 | { |
||
114 | return file_exists($file); |
||
115 | } |
||
116 | } |
||
117 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.