2018-02-23 21:39:33 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Storage;
|
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function array_map;
|
|
|
|
use function implode;
|
|
|
|
|
2018-02-23 21:39:33 +01:00
|
|
|
class Assertion
|
|
|
|
{
|
|
|
|
/**
|
2018-05-13 01:38:43 +02:00
|
|
|
* @var array<int, array<int, string>> the rule being asserted
|
2018-02-23 21:39:33 +01:00
|
|
|
*/
|
|
|
|
public $rule;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int|string the id of the property/variable, or
|
|
|
|
* the parameter offset of the affected arg
|
|
|
|
*/
|
|
|
|
public $var_id;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|int $var_id
|
2018-05-13 01:38:43 +02:00
|
|
|
* @param array<int, array<int, string>> $rule
|
2018-02-23 21:39:33 +01:00
|
|
|
*/
|
2019-07-04 21:05:55 +02:00
|
|
|
public function __construct($var_id, array $rule)
|
2018-02-23 21:39:33 +01:00
|
|
|
{
|
|
|
|
$this->rule = $rule;
|
|
|
|
$this->var_id = $var_id;
|
|
|
|
}
|
2019-01-23 05:42:54 +01:00
|
|
|
|
|
|
|
/**
|
2019-03-22 20:59:10 +01:00
|
|
|
* @param array<string, array<string, array{0:\Psalm\Type\Union}>> $template_type_map
|
2019-01-23 05:42:54 +01:00
|
|
|
*/
|
|
|
|
public function getUntemplatedCopy(array $template_type_map) : self
|
|
|
|
{
|
|
|
|
return new Assertion(
|
|
|
|
$this->var_id,
|
|
|
|
array_map(
|
|
|
|
/**
|
|
|
|
* @param array<int, string> $rules
|
|
|
|
*/
|
|
|
|
function (array $rules) use ($template_type_map) : array {
|
|
|
|
$first_rule = $rules[0];
|
|
|
|
|
|
|
|
if ($template_type_map) {
|
2019-06-23 05:30:40 +02:00
|
|
|
$rule_tokens = \Psalm\Type::tokenize($first_rule);
|
|
|
|
|
|
|
|
$substitute = false;
|
|
|
|
|
2019-01-23 05:42:54 +01:00
|
|
|
foreach ($rule_tokens as &$rule_token) {
|
2019-06-23 05:30:40 +02:00
|
|
|
if (isset($template_type_map[$rule_token[0]])) {
|
|
|
|
foreach ($template_type_map[$rule_token[0]] as list($type)) {
|
|
|
|
$substitute = true;
|
|
|
|
$rule_token[0] = $type->getId();
|
2019-03-22 20:59:10 +01:00
|
|
|
}
|
2019-01-23 05:42:54 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-23 05:30:40 +02:00
|
|
|
|
|
|
|
if ($substitute) {
|
|
|
|
return [implode(
|
|
|
|
'',
|
|
|
|
array_map(
|
|
|
|
function ($f) {
|
|
|
|
return $f[0];
|
|
|
|
},
|
|
|
|
$rule_tokens
|
|
|
|
)
|
|
|
|
)];
|
|
|
|
}
|
2019-01-23 05:42:54 +01:00
|
|
|
}
|
|
|
|
|
2019-06-23 05:30:40 +02:00
|
|
|
return [$first_rule];
|
2019-01-23 05:42:54 +01:00
|
|
|
},
|
|
|
|
$this->rule
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2018-02-23 21:39:33 +01:00
|
|
|
}
|