<commit_before><?php

/**
 * OrmTagGenerator
 *
 * This class generates DocBlock Tags for the ORM properties of a Dataobject of DataExtension
 * and adds $owner Tags for added DataExtensions
 *
 * @package IDEAnnotator/Generators
 */
class OrmTagGenerator extends AbstractTagGenerator
{
    /**
     * @var array
     * Available properties to generate docblocks for.
     */
    protected static $propertyTypes = array(
        'Owner',
        'DB',
        'HasOne',
        'BelongsTo',
        'HasMany',
        'ManyMany',
        'BelongsManyMany',
        'Extensions',
    );

    /**
     * Default tagname is will be @string .
     *
     * All exceptions for @see DBField types are listed here
     *
     * @see generateDBTags();
     * @var array
     */
    protected static $dbfield_tagnames = array(
        'Int'     => 'int',
        'DBInt'   => 'int',
        'Boolean' => 'boolean',
        'Float'   => 'float',
        'DBFloat' => 'float',
        'Decimal' => 'float'
    );

    /**
     * Generates all ORM Tags
     */
    protected function generateTags()
    {
        foreach (self::$propertyTypes as $type) {
            $function = 'generate' . $type . 'Tags';
            $this->{$function}();
        }
    }

    /**
     * Generate the $db property values.
     */
    protected function generateDBTags()
    {
        if ($fields = (array)$this->getClassConfig('db')) {
            foreach ($fields as $fieldName => $dbFieldType) {
                $this->pushPropertyTag($this->getTagNameForDBField($dbFieldType) . " \$$fieldName");
            }
        }
    }

    /**
     * @param string $dbFieldType
     * @return string
     */
    public function getTagNameForDBField($dbFieldType)
    {
        // some fields in 3rd-party modules require a name...
        $fieldObj = Object::create_from_string($dbFieldType, 'DummyName');

        foreach(self::$dbfield_tagnames as $dbClass => $tagName) {
            if(class_exists($dbClass)) {
                $obj = Object::create_from_string($dbClass);
                if($fieldObj instanceof $obj) {
                    return $tagName;
                }
            }
        }
        return 'string';
    }

    /**
     * Generate the $belongs_to property values.
     */
    protected function generateBelongsToTags()
    {
        if ($fields = (array)$this->getClassConfig('belongs_to')) {
            foreach ($fields as $fieldName => $dataObjectName) {
                $this->pushMethodTag($fieldName, $dataObjectName . " \$$fieldName");
            }
        }
    }

    /**
     * Generate the $has_one property and method values.
     */
    protected function generateHasOneTags()
    {
        if ($fields = (array)$this->getClassConfig('has_one')) {
            foreach ($fields as $fieldName => $dataObjectName) {
                $this->pushPropertyTag("int \${$fieldName}ID");
                $this->pushMethodTag($fieldName, "{$dataObjectName} {$fieldName}()");
            }
        }
    }

    /**
     * Generate the $has_many method values.
     */
    protected function generateHasManyTags()
    {
        $this->generateTagsForDataLists($this->getClassConfig('has_many'), 'DataList');
    }

    /**
     * Generate the $many_many method values.
     */
    protected function generateManyManyTags()
    {
        $this->generateTagsForDataLists($this->getClassConfig('many_many'), 'ManyManyList');
    }

    /**
     * Generate the $belongs_many_many method values.
     */
    protected function generateBelongsManyManyTags()
    {
        $this->generateTagsForDataLists($this->getClassConfig('belongs_many_many'), 'ManyManyList');
    }

