<commit_before><?php
namespace Nkey\Caribu\Orm;

use Nkey\Caribu\Model\AbstractModel;
use Nkey\Caribu\Orm\OrmException;

use \ReflectionClass;
use \ReflectionProperty;
use \ReflectionObject;
use \ReflectionException;
use \ReflectionMethod;

/**
 * Annotation provider for Caribu Orm
 *
 * This class is part of Caribu package
 *
 * @author <NAME> <<EMAIL>>
 */
trait OrmAnnotation
{
    use OrmDataTypeConverter;

    /**
     * Retrieve the annotated table name
     *
     * @param string $class The name of class
     * @param string $fallback As fallback if nothing was found
     *
     * @return string The name of table
     *
     * @throws OrmException
     */
    private static function getAnnotatedTableName($class, $fallback)
    {
        try {
            $rf = new ReflectionClass($class);

            $docComments = $rf->getDocComment();

            $matches = array();
            if (preg_match('/@table (\w+)/', $docComments, $matches)) {
                $fallback = $matches[1];
            }

            return $fallback;
        } catch (ReflectionException $ex) {
            throw OrmException::fromPrevious($ex);
        }
    }

    /**
     * Get the annotated primary key property name
     *
     * The property is annotated with the @id annotation
     *
     * @param string $class The name of class to retrieve the primary key property
     *
     * @return string The name of property which represents the primary key
     *
     * @throws OrmException
     */
    private static function getAnnotatedPrimaryKeyProperty($class)
    {
        try {
            $rf = new ReflectionClass($class);

            $properties = $rf->getProperties();

            $properyName = null;

            foreach ($properties as $property) {
                assert($property instanceof ReflectionProperty);
                if (self::isIdAnnotated($property->getDocComment())) {
                    $properyName = $property->getName();
                    break;
                }
            }

            return $properyName;
        } catch (ReflectionException $ex) {
            throw OrmException::fromPrevious($ex);
        }
    }

    /**
     * Get the annotated primary key
     *
     * The property is annotated with the @id annotation
     * The propery may have a @column annotation to modify the database column name
     *
     * @param string $class The name of class to retrieve the primary key column of
     *
     * @return string|null The name of primary key column
     *
     * @throws OrmException
     */
    private static function getAnnotatedPrimaryKeyColumn($class)
    {
        try {
            $rf = new ReflectionClass($class);

            $properties = $rf->getProperties();

            $columnName = null;

            foreach ($properties as $property) {
                assert($property instanceof ReflectionProperty);
                $docComment = $property->getDocComment();
                if (self::isIdAnnotated($docComment)) {
                    if (null === ($columnName = self::getAnnotatedColumn($docComment))) {
                        $columnName = $property->getName();
                    }
                }
            }

            return $columnName;
        } catch (ReflectionException $ex) {
            throw OrmException::fromPrevious($ex);
        }
    }

    /**
     * Get the property type via annotation
     *
     * @param string $class The name of class to retrieve a particular property type
     * @param string $propertyName The name of property to retrieve the type of
     *
     * @return string|null The property type
     */
    private static function getAnnotatedPropertyType($class, $propertyName)
    {
        $type = null;
        $rf = new ReflectionClass($class);

        foreach ($rf->getProperties() as $property) {
            assert($property instanceof ReflectionProperty);

            $docComments = $property->getDocComment();

            $isDestinationProperty = false;

            if ($property->getName() == $propertyName) {
                $isDestinationProperty = true;
            }

            if (! $isDestinationProperty) {
                continue;
            }

            if (null !== ($type = self::getAnnotatedType($docComments, $rf->getNamespaceName()))) {
                break;
            }
        }

        return $type;
    }

    /**
     * Build a full qualified class name including namespace
     *
     * @param string $ns The namespace of class
     * @param string $class The name of class
     *
     * @return string The full qualified class name
     */
    private static function fullQualifiedName($ns, $class)
    {
        return sprintf("\\%s\\%s", $ns, $class);
    }

