1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Break out more specific possibly defined offset errors

This commit is contained in:
Brown 2019-11-11 09:59:56 -05:00
parent 46d163996e
commit af5f83602e
7 changed files with 59 additions and 2 deletions

View File

@ -310,6 +310,8 @@
<xs:element name="PossiblyNullPropertyFetch" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyNullReference" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyUndefinedArrayOffset" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyUndefinedIntArrayOffset" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyUndefinedStringArrayOffset" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyUndefinedMethod" type="MethodIssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyUndefinedGlobalVariable" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyUndefinedVariable" type="IssueHandlerType" minOccurs="0" />

View File

@ -1951,6 +1951,32 @@ echo $arr["b"];
```
### PossiblyUndefinedIntArrayOffset
Emitted when the config flag `ensureArrayIntOffsetsExist` is set to `true` and an integer-keyed offset is not checked for existence
```php
/**
* @param array<int, string> $arr
*/
function foo(array $arr) : void {
echo $arr[0];
}
```
### PossiblyUndefinedStringArrayOffset
Emitted when the config flag `ensureArrayStringOffsetsExist` is set to `true` and an integer-keyed offset is not checked for existence
```php
/**
* @param array<string, string> $arr
*/
function foo(array $arr) : void {
echo $arr["hello"];
}
```
### PossiblyUndefinedGlobalVariable
Emitted when trying to access a variable in the global scope that may not be defined

View File

@ -1272,6 +1272,12 @@ class Config
*/
public static function getParentIssueType($issue_type)
{
if ($issue_type === 'PossiblyUndefinedIntArrayOffset'
|| $issue_type === 'PossiblyUndefinedStringArrayOffset'
) {
return 'PossiblyUndefinedArrayOffset';
}
if (strpos($issue_type, 'Possibly') === 0) {
$stripped_issue_type = preg_replace('/^Possibly(False|Null)?/', '', $issue_type);

View File

@ -25,6 +25,8 @@ use Psalm\Issue\PossiblyNullArrayAccess;
use Psalm\Issue\PossiblyNullArrayAssignment;
use Psalm\Issue\PossiblyNullArrayOffset;
use Psalm\Issue\PossiblyUndefinedArrayOffset;
use Psalm\Issue\PossiblyUndefinedIntArrayOffset;
use Psalm\Issue\PossiblyUndefinedStringArrayOffset;
use Psalm\IssueBuffer;
use Psalm\Type;
use Psalm\Type\Atomic\ObjectLike;
@ -1263,7 +1265,7 @@ class ArrayFetchAnalyzer
if (!$found_match) {
if (IssueBuffer::accepts(
new PossiblyUndefinedArrayOffset(
new PossiblyUndefinedIntArrayOffset(
'Possibly undefined array offset \''
. $offset_type->getId() . '\' '
. 'is risky given expected type \''
@ -1312,7 +1314,7 @@ class ArrayFetchAnalyzer
if (!$found_match) {
if (IssueBuffer::accepts(
new PossiblyUndefinedArrayOffset(
new PossiblyUndefinedStringArrayOffset(
'Possibly undefined array offset \''
. $offset_type->getId() . '\' '
. 'is risky given expected type \''

View File

@ -0,0 +1,6 @@
<?php
namespace Psalm\Issue;
class PossiblyUndefinedIntArrayOffset extends CodeIssue
{
}

View File

@ -0,0 +1,6 @@
<?php
namespace Psalm\Issue;
class PossiblyUndefinedStringArrayOffset extends CodeIssue
{
}

View File

@ -146,6 +146,15 @@ class DocumentationTest extends TestCase
$this->project_analyzer->trackUnusedSuppressions();
}
$is_array_offset_test = strpos($error_message, 'ArrayOffset') && strpos($error_message, 'PossiblyUndefined') !== false;
$this->project_analyzer->getConfig()->ensure_array_string_offsets_exist = $is_array_offset_test;
$this->project_analyzer->getConfig()->ensure_array_int_offsets_exist = $is_array_offset_test;
if (strpos($error_message, 'ArrayOffset')) {
}
foreach ($error_levels as $error_level) {
$this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
}