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:
parent
46d163996e
commit
af5f83602e
@ -310,6 +310,8 @@
|
|||||||
<xs:element name="PossiblyNullPropertyFetch" type="IssueHandlerType" minOccurs="0" />
|
<xs:element name="PossiblyNullPropertyFetch" type="IssueHandlerType" minOccurs="0" />
|
||||||
<xs:element name="PossiblyNullReference" type="IssueHandlerType" minOccurs="0" />
|
<xs:element name="PossiblyNullReference" type="IssueHandlerType" minOccurs="0" />
|
||||||
<xs:element name="PossiblyUndefinedArrayOffset" 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="PossiblyUndefinedMethod" type="MethodIssueHandlerType" minOccurs="0" />
|
||||||
<xs:element name="PossiblyUndefinedGlobalVariable" type="IssueHandlerType" minOccurs="0" />
|
<xs:element name="PossiblyUndefinedGlobalVariable" type="IssueHandlerType" minOccurs="0" />
|
||||||
<xs:element name="PossiblyUndefinedVariable" type="IssueHandlerType" minOccurs="0" />
|
<xs:element name="PossiblyUndefinedVariable" type="IssueHandlerType" minOccurs="0" />
|
||||||
|
@ -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
|
### PossiblyUndefinedGlobalVariable
|
||||||
|
|
||||||
Emitted when trying to access a variable in the global scope that may not be defined
|
Emitted when trying to access a variable in the global scope that may not be defined
|
||||||
|
@ -1272,6 +1272,12 @@ class Config
|
|||||||
*/
|
*/
|
||||||
public static function getParentIssueType($issue_type)
|
public static function getParentIssueType($issue_type)
|
||||||
{
|
{
|
||||||
|
if ($issue_type === 'PossiblyUndefinedIntArrayOffset'
|
||||||
|
|| $issue_type === 'PossiblyUndefinedStringArrayOffset'
|
||||||
|
) {
|
||||||
|
return 'PossiblyUndefinedArrayOffset';
|
||||||
|
}
|
||||||
|
|
||||||
if (strpos($issue_type, 'Possibly') === 0) {
|
if (strpos($issue_type, 'Possibly') === 0) {
|
||||||
$stripped_issue_type = preg_replace('/^Possibly(False|Null)?/', '', $issue_type);
|
$stripped_issue_type = preg_replace('/^Possibly(False|Null)?/', '', $issue_type);
|
||||||
|
|
||||||
|
@ -25,6 +25,8 @@ use Psalm\Issue\PossiblyNullArrayAccess;
|
|||||||
use Psalm\Issue\PossiblyNullArrayAssignment;
|
use Psalm\Issue\PossiblyNullArrayAssignment;
|
||||||
use Psalm\Issue\PossiblyNullArrayOffset;
|
use Psalm\Issue\PossiblyNullArrayOffset;
|
||||||
use Psalm\Issue\PossiblyUndefinedArrayOffset;
|
use Psalm\Issue\PossiblyUndefinedArrayOffset;
|
||||||
|
use Psalm\Issue\PossiblyUndefinedIntArrayOffset;
|
||||||
|
use Psalm\Issue\PossiblyUndefinedStringArrayOffset;
|
||||||
use Psalm\IssueBuffer;
|
use Psalm\IssueBuffer;
|
||||||
use Psalm\Type;
|
use Psalm\Type;
|
||||||
use Psalm\Type\Atomic\ObjectLike;
|
use Psalm\Type\Atomic\ObjectLike;
|
||||||
@ -1263,7 +1265,7 @@ class ArrayFetchAnalyzer
|
|||||||
|
|
||||||
if (!$found_match) {
|
if (!$found_match) {
|
||||||
if (IssueBuffer::accepts(
|
if (IssueBuffer::accepts(
|
||||||
new PossiblyUndefinedArrayOffset(
|
new PossiblyUndefinedIntArrayOffset(
|
||||||
'Possibly undefined array offset \''
|
'Possibly undefined array offset \''
|
||||||
. $offset_type->getId() . '\' '
|
. $offset_type->getId() . '\' '
|
||||||
. 'is risky given expected type \''
|
. 'is risky given expected type \''
|
||||||
@ -1312,7 +1314,7 @@ class ArrayFetchAnalyzer
|
|||||||
|
|
||||||
if (!$found_match) {
|
if (!$found_match) {
|
||||||
if (IssueBuffer::accepts(
|
if (IssueBuffer::accepts(
|
||||||
new PossiblyUndefinedArrayOffset(
|
new PossiblyUndefinedStringArrayOffset(
|
||||||
'Possibly undefined array offset \''
|
'Possibly undefined array offset \''
|
||||||
. $offset_type->getId() . '\' '
|
. $offset_type->getId() . '\' '
|
||||||
. 'is risky given expected type \''
|
. 'is risky given expected type \''
|
||||||
|
6
src/Psalm/Issue/PossiblyUndefinedIntArrayOffset.php
Normal file
6
src/Psalm/Issue/PossiblyUndefinedIntArrayOffset.php
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
namespace Psalm\Issue;
|
||||||
|
|
||||||
|
class PossiblyUndefinedIntArrayOffset extends CodeIssue
|
||||||
|
{
|
||||||
|
}
|
6
src/Psalm/Issue/PossiblyUndefinedStringArrayOffset.php
Normal file
6
src/Psalm/Issue/PossiblyUndefinedStringArrayOffset.php
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?php
|
||||||
|
namespace Psalm\Issue;
|
||||||
|
|
||||||
|
class PossiblyUndefinedStringArrayOffset extends CodeIssue
|
||||||
|
{
|
||||||
|
}
|
@ -146,6 +146,15 @@ class DocumentationTest extends TestCase
|
|||||||
$this->project_analyzer->trackUnusedSuppressions();
|
$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) {
|
foreach ($error_levels as $error_level) {
|
||||||
$this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
|
$this->project_analyzer->getCodebase()->config->setCustomErrorLevel($error_level, Config::REPORT_SUPPRESS);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user