-
Notifications
You must be signed in to change notification settings - Fork 506
Fix narrowing of superglobals #3901
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Superglobals; | ||
|
||
use function PHPStan\Testing\assertNativeType; | ||
use function PHPStan\Testing\assertType; | ||
|
||
class Superglobals | ||
{ | ||
|
||
public function originalTypes(): void | ||
{ | ||
assertType('array<mixed>', $GLOBALS); | ||
assertType('array<mixed>', $_SERVER); | ||
assertType('array<mixed>', $_GET); | ||
assertType('array<mixed>', $_POST); | ||
assertType('array<mixed>', $_FILES); | ||
assertType('array<mixed>', $_COOKIE); | ||
assertType('array<mixed>', $_SESSION); | ||
assertType('array<mixed>', $_REQUEST); | ||
assertType('array<mixed>', $_ENV); | ||
} | ||
|
||
public function canBeOverwritten(): void | ||
{ | ||
$GLOBALS = []; | ||
assertType('array{}', $GLOBALS); | ||
assertNativeType('array{}', $GLOBALS); | ||
} | ||
|
||
public function canBePartlyOverwritten(): void | ||
{ | ||
$GLOBALS['foo'] = 'foo'; | ||
assertType("non-empty-array&hasOffsetValue('foo', 'foo')", $GLOBALS); | ||
assertNativeType("non-empty-array&hasOffsetValue('foo', 'foo')", $GLOBALS); | ||
} | ||
|
||
public function canBeNarrowed(): void | ||
{ | ||
if (isset($GLOBALS['foo'])) { | ||
assertType("non-empty-array&hasOffsetValue('foo', mixed~null)", $GLOBALS); | ||
assertNativeType("non-empty-array<mixed>&hasOffset('foo')", $GLOBALS); // https://github.com/phpstan/phpstan/issues/8395 | ||
} else { | ||
assertType('array<mixed>', $GLOBALS); | ||
assertNativeType('array<mixed>', $GLOBALS); | ||
} | ||
assertType('array<mixed>', $GLOBALS); | ||
assertNativeType('array<mixed>', $GLOBALS); | ||
} | ||
|
||
} | ||
|
||
function functionScope() { | ||
assertType('array<mixed>', $GLOBALS); | ||
assertNativeType('array<mixed>', $GLOBALS); | ||
} | ||
|
||
assertType('array<mixed>', $GLOBALS); | ||
assertNativeType('array<mixed>', $GLOBALS); | ||
|
||
function badNarrowing() { | ||
if (empty($_GET['id'])) { | ||
echo "b"; | ||
} else { | ||
echo "b"; | ||
} | ||
assertType('array<mixed>', $_GET); | ||
assertType('mixed', $_GET['id']); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug12772; | ||
|
||
class HelloWorld | ||
{ | ||
public function sayHello(DateTimeImutable $date): void | ||
{ | ||
foreach ($tables as $table) { | ||
|
||
// If view exists, and 'add drop view' is selected: Drop it! | ||
if ($_POST['what'] !== 'nocopy' && isset($_POST['drop_if_exists']) && $_POST['drop_if_exists'] === 'true') { | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bug12771; | ||
|
||
final class ErrorReportController | ||
{ | ||
|
||
public function __invoke(ServerRequest $request) | ||
{ | ||
if ( | ||
isset($_SESSION['prev_error_subm_time'], $_SESSION['error_subm_count']) | ||
&& $_SESSION['error_subm_count'] >= 3 | ||
&& ($_SESSION['prev_error_subm_time'] - time()) <= 3000 | ||
) { | ||
$_SESSION['error_subm_count'] = 0; | ||
$_SESSION['prev_errors'] = ''; | ||
} else { | ||
$_SESSION['prev_error_subm_time'] = time(); | ||
$_SESSION['error_subm_count'] = isset($_SESSION['error_subm_count']) | ||
? $_SESSION['error_subm_count'] + 1 | ||
: 0; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add also
$_ENV['CI']
test explicitly, as I want to make sure GitHub CI environment variable is never assumed to exist on runtime - https://github.com/atk4/ui/blob/235416b31066827226e462f1916d9fc99c0d14b4/demos/init-db.php#L556. And thank you very much for looking into this issue!Our reproducible environment is the same as here, GitHub CI, so as long as the tests passes here, our should pass then too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get your point, but adaptions here are not related to any environment variables. If Ondřej is fine with this, I expect it to be merged very soon and then we should also see the effects