Skip to content
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

Treat #[Pure(true)] in PhpStorm stubs as hasSideEffects => true #3880

Merged
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
15 changes: 12 additions & 3 deletions bin/functionMetadata_original.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,13 @@
'chown' => ['hasSideEffects' => true],
'copy' => ['hasSideEffects' => true],
'count' => ['hasSideEffects' => false],
'connection_aborted' => ['hasSideEffects' => true],
'connection_status' => ['hasSideEffects' => true],
'error_log' => ['hasSideEffects' => true],
'fclose' => ['hasSideEffects' => true],
'fflush' => ['hasSideEffects' => true],
'fgetc' => ['hasSideEffects' => true],
'fgetcsv' => ['hasSideEffects' => true],
'fgets' => ['hasSideEffects' => true],
'fgetss' => ['hasSideEffects' => true],
'file_get_contents' => ['hasSideEffects' => true],
'file_put_contents' => ['hasSideEffects' => true],
'flock' => ['hasSideEffects' => true],
'fopen' => ['hasSideEffects' => true],
Expand All @@ -98,6 +95,18 @@
'mb_str_pad' => ['hasSideEffects' => false],
'mkdir' => ['hasSideEffects' => true],
'move_uploaded_file' => ['hasSideEffects' => true],
'ob_clean' => ['hasSideEffects' => true],
'ob_end_clean' => ['hasSideEffects' => true],
'ob_end_flush' => ['hasSideEffects' => true],
'ob_flush' => ['hasSideEffects' => true],
'ob_get_clean' => ['hasSideEffects' => true],
'ob_get_contents' => ['hasSideEffects' => true],
'ob_get_length' => ['hasSideEffects' => true],
'ob_get_level' => ['hasSideEffects' => true],
'ob_get_status' => ['hasSideEffects' => true],
'ob_list_handlers' => ['hasSideEffects' => true],
'output_add_rewrite_var' => ['hasSideEffects' => true],
'output_reset_rewrite_vars' => ['hasSideEffects' => true],
'pclose' => ['hasSideEffects' => true],
'popen' => ['hasSideEffects' => true],
'readfile' => ['hasSideEffects' => true],
Expand Down
74 changes: 61 additions & 13 deletions bin/generate-function-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,63 @@
/** @var string[] */
public array $functions = [];

/** @var list<string> */
public array $impureFunctions = [];

/** @var string[] */
public array $methods = [];

public function enterNode(Node $node)
{
if ($node instanceof Node\Stmt\Function_) {
assert(isset($node->namespacedName));
$functionName = $node->namespacedName->toLowerString();

foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === Pure::class) {
$this->functions[] = $node->namespacedName->toLowerString();
if ($attr->name->toString() !== Pure::class) {
continue;
}

// The following functions have side effects, but their state is managed within the PHPStan scope:
if (in_array($functionName, [
'stat',
'lstat',
'file_exists',
'is_writable',
'is_writeable',
'is_readable',
'is_executable',
'is_file',
'is_dir',
'is_link',
'filectime',
'fileatime',
'filemtime',
'fileinode',
'filegroup',
'fileowner',
'filesize',
'filetype',
'fileperms',
'ftell',
'ini_get',
'function_exists',
'json_last_error',
'json_last_error_msg',
], true)) {
$this->functions[] = $functionName;
break 2;
}

// PhpStorm stub's #[Pure(true)] means the function has side effects but its return value is important.
// In PHPStan's criteria, these functions are simply considered as ['hasSideEffect' => true].
if (isset($attr->args[0]->value->name->name) && $attr->args[0]->value->name->name === 'true') {
$this->impureFunctions[] = $functionName;
} else {
$this->functions[] = $functionName;
}
break 2;
}
}
}
Expand Down Expand Up @@ -74,26 +119,29 @@ public function enterNode(Node $node)
);
}

/** @var array<string, array{hasSideEffects: bool}> $metadata */
$metadata = require __DIR__ . '/functionMetadata_original.php';
foreach ($visitor->functions as $functionName) {
if (array_key_exists($functionName, $metadata)) {
if ($metadata[$functionName]['hasSideEffects']) {
if (in_array($functionName, [
'mt_rand',
'rand',
'random_bytes',
'random_int',
'connection_aborted',
'connection_status',
'file_get_contents',
], true)) {
continue;
}
throw new ShouldNotHappenException($functionName);
}
}
$metadata[$functionName] = ['hasSideEffects' => false];
}
foreach ($visitor->impureFunctions as $functionName) {
if (array_key_exists($functionName, $metadata)) {
if (in_array($functionName, [
'ob_get_contents',
], true)) {
continue;
}
if ($metadata[$functionName]['hasSideEffects']) {
throw new ShouldNotHappenException($functionName);
}
}
$metadata[$functionName] = ['hasSideEffects' => true];
}

foreach ($visitor->methods as $methodName) {
if (array_key_exists($methodName, $metadata)) {
Expand Down
Loading
Loading