<commit_before><?php
/**
 * NoFramework
 *
 * @author <NAME> <<EMAIL>>
 * @license http://www.opensource.org/licenses/mit-license.php MIT
 * @link http://noframework.com
 */

namespace NoFramework;

/**
 * Implemention of php magic methods for accessing inexistent properties.
 */
trait MagicProperties
{
    /**
     * Here is the registry of values for magic properties.
     * This thing is not just an array, it can be an object
     * implementing ArrayAccess interface, and can be injected from
     * outside on construction. 
     */
    protected $__property = [];

    /**
     * Whether the value for this property exists in registry;
     */
    public function __isset($property)
    {
        return isset($this->__property[$property]);
    }

    /**
     * Try to get the value for this property from the registry;
     * else try to evaluate it. 
     */
    public function __get($property)
    {
        if (isset($this->__property[$property])) {
            return $this->__property[$property];

        } elseif ($this->isMagicProperty($property)) {
            return $this->getMagicProperty($property);

        } else {
            trigger_error(sprintf('Cannot read property %s::$%s',
                get_called_class(),
                $property
            ), E_USER_ERROR);
        }
    }

    /**
     * All magic properties are read only.
     * This can be adjusted by reimplementing __set, or you may
     * create setter, or just declare property as public.
     */
    public function __set($property, $value)
    {
        if (!isset($this->__property[$property])
            and !$this->isMagicProperty($property)
            and !property_exists($this, $property)
        ) {
            $this->$property = $value;

        } else {
            trigger_error(sprintf('Cannot write property %s::$%s',
                get_called_class(),
                $property
            ), E_USER_ERROR);
        }
    }

    /**
     * Hack of my dream. If there is a declared protected property and thus in __property,
     * then there will be inconsistency between calling that property from outside and from inside.
     * Magic is prefferable - so unset real property, but there is no way to kill it from outside,
     * even via reflection.
     * By implementing __unset this way we may use unset($object->$property) to remove confilcts.
     */
    public function __unset($property)
    {
        if (isset($this->__property[$property])) {
            unset($this->$property);

        } else {
            trigger_error(sprintf('Cannot remove property %s::$%s',
                get_called_class(),
                $property
            ), E_USER_ERROR);
        }
    }

    /**
     * Is this a magic property (in common case: is there a method
     * prefixed by '__property_')
     */
    protected function isMagicProperty($property)
    {
        return method_exists($this, '__property_' . $property);
    }

    /**
     * Get magic property (call magic method)
     */
    protected function getMagicProperty($property)
    {
        return $this->__property[$property]
            = $this->{'__property_' . $property}();
    }
}
<commit_msg>Revert "comments in MagicProperties"<commit_after><?php
/**
 * NoFramework
 *
 * @author <NAME> <<EMAIL>>
 * @license http://www.opensource.org/licenses/mit-license.php MIT
 * @link http://noframework.com
 */

namespace NoFramework;

trait MagicProperties
{
    protected $__property = [];

    public function __isset($property)
    {
        return isset($this->__property[$property]);
    }

    public function __get($property)
    {
        if (isset($this->__property[$property])) {
            return $this->__property[$property];

        } elseif ($this->isMagicProperty($property)) {
            return $this->getMagicProperty($property);

        } else {
            trigger_error(sprintf('Cannot read property %s::$%s',
                get_called_class(),
                $property
            ), E_USER_ERROR);
        }
    }

    public function __set($property, $value)
    {
        if (!isset($this->__property[$property])
            and !$this->isMagicProperty($property)
            and !array_key_exists($property, get_object_vars($this))
        ) {
            $this->$property = $value;

        } else {
            trigger_error(sprintf('Cannot write property %s::$%s',
                get_called_class(),
                $property
            ), E_USER_ERROR);
        }
    }

    public function __unset($property)
    {
        if (isset($this->__property[$property])) {
            unset($this->$property);

        } else {
            trigger_error(sprintf('Cannot remove property %s::$%s',
                get_called_class(),
                $property
            ), E_USER_ERROR);
        }
    }

    protected function isMagicProperty($property)
    {
        return method_exists($this, '__property_' . $property);
    }

    protected function getMagicProperty($property)
    {
        return $this->__property[$property]
            = $this->{'__property_' . $property}();
    }
}

{'fraction_non_alphanumeric': 0.12135447249951067, 'fraction_numerical': 0.0, 'mean_word_length': 2.125382262996942, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2086050', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1373, 'n_words': 353}