<commit_before><?php

namespace mwijngaard\ArrayAbstraction;

use mwijngaard\ArrayAbstraction\Exception\NotSupportedOnObjectException;
use mwijngaard\ArrayAbstraction\Exception\NotSupportedOnTypeException;
use mwijngaard\ArrayAbstraction\Proxy\ProxyInterface;

/**
 * Class Utils
 * @package mwijngaard\ArrayAbstraction
 *
 * Contains all the array-related functionality that we're trying to replace.
 */
class Utils
{
    public static function offsetGet($val, $offset)
    {
        if (is_array($val)) {
            return $val[$offset];
        } elseif (is_object($val)) {
            if ($val instanceof \ArrayAccess) {
                return $val[$offset];
            } elseif ($val instanceof \Traversable) {
                foreach ($val as $current_offset => $value) {
                    if ($current_offset === $offset) {
                        return $value;
                    }
                }
                // should this be an OutOfBoundsException?
                trigger_error(sprintf("Undefined offset: %s", $offset), E_NOTICE);
                return null;
            } else {
                throw new NotSupportedOnObjectException($val);
            }
        } else {
            throw new NotSupportedOnTypeException($val);
        }
    }

    public static function offsetExists($val, $offset)
    {
        if (is_array($val)) {
            return isset($val[$offset]);
        } elseif (is_object($val)) {
            if ($val instanceof \ArrayAccess) {
                return isset($val[$offset]);
            } elseif ($val instanceof \Traversable) {
                foreach ($val as $key => $value) {
                    if ($key === $offset && $value !== null) {
                        return true;
                    }
                }
                return false;
            } else {
                throw new NotSupportedOnObjectException($val);
            }
        } else {
            throw new NotSupportedOnTypeException($val);
        }
    }

    public static function offsetSet($val, $offset, $value)
    {
        if (is_array($val)) {
            $val[$offset] = $value;
        } elseif (is_object($val)) {
            if ($val instanceof \ArrayAccess) {
                $val[$offset] = $value;
            } else {
                throw new NotSupportedOnObjectException($val);
            }
        } else {
            throw new NotSupportedOnTypeException($val);
        }
    }

    public static function offsetUnset($val, $offset)
    {
        if (is_array($val)) {
            unset($val[$offset]);
        } elseif (is_object($val)) {
            if ($val instanceof \ArrayAccess) {
                unset($val[$offset]);
            } else {
                throw new NotSupportedOnObjectException($val);
            }
        } else {
            throw new NotSupportedOnTypeException($val);
        }
    }

    public static function getIterator($val)
    {
        if (is_array($val)) {
            return new \ArrayIterator($val);
        } elseif (is_object($val)) {
            if ($val instanceof \IteratorAggregate) {
                return $val->getIterator();
            } elseif ($val instanceof \Traversable) {
                return new \IteratorIterator($val);
            } else {
                throw new NotSupportedOnObjectException($val);
            }
        } else {
            throw new NotSupportedOnTypeException($val);
        }
    }

    public static function count($val)
    {
        if (is_array($val)) {
            return count($val);
        } elseif (is_object($val)) {
            if ($val instanceof \Countable) {
                return count($val);
            } elseif ($val instanceof \Traversable) {
                $ctr = 0;
                foreach ($val as $value) {
                    $ctr++;
                }
                return $ctr;
            } else {
                throw new NotSupportedOnObjectException($val);
            }<commit_msg>Replaced foreach loop by iterator_count<commit_after><?php

namespace mwijngaard\ArrayAbstraction;

use mwijngaard\ArrayAbstraction\Exception\NotSupportedOnObjectException;
use mwijngaard\ArrayAbstraction\Exception\NotSupportedOnTypeException;
use mwijngaard\ArrayAbstraction\Proxy\ProxyInterface;

/**
 * Class Utils
 * @package mwijngaard\ArrayAbstraction
 *
 * Contains all the array-related functionality that we're trying to replace.
 */
class Utils
{
    public static function offsetGet($val, $offset)
    {
        if (is_array($val)) {
            return $val[$offset];
        } elseif (is_object($val)) {
            if ($val instanceof \ArrayAccess) {
                return $val[$offset];
            } elseif ($val instanceof \Traversable) {
                foreach ($val as $current_offset => $value) {
                    if ($current_offset === $offset) {
                        return $value;
                    }
                }
                // should this be an OutOfBoundsException?
                trigger_error(sprintf("Undefined offset: %s", $offset), E_NOTICE);
                return null;
            } else {
                throw new NotSupportedOnObjectException($val);
            }
        } else {
            throw new NotSupportedOnTypeException($val);
        }
    }

    public static function offsetExists($val, $offset)
    {
        if (is_array($val)) {
            return isset($val[$offset]);
        } elseif (is_object($val)) {
            if ($val instanceof \ArrayAccess) {
                return isset($val[$offset]);
            } elseif ($val instanceof \Traversable) {
                foreach ($val as $key => $value) {
                    if ($key === $offset && $value !== null) {
                        return true;
                    }
                }
                return false;
            } else {
                throw new NotSupportedOnObjectException($val);
            }
        } else {
            throw new NotSupportedOnTypeException($val);
        }
    }

    public static function offsetSet($val, $offset, $value)
    {
        if (is_array($val)) {
            $val[$offset] = $value;
        } elseif (is_object($val)) {
            if ($val instanceof \ArrayAccess) {
                $val[$offset] = $value;
            } else {
                throw new NotSupportedOnObjectException($val);
            }
        } else {
            throw new NotSupportedOnTypeException($val);
        }
    }

    public static function offsetUnset($val, $offset)
    {
        if (is_array($val)) {
            unset($val[$offset]);
        } elseif (is_object($val)) {
            if ($val instanceof \ArrayAccess) {
                unset($val[$offset]);
            } else {
                throw new NotSupportedOnObjectException($val);
            }
        } else {
            throw new NotSupportedOnTypeException($val);
        }
    }

    public static function getIterator($val)
    {
        if (is_array($val)) {
            return new \ArrayIterator($val);
        } elseif (is_object($val)) {
            if ($val instanceof \IteratorAggregate) {
                return $val->getIterator();
            } elseif ($val instanceof \Traversable) {
                return new \IteratorIterator($val);
            } else {
                throw new NotSupportedOnObjectException($val);
            }
        } else {
            throw new NotSupportedOnTypeException($val);
        }
    }

    public static function count($val)
    {
        if (is_array($val)) {
            return count($val);
        } elseif (is_object($val)) {
            if ($val instanceof \Countable) {
                return count($val);
            } elseif ($val instanceof \Traversable) {
                return iterator_count($val);
            } else {
                throw new NotSupportedOnObjectException($val);
            }
{'fraction_non_alphanumeric': 0.10916253653577329, 'fraction_numerical': 0.00012708095056551024, 'mean_word_length': 1.572736188296829, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6907829', 'n_tokens_mistral': 2034, 'n_tokens_neox': 2005, 'n_words': 463}