1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 18:36:58 +01:00
psalm/src/Psalm/Internal/Clause.php

283 lines
7.3 KiB
PHP
Raw Normal View History

<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal;
2021-12-03 20:11:20 +01:00
use Psalm\Internal\Algebra;
use function array_diff;
2019-07-05 22:24:00 +02:00
use function array_keys;
use function array_map;
2021-06-08 04:55:21 +02:00
use function array_unique;
2019-07-05 22:24:00 +02:00
use function array_values;
use function count;
use function implode;
use function json_encode;
2019-07-05 22:24:00 +02:00
use function ksort;
use function md5;
2021-12-03 21:07:25 +01:00
use function reset;
2019-07-05 22:24:00 +02:00
use function sort;
use function strpos;
2021-12-03 21:07:25 +01:00
use function substr;
/**
* @internal
*
* @psalm-immutable
*/
class Clause
{
2020-08-26 21:35:29 +02:00
/** @var int */
public $creating_conditional_id;
/** @var int */
public $creating_object_id;
/**
* An array of strings of the form
* [
* '$a' => ['falsy'],
* '$b' => ['!falsy'],
* '$c' => ['!null'],
* '$d' => ['string', 'int']
* ]
*
* representing the formula
*
* !$a || $b || $c !== null || is_string($d) || is_int($d)
*
* @var array<string, non-empty-list<string>>
*/
public $possibilities;
/**
* An array of things that are not true
* [
* '$a' => ['!falsy'],
* '$b' => ['falsy'],
* '$c' => ['null'],
* '$d' => ['!string', '!int']
* ]
* represents the formula
*
* $a && !$b && $c === null && !is_string($d) && !is_int($d)
*
* @var array<string, non-empty-list<string>>|null
*/
public $impossibilities;
/** @var bool */
public $wedge;
/** @var bool */
public $reconcilable;
2018-05-07 20:52:45 +02:00
/** @var bool */
public $generated = false;
/** @var array<string, bool> */
public $redefined_vars = [];
2020-08-26 21:35:29 +02:00
/** @var string|int */
public $hash;
/**
* @param array<string, non-empty-list<string>> $possibilities
* @param array<string, bool> $redefined_vars
*/
public function __construct(
array $possibilities,
2020-08-26 21:35:29 +02:00
int $creating_conditional_id,
int $creating_object_id,
2020-10-12 21:46:47 +02:00
bool $wedge = false,
bool $reconcilable = true,
bool $generated = false,
2020-08-26 21:35:29 +02:00
array $redefined_vars = []
) {
$this->possibilities = $possibilities;
$this->wedge = $wedge;
$this->reconcilable = $reconcilable;
2018-05-07 20:52:45 +02:00
$this->generated = $generated;
$this->redefined_vars = $redefined_vars;
2020-08-26 21:35:29 +02:00
$this->creating_conditional_id = $creating_conditional_id;
$this->creating_object_id = $creating_object_id;
if ($wedge || !$reconcilable) {
2020-08-26 21:35:29 +02:00
$this->hash = ($wedge ? 'w' : '') . $creating_object_id;
} else {
ksort($possibilities);
2020-08-26 21:35:29 +02:00
foreach ($possibilities as $i => $_) {
sort($possibilities[$i]);
}
$this->hash = md5((string) json_encode($possibilities));
}
}
public function contains(Clause $other_clause): bool
{
if (count($other_clause->possibilities) > count($this->possibilities)) {
return false;
}
foreach ($other_clause->possibilities as $var => $possible_types) {
if (!isset($this->possibilities[$var]) || count(array_diff($possible_types, $this->possibilities[$var]))) {
return false;
}
}
return true;
}
2020-10-07 17:46:53 +02:00
/**
* @psalm-mutation-free
*/
public function __toString(): string
2018-05-07 07:26:06 +02:00
{
2020-10-07 17:46:53 +02:00
$clause_strings = array_map(
/**
* @param non-empty-list<string> $values
*/
2021-10-04 00:03:06 +02:00
function (string $var_id, array $values): string {
2020-11-03 23:02:37 +01:00
if ($var_id[0] === '*') {
$var_id = '<expr>';
}
2020-10-07 17:46:53 +02:00
$var_id_clauses = array_map(
2021-10-04 00:03:06 +02:00
function (string $value) use ($var_id): string {
2020-10-07 17:46:53 +02:00
if ($value === 'falsy') {
return '!' . $var_id;
}
if ($value === '!falsy') {
return $var_id;
}
$negate = false;
if ($value[0] === '!') {
$negate = true;
2021-12-03 21:07:25 +01:00
$value = substr($value, 1);
2020-10-07 17:46:53 +02:00
}
if ($value[0] === '=') {
2021-12-03 21:07:25 +01:00
$value = substr($value, 1);
2020-10-07 17:46:53 +02:00
}
if ($negate) {
2020-10-07 17:55:57 +02:00
return $var_id . ' is not ' . $value;
2020-10-07 17:46:53 +02:00
}
2020-10-07 17:55:57 +02:00
return $var_id . ' is ' . $value;
2020-10-07 17:46:53 +02:00
},
$values
);
if (count($var_id_clauses) > 1) {
return '(' . implode(') || (', $var_id_clauses) . ')';
}
return $var_id_clauses[0];
},
array_keys($this->possibilities),
array_values($this->possibilities)
2018-05-07 07:26:06 +02:00
);
2020-10-07 17:46:53 +02:00
if (count($clause_strings) > 1) {
return '(' . implode(') || (', $clause_strings) . ')';
}
2021-12-03 21:07:25 +01:00
return reset($clause_strings);
2018-05-07 07:26:06 +02:00
}
public function makeUnique() : self
{
$possibilities = $this->possibilities;
foreach ($possibilities as $var_id => $var_possibilities) {
$possibilities[$var_id] = array_values(array_unique($var_possibilities));
}
return new self(
$possibilities,
2020-08-26 21:35:29 +02:00
$this->creating_conditional_id,
$this->creating_object_id,
$this->wedge,
$this->reconcilable,
$this->generated,
2020-08-26 21:35:29 +02:00
$this->redefined_vars
);
}
2020-08-26 22:41:40 +02:00
public function removePossibilities(string $var_id) : ?self
{
$possibilities = $this->possibilities;
unset($possibilities[$var_id]);
2020-08-26 22:41:40 +02:00
if (!$possibilities) {
return null;
}
return new self(
$possibilities,
2020-08-26 21:35:29 +02:00
$this->creating_conditional_id,
$this->creating_object_id,
$this->wedge,
$this->reconcilable,
$this->generated,
2020-08-26 21:35:29 +02:00
$this->redefined_vars
);
}
/**
* @param non-empty-list<string> $clause_var_possibilities
*/
public function addPossibilities(string $var_id, array $clause_var_possibilities) : self
{
$possibilities = $this->possibilities;
$possibilities[$var_id] = $clause_var_possibilities;
return new self(
$possibilities,
2020-08-26 21:35:29 +02:00
$this->creating_conditional_id,
$this->creating_object_id,
$this->wedge,
$this->reconcilable,
$this->generated,
2020-08-26 21:35:29 +02:00
$this->redefined_vars
);
}
public function calculateNegation() : self
{
if ($this->impossibilities !== null) {
return $this;
}
$impossibilities = [];
foreach ($this->possibilities as $var_id => $possibility) {
$impossibility = [];
foreach ($possibility as $type) {
if (($type[0] !== '=' && $type[0] !== '~'
&& (!isset($type[1]) || ($type[1] !== '=' && $type[1] !== '~')))
|| strpos($type, '(')
|| strpos($type, 'getclass-')
) {
2021-12-03 20:11:20 +01:00
$impossibility[] = Algebra::negateType($type);
}
}
if ($impossibility) {
$impossibilities[$var_id] = $impossibility;
}
}
$clause = clone $this;
$clause->impossibilities = $impossibilities;
return $clause;
}
}