Reduce value

@param array   $value
@param boolean $inExp

@return array|\Leafo\ScssPhp\Node\Number
487260
protected function reduce($value, $inExp = false)
    {
        list($type) = $value;

        switch ($type) {
            case Type::T_EXPRESSION:
                list(, $op, $left, $right, $inParens) = $value;

                $opName = isset(static::$operatorNames[$op]) ? static::$operatorNames[$op] : $op;
                $inExp = $inExp || $this->shouldEval($left) || $this->shouldEval($right);

                $left = $this->reduce($left, true);

                if ($op !== 'and' && $op !== 'or') {
                    $right = $this->reduce($right, true);
                }

                // special case: looks like css shorthand
                if ($opName == 'div' && ! $inParens && ! $inExp && isset($right[2])
                    && (($right[0] !== Type::T_NUMBER && $right[2] != '')
                    || ($right[0] === Type::T_NUMBER && ! $right->unitless()))
                ) {
                    return $this->expToString($value);
                }

                $left = $this->coerceForExpression($left);
                $right = $this->coerceForExpression($right);

                $ltype = $left[0];
                $rtype = $right[0];

                $ucOpName = ucfirst($opName);
                $ucLType  = ucfirst($ltype);
                $ucRType  = ucfirst($rtype);

                // this tries:
                // 1. op[op name][left type][right type]
                // 2. op[left type][right type] (passing the op as first arg
                // 3. op[op name]
                $fn = "op${ucOpName}${ucLType}${ucRType}";

                if (is_callable([$this, $fn]) ||
                    (($fn = "op${ucLType}${ucRType}") &&
                        is_callable([$this, $fn]) &&
                        $passOp = true) ||
                    (($fn = "op${ucOpName}") &&
                        is_callable([$this, $fn]) &&
                        $genOp = true)
                ) {
                    $coerceUnit = false;

                    if (! isset($genOp) &&
                        $left[0] === Type::T_NUMBER && $right[0] === Type::T_NUMBER
                    ) {
                        $coerceUnit = true;

                        switch ($opName) {
                            case 'mul':
                                $targetUnit = $left[2];

                                foreach ($right[2] as $unit => $exp) {
                                    $targetUnit[$unit] = (isset($targetUnit[$unit]) ? $targetUnit[$unit] : 0) + $exp;
                                }
                                break;

                            case 'div':
                                $targetUnit = $left[2];

                                foreach ($right[2] as $unit => $exp) {
                                    $targetUnit[$unit] = (isset($targetUnit[$unit]) ? $targetUnit[$unit] : 0) - $exp;
                                }
                                break;

                            case 'mod':
                                $targetUnit = $left[2];
                                break;

                            default:
                                $targetUnit = $left->unitless() ? $right[2] : $left[2];
                        }

                        if (! $left->unitless() && ! $right->unitless()) {
                            $left = $left->normalize();
                            $right = $right->normalize();
                        }
                    }

                    $shouldEval = $inParens || $inExp;

                    if (isset($passOp)) {
                        $out = $this->$fn($op, $left, $right, $shouldEval);
                    } else {
                        $out = $this->$fn($left, $right, $shouldEval);
                    }

                    if (isset($out)) {
                        if ($coerceUnit && $out[0] === Type::T_NUMBER) {
                            $out = $out->coerce($targetUnit);
                        }

                        return $out;
                    }
                }

                return $this->expToString($value);

            case Type::T_UNARY:
                list(, $op, $exp, $inParens) = $value;

                $inExp = $inExp || $this->shouldEval($exp);
                $exp = $this->reduce($exp);

                if ($exp[0] === Type::T_NUMBER) {
                    switch ($op) {
                        case '+':
                            return new Node\Number($exp[1], $exp[2]);

                        case '-':
                            return new Node\Number(-$exp[1], $exp[2]);
                    }
                }

                if ($op === 'not') {
                    if ($inExp || $inParens) {
                        if ($exp === static::$false || $exp === static::$null) {
                            return static::$true;
                        }

                        return static::$false;
                    }

                    $op = $op . ' ';
                }

                return [Type::T_STRING, '', [$op, $exp]];

            case Type::T_VARIABLE:
                list(, $name) = $value;

                return $this->reduce($this->get($name));

            case Type::T_LIST:
                foreach ($value[2] as &$item) {
                    $item = $this->reduce($item);
                }

                return $value;

            case Type::T_MAP:
                foreach ($value[1] as &$item) {
                    $item = $this->reduce($item);
                }

                foreach ($value[2] as &$item) {
                    $item = $this->reduce($item);
                }

                return $value;

            case Type::T_STRING:
                foreach ($value[2] as &$item) {
                    if (is_array($item) || $item instanceof \ArrayAccess) {
                        $item = $this->reduce($item);
                    }
                }

                return $value;

            case Type::T_INTERPOLATE:
                $value[1] = $this->reduce($value[1]);

                return $value;

            case Type::T_FUNCTION_CALL:
                list(, $name, $argValues) = $value;

                return $this->fncall($name, $argValues);

            default:
                return $value;
        }
    }
