Skip to content

Commit

Permalink
Identify invalid static property array assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Dec 27, 2019
1 parent 006f788 commit 2f2cd85
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,16 @@ public static function updateArrayType(
return false;
}
}
} elseif ($root_array_expr instanceof PhpParser\Node\Expr\StaticPropertyFetch
&& $root_array_expr->name instanceof PhpParser\Node\Identifier
) {
PropertyAssignmentAnalyzer::analyzeStatic(
$statements_analyzer,
$root_array_expr,
null,
$root_type,
$context
);
} elseif ($root_var_id) {
$context->vars_in_scope[$root_var_id] = $root_type;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Codebase/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
* snippet_from: int,
* snippet_to: int,
* column_from: int,
* column_to: int
* column_to: int,
* selected_text: string
* }
*
* @psalm-type TaggedCodeType = array<int, array{0: int, 1: string}>
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Internal/Codebase/CallMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CallMap
private static $loaded_php_minor_version = null;

/**
* @var array<array<string,string>>|null
* @var array<array<int|string,string>>|null
*/
private static $call_map = null;

Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Codebase/Scanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
* snippet_from: int,
* snippet_to: int,
* column_from: int,
* column_to: int
* column_to: int,
* selected_text: string
* }
*
* @psalm-type PoolData = array{
Expand Down
18 changes: 5 additions & 13 deletions src/Psalm/Internal/Provider/FileReferenceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
* snippet_from: int,
* snippet_to: int,
* column_from: int,
* column_to: int
* column_to: int,
* selected_text: string
* }
*
* @psalm-type TaggedCodeType = array<int, array{0: int, 1: string}>
Expand Down Expand Up @@ -63,14 +64,7 @@ class FileReferenceProvider
private static $file_references_to_missing_class_members = [];

/**
* A lookup table used for getting all the files that reference any other file
*
* @var array<string,array<string,bool>>
*/
private static $referencing_files = [];

/**
* @var array<string, array<int,string>>
* @var array<string, array<string, true>>
*/
private static $files_inheriting_classes = [];

Expand Down Expand Up @@ -187,7 +181,6 @@ function ($file_name) {
*/
public function addFileReferenceToClass(string $source_file, string $fq_class_name_lc)
{
self::$referencing_files[$source_file] = true;
self::$file_references_to_classes[$fq_class_name_lc][$source_file] = true;
}

Expand Down Expand Up @@ -310,7 +303,7 @@ public function addMethodParamUse(string $method_id, int $offset, string $refere
/**
* @param string $file
*
* @return array
* @return array<int, string>
*/
private function calculateFilesReferencingFile(Codebase $codebase, $file)
{
Expand All @@ -333,7 +326,7 @@ private function calculateFilesReferencingFile(Codebase $codebase, $file)
/**
* @param string $file
*
* @return array
* @return array<int, string>
*/
private function calculateFilesInheritingFile(Codebase $codebase, $file)
{
Expand Down Expand Up @@ -1015,7 +1008,6 @@ public function getFileMaps()
public static function clearCache()
{
self::$file_references_to_classes = [];
self::$referencing_files = [];
self::$files_inheriting_classes = [];
self::$deleted_files = null;
self::$file_references = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/IssueBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ public static function getErrorCount()
/**
* @param array<int, array{severity: string, line_from: int, line_to: int, type: string, message: string,
* file_name: string, file_path: string, snippet: string, from: int, to: int, snippet_from: int,
* snippet_to: int, column_from: int, column_to: int}> $issues_data
* snippet_to: int, column_from: int, column_to: int, selected_text: string}> $issues_data
*
* @return void
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Psalm/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -952,9 +952,9 @@ public static function tokenize($string_type, $ignore_space = true)
$was_space = false;
}

/** @var list<array{0: string, 1: int}> $type_tokens */
self::$memoized_tokens[$string_type] = $type_tokens;

/** @var list<array{0: string, 1: int}> */
return $type_tokens;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/PropertyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2819,6 +2819,24 @@ function takesA(?A $a) : void {
}',
'error_message' => 'PossiblyNullArgument',
],
'catchBadArrayStaticProperty' => [
'<?php
namespace Bar;
class Foo {}
class A {
/** @var array<string, object> */
public array $map = [];
/**
* @param string $class
*/
public function get(string $class) : void {
$this->map[$class] = 5;
}
}',
'error_message' => 'InvalidPropertyAssignmentValue'
],
];
}
}

0 comments on commit 2f2cd85

Please sign in to comment.