1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-14 10:17:33 +01:00
psalm/src/Psalm/Internal/Provider/NodeDataProvider.php

134 lines
4.0 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Internal\Provider;
use PhpParser;
2019-11-26 23:48:47 +01:00
use SplObjectStorage;
use Psalm\Type\Union;
class NodeDataProvider implements \Psalm\NodeTypeProvider
{
2019-11-26 23:48:47 +01:00
/** @var SplObjectStorage<PhpParser\Node, Union> */
private $node_types;
2019-11-26 23:48:47 +01:00
/** @var SplObjectStorage<PhpParser\Node, array<string, non-empty-list<non-empty-list<string>>>|null> */
private $node_assertions;
2019-11-26 23:48:47 +01:00
/** @var SplObjectStorage<PhpParser\Node, array<int, \Psalm\Storage\Assertion>> */
private $node_if_true_assertions;
2019-11-26 23:48:47 +01:00
/** @var SplObjectStorage<PhpParser\Node, array<int, \Psalm\Storage\Assertion>> */
private $node_if_false_assertions;
/** @var bool */
public $cache_assertions = true;
2019-11-26 23:48:47 +01:00
public function __construct()
{
/** @psalm-suppress PropertyTypeCoercion */
$this->node_types = new SplObjectStorage();
/** @psalm-suppress PropertyTypeCoercion */
$this->node_assertions = new SplObjectStorage();
/** @psalm-suppress PropertyTypeCoercion */
$this->node_if_true_assertions = new SplObjectStorage();
/** @psalm-suppress PropertyTypeCoercion */
$this->node_if_false_assertions = new SplObjectStorage();
}
/**
* @param PhpParser\Node\Expr|PhpParser\Node\Name|PhpParser\Node\Stmt\Return_ $node
*/
public function setType($node, Union $type) : void
{
2019-11-26 23:48:47 +01:00
$this->node_types[$node] = $type;
}
/**
* @param PhpParser\Node\Expr|PhpParser\Node\Name|PhpParser\Node\Stmt\Return_ $node
*/
public function getType($node) : ?Union
{
2019-11-26 23:48:47 +01:00
return $this->node_types[$node] ?? null;
}
/**
* @param PhpParser\Node\Expr $node
* @param array<string, non-empty-list<non-empty-list<string>>>|null $assertions
*/
public function setAssertions($node, ?array $assertions) : void
{
if (!$this->cache_assertions) {
return;
}
2019-11-26 23:48:47 +01:00
$this->node_assertions[$node] = $assertions;
}
/**
* @param PhpParser\Node\Expr $node
* @return array<string, non-empty-list<non-empty-list<string>>>|null
*/
public function getAssertions($node) : ?array
{
if (!$this->cache_assertions) {
return null;
}
2019-11-26 23:48:47 +01:00
return $this->node_assertions[$node] ?? null;
}
/**
* @param PhpParser\Node\Expr\FuncCall|PhpParser\Node\Expr\MethodCall|PhpParser\Node\Expr\StaticCall $node
* @param array<int, \Psalm\Storage\Assertion> $assertions
*/
public function setIfTrueAssertions($node, array $assertions) : void
{
2019-11-26 23:48:47 +01:00
$this->node_if_true_assertions[$node] = $assertions;
}
/**
* @param PhpParser\Node\Expr\FuncCall|PhpParser\Node\Expr\MethodCall|PhpParser\Node\Expr\StaticCall $node
* @return array<int, \Psalm\Storage\Assertion>|null
*/
public function getIfTrueAssertions($node) : ?array
{
2019-11-26 23:48:47 +01:00
return $this->node_if_true_assertions[$node] ?? null;
}
/**
* @param PhpParser\Node\Expr\FuncCall|PhpParser\Node\Expr\MethodCall|PhpParser\Node\Expr\StaticCall $node
* @param array<int, \Psalm\Storage\Assertion> $assertions
*/
public function setIfFalseAssertions($node, array $assertions) : void
{
2019-11-26 23:48:47 +01:00
$this->node_if_false_assertions[$node] = $assertions;
}
/**
* @param PhpParser\Node\Expr\FuncCall|PhpParser\Node\Expr\MethodCall|PhpParser\Node\Expr\StaticCall $node
* @return array<int, \Psalm\Storage\Assertion>|null
*/
public function getIfFalseAssertions($node) : ?array
{
2019-11-26 23:48:47 +01:00
return $this->node_if_false_assertions[$node] ?? null;
}
/**
* @param PhpParser\Node\Expr $node
*/
public function isPureCompatible($node) : bool
{
$node_type = self::getType($node);
return ($node_type && $node_type->external_mutation_free) || isset($node->pure);
}
/**
* @param PhpParser\Node\Expr $node
*/
public function clearNodeOfTypeAndAssertions($node) : void
{
2019-11-26 23:48:47 +01:00
unset($this->node_types[$node], $this->node_assertions[$node]);
}
}