<commit_before><?php

/**
 * Entity to represent a semantic version number range.
 *
 * PHP Version 5.4
 *
 * @copyright   (c) 2014 <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\SemanticVersion\BoundingPair;

use ptlis\SemanticVersion\ComparatorVersion\ComparatorVersion;
use ptlis\SemanticVersion\Comparator\EqualTo;
use ptlis\SemanticVersion\Comparator\GreaterThan;
use ptlis\SemanticVersion\Exception\InvalidBoundingPairException;
use ptlis\SemanticVersion\InRange\InRangeInterface;
use ptlis\SemanticVersion\Version\VersionInterface;

/**
 * Entity to represent a semantic version number range.
 */
class BoundingPair implements InRangeInterface
{
    /**
     * @var ComparatorVersion|null
     */
    private $upper;

    /**
     * @var ComparatorVersion|null
     */
    private $lower;


    /**
     * @throws InvalidBoundingPairException
     *
     * @param ComparatorVersion|null $lower
     *
     * @return BoundingPair
     */
    public function setLower(ComparatorVersion $lower = null)
    {
        if (is_null($lower)) {
            $this->lower = null;

        } elseif (is_null($this->upper) || ($this->upper->isSatisfiedBy($lower->getVersion())
                && $lower->isSatisfiedBy($this->upper->getVersion()))) {
            $this->lower = $lower;

        } else {
            throw new InvalidBoundingPairException(
                'The provided version is outside the bounds allowed by the upper bound.'
            );
        }

        return $this;
    }


    /**
     * @return ComparatorVersion|null
     */
    public function getLower()
    {
        return $this->lower;
    }


    /**
     * @throws InvalidBoundingPairException
     *
     * @param ComparatorVersion|null $upper
     *
     * @return BoundingPair
     */
    public function setUpper(ComparatorVersion $upper = null)
    {
        if (is_null($upper)) {
            $this->upper = null;

        } elseif (is_null($this->lower) || ($this->lower->isSatisfiedBy($upper->getVersion())
                && $upper->isSatisfiedBy($this->lower->getVersion()))) {
            $this->upper = $upper;

        } else {
            throw new InvalidBoundingPairException(
                'The provided version is outside the bounds allowed by the lower bound.'
            );
        }

        return $this;
    }


    /**
     * @return ComparatorVersion|null
     */
    public function getUpper()
    {
        return $this->upper;
    }


    /**
     * @throws InvalidBoundingPairException
     *
     * @param ComparatorVersion $version1
     * @param ComparatorVersion $version2
     *
     * @return $this
     */
    public function setUpperLower(ComparatorVersion $version1, ComparatorVersion $version2)
    {
        $equalTo = new EqualTo();
        $greaterThan = new GreaterThan();

        // If the versions are equal it doesn't matter
        if ($equalTo->compare($version1->getVersion(), $version2->getVersion())) {
            $upper = $version1;
            $lower = $version2;

        // $version1 is greater
        } elseif ($greaterThan->compare($version1->getVersion(), $version2->getVersion())) {
            $upper = $version1;
            $lower = $version2;

        // $version2 is greater
        } else {
            $upper = $version2;
            $lower = $version1;
        }

        if (!$upper->isSatisfiedBy($lower->getVersion()) || !$lower->isSatisfiedBy($upper->getVersion())) {
            throw new InvalidBoundingPairException(
                'The provided versions conflict.'
            );
        }

        $this->upper = $upper;
        $this->lower = $lower;

        return $this;
    }


    /**
     * Returns a string representation of the bounding pair.
     *
     * @return string
     */
    public function __toString()
    {
        $strRange = '';

        if (!is_null($this->lower) && !is_null($this->upper)
                && $this->lower->__toString() == $this->upper->__toString()) {
            $strRange = $this->lower->__toString();
        } else {
            if (!is_null($this->lower)) {
                $strRange .= $this->lower->__toString();
            }

            if (!is_null($this->upper)) {
                $strRange .= $this->upper->__toString();
            }
        }

        return trim($strRange);
    }


    /**
     * Returns true if the provided version satisfies the requirements of the bounding pair.
     *
     * @param VersionInterface $version
     *
     * @return boolean
     */
    public function isSatisfiedBy(VersionInterface $version)
    {
        $satisfied = false;

        // Upper & Lower set, both are satisfied by version
        if ($this->lowerSatisfiedBy($version) && $this->upperSatisfiedBy($version)) {
            $satisfied = true;
        }

        return $satisfied;
    }


    /**
     * Returns true if the upper comparator version is satisfied by the passed version. (is either null or the version
     * is lower than the lower version + comparator pair).
     *
     * @param VersionInterface $version
     *
     * @return bool
     */
    private function lowerSatisfiedBy(VersionInterface $version)
    {
        return is_null($this->lower) || (!is_null($this->lower) && $this->lower->isSatisfiedBy($version));
    }


    /**
     * Returns true if the lower comparator version is satisfied by the passed version (is either null or the version
     * is lower than the upper version + comparator pair).
     *
     * @param VersionInterface $version
     *
     * @return bool
     */
    private function upperSatisfiedBy(VersionInterface $version)
    {
        return is_null($this->upper) || (!is_null($this->upper) && $this->upper->isSatisfiedBy($version));
    }


    /**
     * Deep clone.
     */
    public function __clone()
    {
        if (!is_null($this->upper)) {
            $this->upper = clone $this->upper;
        }

        if (!is_null($this->lower)) {
            $this->lower = clone $this->lower;
        }
    }
}
<commit_msg>Simplified logic of BoundingPair::setUpperLower<commit_after><?php

