diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7df0721..c92fc1c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -16,7 +16,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ] + php: [ '8.1', '8.2', '8.3', '8.4' ] os: [ ubuntu-latest, macos-latest, windows-latest ] stability: [ prefer-lowest, prefer-stable ] steps: diff --git a/composer.json b/composer.json index f2f80b3..48afd84 100644 --- a/composer.json +++ b/composer.json @@ -17,8 +17,8 @@ ], "require": { "php": "^8.1", - "phplrt/position-contracts": "^4.0", - "phplrt/source-contracts": "^4.0", + "phplrt/position-contracts": "^3.7", + "phplrt/source-contracts": "^3.7", "symfony/deprecation-contracts": "^2.5|^3.0" }, "autoload": { @@ -27,7 +27,7 @@ } }, "require-dev": { - "phplrt/source": "^4.0", + "phplrt/source": "^3.7", "phpunit/phpunit": "^10.5|^11.0", "phpstan/extension-installer": "^1.4", "phpstan/phpstan": "^1.11", @@ -39,12 +39,12 @@ } }, "provide": { - "phplrt/position-contracts-implementation": "^4.0" + "phplrt/position-contracts-implementation": "^3.7" }, "extra": { "branch-alias": { - "dev-master": "4.x-dev", - "dev-main": "4.x-dev" + "dev-master": "3.x-dev", + "dev-main": "3.x-dev" } }, "config": { diff --git a/src/Interval.php b/src/Interval.php new file mode 100644 index 0000000..c87d374 --- /dev/null +++ b/src/Interval.php @@ -0,0 +1,45 @@ +from = $from; + $this->to = $to; + } + + public function getFrom(): PositionInterface + { + return $this->from; + } + + public function getTo(): PositionInterface + { + return $this->to; + } + + public function getLength(): int + { + return \max(0, \abs($this->to->getOffset() - $this->from->getOffset())); + } +} diff --git a/src/IntervalFactoryTrait.php b/src/IntervalFactoryTrait.php new file mode 100644 index 0000000..15c0163 --- /dev/null +++ b/src/IntervalFactoryTrait.php @@ -0,0 +1,43 @@ + $offset + * @param int<0, max> $length + * + * @throws SourceExceptionInterface + */ + public static function fromOffset($source, int $offset = 0, int $length = 0): IntervalInterface + { + return new Interval( + Position::fromOffset($source, $offset), + Position::fromOffset($source, $offset + $length) + ); + } + + /** + * @param int<1, max> $line + * @param int<1, max> $column + * @param int<0, max> $length + * + * @throws SourceExceptionInterface + */ + public static function fromPosition($source, int $line = 1, int $column = 1, int $length = 0): IntervalInterface + { + return new Interval( + $from = Position::fromPosition($source, $line, $column), + Position::fromOffset($source, $from->getOffset() + $length) + ); + } +} diff --git a/src/PositionFactoryTrait.php b/src/PositionFactoryTrait.php index a305d8b..5b146dc 100644 --- a/src/PositionFactoryTrait.php +++ b/src/PositionFactoryTrait.php @@ -98,7 +98,7 @@ public static function start(): PositionInterface * @throws SourceExceptionInterface in case of an error in creating the * source object */ - public static function end(mixed $source): PositionInterface + public static function end($source): PositionInterface { $factory = self::getPositionFactory(); diff --git a/src/PositionInterface.php b/src/PositionInterface.php new file mode 100644 index 0000000..0adaf54 --- /dev/null +++ b/src/PositionInterface.php @@ -0,0 +1,12 @@ +