    /**
     * @param array $fields
     * @param string $listType
     */
    protected function generateTagsForDataLists($fields, $listType = 'DataList')
    {
        if(!empty($fields)) {
            foreach ((array)$fields as $fieldName => $dataObjectName) {
                // we might have dot notations
                $dataObjectName = explode(".", $dataObjectName, 2)[0];
                $this->pushMethodTag($fieldName, "{$listType}|{$dataObjectName}[] {$fieldName}()");
            }
        }
    }
}<commit_msg>replace oneliner explode()[0] with two lines for php 5.3 :(<commit_after><?php

/**
 * OrmTagGenerator
 *
 * This class generates DocBlock Tags for the ORM properties of a Dataobject of DataExtension
 * and adds $owner Tags for added DataExtensions
 *
 * @package IDEAnnotator/Generators
 */
class OrmTagGenerator extends AbstractTagGenerator
{
    /**
     * @var array
     * Available properties to generate docblocks for.
     */
    protected static $propertyTypes = array(
        'Owner',
        'DB',
        'HasOne',
        'BelongsTo',
        'HasMany',
        'ManyMany',
        'BelongsManyMany',
        'Extensions',
    );

    /**
     * Default tagname is will be @string .
     *
     * All exceptions for @see DBField types are listed here
     *
     * @see generateDBTags();
     * @var array
     */
    protected static $dbfield_tagnames = array(
        'Int'     => 'int',
        'DBInt'   => 'int',
        'Boolean' => 'boolean',
        'Float'   => 'float',
        'DBFloat' => 'float',
        'Decimal' => 'float'
    );

    /**
     * Generates all ORM Tags
     */
    protected function generateTags()
    {
        foreach (self::$propertyTypes as $type) {
            $function = 'generate' . $type . 'Tags';
            $this->{$function}();
        }
    }

    /**
     * Generate the $db property values.
     */
    protected function generateDBTags()
    {
        if ($fields = (array)$this->getClassConfig('db')) {
            foreach ($fields as $fieldName => $dbFieldType) {
                $this->pushPropertyTag($this->getTagNameForDBField($dbFieldType) . " \$$fieldName");
            }
        }
    }

    /**
     * @param string $dbFieldType
     * @return string
     */
    public function getTagNameForDBField($dbFieldType)
    {
        // some fields in 3rd-party modules require a name...
        $fieldObj = Object::create_from_string($dbFieldType, 'DummyName');

        foreach(self::$dbfield_tagnames as $dbClass => $tagName) {
            if(class_exists($dbClass)) {
                $obj = Object::create_from_string($dbClass);
                if($fieldObj instanceof $obj) {
                    return $tagName;
                }
            }
        }
        return 'string';
    }

    /**
     * Generate the $belongs_to property values.
     */
    protected function generateBelongsToTags()
    {
        if ($fields = (array)$this->getClassConfig('belongs_to')) {
            foreach ($fields as $fieldName => $dataObjectName) {
                $this->pushMethodTag($fieldName, $dataObjectName . " \$$fieldName");
            }
        }
    }

    /**
     * Generate the $has_one property and method values.
     */
    protected function generateHasOneTags()
    {
        if ($fields = (array)$this->getClassConfig('has_one')) {
            foreach ($fields as $fieldName => $dataObjectName) {
                $this->pushPropertyTag("int \${$fieldName}ID");
                $this->pushMethodTag($fieldName, "{$dataObjectName} {$fieldName}()");
            }
        }
    }

    /**
     * Generate the $has_many method values.
     */
    protected function generateHasManyTags()
    {
        $this->generateTagsForDataLists($this->getClassConfig('has_many'), 'DataList');
    }

    /**
     * Generate the $many_many method values.
     */
    protected function generateManyManyTags()
    {
        $this->generateTagsForDataLists($this->getClassConfig('many_many'), 'ManyManyList');
    }

    /**
     * Generate the $belongs_many_many method values.
     */
    protected function generateBelongsManyManyTags()
    {
        $this->generateTagsForDataLists($this->getClassConfig('belongs_many_many'), 'ManyManyList');
    }

    /**
     * @param array $fields
     * @param string $listType
     */
    protected function generateTagsForDataLists($fields, $listType = 'DataList')
    {
        if(!empty($fields)) {
            foreach ((array)$fields as $fieldName => $dataObjectName) {
                // we might have dot notations
                $dataObjectName = explode(".", $dataObjectName, 2);
                $dataObjectName = $dataObjectName[0]; // php 5.3 : can't use a oneliner yet...
                $this->pushMethodTag($fieldName, "{$listType}|{$dataObjectName}[] {$fieldName}()");
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.13498590225563908, 'fraction_numerical': 0.0012922932330827067, 'mean_word_length': 2.0655383507382066, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, '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': '3380037', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2392, 'n_words': 522}