Skip to content

Commit

Permalink
Allow checking $_SESSION for null without altering type
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Jan 15, 2020
1 parent 1202ba4 commit c3edbdb
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,9 @@ function (\Psalm\Internal\Clause $c) use ($mixed_var_ids) {
) {
$var_id = ExpressionAnalyzer::getVarId($stmt->left, $context->self);

if (!$var_id || !isset($changed_var_ids[$var_id])) {
if (!$var_id
|| ($var_id !== '$_SESSION' && $var_id !== '$_SERVER' && !isset($changed_var_ids[$var_id]))
) {
if ($naive_type->from_docblock) {
if (IssueBuffer::accepts(
new \Psalm\Issue\DocblockTypeContradiction(
Expand Down
4 changes: 0 additions & 4 deletions src/Psalm/Internal/Analyzer/StatementsAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2286,10 +2286,6 @@ public function getGlobalType(string $var_id) : Type\Union
if ($this->isSuperGlobal($var_id)) {
$type = Type::getArray();

if ($var_id === '$_SESSION') {
$type->possibly_undefined = true;
}

return $type;
}

Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Type/NegatedAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ public static function reconcile(
if (!$existing_var_type->isNullable()
&& $key
&& strpos($key, '[') === false
&& $key !== '$_SESSION'
) {
foreach ($existing_var_type->getAtomicTypes() as $atomic) {
if (!$existing_var_type->hasMixed()
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Type/Reconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ public static function reconcileKeyedTypes(
&& !$result_type->hasType('iterable')
&& (!$has_isset || substr($key, -1, 1) !== ']')
&& !($statements_analyzer->getSource()->getSource() instanceof TraitAnalyzer)
&& $key !== '$_SESSION'
) {
$reconciled_parts = array_map(
function (array $new_type_part_parts): string {
Expand Down
4 changes: 4 additions & 0 deletions tests/TypeReconciliation/IssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,10 @@ function foo(array $columns, array $options) : void {
'<?php
$a = $_SESSION ?? [];',
],
'sessionIssetNull' => [
'<?php
$a = isset($_SESSION) ? $_SESSION : [];',
],
'issetSeparateNegated' => [
'<?php
function foo(?string $a, ?string $b): string {
Expand Down

0 comments on commit c3edbdb

Please sign in to comment.