1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-12 01:09:38 +01:00
psalm/src/Psalm/Context.php

82 lines
2.4 KiB
PHP
Raw Normal View History

<?php
2016-07-26 00:37:44 +02:00
namespace Psalm;
class Context
{
/** @var array<Type\Union> */
public $vars_in_scope = [];
public $vars_possibly_in_scope = [];
/** @var boolean */
public $in_loop = false;
/** @var string|null */
public $self;
/** @var string|null */
public $parent;
/** @var string */
public $file_name;
/**
* @param string $file_name
* @param string|null $self
*/
public function __construct($file_name, $self = null)
{
$this->file_name = $file_name;
$this->self = $self;
}
public function __clone()
{
foreach ($this->vars_in_scope as $key => &$type) {
$type = clone $type;
}
}
/**
* Updates the parent context, looking at the changes within a block
* and then applying those changes, where necessary, to the parent context
*
* @param Context $start_context
* @param Context $end_context
* @param bool $has_leaving_statements whether or not the parent scope is abandoned between $start_context and $end_context
* @return void
*/
2016-08-10 07:54:45 +02:00
public function update(Context $start_context, Context $end_context, $has_leaving_statements, array $vars_to_update, array &$updated_vars)
{
foreach ($this->vars_in_scope as $var => &$context_type) {
$old_type = $start_context->vars_in_scope[$var];
// if we're leaving, we're effectively deleting the possibility of the if types
2016-07-25 21:05:58 +02:00
$new_type = !$has_leaving_statements ? $end_context->vars_in_scope[$var] : null;
2016-08-10 07:54:45 +02:00
// this is only true if there was some sort of type negation
if (in_array($var, $vars_to_update)) {
// if the type changed within the block of statements, process the replacement
if ((string)$old_type !== (string)$new_type) {
$context_type->substitute($old_type, $new_type);
$updated_vars[$var] = true;
}
}
}
}
public static function getRedefinedVars(Context $original_context, Context $new_context)
{
$redefined_vars = [];
foreach ($original_context->vars_in_scope as $var => $context_type) {
if ((string)$new_context->vars_in_scope[$var] !== (string)$context_type) {
$redefined_vars[$var] = $new_context->vars_in_scope[$var];
}
}
return $redefined_vars;
}
}