<commit_before><?php

/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\ORM\Tools\Export\Driver;

use Doctrine\ORM\Mapping\ClassMetadataInfo,
    Doctrine\ORM\Mapping\AssociationMapping;

/**
 * ClassMetadata exporter for PHP classes with annotations
 *
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link    www.doctrine-project.org
 * @since   2.0
 * @version $Revision$
 * @author  <NAME> <<EMAIL>>
 */
class AnnotationExporter extends AbstractExporter
{
    protected $_extension = '.php';

    private $_isNew = false;
    private $_outputPath;
    private $_numSpaces;
    private $_spaces;
    private $_classToExtend;
    private $_currentCode;

    public function __construct($dir = null)
    {
        parent::__construct($dir);
        $this->setNumSpaces(4);
    }

    /**
     * Converts a single ClassMetadata instance to the exported format
     * and returns it
     *
     * @param ClassMetadataInfo $metadata 
     * @return string $exported
     */
    public function exportClassMetadata(ClassMetadataInfo $metadata)
    {
        $this->_currentCode = null;
        if (file_exists($this->_outputPath)) {
            $this->_currentCode = file_get_contents($this->_outputPath);
        }

        ob_start();
        include($this->_isNew ? 'annotation.tpl.php' : 'annotation_body.tpl.php');
        $code = ob_get_contents();
        ob_end_clean();

        $code = str_replace(array('[?php', '?]'), array('<?php', '?>'), $code);
        $code = explode("\n", $code);

        if ($this->_currentCode) {
            $body = $code;
            $code = $this->_currentCode;
            $code = explode("\n", $code);
            unset($code[array_search('}', $code)]);
            foreach ($body as $line) {
                $code[] = $line;
            }
            $code[] = '}';
        }

        $code = array_values($code);

        // Remove empty lines before last "}"
        for ($i = count($code) - 1; $i > 0; --$i) {
            $line = trim($code[$i]);
            if ($line && $line != '}') {
                break;
            }
            if ( ! $line) {
                unset($code[$i]);
            }
        }
        $code = array_values($code);
        $exported = implode("\n", $code);

        return $exported;
    }

    /**
     * Set the number of spaces the exported class should have
     *
     * @param integer $numSpaces 
     * @return void
     */
    public function setNumSpaces($numSpaces)
    {
        $this->_spaces = str_repeat(' ', $numSpaces);
        $this->_numSpaces = $numSpaces;
    }

    /**
     * Set the name of the class the generated classes should extend from
     *
     * @return void
     */
    public function setClassToExtend($classToExtend)
    {
        $this->_classToExtend = $classToExtend;
    }

    /**
     * This method is overriden so that each class is outputted
     * to the appropriate path where namespaces become directories.
     *
     * Export each ClassMetadata instance to a single Doctrine Mapping file
     * named after the entity
     *
     * @return void
     */
    public function export()
    {
        if ( ! is_dir($this->_outputDir)) {
            mkdir($this->_outputDir, 0777);
        }

        foreach ($this->_metadatas as $metadata) {
            $outputPath = $this->_outputDir . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->_extension;
            $outputDir = dirname($outputPath);
            if ( ! is_dir($outputDir)) {
                mkdir($outputDir, 0777, true);
            }
            if ( ! file_exists($outputPath)) {
               $this->_isNew = true;
            }
            $this->_outputPath = $outputPath;
            $output = $this->exportClassMetadata($metadata);
            file_put_contents($outputPath, $output);
        }
    }

    private function _hasProperty($property, $metadata)
    {
        if ($this->_isNew) {
            return false;
        } else {
            return strpos($this->_currentCode, '$' . $property) !== false ? true : false;
        }
    }

    private function _hasMethod($method, $metadata)
    {
        if ($this->_isNew) {
            return false;
        } else {
            return strpos($this->_currentCode, 'function ' . $method) !== false ? true : false;
        }
    }

    private function _hasNamespace($metadata)
    {
        return strpos($metadata->name, '\\') ? true : false;
    }

    private function _extendsClass()
    {
        return $this->_classToExtend ? true : false;
    }

    private function _getClassToExtend()
    {
        return $this->_classToExtend;
    }

    private function _getClassToExtendName()
    {
        $refl = new \ReflectionClass($this->getClassToExtend());
        return $refl->getShortName();
    }

    private function _getClassToExtendNamespace()
    {
        $refl = new \ReflectionClass($this->getClassToExtend());
        return $refl->getNamespaceName() ? $refl->getNamespaceName():$refl->getShortName();        
    }

