<commit_before><?php

declare(strict_types=1);

namespace Brick\Geo;

use Brick\Geo\Exception\CoordinateSystemException;
use Brick\Geo\Exception\EmptyGeometryException;

/**
 * Represents a 2D bounding box calculated from a set of points.
 * This class is immutable.
 */
class BoundingBox
{
    private ?float $swX = null;

    private ?float $swY = null;

    private ?float $neX = null;

    private ?float $neY = null;

    private ?int $srid = null;

    /**
     * Returns a copy of this BoundingBox extended with the given Point.
     *
     * @throws CoordinateSystemException
     */
    public function extendedWithPoint(Point $point) : BoundingBox
    {
        if ($point->isEmpty()) {
            return $this;
        }

        if ($this->srid === null) {
            $this->srid = $point->SRID();
        } elseif ($this->srid !== $point->SRID()) {
            throw CoordinateSystemException::sridMix($this->srid, $point->SRID());
        }

        $x = $point->x();
        $y = $point->y();

        $swX = ($this->swX === null) ? $x : min($this->swX, $x);
        $swY = ($this->swY === null) ? $y : min($this->swY, $y);

        $neX = ($this->neX === null) ? $x : max($this->neX, $x);
        $neY = ($this->neY === null) ? $y : max($this->neY, $y);

        if ($swX === $this->swX && $swY === $this->swY && $neX === $this->neX && $neY === $this->neY) {
            return $this;
        }

        $that = clone $this;

        $that->swX = $swX;
        $that->swY = $swY;
        $that->neX = $neX;
        $that->neY = $neY;

        return $that;
    }

    /**
     * Returns a copy of this BoundingBox extended with the given BoundingBox.
     *
     * @throws CoordinateSystemException
     */
    public function extendedWithBoundingBox(BoundingBox $boundingBox) : BoundingBox
    {
        if ($boundingBox->isEmpty()) {
            return $this;
        }

        return $this
            ->extendedWithPoint($boundingBox->getSouthWest())
            ->extendedWithPoint($boundingBox->getNorthEast());
    }

    public function isEmpty() : bool
    {
        return $this->srid === null;
    }

    /**
     * Returns the south-west XY point.
     *
     * @throws EmptyGeometryException
     */
    public function getSouthWest() : Point
    {
        if ($this->srid === null || $this->swX === null || $this->swY === null) {
            throw new EmptyGeometryException('The bounding box is empty.');
        }

        return new Point(CoordinateSystem::xy($this->srid), $this->swX, $this->swY);
    }

    /**
     * Returns the north-east XY point.
     *
     * @throws EmptyGeometryException
     */
    public function getNorthEast() : Point
    {
        if ($this->srid === null || $this->neX === null || $this->neY === null) {
            throw new EmptyGeometryException('The bounding box is empty.');
        }

        return new Point(CoordinateSystem::xy($this->srid), $this->neX, $this->neY);
    }
}<commit_msg>Use Point::xy() factory method in BoundingBox<commit_after><?php

declare(strict_types=1);

namespace Brick\Geo;

use Brick\Geo\Exception\CoordinateSystemException;
use Brick\Geo\Exception\EmptyGeometryException;

/**
 * Represents a 2D bounding box calculated from a set of points.
 * This class is immutable.
 */
class BoundingBox
{
    private ?float $swX = null;

    private ?float $swY = null;

    private ?float $neX = null;

    private ?float $neY = null;

    private ?int $srid = null;

    /**
     * Returns a copy of this BoundingBox extended with the given Point.
     *
     * @throws CoordinateSystemException
     */
    public function extendedWithPoint(Point $point) : BoundingBox
    {
        if ($point->isEmpty()) {
            return $this;
        }

        if ($this->srid === null) {
            $this->srid = $point->SRID();
        } elseif ($this->srid !== $point->SRID()) {
            throw CoordinateSystemException::sridMix($this->srid, $point->SRID());
        }

        $x = $point->x();
        $y = $point->y();

        $swX = ($this->swX === null) ? $x : min($this->swX, $x);
        $swY = ($this->swY === null) ? $y : min($this->swY, $y);

        $neX = ($this->neX === null) ? $x : max($this->neX, $x);
        $neY = ($this->neY === null) ? $y : max($this->neY, $y);

        if ($swX === $this->swX && $swY === $this->swY && $neX === $this->neX && $neY === $this->neY) {
            return $this;
        }

        $that = clone $this;

        $that->swX = $swX;
        $that->swY = $swY;
        $that->neX = $neX;
        $that->neY = $neY;

        return $that;
    }

    /**
     * Returns a copy of this BoundingBox extended with the given BoundingBox.
     *
     * @throws CoordinateSystemException
     */
    public function extendedWithBoundingBox(BoundingBox $boundingBox) : BoundingBox
    {
        if ($boundingBox->isEmpty()) {
            return $this;
        }

        return $this
            ->extendedWithPoint($boundingBox->getSouthWest())
            ->extendedWithPoint($boundingBox->getNorthEast());
    }

    public function isEmpty() : bool
    {
        return $this->srid === null;
    }

    /**
     * Returns the south-west XY point.
     *
     * @throws EmptyGeometryException
     */
    public function getSouthWest() : Point
    {
        if ($this->swX === null || $this->swY === null || $this->srid === null) {
            throw new EmptyGeometryException('The bounding box is empty.');
        }

        return Point::xy($this->swX, $this->swY, $this->srid);
    }

    /**
     * Returns the north-east XY point.
     *
     * @throws EmptyGeometryException
     */
    public function getNorthEast() : Point
    {
        if ($this->neX === null || $this->neY === null || $this->srid === null) {
            throw new EmptyGeometryException('The bounding box is empty.');
        }

        return Point::xy($this->neX, $this->neY, $this->srid);
    }
}
{'fraction_non_alphanumeric': 0.16458262926664413, 'fraction_numerical': 0.0006759040216289286, 'mean_word_length': 2.1417197452229297, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 91, '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': '4850669', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1857, 'n_words': 449}