324143
public static function FISHERINV($value)
    {
        $value    = PHPExcel_Calculation_Functions::flattenSingleValue($value);

        if (is_numeric($value)) {
            return (exp(2 * $value) - 1) / (exp(2 * $value) + 1);
        }
        return PHPExcel_Calculation_Functions::VALUE();
    }
710479
protected function splitQueryValue($value)
    {
        $data = explode('=', $value, 2);
        if (1 === count($data)) {
            $data[] = null;
        }
        return $data;
    }
594956
public function count($value)
    {
        if (is_array($value) === true || $value instanceof Countable) {
            return count($value);
        } elseif ($value instanceof ArrayAccess) {
            if ($value->offsetExists(0)) {
                return true;
            }
        } elseif ($value instanceof Iterator) {
            $value->rewind();
            if ($value->valid()) {
                return true;
            }
        } elseif ($value instanceof Traversable) {
            foreach ($value as $dummy) {
                return true;
            }
        }

        return 0;
    }
275876
protected function mapToSchema($value, string $schemaClass)
    {
        $value = (array) $value;
        $schema = Ioc::make($schemaClass);
        $mapper = Ioc::makeWith(self::class, compact('schema'));

        if (!isset($value[0])) {
            $value = [$value];
        }

        foreach ($value as $key => $subValue) {
            $value[$key] = $mapper->map($subValue);
        }

        return $value;
    }
963508
public function incr($key, $value = 1) {
        $value = (int) $value;
        try {
            $result = ($value > 1)
                ? $this->getRedis()->incrBy($key, $value)
                : $this->getRedis()->incr($key);
            if ($result !== false) {
                return $result;
            }
            throw new ImpossibleValueException();
        } catch (RedisException $ex) {
            throw new ConnectException();
        }
    }
657699
public static function convertUnmappedToPHPValue($value)
    {
        $type  = key($value);
        $value = current($value);

        switch ($type) {
            case 'S':
            case 'B':
            case 'BOOL':
                return $value;
            case 'NULL':
                return null;
            case 'N':
                // Use type coercion to unmarshal numbers to int/float.
                return $value + 0;
            case 'M':
            case 'L':
                foreach ($value as $k => $v) {
                    $value[$k] = self::convertUnmappedToPHPValue($v);
                }

                return $value;
            case 'BS':
            case 'NS':
            case 'SS':
                $childType = substr($type, 0, 1);

                return array_map(
                    function ($childValue) use ($childType) {
                        return self::convertUnmappedToPHPValue([$childType => $childValue]);
                    },
                    $value
                );
        }

        throw new \UnexpectedValueException("Unexpected type: $type.");
    }
637558
protected function value_in_format($value, $format) {

		if($format === '*') return true;

		$formats = explode(',', $format);

		foreach($formats as $format) {

			if(ctype_digit($format)) {
				if($value === (int) $format) return true;
				continue;
			}

			$values = $this->expand_format_expression($format);
			if(in_array($value, $values)) return true;

		}

		return false;

	}