    private function _getClassName($metadata)
    {
        if ($pos = strpos($metadata->name, '\\')) {
            return substr($metadata->name, $pos + 1, strlen($metadata->name));
        } else {
            return $metadata->name;
        }<commit_msg>[2.0][DDC-71] Fixing call to incorrect method name<commit_after><?php

/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\ORM\Tools\Export\Driver;

use Doctrine\ORM\Mapping\ClassMetadataInfo,
    Doctrine\ORM\Mapping\AssociationMapping;

/**
 * ClassMetadata exporter for PHP classes with annotations
 *
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link    www.doctrine-project.org
 * @since   2.0
 * @version $Revision$
 * @author  <NAME> <<EMAIL>>
 */
class AnnotationExporter extends AbstractExporter
{
    protected $_extension = '.php';

    private $_isNew = false;
    private $_outputPath;
    private $_numSpaces;
    private $_spaces;
    private $_classToExtend;
    private $_currentCode;

    public function __construct($dir = null)
    {
        parent::__construct($dir);
        $this->setNumSpaces(4);
    }

    /**
     * Converts a single ClassMetadata instance to the exported format
     * and returns it
     *
     * @param ClassMetadataInfo $metadata 
     * @return string $exported
     */
    public function exportClassMetadata(ClassMetadataInfo $metadata)
    {
        $this->_currentCode = null;
        if (file_exists($this->_outputPath)) {
            $this->_currentCode = file_get_contents($this->_outputPath);
        }

        ob_start();
        include($this->_isNew ? 'annotation.tpl.php' : 'annotation_body.tpl.php');
        $code = ob_get_contents();
        ob_end_clean();

        $code = str_replace(array('[?php', '?]'), array('<?php', '?>'), $code);
        $code = explode("\n", $code);

        if ($this->_currentCode) {
            $body = $code;
            $code = $this->_currentCode;
            $code = explode("\n", $code);
            unset($code[array_search('}', $code)]);
            foreach ($body as $line) {
                $code[] = $line;
            }
            $code[] = '}';
        }

        $code = array_values($code);

        // Remove empty lines before last "}"
        for ($i = count($code) - 1; $i > 0; --$i) {
            $line = trim($code[$i]);
            if ($line && $line != '}') {
                break;
            }
            if ( ! $line) {
                unset($code[$i]);
            }
        }
        $code = array_values($code);
        $exported = implode("\n", $code);

        return $exported;
    }

    /**
     * Set the number of spaces the exported class should have
     *
     * @param integer $numSpaces 
     * @return void
     */
    public function setNumSpaces($numSpaces)
    {
        $this->_spaces = str_repeat(' ', $numSpaces);
        $this->_numSpaces = $numSpaces;
    }

    /**
     * Set the name of the class the generated classes should extend from
     *
     * @return void
     */
    public function setClassToExtend($classToExtend)
    {
        $this->_classToExtend = $classToExtend;
    }

    /**
     * This method is overriden so that each class is outputted
     * to the appropriate path where namespaces become directories.
     *
     * Export each ClassMetadata instance to a single Doctrine Mapping file
     * named after the entity
     *
     * @return void
     */
    public function export()
    {
        if ( ! is_dir($this->_outputDir)) {
            mkdir($this->_outputDir, 0777);
        }

        foreach ($this->_metadatas as $metadata) {
            $outputPath = $this->_outputDir . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->_extension;
            $outputDir = dirname($outputPath);
            if ( ! is_dir($outputDir)) {
                mkdir($outputDir, 0777, true);
            }
            if ( ! file_exists($outputPath)) {
               $this->_isNew = true;
            }
            $this->_outputPath = $outputPath;
            $output = $this->exportClassMetadata($metadata);
            file_put_contents($outputPath, $output);
        }
    }

    private function _hasProperty($property, $metadata)
    {
        if ($this->_isNew) {
            return false;
        } else {
            return strpos($this->_currentCode, '$' . $property) !== false ? true : false;
        }
    }

    private function _hasMethod($method, $metadata)
    {
        if ($this->_isNew) {
            return false;
        } else {
            return strpos($this->_currentCode, 'function ' . $method) !== false ? true : false;
        }
    }

    private function _hasNamespace($metadata)
    {
        return strpos($metadata->name, '\\') ? true : false;
    }

    private function _extendsClass()
    {
        return $this->_classToExtend ? true : false;
    }

    private function _getClassToExtend()
    {
        return $this->_classToExtend;
    }

    private function _getClassToExtendName()
    {
        $refl = new \ReflectionClass($this->_getClassToExtend());
        return $refl->getShortName();
    }

    private function _getClassToExtendNamespace()
    {
        $refl = new \ReflectionClass($this->_getClassToExtend());
        return $refl->getNamespaceName() ? $refl->getNamespaceName():$refl->getShortName();        
    }

    private function _getClassName($metadata)
    {
        if ($pos = strpos($metadata->name, '\\')) {
            return substr($metadata->name, $pos + 1, strlen($metadata->name));
        } else {
            return $metadata->name;
        }
{'fraction_non_alphanumeric': 0.1252096143096702, 'fraction_numerical': 0.0025552982512177594, 'mean_word_length': 2.2606092163499087, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, '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': '6645133', 'n_tokens_mistral': 3660, 'n_tokens_neox': 3484, 'n_words': 946}