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

94 lines
2.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Psalm;
class Clause
{
/**
* An array of strings of the form
* [
* '$a' => ['empty'],
* '$b' => ['!empty'],
* '$c' => ['!null'],
* '$d' => ['string', 'int']
* ]
*
* representing the formula
*
* !$a || $b || $c !== null || is_string($d) || is_int($d)
*
* @var array<string, array<string>>
*/
public $possibilities;
/**
* An array of things that are not true
* [
* '$a' => ['!empty'],
* '$b' => ['empty'],
* '$c' => ['null'],
* '$d' => ['!string', '!int']
* ]
* represents the formula
*
* $a && !$b && $c === null && !is_string($d) && !is_int($d)
*
* @var array<string, array<string>>|null
*/
public $impossibilities;
/** @var bool */
public $wedge;
/** @var bool */
public $reconcilable;
/**
* @param array<string, array<string>> $possibilities
* @param bool $wedge
* @param bool $reconcilable
*/
public function __construct(array $possibilities, $wedge = false, $reconcilable = true)
{
$this->possibilities = $possibilities;
$this->wedge = $wedge;
$this->reconcilable = $reconcilable;
}
/**
* @param Clause $other_clause
*
* @return bool
*/
public function contains(Clause $other_clause)
{
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;
}
/**
* Gets a hash of the object will be unique if we're unable to easily reconcile this with others
*
* @return string
*/
public function getHash()
{
ksort($this->possibilities);
foreach ($this->possibilities as &$possible_types) {
sort($possible_types);
}
return md5(json_encode($this->possibilities)) .
($this->wedge || !$this->reconcilable ? spl_object_hash($this) : '');
}
}