505167
private function convertValue($value)
    {
        if ($value instanceof Node)
        {
            return $this->em->loadNode($value);
        }

        elseif ($value instanceof Relationship)
        {
            return $this->em->loadRelation($value);
        }

        else
        {
            return $value;
        }
    }
1138167
public function inStep($value, $offset)
    {
        $parts = \explode('/', $offset, 2);

        if (empty($parts[1])) {
            return false;
        }

        if (\strpos($offset, '*/') !== false || \strpos($offset, '0/') !== false) {
            return $value % $parts[1] === 0;
        }

        $parts    = \explode('/', $offset, 2);
        $subparts = \explode('-', $parts[0], 2) + [1 => $value];

        return $this->inStepRange($value, $subparts[0], $subparts[1], $parts[1]);
    }
754984
public function reduce($closure, $initial = false)
    {
        $result = $initial;
        foreach ($this as $val) {
            $result = $closure($result, $val);
        }
        return $result;
    }
1315918
protected function resolveValue($value)
    {
        // clear
        $value = trim($value);

        // @key
        if(is_null($value) or $value == '') {
            return null;
        }
        // @key 10
        if(is_numeric($value)) {
            return $value + 0; // keeps type (int, float...)
        }
        // @key true
        elseif($value === 'true') {
            return true;
        }
        // @key false
        elseif($value === 'false') {
            return false;
        }
        // @key [value, value, value]
        elseif(preg_match('#^\[(.+)\]$#', $value, $out)) {
            $values = explode(',', $out[1]);
            $array = [];
            foreach($values as $item) {
                $array[] = $this->resolveValue($item);
            }
            return $array;
        }
        // @key {key: value, key: value}
        elseif(preg_match('#^\{(.+)\}$#', $value, $out)) {
            $props = explode(',', $out[1]);
            $object = [];
            foreach($props as $prop) {
                list($name, $item) = explode(':', $prop);
                $object[trim($name)] = $this->resolveValue($item);
            }
            return (object)$object;
        }
        // @key Some\Class
        elseif(false) {

        }
        // @key Some\Class(value, value, value)
        elseif(false) {

        }

        // @key 'value'
        return trim($value, '\'""');
    }
240420
private static function isAssoc($value)
	{
		if(!is_array($value)) return false;
		return array_keys($value) !== range(0, count($value) - 1);
	}
804251
public static function int($value=0, bool $flatten=false) {
		ref\cast::int($value, $flatten);
		return $value;
	}
637077
protected function coerceValue($value)
    {
        if (is_array($value) || $value instanceof \ArrayAccess) {
            return $value;
        }

        if (is_bool($value)) {
            return $this->toBool($value);
        }

        if ($value === null) {
            return static::$null;
        }

        if (is_numeric($value)) {
            return new Node\Number($value, '');
        }

        if ($value === '') {
            return static::$emptyString;
        }

        if (preg_match('/^(#([0-9a-f]{6})|#([0-9a-f]{3}))$/i', $value, $m)) {
            $color = [Type::T_COLOR];

            if (isset($m[3])) {
                $num = hexdec($m[3]);

                foreach ([3, 2, 1] as $i) {
                    $t = $num & 0xf;
                    $color[$i] = $t << 4 | $t;
                    $num >>= 4;
                }
            } else {
                $num = hexdec($m[2]);

                foreach ([3, 2, 1] as $i) {
                    $color[$i] = $num & 0xff;
                    $num >>= 8;
                }
            }

            return $color;
        }

        return [Type::T_KEYWORD, $value];
    }
398883
protected function _splitQueryValue($value)
    {
        $data = explode('=', $value, 2);
        if (1 === count($data)) {
            $data[] = null;
        }
        return $data;
    }
306022
protected function inArray($value)
    {
        if ($this->strict) {
            $isIn = in_array($value, $this->range, true);
        } else {
            $isIn = false;
            foreach ($this->range as $r) {
                $isIn = (strcmp($r, $value) === 0);
                if ($isIn) break;
            }
        }
        return $isIn;
    }