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
|
|
|
*/
|
2019-12-30 16:01:31 +01:00
|
|
|
public function getUntemplatedCopy(array $template_type_map, ?string $this_var_id) : self
|
2019-01-23 05:42:54 +01:00
|
|
|
{
|
|
|
|
return new Assertion(
|
2019-12-30 17:07:04 +01:00
|
|
|
\is_string($this->var_id) && $this_var_id
|
|
|
|
? \str_replace('$this->', $this_var_id . '->', $this->var_id)
|
2019-12-30 16:01:31 +01:00
|
|
|
: $this->var_id,
|
2019-01-23 05:42:54 +01:00
|
|
|
array_map(
|
|
|
|
/**
|
|
|
|
* @param array<int, string> $rules
|
|
|
|
*/
|
|
|
|
function (array $rules) use ($template_type_map) : array {
|
|
|
|
$first_rule = $rules[0];
|
|
|
|
|
|
|
|
if ($template_type_map) {
|
2020-05-14 01:12:45 +02:00
|
|
|
$rule_tokens = \Psalm\Internal\Type\TypeTokenizer::tokenize($first_rule);
|
2019-06-23 05:30:40 +02:00
|
|
|
|
|
|
|
$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;
|
2019-07-21 07:40:19 +02:00
|
|
|
|
2020-01-04 18:20:26 +01:00
|
|
|
$first_type = \array_values($type->getAtomicTypes())[0];
|
2019-12-29 00:37:55 +01:00
|
|
|
|
|
|
|
if ($first_type instanceof \Psalm\Type\Atomic\TTemplateParam) {
|
|
|
|
$rule_token[0] = $first_type->param_name;
|
|
|
|
} else {
|
|
|
|
$rule_token[0] = $first_type->getKey();
|
|
|
|
}
|
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
|
|
|
}
|