<commit_before><?php

/**
 * PHP Version 5.3
 *
 * @copyright   (c) 2006-2015 <NAME>
 * @author      <NAME> <<EMAIL>>
 * @license     http://opensource.org/licenses/MIT MIT
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ptlis\ConNeg\Type\Builder;

use ptlis\ConNeg\Exception\InvalidTypeException;
use ptlis\ConNeg\Type\Type;

/**
 * MIME type builder.
 */
class MimeTypeBuilder extends AbstractTypeBuilder
{
    /**
     * {@inheritDoc}
     */
    protected function validateType($type)
    {
        if ($this->isFromApp && strlen($type) > 0) {
            $typeParts = explode('/', $type);

            // Too many/few slashes
            if (2 !== count($typeParts)) {
                throw new InvalidTypeException('"' . $type . '" is not a valid mime type');
            }

            // Wildcards disallowed in app types
            if (in_array('*', $typeParts)) {
                throw new InvalidTypeException('Wildcards are not allowed in application-provided types.');
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    protected function normalizeType($type)
    {
        $typeParts = explode('/', $type);

        // Ignore misformatted types
        if (2 !== count($typeParts)) {
            $type = '';

        } else {

            // Types in form of */foo aren't valid
            list($mimeType, $subType) = $typeParts;
            if ('*' === $mimeType && '*' !== $subType) {
                $type = '';
            }
        }

        return $type;
    }

    /**
     * {@inheritDoc}
     *
     * @return Type
     */
    public function get()
    {
        // Defaults for absent type
        $mimeType = '';
        $subType = '';
        $precedence = Type::ABSENT_TYPE;
        $qFactor = 0;

        // A type was present
        $explodedType = explode('/', $this->type);
        if (2 === count($explodedType)) {

            list($mimeType, $subType) = $explodedType;

            $precedence = Type::EXACT_TYPE;
            $qFactor = $this->qFactor;

            if ('*' === $mimeType) {
                $precedence = Type::WILDCARD_TYPE;

            } elseif ('*' === $subType) {
                $precedence = Type::WILDCARD_SUBTYPE;
            }
        }

        $type = '';
        if (Type::ABSENT_TYPE !== $precedence) {
            $type = $mimeType . '/' . $subType;
        }

        return new Type(
            $type,
            $qFactor,
            $precedence
        );
    }
}<commit_msg>Remove unnecessary rebuilding of mime type<commit_after><?php

/**
 * PHP Version 5.3
 *
 * @copyright   (c) 2006-2015 <NAME>
 * @author      <NAME> <<EMAIL>>
 * @license     http://opensource.org/licenses/MIT MIT
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace ptlis\ConNeg\Type\Builder;

use ptlis\ConNeg\Exception\InvalidTypeException;
use ptlis\ConNeg\Type\Type;

/**
 * MIME type builder.
 */
class MimeTypeBuilder extends AbstractTypeBuilder
{
    /**
     * {@inheritDoc}
     */
    protected function validateType($type)
    {
        if ($this->isFromApp && strlen($type) > 0) {
            $typeParts = explode('/', $type);

            // Too many/few slashes
            if (2 !== count($typeParts)) {
                throw new InvalidTypeException('"' . $type . '" is not a valid mime type');
            }

            // Wildcards disallowed in app types
            if (in_array('*', $typeParts)) {
                throw new InvalidTypeException('Wildcards are not allowed in application-provided types.');
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    protected function normalizeType($type)
    {
        $typeParts = explode('/', $type);

        // Ignore misformatted types
        if (2 !== count($typeParts)) {
            $type = '';

        } else {

            // Types in form of */foo aren't valid
            list($mimeType, $subType) = $typeParts;
            if ('*' === $mimeType && '*' !== $subType) {
                $type = '';
            }
        }

        return $type;
    }

    /**
     * {@inheritDoc}
     *
     * @return Type
     */
    public function get()
    {
        // Defaults for absent type
        $mimeType = '';
        $subType = '';
        $precedence = Type::ABSENT_TYPE;
        $qFactor = 0;

        // A type was present
        $explodedType = explode('/', $this->type);
        if (2 === count($explodedType)) {

            list($mimeType, $subType) = $explodedType;

            $precedence = Type::EXACT_TYPE;
            $qFactor = $this->qFactor;

            if ('*' === $mimeType) {
                $precedence = Type::WILDCARD_TYPE;

            } elseif ('*' === $subType) {
                $precedence = Type::WILDCARD_SUBTYPE;
            }
        }

        return new Type(
            $this->type,
            $qFactor,
            $precedence
        );
    }
}
{'fraction_non_alphanumeric': 0.14263043047014481, 'fraction_numerical': 0.005951200158698671, 'mean_word_length': 1.6384092098377814, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 20, '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': '2215998', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1443, 'n_words': 348}