Skip to content

Commit

Permalink
Add configuration for maximum size of shaped array
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Feb 15, 2022
1 parent f72f2f6 commit 87d9a01
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<xs:attribute name="cacheDirectory" type="xs:string" />
<xs:attribute name="errorBaseline" type="xs:string" />
<xs:attribute name="maxStringLength" type="xs:string" />
<xs:attribute name="maxShapedArraySize" type="xs:string" default="100" />
<xs:attribute name="name" type="xs:string" />
<xs:attribute name="phpVersion" type="xs:string" />
<xs:attribute name="serializer" type="xs:string" />
Expand Down
10 changes: 10 additions & 0 deletions src/Psalm/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,11 @@ class Config
*/
public $max_avg_path_length = 70;

/**
* @var int
*/
public $max_shaped_array_size = 100;

/**
* @var string[]
*/
Expand Down Expand Up @@ -1080,6 +1085,11 @@ private static function fromXmlAndPaths(
$config->max_string_length = $attribute_text;
}

if (isset($config_xml['maxShapedArraySize'])) {
$attribute_text = (int)$config_xml['maxShapedArraySize'];
$config->max_shaped_array_size = $attribute_text;
}

if (isset($config_xml['inferPropertyTypesFromConstructor'])) {
$attribute_text = (string) $config_xml['inferPropertyTypesFromConstructor'];
$config->infer_property_types_from_constructor = $attribute_text === 'true' || $attribute_text === '1';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,12 @@ private static function analyzeArrayItem(
}
}

$config = $codebase->config;

if ($item_value_type = $statements_analyzer->node_data->getType($item->value)) {
if ($item_key_value !== null && count($array_creation_info->property_types) <= 100) {
if ($item_key_value !== null
&& count($array_creation_info->property_types) <= $config->max_shaped_array_size
) {
$array_creation_info->property_types[$item_key_value] = $item_value_type;
} else {
$array_creation_info->can_create_objectlike = false;
Expand All @@ -491,7 +495,9 @@ private static function analyzeArrayItem(
} else {
$array_creation_info->item_value_atomic_types[] = new TMixed();

if ($item_key_value !== null && count($array_creation_info->property_types) <= 100) {
if ($item_key_value !== null
&& count($array_creation_info->property_types) <= $config->max_shaped_array_size
) {
$array_creation_info->property_types[$item_key_value] = Type::getMixed();
} else {
$array_creation_info->can_create_objectlike = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -637,13 +637,17 @@ private static function handleArrayItem(
return false;
}

$config = $codebase->config;

$array_creation_info->all_list = $array_creation_info->all_list && $item_is_list_item;

if ($item->key instanceof PhpParser\Node\Scalar\String_
|| $item->key instanceof PhpParser\Node\Scalar\LNumber
|| !$item->key
) {
if ($item_key_value !== null && count($array_creation_info->property_types) <= 50) {
if ($item_key_value !== null
&& count($array_creation_info->property_types) <= $config->max_shaped_array_size
) {
$array_creation_info->property_types[$item_key_value] = $single_item_value_type;
} else {
$array_creation_info->can_create_objectlike = false;
Expand All @@ -657,7 +661,7 @@ private static function handleArrayItem(

if (count($dim_type->getAtomicTypes()) > 1
|| $dim_type->hasMixed()
|| count($array_creation_info->property_types) > 50
|| count($array_creation_info->property_types) > $config->max_shaped_array_size
) {
$array_creation_info->can_create_objectlike = false;
} else {
Expand Down

0 comments on commit 87d9a01

Please sign in to comment.