Skip to content

Declare assert-if-true for filesystem functions #4234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ parameters:
- ../stubs/core.stub
- ../stubs/typeCheckingFunctions.stub
- ../stubs/Countable.stub
- ../stubs/file.stub
earlyTerminatingMethodCalls: []
earlyTerminatingFunctionCalls: []
resultCachePath: %tmpDir%/resultCache.php
Expand Down
190 changes: 190 additions & 0 deletions stubs/file.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php

/**
* @param ?resource $context
* @return Directory|false
* @phpstan-assert-if-true =non-empty-string $directory
*/
function dir(string $directory, $context = null) {}

/**
* @phpstan-assert-if-true =non-empty-string $directory
*/
function chdir(string $directory): bool {}

/**
* @phpstan-assert-if-true =non-empty-string $directory
*/
function chroot(string $directory): bool {}

/**
* @param ?resource $context
* @return resource|false
* @phpstan-assert-if-true =non-empty-string $directory
*/
function opendir(string $directory, $context = null) {}

/**
* @param ?resource $context
* @phpstan-assert-if-true =non-empty-string $directory
* @return list<string>|false
*/
function scandir(string $directory, int $sorting_order = 0, $context = null) {}

/**
* @phpstan-assert-if-true =non-empty-string $filename
*/
function is_writable(string $filename): bool {}

/**
* @phpstan-assert-if-true =non-empty-string $filename
*/
function is_readable(string $filename): bool {}

/**
* @phpstan-assert-if-true =non-empty-string $filename
*/
function is_executable(string $filename): bool {}

/**
* @param ?resource $context
* @phpstan-assert-if-true =non-empty-string $filename
* @return list<string>|false
*/
function file(string $filename, int $flags = 0, $context = null) {}

/**
* @param ?resource $context
* @return string|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function file_get_contents(string $filename, bool $use_include_path = false, $context = null, int $offset = 0, ?int $length = null) {}

/**
* @param ?resource $context
* @return 0|positive-int|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function file_put_contents(string $filename, mixed $data, int $flags = 0, $context = null) {}

/**
* @return 0|positive-int|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function fileatime(string $filename) {}

/**
* @return 0|positive-int|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function filectime(string $filename) {}

/**
* @return 0|positive-int|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function filegroup(string $filename) {}

/**
* @return 0|positive-int|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function fileinode(string $filename) {}

/**
* @return 0|positive-int|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function filemtime(string $filename) {}

/**
* @return 0|positive-int|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function fileowner(string $filename) {}

/**
* @return 0|positive-int|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function fileperms(string $filename) {}

/**
* @return 0|positive-int|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function filesize(string $filename) {}

/**
* @return string|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function filetype(string $filename) {}

/**
* @param ?resource $context
* @return resource|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function fopen(string $filename, string $mode, bool $use_include_path = false, $context = null) {}

/**
* @return int|false
* @phpstan-assert-if-true =non-empty-string $path
*/
function linkinfo(string $path) {}

/**
* @return array<string, int>|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function lstat(string $filename) {}

/**
* @param ?resource $context
* @phpstan-assert-if-true =non-empty-string $directory
*/
function mkdir(string $directory, int $permissions = 0777, bool $recursive = false, $context = null): bool {}

/**
* @return 0|positive-int|false
* @param ?resource $context
* @phpstan-assert-if-true =non-empty-string $filename
*/
function readfile(string $filename, bool $use_include_path = false, $context = null) {}

/**
* @return non-empty-string|false
* @phpstan-assert-if-true =non-empty-string $path
*/
function readlink(string $path) {}

/**
* @return non-empty-string|false
* @phpstan-assert-if-true =non-empty-string $path
*/
function realpath(string $path) {}

/**
* @param ?resource $context
* @phpstan-assert-if-true =non-empty-string $directory
*/
function rmdir(string $directory, $context): bool {}

/**
* @return array<string, int>|false
* @phpstan-assert-if-true =non-empty-string $filename
*/
function stat(string $filename) {}

/**
* @phpstan-assert-if-true =non-empty-string $filename
*/
function touch(string $filename, ?int $mtime, ?int $atime): bool {}

/**
* @param ?resource $context
* @phpstan-assert-if-true =non-empty-string $filename
*/
function unlink(string $filename, $context = null): bool {}

194 changes: 194 additions & 0 deletions tests/PHPStan/Analyser/nsrt/non-empty-string-file-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
<?php

namespace NonEmptyStringFileFamily;

use function PHPStan\Testing\assertType;

class Foo
{
/**
* @param non-empty-string $nonES
* @param non-falsy-string $nonFalsyString
* @param numeric-string $numericString
*/
public function strTypes($nonES, $nonFalsyString, $numericString) {
if (dir($nonES)) {
assertType('non-empty-string', $nonES);
} else {
assertType('non-empty-string', $nonES);
}
assertType('non-empty-string', $nonES);

if (dir($nonFalsyString)) {
assertType('non-falsy-string', $nonFalsyString);
}
assertType('non-falsy-string', $nonFalsyString);

if (dir($numericString)) {
assertType('non-empty-string&numeric-string', $numericString);
}
assertType('numeric-string', $numericString);
}

public function fileNonEmptyStrings(string $s): void
{
if (dir($s)) {
assertType('non-empty-string', $s);
} else {
assertType('string', $s);
}
assertType('string', $s);

if (chdir($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (chroot($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (opendir($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (scandir($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (is_writable($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (is_readable($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (is_executable($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (file($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (file_get_contents($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (file_put_contents($s, '')) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (fileatime($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (filectime($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (filegroup($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (fileinode($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (filemtime($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (fileowner($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (fileperms($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (filesize($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (filetype($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (fopen($s, "r")) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (linkinfo($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (lstat($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (mkdir($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (readfile($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (readlink($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (realpath($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (rmdir($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (stat($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (touch($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

if (unlink($s)) {
assertType('non-empty-string', $s);
}
assertType('string', $s);

}

}
Loading
Loading