/**
 * Entity to represent a semantic version number range.
 *
 * PHP Version 5.4
 *
 * @copyright   (c) 2014 <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\SemanticVersion\BoundingPair;

use ptlis\SemanticVersion\ComparatorVersion\ComparatorVersion;
use ptlis\SemanticVersion\Comparator\GreaterThan;
use ptlis\SemanticVersion\Exception\InvalidBoundingPairException;
use ptlis\SemanticVersion\InRange\InRangeInterface;
use ptlis\SemanticVersion\Version\VersionInterface;

/**
 * Entity to represent a semantic version number range.
 */
class BoundingPair implements InRangeInterface
{
    /**
     * @var ComparatorVersion|null
     */
    private $upper;

    /**
     * @var ComparatorVersion|null
     */
    private $lower;


    /**
     * @throws InvalidBoundingPairException
     *
     * @param ComparatorVersion|null $lower
     *
     * @return BoundingPair
     */
    public function setLower(ComparatorVersion $lower = null)
    {
        if (is_null($lower)) {
            $this->lower = null;

        } elseif (is_null($this->upper) || ($this->upper->isSatisfiedBy($lower->getVersion())
                && $lower->isSatisfiedBy($this->upper->getVersion()))) {
            $this->lower = $lower;

        } else {
            throw new InvalidBoundingPairException(
                'The provided version is outside the bounds allowed by the upper bound.'
            );
        }

        return $this;
    }


    /**
     * @return ComparatorVersion|null
     */
    public function getLower()
    {
        return $this->lower;
    }


    /**
     * @throws InvalidBoundingPairException
     *
     * @param ComparatorVersion|null $upper
     *
     * @return BoundingPair
     */
    public function setUpper(ComparatorVersion $upper = null)
    {
        if (is_null($upper)) {
            $this->upper = null;

        } elseif (is_null($this->lower) || ($this->lower->isSatisfiedBy($upper->getVersion())
                && $upper->isSatisfiedBy($this->lower->getVersion()))) {
            $this->upper = $upper;

        } else {
            throw new InvalidBoundingPairException(
                'The provided version is outside the bounds allowed by the lower bound.'
            );
        }

        return $this;
    }


    /**
     * @return ComparatorVersion|null
     */
    public function getUpper()
    {
        return $this->upper;
    }


    /**
     * @throws InvalidBoundingPairException
     *
     * @param ComparatorVersion $version1
     * @param ComparatorVersion $version2
     *
     * @return $this
     */
    public function setUpperLower(ComparatorVersion $version1, ComparatorVersion $version2)
    {
        $greaterThan = new GreaterThan();

        // $version1 is greater
        if ($greaterThan->compare($version1->getVersion(), $version2->getVersion())) {
            $upper = $version1;
            $lower = $version2;

        // $version2 is greater or versions are equal
        } else {
            $upper = $version2;
            $lower = $version1;
        }

        if (!$upper->isSatisfiedBy($lower->getVersion()) || !$lower->isSatisfiedBy($upper->getVersion())) {
            throw new InvalidBoundingPairException(
                'The provided versions conflict.'
            );
        }

        $this->upper = $upper;
        $this->lower = $lower;

        return $this;
    }


    /**
     * Returns a string representation of the bounding pair.
     *
     * @return string
     */
    public function __toString()
    {
        $strRange = '';

        if (!is_null($this->lower) && !is_null($this->upper)
                && $this->lower->__toString() == $this->upper->__toString()) {
            $strRange = $this->lower->__toString();
        } else {
            if (!is_null($this->lower)) {
                $strRange .= $this->lower->__toString();
            }

            if (!is_null($this->upper)) {
                $strRange .= $this->upper->__toString();
            }
        }

        return trim($strRange);
    }


    /**
     * Returns true if the provided version satisfies the requirements of the bounding pair.
     *
     * @param VersionInterface $version
     *
     * @return boolean
     */
    public function isSatisfiedBy(VersionInterface $version)
    {
        $satisfied = false;

        // Upper & Lower set, both are satisfied by version
        if ($this->lowerSatisfiedBy($version) && $this->upperSatisfiedBy($version)) {
            $satisfied = true;
        }

        return $satisfied;
    }


    /**
     * Returns true if the upper comparator version is satisfied by the passed version. (is either null or the version
     * is lower than the lower version + comparator pair).
     *
     * @param VersionInterface $version
     *
     * @return bool
     */
    private function lowerSatisfiedBy(VersionInterface $version)
    {
        return is_null($this->lower) || (!is_null($this->lower) && $this->lower->isSatisfiedBy($version));
    }


    /**
     * Returns true if the lower comparator version is satisfied by the passed version (is either null or the version
     * is lower than the upper version + comparator pair).
     *
     * @param VersionInterface $version
     *
     * @return bool
     */
    private function upperSatisfiedBy(VersionInterface $version)
    {
        return is_null($this->upper) || (!is_null($this->upper) && $this->upper->isSatisfiedBy($version));
    }


    /**
     * Deep clone.
     */
    public function __clone()
    {
        if (!is_null($this->upper)) {
            $this->upper = clone $this->upper;
        }

        if (!is_null($this->lower)) {
            $this->lower = clone $this->lower;
        }
    }
}

{'fraction_non_alphanumeric': 0.12402335718397894, 'fraction_numerical': 0.0032897442223867096, 'mean_word_length': 2.117149448859267, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 132, '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': '2271903', 'n_tokens_mistral': 3452, 'n_tokens_neox': 3308, 'n_words': 799}