Skip to content

Commit d36abfb

Browse files
committed
Declare assert-if-true for filesystem functions
1 parent 745ae5a commit d36abfb

File tree

5 files changed

+410
-0
lines changed

5 files changed

+410
-0
lines changed

conf/config.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ parameters:
127127
- ../stubs/core.stub
128128
- ../stubs/typeCheckingFunctions.stub
129129
- ../stubs/Countable.stub
130+
- ../stubs/file.stub
130131
earlyTerminatingMethodCalls: []
131132
earlyTerminatingFunctionCalls: []
132133
resultCachePath: %tmpDir%/resultCache.php

stubs/file.stub

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
<?php
2+
3+
/**
4+
* @param ?resource $context
5+
* @return Directory|false
6+
* @phpstan-assert-if-true =non-empty-string $directory
7+
*/
8+
function dir(string $directory, $context = null) {}
9+
10+
/**
11+
* @phpstan-assert-if-true =non-empty-string $directory
12+
*/
13+
function chdir(string $directory): bool {}
14+
15+
/**
16+
* @phpstan-assert-if-true =non-empty-string $directory
17+
*/
18+
function chroot(string $directory): bool {}
19+
20+
/**
21+
* @param ?resource $context
22+
* @return resource|false
23+
* @phpstan-assert-if-true =non-empty-string $directory
24+
*/
25+
function opendir(string $directory, $context = null) {}
26+
27+
/**
28+
* @param ?resource $context
29+
* @phpstan-assert-if-true =non-empty-string $directory
30+
* @return list<string>|false
31+
*/
32+
function scandir(string $directory, int $sorting_order = 0, $context = null) {}
33+
34+
/**
35+
* @phpstan-assert-if-true =non-empty-string $filename
36+
*/
37+
function is_writable(string $filename): bool {}
38+
39+
/**
40+
* @phpstan-assert-if-true =non-empty-string $filename
41+
*/
42+
function is_readable(string $filename): bool {}
43+
44+
/**
45+
* @phpstan-assert-if-true =non-empty-string $filename
46+
*/
47+
function is_executable(string $filename): bool {}
48+
49+
/**
50+
* @param ?resource $context
51+
* @phpstan-assert-if-true =non-empty-string $filename
52+
* @return list<string>|false
53+
*/
54+
function file(string $filename, int $flags = 0, $context = null) {}
55+
56+
/**
57+
* @param ?resource $context
58+
* @return string|false
59+
* @phpstan-assert-if-true =non-empty-string $filename
60+
*/
61+
function file_get_contents(string $filename, bool $use_include_path = false, $context = null, int $offset = 0, ?int $length = null) {}
62+
63+
/**
64+
* @param ?resource $context
65+
* @return 0|positive-int|false
66+
* @phpstan-assert-if-true =non-empty-string $filename
67+
*/
68+
function file_put_contents(string $filename, mixed $data, int $flags = 0, $context = null) {}
69+
70+
/**
71+
* @return 0|positive-int|false
72+
* @phpstan-assert-if-true =non-empty-string $filename
73+
*/
74+
function fileatime(string $filename) {}
75+
76+
/**
77+
* @return 0|positive-int|false
78+
* @phpstan-assert-if-true =non-empty-string $filename
79+
*/
80+
function filectime(string $filename) {}
81+
82+
/**
83+
* @return 0|positive-int|false
84+
* @phpstan-assert-if-true =non-empty-string $filename
85+
*/
86+
function filegroup(string $filename) {}
87+
88+
/**
89+
* @return 0|positive-int|false
90+
* @phpstan-assert-if-true =non-empty-string $filename
91+
*/
92+
function fileinode(string $filename) {}
93+
94+
/**
95+
* @return 0|positive-int|false
96+
* @phpstan-assert-if-true =non-empty-string $filename
97+
*/
98+
function filemtime(string $filename) {}
99+
100+
/**
101+
* @return 0|positive-int|false
102+
* @phpstan-assert-if-true =non-empty-string $filename
103+
*/
104+
function fileowner(string $filename) {}
105+
106+
/**
107+
* @return 0|positive-int|false
108+
* @phpstan-assert-if-true =non-empty-string $filename
109+
*/
110+
function fileperms(string $filename) {}
111+
112+
/**
113+
* @return 0|positive-int|false
114+
* @phpstan-assert-if-true =non-empty-string $filename
115+
*/
116+
function filesize(string $filename) {}
117+
118+
/**
119+
* @return string|false
120+
* @phpstan-assert-if-true =non-empty-string $filename
121+
*/
122+
function filetype(string $filename) {}
123+
124+
/**
125+
* @param ?resource $context
126+
* @return resource|false
127+
* @phpstan-assert-if-true =non-empty-string $filename
128+
*/
129+
function fopen(string $filename, string $mode, bool $use_include_path = false, $context = null) {}
130+
131+
/**
132+
* @return int|false
133+
* @phpstan-assert-if-true =non-empty-string $path
134+
*/
135+
function linkinfo(string $path) {}
136+
137+
/**
138+
* @return array<string, int>|false
139+
* @phpstan-assert-if-true =non-empty-string $filename
140+
*/
141+
function lstat(string $filename) {}
142+
143+
/**
144+
* @param ?resource $context
145+
* @phpstan-assert-if-true =non-empty-string $directory
146+
*/
147+
function mkdir(string $directory, int $permissions = 0777, bool $recursive = false, $context = null): bool {}
148+
149+
/**
150+
* @return 0|positive-int|false
151+
* @param ?resource $context
152+
* @phpstan-assert-if-true =non-empty-string $filename
153+
*/
154+
function readfile(string $filename, bool $use_include_path = false, $context = null) {}
155+
156+
/**
157+
* @return non-empty-string|false
158+
* @phpstan-assert-if-true =non-empty-string $path
159+
*/
160+
function readlink(string $path) {}
161+
162+
/**
163+
* @return non-empty-string|false
164+
* @phpstan-assert-if-true =non-empty-string $path
165+
*/
166+
function realpath(string $path) {}
167+
168+
/**
169+
* @param ?resource $context
170+
* @phpstan-assert-if-true =non-empty-string $directory
171+
*/
172+
function rmdir(string $directory, $context): bool {}
173+
174+
/**
175+
* @return array<string, int>|false
176+
* @phpstan-assert-if-true =non-empty-string $filename
177+
*/
178+
function stat(string $filename) {}
179+
180+
/**
181+
* @phpstan-assert-if-true =non-empty-string $filename
182+
*/
183+
function touch(string $filename, ?int $mtime, ?int $atime): bool {}
184+
185+
/**
186+
* @param ?resource $context
187+
* @phpstan-assert-if-true =non-empty-string $filename
188+
*/
189+
function unlink(string $filename, $context = null): bool {}
190+
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
<?php
2+
3+
namespace NonEmptyStringFileFamily;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class Foo
8+
{
9+
/**
10+
* @param non-empty-string $nonES
11+
* @param non-falsy-string $nonFalsyString
12+
* @param numeric-string $numericString
13+
*/
14+
public function strTypes($nonES, $nonFalsyString, $numericString) {
15+
if (dir($nonES)) {
16+
assertType('non-empty-string', $nonES);
17+
} else {
18+
assertType('non-empty-string', $nonES);
19+
}
20+
assertType('non-empty-string', $nonES);
21+
22+
if (dir($nonFalsyString)) {
23+
assertType('non-falsy-string', $nonFalsyString);
24+
}
25+
assertType('non-falsy-string', $nonFalsyString);
26+
27+
if (dir($numericString)) {
28+
assertType('non-empty-string&numeric-string', $numericString);
29+
}
30+
assertType('numeric-string', $numericString);
31+
}
32+
33+
public function fileNonEmptyStrings(string $s): void
34+
{
35+
if (dir($s)) {
36+
assertType('non-empty-string', $s);
37+
} else {
38+
assertType('string', $s);
39+
}
40+
assertType('string', $s);
41+
42+
if (chdir($s)) {
43+
assertType('non-empty-string', $s);
44+
}
45+
assertType('string', $s);
46+
47+
if (chroot($s)) {
48+
assertType('non-empty-string', $s);
49+
}
50+
assertType('string', $s);
51+
52+
if (opendir($s)) {
53+
assertType('non-empty-string', $s);
54+
}
55+
assertType('string', $s);
56+
57+
if (scandir($s)) {
58+
assertType('non-empty-string', $s);
59+
}
60+
assertType('string', $s);
61+
62+
if (is_writable($s)) {
63+
assertType('non-empty-string', $s);
64+
}
65+
assertType('string', $s);
66+
67+
if (is_readable($s)) {
68+
assertType('non-empty-string', $s);
69+
}
70+
assertType('string', $s);
71+
72+
if (is_executable($s)) {
73+
assertType('non-empty-string', $s);
74+
}
75+
assertType('string', $s);
76+
77+
if (file($s)) {
78+
assertType('non-empty-string', $s);
79+
}
80+
assertType('string', $s);
81+
82+
if (file_get_contents($s)) {
83+
assertType('non-empty-string', $s);
84+
}
85+
assertType('string', $s);
86+
87+
if (file_put_contents($s, '')) {
88+
assertType('non-empty-string', $s);
89+
}
90+
assertType('string', $s);
91+
92+
if (fileatime($s)) {
93+
assertType('non-empty-string', $s);
94+
}
95+
assertType('string', $s);
96+
97+
if (filectime($s)) {
98+
assertType('non-empty-string', $s);
99+
}
100+
assertType('string', $s);
101+
102+
if (filegroup($s)) {
103+
assertType('non-empty-string', $s);
104+
}
105+
assertType('string', $s);
106+
107+
if (fileinode($s)) {
108+
assertType('non-empty-string', $s);
109+
}
110+
assertType('string', $s);
111+
112+
if (filemtime($s)) {
113+
assertType('non-empty-string', $s);
114+
}
115+
assertType('string', $s);
116+
117+
if (fileowner($s)) {
118+
assertType('non-empty-string', $s);
119+
}
120+
assertType('string', $s);
121+
122+
if (fileperms($s)) {
123+
assertType('non-empty-string', $s);
124+
}
125+
assertType('string', $s);
126+
127+
if (filesize($s)) {
128+
assertType('non-empty-string', $s);
129+
}
130+
assertType('string', $s);
131+
132+
if (filetype($s)) {
133+
assertType('non-empty-string', $s);
134+
}
135+
assertType('string', $s);
136+
137+
if (fopen($s, "r")) {
138+
assertType('non-empty-string', $s);
139+
}
140+
assertType('string', $s);
141+
142+
if (linkinfo($s)) {
143+
assertType('non-empty-string', $s);
144+
}
145+
assertType('string', $s);
146+
147+
if (lstat($s)) {
148+
assertType('non-empty-string', $s);
149+
}
150+
assertType('string', $s);
151+
152+
if (mkdir($s)) {
153+
assertType('non-empty-string', $s);
154+
}
155+
assertType('string', $s);
156+
157+
if (readfile($s)) {
158+
assertType('non-empty-string', $s);
159+
}
160+
assertType('string', $s);
161+
162+
if (readlink($s)) {
163+
assertType('non-empty-string', $s);
164+
}
165+
assertType('string', $s);
166+
167+
if (realpath($s)) {
168+
assertType('non-empty-string', $s);
169+
}
170+
assertType('string', $s);
171+
172+
if (rmdir($s)) {
173+
assertType('non-empty-string', $s);
174+
}
175+
assertType('string', $s);
176+
177+
if (stat($s)) {
178+
assertType('non-empty-string', $s);
179+
}
180+
assertType('string', $s);
181+
182+
if (touch($s)) {
183+
assertType('non-empty-string', $s);
184+
}
185+
assertType('string', $s);
186+
187+
if (unlink($s)) {
188+
assertType('non-empty-string', $s);
189+
}
190+
assertType('string', $s);
191+
192+
}
193+
194+
}

0 commit comments

Comments
 (0)