Skip to content

Commit

Permalink
Partially fix vimeo#9698
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Oct 20, 2023
1 parent 24168f6 commit 36c2eee
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 6 deletions.
11 changes: 11 additions & 0 deletions src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,17 @@ public static function handleIterable(
'key',
);

if ($iterator_value_type->ignore_nullable_issues_foreach) {

Check failure on line 922 in src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php

View workflow job for this annotation

GitHub Actions / build

PossiblyNullPropertyFetch

src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php:922:25: PossiblyNullPropertyFetch: Cannot get property on possibly null variable $iterator_value_type of type Psalm\Type\Union|null (see https://psalm.dev/082)
$iterator_value_type = $iterator_value_type->setProperties([

Check failure on line 923 in src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php

View workflow job for this annotation

GitHub Actions / build

PossiblyNullReference

src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php:923:70: PossiblyNullReference: Cannot call method setProperties on possibly null value (see https://psalm.dev/083)
'ignore_nullable_issues' => true
]);
}
if ($iterator_key_type->ignore_nullable_issues_foreach) {

Check failure on line 927 in src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php

View workflow job for this annotation

GitHub Actions / build

PossiblyNullPropertyFetch

src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php:927:25: PossiblyNullPropertyFetch: Cannot get property on possibly null variable $iterator_key_type of type Psalm\Type\Union|null (see https://psalm.dev/082)
$iterator_key_type = $iterator_key_type->setProperties([

Check failure on line 928 in src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php

View workflow job for this annotation

GitHub Actions / build

PossiblyNullReference

src/Psalm/Internal/Analyzer/Statements/Block/ForeachAnalyzer.php:928:66: PossiblyNullReference: Cannot call method setProperties on possibly null value (see https://psalm.dev/083)
'ignore_nullable_issues' => true
]);
}

if ($iterator_value_type && !$iterator_value_type->isMixed()) {
$value_type = Type::combineUnionTypes($value_type, $iterator_value_type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,9 @@ public static function analyze(
if ($stmt_var_type->ignore_nullable_issues) {
$stmt_type->ignore_nullable_issues = true;
}
if ($stmt_var_type->ignore_nullable_issues_foreach) {
$stmt_type->ignore_nullable_issues_foreach = true;
}
$stmt_type = $stmt_type->freeze();
$statements_analyzer->node_data->setType($stmt, $stmt_type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Psalm\Internal\PhpVisitor\Reflector;

use AssertionError;
use Iterator;
use PhpParser;
use Psalm\Aliases;
use Psalm\CodeLocation;
Expand Down Expand Up @@ -1040,15 +1041,18 @@ private static function handleReturn(
);
}

// we make sure we only add ignore flag for internal stubs if the config is set to true
if ($docblock_info->ignore_nullable_return
&& $storage->return_type
&& ($codebase->config->ignore_internal_nullable_issues
|| !in_array($file_storage->file_path, $codebase->config->internal_stubs)
)
) {
/** @psalm-suppress InaccessibleProperty We just created this type */
$storage->return_type->ignore_nullable_issues = true;
// we make sure we only add ignore flag for internal stubs if the config is set to true
if ($codebase->config->ignore_internal_nullable_issues
|| !in_array($file_storage->file_path, $codebase->config->internal_stubs)
) {
/** @psalm-suppress InaccessibleProperty We just created this type */
$storage->return_type->ignore_nullable_issues = true;
} elseif ($storage instanceof MethodStorage && $storage->defining_fqcln === Iterator::class) {
$storage->return_type->ignore_nullable_issues_foreach = true;

Check failure on line 1054 in src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php

View workflow job for this annotation

GitHub Actions / build

InaccessibleProperty

src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeDocblockScanner.php:1054:17: InaccessibleProperty: Psalm\Type\Union::$ignore_nullable_issues_foreach is marked readonly (see https://psalm.dev/054)
}
}

// we make sure we only add ignore flag for internal stubs if the config is set to true
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Type/TypeExpander.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public static function expandUnion(
);

$fleshed_out_type->from_docblock = $return_type->from_docblock;
$fleshed_out_type->ignore_nullable_issues_foreach = $return_type->ignore_nullable_issues_foreach;
$fleshed_out_type->ignore_nullable_issues = $return_type->ignore_nullable_issues;
$fleshed_out_type->ignore_falsable_issues = $return_type->ignore_falsable_issues;
$fleshed_out_type->possibly_undefined = $return_type->possibly_undefined;
Expand Down
8 changes: 8 additions & 0 deletions src/Psalm/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,10 @@ public static function combineUnionTypes(
$combined_type->ignore_nullable_issues = true;
}

if ($type_1->ignore_nullable_issues_foreach || $type_2->ignore_nullable_issues_foreach) {
$combined_type->ignore_nullable_issues_foreach = true;
}

if ($type_1->ignore_falsable_issues || $type_2->ignore_falsable_issues) {
$combined_type->ignore_falsable_issues = true;
}
Expand Down Expand Up @@ -814,6 +818,10 @@ public static function intersectUnionTypes(
$combined_type->ignore_nullable_issues = true;
}

if ($type_1->ignore_nullable_issues_foreach && $type_2->ignore_nullable_issues_foreach) {
$combined_type->ignore_nullable_issues_foreach = true;
}

if ($type_1->ignore_falsable_issues && $type_2->ignore_falsable_issues) {
$combined_type->ignore_falsable_issues = true;
}
Expand Down
11 changes: 11 additions & 0 deletions src/Psalm/Type/MutableUnion.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ final class MutableUnion implements TypeNode
*/
public $failed_reconciliation = false;

/**
* Whether or not to ignore issues with possibly-null values when using foreach
*
* @var bool
*/
public $ignore_nullable_issues_foreach = false;

/**
* Whether or not to ignore issues with possibly-null values
*
Expand Down Expand Up @@ -398,6 +405,10 @@ public function substitute($old_type, $new_type = null): self
$this->ignore_nullable_issues = true;
}

if ($new_type && $new_type->ignore_nullable_issues_foreach) {
$this->ignore_nullable_issues_foreach = true;
}

if ($new_type && $new_type->ignore_falsable_issues) {
$this->ignore_falsable_issues = true;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Psalm/Type/Reconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,10 @@ private static function getValueForKey(
/** @psalm-suppress InaccessibleProperty We just created this type */
$new_base_type_candidate->ignore_nullable_issues = true;
}
if ($existing_keys[$base_key]->ignore_nullable_issues_foreach) {
/** @psalm-suppress InaccessibleProperty We just created this type */
$new_base_type_candidate->ignore_nullable_issues_foreach = true;
}
} elseif ($existing_key_type_part instanceof TClassStringMap) {
return Type::getMixed();
} elseif ($existing_key_type_part instanceof TNever
Expand Down
8 changes: 8 additions & 0 deletions src/Psalm/Type/Union.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ final class Union implements TypeNode
*/
public $ignore_nullable_issues = false;


/**
* Whether or not to ignore issues with possibly-null values when using foreach
*
* @var bool
*/
public $ignore_nullable_issues_foreach = false;

/**
* Whether or not to ignore issues with possibly-false values
*
Expand Down

0 comments on commit 36c2eee

Please sign in to comment.