    /**
     * Get the value from property
     *
     * @param unknown $from
     * @param unknown $toClass
     * @param unknown $property
     * @param unknown $namespace
     */
    private static function getAnnotatedPropertyValue($from, $toClass, $property, $namespace)
    {
        $value = $property->getValue($from);

        $type = self::getAnnotatedPropertyType($toClass, $property->getName());

        if (null !== $type && !self::isPrimitive($type) && !class_exists($type)) {
            $type = self::fullQualifiedName($namespace, $type);
        }

        if (null !== $type && !self::isPrimitive($type) && class_exists($type)) {
            $rfPropertyType = new ReflectionClass($type);
            if ($rfPropertyType->getParentClass() &&
                strcmp($rfPropertyType->getParentClass()->name, 'Nkey\Caribu\Model\AbstractModel') == 0
                ) {
                $getById = new ReflectionMethod($type, "get");
                $value = $getById->invoke(null, $value);<commit_msg>Documentation improved<commit_after><?php
namespace Nkey\Caribu\Orm;

use Nkey\Caribu\Model\AbstractModel;
use Nkey\Caribu\Orm\OrmException;

use \ReflectionClass;
use \ReflectionProperty;
use \ReflectionObject;
use \ReflectionException;
use \ReflectionMethod;

/**
 * Annotation provider for Caribu Orm
 *
 * This class is part of Caribu package
 *
 * @author <NAME> <<EMAIL>>
 */
trait OrmAnnotation
{
    use OrmDataTypeConverter;

    /**
     * Retrieve the annotated table name
     *
     * @param string $class The name of class
     * @param string $fallback As fallback if nothing was found
     *
     * @return string The name of table
     *
     * @throws OrmException
     */
    private static function getAnnotatedTableName($class, $fallback)
    {
        try {
            $rf = new ReflectionClass($class);

            $docComments = $rf->getDocComment();

            $matches = array();
            if (preg_match('/@table (\w+)/', $docComments, $matches)) {
                $fallback = $matches[1];
            }

            return $fallback;
        } catch (ReflectionException $ex) {
            throw OrmException::fromPrevious($ex);
        }
    }

    /**
     * Get the annotated primary key property name
     *
     * The property is annotated with the @id annotation
     *
     * @param string $class The name of class to retrieve the primary key property
     *
     * @return string The name of property which represents the primary key
     *
     * @throws OrmException
     */
    private static function getAnnotatedPrimaryKeyProperty($class)
    {
        try {
            $rf = new ReflectionClass($class);

            $properties = $rf->getProperties();

            $properyName = null;

            foreach ($properties as $property) {
                assert($property instanceof ReflectionProperty);
                if (self::isIdAnnotated($property->getDocComment())) {
                    $properyName = $property->getName();
                    break;
                }
            }

            return $properyName;
        } catch (ReflectionException $ex) {
            throw OrmException::fromPrevious($ex);
        }
    }

    /**
     * Get the annotated primary key
     *
     * The property is annotated with the @id annotation
     * The propery may have a @column annotation to modify the database column name
     *
     * @param string $class The name of class to retrieve the primary key column of
     *
     * @return string|null The name of primary key column
     *
     * @throws OrmException
     */
    private static function getAnnotatedPrimaryKeyColumn($class)
    {
        try {
            $rf = new ReflectionClass($class);

            $properties = $rf->getProperties();

            $columnName = null;

            foreach ($properties as $property) {
                assert($property instanceof ReflectionProperty);
                $docComment = $property->getDocComment();
                if (self::isIdAnnotated($docComment)) {
                    if (null === ($columnName = self::getAnnotatedColumn($docComment))) {
                        $columnName = $property->getName();
                    }
                }
            }

            return $columnName;
        } catch (ReflectionException $ex) {
            throw OrmException::fromPrevious($ex);
        }
    }

    /**
     * Get the property type via annotation
     *
     * @param string $class The name of class to retrieve a particular property type
     * @param string $propertyName The name of property to retrieve the type of
     *
     * @return string|null The property type
     */
    private static function getAnnotatedPropertyType($class, $propertyName)
    {
        $type = null;
        $rf = new ReflectionClass($class);

        foreach ($rf->getProperties() as $property) {
            assert($property instanceof ReflectionProperty);

            $docComments = $property->getDocComment();

            $isDestinationProperty = false;

            if ($property->getName() == $propertyName) {
                $isDestinationProperty = true;
            }

            if (! $isDestinationProperty) {
                continue;
            }

            if (null !== ($type = self::getAnnotatedType($docComments, $rf->getNamespaceName()))) {
                break;
            }
        }

        return $type;
    }

    /**
     * Build a full qualified class name including namespace
     *
     * @param string $ns The namespace of class
     * @param string $class The name of class
     *
     * @return string The full qualified class name
     */
    private static function fullQualifiedName($ns, $class)
    {
        return sprintf("\\%s\\%s", $ns, $class);
    }

    /**
     * Get the value from property
     *
     * @param object $from The source object
     * @param string $toClass The type of destination class
     * @param ReflectionProperty $property The property to get value of
     * @param string $namespace The namespace of destination class
     *
     * @return array The type and value from property
     */
    private static function getAnnotatedPropertyValue($from, $toClass, $property, $namespace)
    {
        $value = $property->getValue($from);

        $type = self::getAnnotatedPropertyType($toClass, $property->getName());

        if (null !== $type && !self::isPrimitive($type) && !class_exists($type)) {
            $type = self::fullQualifiedName($namespace, $type);
        }

        if (null !== $type && !self::isPrimitive($type) && class_exists($type)) {
            $rfPropertyType = new ReflectionClass($type);
            if ($rfPropertyType->getParentClass() &&
                strcmp($rfPropertyType->getParentClass()->name, 'Nkey\Caribu\Model\AbstractModel') == 0
                ) {
                $getById = new ReflectionMethod($type, "get");
                $value = $getById->invoke(null, $value);
{'fraction_non_alphanumeric': 0.10752780153977759, 'fraction_numerical': 0.00034217279726261765, 'mean_word_length': 2.0295413319512825, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 43, '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': '484863', 'n_tokens_mistral': 3109, 'n_tokens_neox': 2988, 'n_words': 863}