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
|
|
|
|
|
|
|
/**
|
2020-11-27 17:43:23 +01:00
|
|
|
* @param array<string, array<string, \Psalm\Internal\Type\TemplateBound>> $inferred_upper_bounds
|
2019-01-23 05:42:54 +01:00
|
|
|
*/
|
2020-11-27 17:43:23 +01:00
|
|
|
public function getUntemplatedCopy(array $inferred_upper_bounds, ?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
|
2020-10-17 18:36:44 +02:00
|
|
|
*
|
|
|
|
* @return array{0: string}
|
2019-01-23 05:42:54 +01:00
|
|
|
*/
|
2020-11-27 17:43:23 +01:00
|
|
|
function (array $rules) use ($inferred_upper_bounds) : array {
|
2019-01-23 05:42:54 +01:00
|
|
|
$first_rule = $rules[0];
|
|
|
|
|
2020-11-27 17:43:23 +01:00
|
|
|
if ($inferred_upper_bounds) {
|
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) {
|
2020-11-27 17:43:23 +01:00
|
|
|
if (isset($inferred_upper_bounds[$rule_token[0]])) {
|
|
|
|
foreach ($inferred_upper_bounds[$rule_token[0]] as $bound) {
|
2019-06-23 05:30:40 +02:00
|
|
|
$substitute = true;
|
2019-07-21 07:40:19 +02:00
|
|
|
|
2020-11-27 17:43:23 +01:00
|
|
|
$first_type = \array_values($bound->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
|
|
|
}
|