<commit_before><?php
/**
 * @title            Annotation Class
 * @desc             This class makes use of annotations (similar to Java >= 5).
 *                   To use annotations, you must inherit your class with this Annotation class.
 *
 * @author           <NAME> <<EMAIL>>
 * @copyright        (c) 2012-2018, <NAME>. All Rights Reserved.
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
 * @package          PH7 / Framework / Annotation
 * @version          1.1
 */

namespace PH7\Framework\Annotation;

defined('PH7') or exit('Restricted access');

use PH7\Framework\Cache\Cache;
use PH7\Framework\CArray\CArray;
use ReflectionClass;

abstract class Annotation
{
    const CACHE_GROUP = 'str/annotation';

    /** @var Cache */
    private $oCache;

    /**
     * Get an Annotation.
     *
     * @param string $sName The name of the annotation.
     *
     * @return string|null The value annotation. If the annotation name is not found, Returns NULL.
     */
    public function getAnnotation($sName)
    {
        $aAnnotations = $this->getAnnotations();

        return CArray::getValueByKey($sName, $aAnnotations);
    }

    /**
     * Get the annotations.
     *
     * @return array
     */
    public function getAnnotations()
    {
        $sClassName = get_class($this);

        $this->initializeAnnotations($sClassName);

        if (!$aChema = $this->oCache->get()) {
            $aClassVars = get_class_vars($sClassName);

            $oReflection = new ReflectionClass($this);

            $aChema = array();

            foreach ($aClassVars as $sName => $sValue) {
                $oProperty = $oReflection->getProperty($sName);
                $sComment = $oProperty->getDocComment();

                $sComment = preg_replace('/\/\*\*(.*)\*\//', '$1', $sComment);
                $aComment = preg_split('/\n/', $sComment);

                $sKey = $sVal = null; // Set default values
                $aChema[$sName] = array();

                foreach ($aComment as $sCommentLine) {
                    if (preg_match('/@(.*?): (.*)/i', $sCommentLine, $aMatches)) {
                        $sKey = $aMatches[1];
                        $sVal = $aMatches[2];

                        $aChema[$sName][trim($sKey)] = trim($sVal);
                    }
                }
            }

            unset($oReflection);

            $this->saveAnnotations($aChema);
        }

        return $aChema;
    }

    /**
     * Initialize Cache Annotations.
     *
     * @param string $sClassName
     */
    protected function initializeAnnotations($sClassName)
    {
        $this->oCache = (new Cache)->start(
            static::CACHE_GROUP,
            $sClassName,
            null // The last parameter is NULL, then the cache will never expire
        );
    }

    /**
     * Save the Annotations in the cache.
     *
     * @param array $aAnnotations
     *<commit_msg>Fix typo in variable name (schema and not chema!)<commit_after><?php
/**
 * @title            Annotation Class
 * @desc             This class makes use of annotations (similar to Java >= 5).
 *                   To use annotations, you must inherit your class with this Annotation class.
 *
 * @author           <NAME> <<EMAIL>>
 * @copyright        (c) 2012-2018, <NAME>. All Rights Reserved.
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
 * @package          PH7 / Framework / Annotation
 * @version          1.1
 */

namespace PH7\Framework\Annotation;

defined('PH7') or exit('Restricted access');

use PH7\Framework\Cache\Cache;
use PH7\Framework\CArray\CArray;
use ReflectionClass;

abstract class Annotation
{
    const CACHE_GROUP = 'str/annotation';

    /** @var Cache */
    private $oCache;

    /**
     * Get an Annotation.
     *
     * @param string $sName The name of the annotation.
     *
     * @return string|null The value annotation. If the annotation name is not found, Returns NULL.
     */
    public function getAnnotation($sName)
    {
        $aAnnotations = $this->getAnnotations();

        return CArray::getValueByKey($sName, $aAnnotations);
    }

    /**
     * Get the annotations.
     *
     * @return array
     */
    public function getAnnotations()
    {
        $sClassName = get_class($this);

        $this->initializeAnnotations($sClassName);

        if (!$aSchema = $this->oCache->get()) {
            $aClassVars = get_class_vars($sClassName);

            $oReflection = new ReflectionClass($this);

            $aSchema = array();

            foreach ($aClassVars as $sName => $sValue) {
                $oProperty = $oReflection->getProperty($sName);
                $sComment = $oProperty->getDocComment();

                $sComment = preg_replace('/\/\*\*(.*)\*\//', '$1', $sComment);
                $aComment = preg_split('/\n/', $sComment);

                $sKey = $sVal = null; // Set default values
                $aSchema[$sName] = array();

                foreach ($aComment as $sCommentLine) {
                    if (preg_match('/@(.*?): (.*)/i', $sCommentLine, $aMatches)) {
                        $sKey = $aMatches[1];
                        $sVal = $aMatches[2];

                        $aSchema[$sName][trim($sKey)] = trim($sVal);
                    }
                }
            }

            unset($oReflection);

            $this->saveAnnotations($aSchema);
        }

        return $aSchema;
    }

    /**
     * Initialize Cache Annotations.
     *
     * @param string $sClassName
     */
    protected function initializeAnnotations($sClassName)
    {
        $this->oCache = (new Cache)->start(
            static::CACHE_GROUP,
            $sClassName,
            null // The last parameter is NULL, then the cache will never expire
        );
    }

    /**
     * Save the Annotations in the cache.
     *
     * @param array $aAnnotations
     *
{'fraction_non_alphanumeric': 0.1297288249079344, 'fraction_numerical': 0.007030465349849347, 'mean_word_length': 1.8144135657089024, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, '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': '5320123', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1656, 'n_words': 428}