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

error_log() is impure #2884

Merged
merged 2 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/functionMetadata_original.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
'count' => ['hasSideEffects' => false],
'connection_aborted' => ['hasSideEffects' => true],
'connection_status' => ['hasSideEffects' => true],
'error_log' => ['hasSideEffects' => true],
'fclose' => ['hasSideEffects' => true],
'fflush' => ['hasSideEffects' => true],
'fgetc' => ['hasSideEffects' => true],
Expand Down
1 change: 1 addition & 0 deletions resources/functionMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,7 @@
'dngettext' => ['hasSideEffects' => false],
'doubleval' => ['hasSideEffects' => false],
'error_get_last' => ['hasSideEffects' => false],
'error_log' => ['hasSideEffects' => true],
'escapeshellarg' => ['hasSideEffects' => false],
'escapeshellcmd' => ['hasSideEffects' => false],
'exp' => ['hasSideEffects' => false],
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1423,6 +1423,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10302-interface-extends.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10302-trait-extends.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10302-trait-implements.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/impure-error-log.php');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't put at the end, but somewhere random in the middle. This is to prevent conflicts when merging into 1.11.x

}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Analyser/data/impure-error-log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace ImpureErrorLog;

use function PHPStan\Testing\assertType;

$message = 'foo';
$logfile = 'bar/baz.txt';
if (!error_log($message, 3, $logfile)) {
assertType('bool', error_log($message, 3, $logfile));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please always put the code you're testing into an anonymous function. The root scope behaves a little bit differently (see Scope::canAnyVariableExist()) and when we are not testing something specific to the root scope I like to avoid issues with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right, forgot about that. thanks for the headsup.